diff --git a/BauSystem_15/src/de/steamwar/bausystem/world/AutoLoader_15.java b/BauSystem_15/src/de/steamwar/bausystem/world/AutoLoader_15.java index 89fc7fa..a69a11f 100644 --- a/BauSystem_15/src/de/steamwar/bausystem/world/AutoLoader_15.java +++ b/BauSystem_15/src/de/steamwar/bausystem/world/AutoLoader_15.java @@ -24,6 +24,7 @@ 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.Openable; import org.bukkit.block.data.Powerable; import org.bukkit.block.data.type.Switch; import org.bukkit.event.player.PlayerInteractEvent; @@ -48,11 +49,18 @@ class AutoLoader_15 { if (!(data instanceof Powerable)) return false; + Powerable powerable = (Powerable) data; powerable.setPowered(active); block.setBlockData(powerable); - if (data instanceof Switch){ + if(data instanceof Openable) { + Openable openable = (Openable) data; + openable.setOpen(active); + block.setBlockData(openable); + } + + if (data instanceof Switch) { Switch swtch = (Switch) data; Block relative; switch(swtch.getFace()){ @@ -67,7 +75,7 @@ class AutoLoader_15 { break; } updateBlock(relative); - }else if(block.getType() == Material.TRIPWIRE) { + } else if(block.getType() == Material.TRIPWIRE) { updateBlock(block); } return true; @@ -90,11 +98,7 @@ class AutoLoader_15 { Material material = block.getType(); if (material == Material.LEVER) { - if (powerable.isPowered()) { - return new Detoloader("Hebel", 0).setActive(false); - } else { - return new Detoloader("Hebel", 0).setActive(true); - } + return new Detoloader("Hebel", 0).setActive(!powerable.isPowered()); } else if (material == Material.STONE_BUTTON) { return new Detoloader("Knopf", Detoloader.STONE_BUTTON); } else if (material.name().contains("PRESSURE_PLATE")){ @@ -111,6 +115,8 @@ class AutoLoader_15 { } static boolean getLever(Block block) { - return ((Powerable)block.getBlockData()).isPowered(); + if(!(block.getBlockData() instanceof Powerable)) + return false; + return ((Powerable) block.getBlockData()).isPowered(); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java index bd8317e..52bc21e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonator.java @@ -34,6 +34,7 @@ public class CommandDetonator implements CommandExecutor { player.sendMessage("§8/§edetonator wand §8- §7Legt den Fernzünder ins Inventar"); player.sendMessage("§8/§edetonator detonate §8- §7Benutzt den Fernzünder"); player.sendMessage("§8/§edetonator reset §8- §7Löscht alle markierten Positionen"); + player.sendMessage("§8/§edetonator remove §8- §7Entfernt den Fernzünder"); } private boolean permissionCheck(Player player) { @@ -65,16 +66,20 @@ public class CommandDetonator implements CommandExecutor { player.updateInventory(); Detonator.getDetonator(player); break; - case "delete": - case "reset": case "remove": Detonator.deleteDetonator(player); + player.getInventory().removeItem(Detonator.WAND); break; case "detonate": case "click": case "use": Detonator.getDetonator(player).execute(); break; + case "clear": + case "delete": + case "reset": + Detonator.getDetonator(player).clearLocs(); + break; default: help(player); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonatorTabCompleter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonatorTabCompleter.java index 32d98ec..4a20f89 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonatorTabCompleter.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandDetonatorTabCompleter.java @@ -40,6 +40,7 @@ public class CommandDetonatorTabCompleter implements TabCompleter { tabComplete.add("wand"); tabComplete.add("reset"); tabComplete.add("detonate"); + tabComplete.add("remove"); if (args.length >= 2) { return new ArrayList<>(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/Detonator.java b/BauSystem_Main/src/de/steamwar/bausystem/world/Detonator.java index bdb62b6..6a7e214 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/Detonator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/Detonator.java @@ -20,13 +20,15 @@ package de.steamwar.bausystem.world; import de.steamwar.bausystem.BauSystem; -import de.steamwar.core.Core; +import de.steamwar.core.VersionedCallable; +import de.steamwar.core.VersionedRunnable; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.TextComponent; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.HandlerList; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -58,8 +60,8 @@ public class Detonator implements Listener { WAND.setItemMeta(im); } - public static Detonator getDetonator(Player player){ - if(!players.containsKey(player)) + public static Detonator getDetonator(Player player) { + if (!players.containsKey(player)) return new Detonator(player); return players.get(player); } @@ -71,37 +73,29 @@ public class Detonator implements Listener { } public static void deleteDetonator(Player player) { - if(players.containsKey(player)) - players.remove(player); + if (players.containsKey(player)) + HandlerList.unregisterAll(players.remove(player)); } public void execute() { - for (Detoloader.DetonatorActivation activation: getLocations()) { + for (Detoloader.DetonatorActivation activation : getLocations()) { - Boolean powered; - if(activation.activation == -1) switch (Core.getVersion()) { - case 15: - try { - powered = AutoLoader_15.getLever(activation.location.getBlock()); - }catch (ClassCastException e) { - locs.remove(activation); - return; - } - AutoLoader_15.setRedstone(activation.location, !powered); - break; - default: - powered = AutoLoader_12.getLever(activation.location.getBlock()); + if (activation.activation == -1) { + VersionedRunnable.call(new VersionedRunnable(() -> { + boolean powered = AutoLoader_12.getLever(activation.location.getBlock()); AutoLoader_12.setRedstone(activation.location, !powered); - }else switch (Core.getVersion()) { - case 15: - AutoLoader_15.setRedstone(activation.location, true); - Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), () -> - AutoLoader_15.setRedstone(activation.location, false), activation.activation); - break; - default: + }, 12), new VersionedRunnable(() -> { + boolean powered = AutoLoader_15.getLever(activation.location.getBlock()); + AutoLoader_15.setRedstone(activation.location, !powered); + }, 15)); + } else { + VersionedRunnable.call(new VersionedRunnable(() -> { AutoLoader_12.setRedstone(activation.location, true); - Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), () -> - AutoLoader_12.setRedstone(activation.location, false), activation.activation); + Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), () -> AutoLoader_12.setRedstone(activation.location, false), activation.activation); + }, 12), new VersionedRunnable(() -> { + AutoLoader_15.setRedstone(activation.location, true); + Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), () -> AutoLoader_15.setRedstone(activation.location, false), activation.activation); + }, 15)); } } player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§a" + locs.size() + " Punkt" + (locs.size() > 1 ? "e" : "") + " ausgelöst!")); @@ -109,49 +103,51 @@ public class Detonator implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { - if(!event.getPlayer().equals(player)) + if (!event.getPlayer().equals(player)) return; + if (event.getItem() == null) return; if (event.getItem().isSimilar(WAND)) { event.setCancelled(true); switch (event.getAction()) { case LEFT_CLICK_BLOCK: - Detoloader detoloader; - switch(Core.getVersion()){ - case 15: - detoloader = AutoLoader_15.onPlayerInteractLoader(event); - break; - default: - detoloader = AutoLoader_12.onPlayerInteractLoader(event); + Detoloader detoloader = VersionedCallable.call( + new VersionedCallable<>(() -> AutoLoader_12.onPlayerInteractLoader(event), 12), + new VersionedCallable<>(() -> AutoLoader_15.onPlayerInteractLoader(event), 15)); + if (detoloader == null) { + return; + } else if (detoloader.activation == -1) { + print(detoloader.getBlock(), detoloader.addBack); + return; } - if(detoloader == null || detoloader.activation == -1) return; - if(event.getPlayer().isSneaking()) { - if(locs.stream().filter(detonatorActivation -> detonatorActivation.location.equals(event.getClickedBlock().getLocation())).collect(Collectors.toList()).size() == 1) { + + if (event.getPlayer().isSneaking()) { + if (locs.stream().filter(detonatorActivation -> detonatorActivation.location.equals(event.getClickedBlock().getLocation())).collect(Collectors.toList()).size() == 1) { locs.removeIf(detonatorActivation -> detonatorActivation.location.equals(event.getClickedBlock().getLocation())); print(detoloader.addBack ? "§e" + detoloader.getBlock() + " entfernt" : detoloader.getBlock(), detoloader.addBack); - }else { - if(detoloader.getActivation() == 0) { + } else { + if (detoloader.getActivation() == 0) { locs.add(new Detoloader.DetonatorActivation(event.getClickedBlock().getLocation())); - }else { + } else { locs.add(new Detoloader.DetonatorActivation(detoloader.getActivation(), event.getClickedBlock().getLocation())); } print(detoloader.addBack ? "§e" + detoloader.getBlock() + " hinzugefügt" : detoloader.getBlock(), detoloader.addBack); } - }else { + } else { locs.clear(); - if(detoloader.getActivation() == 0) { + if (detoloader.getActivation() == 0) { locs.add(new Detoloader.DetonatorActivation(event.getClickedBlock().getLocation())); - }else { + } else { locs.add(new Detoloader.DetonatorActivation(detoloader.getActivation(), event.getClickedBlock().getLocation())); } print(detoloader.addBack ? "§e" + detoloader.getBlock() + " getsetzt" : detoloader.getBlock(), detoloader.addBack); } - break; + break; case RIGHT_CLICK_AIR: case RIGHT_CLICK_BLOCK: execute(); @@ -162,7 +158,8 @@ public class Detonator implements Listener { @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { - deleteDetonator(event.getPlayer()); + if (event.getPlayer().equals(player)) + deleteDetonator(player); } Set getLocations() { @@ -173,10 +170,15 @@ public class Detonator implements Listener { return player; } - void print(String message, boolean withSize){ - if(withSize) + public void clearLocs() { + locs.clear(); + } + + void print(String message, boolean withSize) { + if (withSize) { getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message + " §8" + getLocations().size())); - else + }else { getPlayer().spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message)); + } } }