From 622d54c031753d8d41cff89983cb3bd2732c9b3f Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 18 Apr 2021 13:58:02 +0200 Subject: [PATCH] Add Detonator Signed-off-by: Chaoscaot --- .../de/steamwar/bausystem/features/.gitkeep | 0 .../detonator/DetonatorEntity_15.java | 81 +++++++ .../features/detonator/Detonator_15.java | 30 +++ .../src/de/steamwar/bausystem/SWUtils.java | 12 + .../detonator/AbstractDetonatorEntity.java | 35 +++ .../features/detonator/Detoblock.java | 50 +++++ .../src/de/steamwar/bausystem/BauSystem.java | 2 + .../features/detonator/Detonator.java | 207 ++++++++++++++++++ .../features/detonator/DetonatorCommand.java | 84 +++++++ .../features/detonator/DetonatorListener.java | 158 +++++++++++++ .../detonator/storage/DetonatorStorage.java | 40 ++++ .../detonator/storage/ItemStorage.java | 184 ++++++++++++++++ 12 files changed, 883 insertions(+) create mode 100644 BauSystem_15/src/de/steamwar/bausystem/features/.gitkeep create mode 100644 BauSystem_15/src/de/steamwar/bausystem/features/detonator/DetonatorEntity_15.java create mode 100644 BauSystem_15/src/de/steamwar/bausystem/features/detonator/Detonator_15.java create mode 100644 BauSystem_API/src/de/steamwar/bausystem/features/detonator/AbstractDetonatorEntity.java create mode 100644 BauSystem_API/src/de/steamwar/bausystem/features/detonator/Detoblock.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/detonator/Detonator.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorCommand.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/detonator/storage/DetonatorStorage.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/detonator/storage/ItemStorage.java diff --git a/BauSystem_15/src/de/steamwar/bausystem/features/.gitkeep b/BauSystem_15/src/de/steamwar/bausystem/features/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/BauSystem_15/src/de/steamwar/bausystem/features/detonator/DetonatorEntity_15.java b/BauSystem_15/src/de/steamwar/bausystem/features/detonator/DetonatorEntity_15.java new file mode 100644 index 00000000..795c14f9 --- /dev/null +++ b/BauSystem_15/src/de/steamwar/bausystem/features/detonator/DetonatorEntity_15.java @@ -0,0 +1,81 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator; + +import net.minecraft.server.v1_15_R1.*; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; + +class DetonatorEntity_15 extends EntityFallingBlock implements AbstractDetonatorEntity { + + private static final Vec3D ZERO = new Vec3D(0, 0, 0); + private final Vector position; + private int references = 0; + + public DetonatorEntity_15(World world, Vector position) { + super(((CraftWorld) world).getHandle(), position.getX(), position.getY(), position.getZ(), Blocks.RED_STAINED_GLASS.getBlockData()); + this.position = position; + + this.h(true); + this.setNoGravity(true); + this.ticksLived = -12000; + } + + @Override + public void display(Player player) { + if (references++ > 0) + return; + + PacketPlayOutSpawnEntity packetPlayOutSpawnEntity = new PacketPlayOutSpawnEntity(getId(), getUniqueID(), position.getX(), position.getY(), position.getZ(), 0, 0, EntityTypes.FALLING_BLOCK, Block.getCombinedId(Blocks.RED_STAINED_GLASS.getBlockData()), ZERO); + PlayerConnection playerConnection = ((CraftPlayer) player).getHandle().playerConnection; + playerConnection.sendPacket(packetPlayOutSpawnEntity); + + PacketPlayOutEntityMetadata packetPlayOutEntityMetadata = new PacketPlayOutEntityMetadata(getId(), datawatcher, true); + playerConnection.sendPacket(packetPlayOutEntityMetadata); + } + + @Override + public boolean hide(Player player, boolean force) { + if (!force && --references > 0) + return false; + + sendDestroy(player); + die(); + return true; + } + + private void sendDestroy(Player player) { + PacketPlayOutEntityDestroy packetPlayOutEntityDestroy = new PacketPlayOutEntityDestroy(getId()); + ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packetPlayOutEntityDestroy); + } + + @Override + public void sendEntity(Player player) { + display(player); + } + + @Override + public void sendEntityDestroy(Player player) { + hide(player, false); + } +} diff --git a/BauSystem_15/src/de/steamwar/bausystem/features/detonator/Detonator_15.java b/BauSystem_15/src/de/steamwar/bausystem/features/detonator/Detonator_15.java new file mode 100644 index 00000000..05b8abd4 --- /dev/null +++ b/BauSystem_15/src/de/steamwar/bausystem/features/detonator/Detonator_15.java @@ -0,0 +1,30 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator; + +import org.bukkit.World; +import org.bukkit.util.Vector; + +public class Detonator_15 { + + static AbstractDetonatorEntity constructEntity(World world, Vector position) { + return new DetonatorEntity_15(world, position); + } +} diff --git a/BauSystem_API/src/de/steamwar/bausystem/SWUtils.java b/BauSystem_API/src/de/steamwar/bausystem/SWUtils.java index 54c3eae0..043950e1 100644 --- a/BauSystem_API/src/de/steamwar/bausystem/SWUtils.java +++ b/BauSystem_API/src/de/steamwar/bausystem/SWUtils.java @@ -19,16 +19,24 @@ package de.steamwar.bausystem; +import lombok.Getter; +import lombok.Setter; import lombok.experimental.UtilityClass; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.plugin.Plugin; @UtilityClass public class SWUtils { + @Getter + @Setter + private static Plugin bausystem; + public static void giveItemToPlayer(Player player, ItemStack itemStack) { if (itemStack == null || itemStack.getType() == Material.AIR) { return; @@ -51,4 +59,8 @@ public class SWUtils { public static void sendToActionbar(Player p, String message) { p.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message)); } + + public static NamespacedKey getNamespaceKey(String name) { + return new NamespacedKey(getBausystem(), name); + } } diff --git a/BauSystem_API/src/de/steamwar/bausystem/features/detonator/AbstractDetonatorEntity.java b/BauSystem_API/src/de/steamwar/bausystem/features/detonator/AbstractDetonatorEntity.java new file mode 100644 index 00000000..1f1a5b8b --- /dev/null +++ b/BauSystem_API/src/de/steamwar/bausystem/features/detonator/AbstractDetonatorEntity.java @@ -0,0 +1,35 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator; + +import de.steamwar.bausystem.shared.AbstractEntity; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; + +public interface AbstractDetonatorEntity extends AbstractEntity { + + void display(Player player); + + boolean hide(Player player, boolean always); + + int getId(); + + Entity getBukkitEntity(); +} diff --git a/BauSystem_API/src/de/steamwar/bausystem/features/detonator/Detoblock.java b/BauSystem_API/src/de/steamwar/bausystem/features/detonator/Detoblock.java new file mode 100644 index 00000000..ce97930d --- /dev/null +++ b/BauSystem_API/src/de/steamwar/bausystem/features/detonator/Detoblock.java @@ -0,0 +1,50 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +public enum Detoblock { + SWITCH(0, true, "Hebel"), + WOOD_BUTTON(30, "Knopf"), + STONE_BUTTON(20, "Knopf"), + PRESSURE_PLATE(30, "Druckplatte"), + WEIGHTED_PRESSURE_PLATE(20, "Druckplatte"), + TRIPWIRE(30, "Tripwire"), + NOTEBLOCK(1, "Noteblock"), + REDSTONETORCH(0, true, "Redstonefackel"), + POWERABLE(0, true, "Aktivierbarer Block"), + INVALID(-1, "Invalider"); + + @Getter + private final int time; + @Getter + private final boolean toggle; + @Getter + private final String name; + + Detoblock(int time, String name) { + this.time = time; + this.name = name; + toggle = false; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 3562cf5a..ae41d271 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -35,6 +35,8 @@ public class BauSystem extends JavaPlugin implements Listener { @Override public void onEnable() { instance = this; + SWUtils.setBausystem(instance); + LinkageUtils.link(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/Detonator.java b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/Detonator.java new file mode 100644 index 00000000..aa6e9546 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/Detonator.java @@ -0,0 +1,207 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.SWUtils; +import de.steamwar.bausystem.config.ColorConfig; +import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage; +import de.steamwar.bausystem.features.detonator.storage.ItemStorage; +import de.steamwar.core.VersionedRunnable; +import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Lightable; +import org.bukkit.block.data.Openable; +import org.bukkit.block.data.Powerable; +import org.bukkit.block.data.type.Switch; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + +import java.util.*; +import java.util.stream.Collectors; + +@UtilityClass +public class Detonator { + + private static final Map> ENTITIES_MAP = new HashMap<>(); + private static final Vector HALF = new Vector(0.5, 0, 0.5); + + public static boolean isDetonator(ItemStack itemStack) { + return ItemStorage.isDetonator(itemStack); + } + + public static void showDetonator(Player p, List locs) { + List vecs = locs.stream().map(Location::toVector).collect(Collectors.toList()); + List entities = new LinkedList<>(); + VersionedRunnable.call(new VersionedRunnable(() -> { + vecs.forEach(vector -> entities.add(Detonator_15.constructEntity(p.getWorld(), vector.add(HALF)))); + }, 15)); + entities.forEach(abstractDetonatorEntity -> abstractDetonatorEntity.display(p)); + ENTITIES_MAP.putIfAbsent(p, entities); + } + + public static void hideDetonator(Player p) { + ENTITIES_MAP.remove(p).forEach(abstractDetonatorEntity -> abstractDetonatorEntity.hide(p, true)); + } + + public static List getDetoEntities(Player p) { + return ENTITIES_MAP.getOrDefault(p, new ArrayList<>()); + } + + public static boolean hasActiveDetonatorShow(Player p) { + return ENTITIES_MAP.containsKey(p); + } + + public static void activateDetonator(DetonatorStorage detonator) { + Player p = detonator.getPlayer(); + Map> deactivate = new HashMap<>(); + Set invalid = new HashSet<>(); + + for (Location location : detonator.getLocations()) { + Block block = location.getBlock(); + Detoblock detoblock = getBlock(block); + + if (detoblock == Detoblock.INVALID) { + invalid.add(location); + continue; + } + + if (detoblock.isToggle()) { + setBlockPower(block, !getBlockPower(block)); + updateButton(block, detoblock); + } else { + setBlockPower(block, true); + updateButton(block, detoblock); + deactivate.computeIfAbsent(detoblock.getTime(), integer -> new HashSet<>()).add(block); + } + } + + int s = detonator.getLocations().size() - invalid.size(); + SWUtils.sendToActionbar(p, ColorConfig.HIGHLIGHT.toString() + s + " Punkt" + (s > 1 ? "e" : "") + " ausgelöst"); + + invalid.forEach(detonator::removeLocation); + if (!invalid.isEmpty()) { + int invalidPoints = invalid.size(); + p.sendMessage(BauSystem.PREFIX + ColorConfig.DISABLE + invalid.size() + " Punkt" + (invalidPoints > 1 ? "e" : "") + "konnte" + (invalidPoints > 1 ? "n" : "") + " nicht ausgeführt werden und wurde" + (invalidPoints > 1 ? "e" : "") + " entfernt"); + detonator.write(); + } + + deactivate.forEach((integer, blocks1) -> + Bukkit.getScheduler().scheduleSyncDelayedTask(BauSystem.getInstance(), () -> blocks1.forEach(block -> { + setBlockPower(block, false); + updateButton(block, getBlock(block)); + }), integer)); + } + + public static void updateButton(Block block, Detoblock detoblock) { + if (block.getBlockData() instanceof Switch) { + Switch sw = (Switch) block.getBlockData(); + update(block.getRelative(sw.getFacing().getOppositeFace())); + } else if (detoblock == Detoblock.TRIPWIRE) { + update(block); + } else if (detoblock == Detoblock.PRESSURE_PLATE || detoblock == Detoblock.WEIGHTED_PRESSURE_PLATE) { + update(block.getRelative(BlockFace.DOWN)); + } else if (detoblock == Detoblock.REDSTONETORCH) { + update(block.getRelative(BlockFace.UP)); + } + } + + private static void update(Block block) { + BlockData data = block.getBlockData(); + block.setType(Material.BARRIER, true); + block.setBlockData(data, true); + } + + private static void setBlockPower(Block block, boolean state) { + BlockData data = block.getBlockData(); + if (data instanceof Powerable) { + Powerable pow = (Powerable) data; + pow.setPowered(state); + } + if (data instanceof Openable) { + Openable openable = (Openable) data; + openable.setOpen(state); + } + if (data instanceof Lightable) { + Lightable lightable = (Lightable) data; + lightable.setLit(state); + } + block.setBlockData(data); + } + + private static boolean getBlockPower(Block block) { + BlockData data = block.getBlockData(); + if (data instanceof Powerable) { + Powerable pow = (Powerable) data; + return pow.isPowered(); + } + if (data instanceof Lightable) { + Lightable lightable = (Lightable) data; + return lightable.isLit(); + } + return false; + } + + static Detoblock getBlock(Block block) { + switch (block.getType()) { + case LEVER: + return Detoblock.SWITCH; + case ACACIA_BUTTON: + case BIRCH_BUTTON: + case DARK_OAK_BUTTON: + case JUNGLE_BUTTON: + case OAK_BUTTON: + case SPRUCE_BUTTON: + return Detoblock.WOOD_BUTTON; + case STONE_BUTTON: + return Detoblock.STONE_BUTTON; + case ACACIA_PRESSURE_PLATE: + case BIRCH_PRESSURE_PLATE: + case DARK_OAK_PRESSURE_PLATE: + case JUNGLE_PRESSURE_PLATE: + case OAK_PRESSURE_PLATE: + case SPRUCE_PRESSURE_PLATE: + case STONE_PRESSURE_PLATE: + return Detoblock.PRESSURE_PLATE; + case HEAVY_WEIGHTED_PRESSURE_PLATE: + case LIGHT_WEIGHTED_PRESSURE_PLATE: + return Detoblock.WEIGHTED_PRESSURE_PLATE; + case TRIPWIRE: + return Detoblock.TRIPWIRE; + case NOTE_BLOCK: + return Detoblock.NOTEBLOCK; + case REDSTONE_TORCH: + case REDSTONE_WALL_TORCH: + return Detoblock.REDSTONETORCH; + default: + if (block.getBlockData() instanceof Powerable) { + return Detoblock.POWERABLE; + } else { + return Detoblock.INVALID; + } + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorCommand.java new file mode 100644 index 00000000..246d80fa --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorCommand.java @@ -0,0 +1,84 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator; + +import de.steamwar.bausystem.SWUtils; +import de.steamwar.bausystem.config.ColorConfig; +import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage; +import de.steamwar.bausystem.features.detonator.storage.ItemStorage; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.command.SWCommand; +import lombok.Getter; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.Arrays; + +@Linked(LinkageType.COMMAND) +public class DetonatorCommand extends SWCommand { + + @Getter + private static final ItemStack WAND; + + static { + WAND = new ItemStack(Material.BLAZE_ROD); + ItemStorage.setDetonator(WAND); + ItemMeta meta = WAND.getItemMeta(); + + meta.setDisplayName(ColorConfig.HIGHLIGHT + "Fernzünder"); + meta.setLore(Arrays.asList(ColorConfig.HIGHLIGHT + "Links Klick " + ColorConfig.OTHER + "- " + ColorConfig.BASE + "Setzte einen Punkt zum Aktivieren", + ColorConfig.HIGHLIGHT + "Links Klick + Shift " + ColorConfig.OTHER + "- " + ColorConfig.BASE + "Füge einen Punkt hinzu", + ColorConfig.HIGHLIGHT + "Rechts Klick " + ColorConfig.OTHER + "- " + ColorConfig.BASE + "Löse alle Punkte aus")); + + WAND.setItemMeta(meta); + } + + protected DetonatorCommand() { + super("detonator", "dt"); + } + + @Register(help = true) + public void genericHelp(Player p, String... args) { + p.sendMessage(ColorConfig.BASE + "---===(" + ColorConfig.HIGHLIGHT + "Fernzünder" + ColorConfig.BASE + ")===---"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "detonator wand " + ColorConfig.OTHER + "-" + ColorConfig.BASE + " Gibt den Fernzünder"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "detonator click " + ColorConfig.OTHER + "-" + ColorConfig.BASE + " Aktiviere einen Fernzünder (Haupthand -> Hotbar -> Inventar)"); + p.sendMessage(ColorConfig.OTHER + "/" + ColorConfig.HIGHLIGHT + "detonator clear " + ColorConfig.OTHER + "-" + ColorConfig.BASE + " Cleare einen Fernzünder"); + } + + @Register("wand") + public void giveWand(Player p) { + SWUtils.giveItemToPlayer(p, getWAND()); + } + + @Register("click") + public void clickDetonator(Player p) { + Detonator.activateDetonator(new ItemStorage(p)); + } + + @Register("clear") + public void clearDetonator(Player p) { + DetonatorStorage storage = new ItemStorage(p); + storage.clear(); + storage.write(); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java new file mode 100644 index 00000000..593708d4 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorListener.java @@ -0,0 +1,158 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.events.PacketEvent; +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.SWUtils; +import de.steamwar.bausystem.config.ColorConfig; +import de.steamwar.bausystem.features.detonator.storage.DetonatorStorage; +import de.steamwar.bausystem.features.detonator.storage.ItemStorage; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerItemHeldEvent; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerSwapHandItemsEvent; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +@Linked(LinkageType.LISTENER) +public class DetonatorListener implements Listener { + + private static final Set HAS_UPDATED = new HashSet<>(); + + public DetonatorListener() { + ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(BauSystem.getInstance(), PacketType.Play.Client.USE_ENTITY) { + @Override + public void onPacketReceiving(PacketEvent event) { + List entities = new ArrayList<>(Detonator.getDetoEntities(event.getPlayer())); + if (entities.isEmpty()) { + return; + } + + PacketContainer container = event.getPacket(); + int entityId = container.getIntegers().read(0); + entities.removeIf(abstractDetonatorEntity -> abstractDetonatorEntity.getId() != entityId); + + if (entities.isEmpty()) { + return; + } + AbstractDetonatorEntity entity = entities.get(0); + Location location = entity.getBukkitEntity().getLocation().getBlock().getLocation(); + addLocationToDetonator(location, event.getPlayer()); + HAS_UPDATED.add(event.getPlayer()); + event.setCancelled(true); + } + }); + } + + private static void addLocationToDetonator(Location location, Player p) { + Detoblock detoblock = Detonator.getBlock(location.getBlock()); + if (detoblock == Detoblock.INVALID) { + SWUtils.sendToActionbar(p, ColorConfig.HIGHLIGHT + "Der Block konnte nicht hinzugefügt werden"); + return; + } + DetonatorStorage detonator = new ItemStorage(p); + if (!p.isSneaking()) { + detonator.clear(); + } + if (detonator.getLocations().contains(location)) { + detonator.removeLocation(location); + SWUtils.sendToActionbar(p, ColorConfig.HIGHLIGHT + detoblock.getName() + " entfernt"); + } else { + detonator.addLocation(location); + SWUtils.sendToActionbar(p, ColorConfig.HIGHLIGHT + detoblock.getName() + " hinzugefügt"); + } + detonator.write(); + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + Player p = event.getPlayer(); + if (Detonator.isDetonator(p.getInventory().getItemInMainHand())) { + event.setCancelled(true); + addLocationToDetonator(event.getBlock().getLocation(), p); + HAS_UPDATED.add(event.getPlayer()); + } + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (!Detonator.isDetonator(event.getItem())) { + return; + } + + if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { + event.setCancelled(true); + DetonatorStorage detonator = new ItemStorage(event.getPlayer()); + Detonator.activateDetonator(detonator); + HAS_UPDATED.add(event.getPlayer()); + } + } + + @EventHandler(ignoreCancelled = true) + public void onPlayerMove(PlayerMoveEvent event) { + if (!Detonator.isDetonator(event.getPlayer().getInventory().getItemInMainHand())) { + if (Detonator.hasActiveDetonatorShow(event.getPlayer())) { + Detonator.hideDetonator(event.getPlayer()); + } + } else { + if (!Detonator.hasActiveDetonatorShow(event.getPlayer())) { + Detonator.showDetonator(event.getPlayer(), new ItemStorage(event.getPlayer()).getLocations()); + } + } + + if (HAS_UPDATED.contains(event.getPlayer())) { + HAS_UPDATED.remove(event.getPlayer()); + if (Detonator.hasActiveDetonatorShow(event.getPlayer())) { + Detonator.hideDetonator(event.getPlayer()); + Detonator.showDetonator(event.getPlayer(), new ItemStorage(event.getPlayer()).getLocations()); + } + } + } + + @EventHandler + public void onPlayerItemHeld(PlayerItemHeldEvent event) { + if (Detonator.isDetonator(event.getPlayer().getInventory().getItemInMainHand())) { + HAS_UPDATED.add(event.getPlayer()); + } + } + + @EventHandler + public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { + if (Detonator.isDetonator(event.getMainHandItem()) || Detonator.isDetonator(event.getOffHandItem())) { + HAS_UPDATED.add(event.getPlayer()); + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/storage/DetonatorStorage.java b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/storage/DetonatorStorage.java new file mode 100644 index 00000000..104abf29 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/storage/DetonatorStorage.java @@ -0,0 +1,40 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator.storage; + +import org.bukkit.Location; +import org.bukkit.entity.Player; + +import java.util.List; + +public interface DetonatorStorage { + + void addLocation(Location location); + + List getLocations(); + + void removeLocation(Location location); + + void clear(); + + void write(); + + Player getPlayer(); +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/storage/ItemStorage.java b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/storage/ItemStorage.java new file mode 100644 index 00000000..dd9a14bf --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/storage/ItemStorage.java @@ -0,0 +1,184 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.detonator.storage; + +import de.steamwar.bausystem.SWUtils; +import lombok.Getter; +import org.bukkit.Location; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.persistence.PersistentDataContainer; +import org.bukkit.persistence.PersistentDataType; + +import java.util.LinkedList; +import java.util.List; +import java.util.function.IntFunction; + +public class ItemStorage implements DetonatorStorage { + + private static final NamespacedKey DETO_KEY = SWUtils.getNamespaceKey("deto"); + private static final NamespacedKey SLOT_KEY = SWUtils.getNamespaceKey("slots"); + private static final IntFunction DETOLOCATION_BUILDER = integer -> SWUtils.getNamespaceKey("deto_loc_" + integer); + @Getter + private final Player p; + private final List locs; + private int slot; + private ItemStack itemStack; + + public ItemStorage(Player p) { + this.p = p; + if (isDetonator(p.getInventory().getItemInMainHand())) { + slot = p.getInventory().getHeldItemSlot(); + itemStack = p.getInventory().getItemInMainHand(); + } else { + ItemStack[] inv = p.getInventory().getContents(); + for (int i = 0; i < inv.length; i++) { + if (isDetonator(inv[i])) { + slot = i; + itemStack = inv[i]; + break; + } + } + } + locs = read(); + } + + public static ItemStack setDetonator(ItemStack item) { + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return item; + } + PersistentDataContainer dataContainer = meta.getPersistentDataContainer(); + dataContainer.set(DETO_KEY, PersistentDataType.BYTE, (byte) 1); + dataContainer.set(SLOT_KEY, PersistentDataType.INTEGER, 0); + item.setItemMeta(meta); + return item; + } + + public static boolean isDetonator(ItemStack item) { + if (item == null) { + return false; + } + ItemMeta meta = item.getItemMeta(); + if (meta == null) { + return false; + } + PersistentDataContainer container = meta.getPersistentDataContainer(); + return container.has(DETO_KEY, PersistentDataType.BYTE) && container.has(SLOT_KEY, PersistentDataType.INTEGER); + } + + @Override + public void addLocation(Location location) { + locs.add(location); + } + + @Override + public List getLocations() { + return locs; + } + + @Override + public void removeLocation(Location location) { + locs.remove(location); + } + + @Override + public void clear() { + internalClear(); + locs.clear(); + apply(); + } + + @Override + public void write() { + write(locs); + } + + @Override + public Player getPlayer() { + return p; + } + + private List read() { + List tempLocs = new LinkedList<>(); + + ItemMeta meta = itemStack.getItemMeta(); + PersistentDataContainer container = meta.getPersistentDataContainer(); + + int length = length(); + for (int i = 0; i < length; i++) { + NamespacedKey key = DETOLOCATION_BUILDER.apply(i); + int[] loc = container.get(key, PersistentDataType.INTEGER_ARRAY); + tempLocs.add(new Location(p.getWorld(), loc[0], loc[1], loc[2])); + } + + return tempLocs; + } + + private int length() { + ItemMeta meta = itemStack.getItemMeta(); + PersistentDataContainer container = meta.getPersistentDataContainer(); + return container.get(SLOT_KEY, PersistentDataType.INTEGER); + } + + private void internalClear() { + ItemMeta meta = itemStack.getItemMeta(); + PersistentDataContainer container = meta.getPersistentDataContainer(); + + int length = length(); + for (int i = 0; i < length; i++) { + NamespacedKey key = DETOLOCATION_BUILDER.apply(i); + container.remove(key); + } + + container.set(SLOT_KEY, PersistentDataType.INTEGER, 0); + + itemStack.setItemMeta(meta); + } + + private void write(List locs) { + internalClear(); + ItemMeta meta = itemStack.getItemMeta(); + PersistentDataContainer container = meta.getPersistentDataContainer(); + + int i = 0; + for (Location loc : locs) { + NamespacedKey key = DETOLOCATION_BUILDER.apply(i); + int[] slotLoc = new int[]{ + loc.getBlockX(), + loc.getBlockY(), + loc.getBlockZ() + }; + container.set(key, PersistentDataType.INTEGER_ARRAY, slotLoc); + i++; + } + + container.set(SLOT_KEY, PersistentDataType.INTEGER, i); + + itemStack.setItemMeta(meta); + apply(); + } + + private void apply() { + p.getInventory().setItem(slot, itemStack); + } +}