From 1942a2c355f42e71b08a76cc8430082cfa6926a3 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 20 Jan 2021 17:24:15 +0100 Subject: [PATCH] Add TNTSimulator.WAND Add TNTSimulatorListener Add CommandSimulatorTabCompleter Add TNTSimulatorAnvil --- .../src/de/steamwar/bausystem/BauSystem.java | 3 + .../canonsimulator/TNTSimulator.java | 65 ++++++++++++------- .../canonsimulator/TNTSimulatorAnvil.java | 61 +++++++++++++++++ .../canonsimulator/TNTSimulatorListener.java | 59 +++++++++++++++++ .../bausystem/canonsimulator/TNTSpawn.java | 2 - .../bausystem/commands/CommandSimulator.java | 9 ++- .../CommandSimulatorTabCompleter.java | 59 +++++++++++++++++ 7 files changed, 232 insertions(+), 26 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulatorAnvil.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulatorListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulatorTabCompleter.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 0d92386..cd8e70b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem; +import de.steamwar.bausystem.canonsimulator.TNTSimulatorListener; import de.steamwar.bausystem.commands.*; import de.steamwar.bausystem.world.*; import de.steamwar.core.CommandRemover; @@ -97,12 +98,14 @@ public class BauSystem extends JavaPlugin implements Listener { getCommand("detonator").setTabCompleter(new CommandDetonatorTabCompleter()); getCommand("script").setExecutor(new CommandScript()); getCommand("simulator").setExecutor(new CommandSimulator()); + getCommand("simulator").setTabCompleter(new CommandSimulatorTabCompleter()); Bukkit.getPluginManager().registerEvents(this, this); Bukkit.getPluginManager().registerEvents(new RegionListener(), this); Bukkit.getPluginManager().registerEvents(new ScriptListener(), this); Bukkit.getPluginManager().registerEvents(new BauScoreboard(), this); Bukkit.getPluginManager().registerEvents(new ClipboardListener(), this); + Bukkit.getPluginManager().registerEvents(new TNTSimulatorListener(), this); new AFKStopper(); autoShutdown = Bukkit.getScheduler().runTaskLater(this, Bukkit::shutdown, 1200); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulator.java index f23153c..5589398 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulator.java @@ -29,6 +29,7 @@ import de.steamwar.inventory.SWListInv; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import org.bukkit.util.Consumer; import org.bukkit.util.Vector; @@ -43,7 +44,9 @@ public class TNTSimulator { private static final Vector Z_VECTOR = new Vector(0, 0, 0.0625); private static final Vector NZ_VECTOR = new Vector(0, 0, -0.0625); - private static Map tntSimulatorMap = new HashMap<>(); + static Map tntSimulatorMap = new HashMap<>(); + + public static final ItemStack WAND = new SWItem(Material.TRIPWIRE_HOOK, "§6Kanonen Simulator", Arrays.asList("§eRechts Klick Block §8- §7Füge einen TNT hinzu", "§eRechts Klick Luft §8- §7Öffne den Simulator", "§eLinks Klick §8- §7Starte die Simulation"), false, null).getItemStack(); public static void openSimulator(Player player) { if (!tntSimulatorMap.containsKey(player)) { @@ -61,14 +64,14 @@ public class TNTSimulator { lore.add("§eX§8: §7" + tntSpawn.getPosition().getX()); lore.add("§eY§8: §7" + tntSpawn.getPosition().getY()); lore.add("§eZ§8: §7" + tntSpawn.getPosition().getZ()); - swListEntryList.add(new SWListInv.SWListEntry<>(new SWItem(Material.TNT, "§eTNT", lore, false, null), tntSpawn)); + swListEntryList.add(new SWListInv.SWListEntry<>(new SWItem(Material.TNT, "§6TNT", lore, false, null), tntSpawn)); }); swListEntryList.sort(Comparator.comparing(SWListInv.SWListEntry::getObject)); SWListInv swListInv = new SWListInv<>(player, "Kanonen Simulator", false, swListEntryList, (clickType, tntSpawn) -> { editTNT(player, tntSpawn); }); - swListInv.setItem(48, new SWItem(Material.BARRIER, "§cLösche alle TNT", clickType -> { + swListInv.setItem(51, new SWItem(Material.BARRIER, "§cLösche alle TNT", clickType -> { tntSimulator.tntSpawns.clear(); openSimulator(player); })); @@ -86,22 +89,27 @@ public class TNTSimulator { tntSimulator.tntSpawns.add(tntSpawn); editTNT(player, tntSpawn); })); - swListInv.setItem(50, new SWItem(Material.FLINT_AND_STEEL, "§eSimulation Starten", clickType -> { - tntSimulator.start(); + swListInv.setItem(47, new SWItem(Material.FLINT_AND_STEEL, "§eSimulation Starten", clickType -> { + startSimulation(player); player.closeInventory(); })); swListInv.open(); } - private static void editTNT(Player player, TNTSpawn tntSpawn) { + static void editTNT(Player player, TNTSpawn tntSpawn) { + if (!tntSimulatorMap.containsKey(player)) { + tntSimulatorMap.put(player, new TNTSimulator()); + } + TNTSimulator tntSimulator = tntSimulatorMap.get(player); + SWInventory swInventory = new SWInventory(player, 54, "TNT"); - swInventory.setItem(48, new SWItem(Material.REDSTONE_BLOCK, "§cZurück", clickType -> { + swInventory.setItem(49, new SWItem(Material.REDSTONE_BLOCK, "§cZurück", clickType -> { openSimulator(player); })); // Delete icon - swInventory.setItem(50, new SWItem(Material.BARRIER, "§cDelete", clickType -> { - tntSimulatorMap.get(player).tntSpawns.remove(tntSpawn); + swInventory.setItem(53, new SWItem(Material.BARRIER, "§cDelete", clickType -> { + tntSimulator.tntSpawns.remove(tntSpawn); openSimulator(player); })); @@ -114,7 +122,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); swInventory.setItem(19, new SWItem(Material.TNT, "§eAnzahl §8- §7" + tntSpawn.getCount(), clickType -> { - changeCount(player, count -> { + changeCount(player, tntSpawn.getCount(), count -> { if (count < 1) count = 1; if (count > 1000) count = 1000; tntSpawn.setCount(count); @@ -138,7 +146,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); swInventory.setItem(20, new SWItem(Material.CLOCK, "§eTick §8- §7" + tntSpawn.getTickOffset(), clickType -> { - changeCount(player, tick -> { + changeCount(player, tntSpawn.getTickOffset(), tick -> { if (tick < 0) tick = 0; if (tick > 8000) tick = 8000; tntSpawn.setTickOffset(tick); @@ -162,7 +170,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); swInventory.setItem(21, new SWItem(Material.CLOCK, "§eFuseTicks §8- §7" + tntSpawn.getFuseTicks(), clickType -> { - changeCount(player, tick -> { + changeCount(player, tntSpawn.getFuseTicks(), tick -> { if (tick < 0) tick = 0; if (tick > 80) tick = 80; tntSpawn.setFuseTicks(tick); @@ -178,15 +186,15 @@ public class TNTSimulator { })); // Velocity Settings - swInventory.setItem(13, new SWItem(tntSpawn.isxVelocity() ? Material.LIME_WOOL : Material.RED_WOOL, "§eVelocity-X §8- §7" + active(tntSpawn.isxVelocity()), clickType -> { + swInventory.setItem(13, new SWItem(tntSpawn.isxVelocity() ? Material.LIME_WOOL : Material.RED_WOOL, "§7Start §eVelocity X §8- §7" + active(tntSpawn.isxVelocity()), clickType -> { tntSpawn.setxVelocity(!tntSpawn.isxVelocity()); editTNT(player, tntSpawn); })); - swInventory.setItem(22, new SWItem(tntSpawn.isyVelocity() ? Material.LIME_WOOL : Material.RED_WOOL, "§eVelocity-Y §8- §7" + active(tntSpawn.isyVelocity()), clickType -> { + swInventory.setItem(22, new SWItem(tntSpawn.isyVelocity() ? Material.LIME_WOOL : Material.RED_WOOL, "§7Start §eVelocity Y §8- §7" + active(tntSpawn.isyVelocity()), clickType -> { tntSpawn.setyVelocity(!tntSpawn.isyVelocity()); editTNT(player, tntSpawn); })); - swInventory.setItem(31, new SWItem(tntSpawn.iszVelocity() ? Material.LIME_WOOL : Material.RED_WOOL, "§eVelocity-Z §8- §7" + active(tntSpawn.iszVelocity()), clickType -> { + swInventory.setItem(31, new SWItem(tntSpawn.iszVelocity() ? Material.LIME_WOOL : Material.RED_WOOL, "§7Start §eVelocity Z §8- §7" + active(tntSpawn.iszVelocity()), clickType -> { tntSpawn.setzVelocity(!tntSpawn.iszVelocity()); editTNT(player, tntSpawn); })); @@ -197,7 +205,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); swInventory.setItem(23, new SWItem(Material.PAPER, "§ePosition-X §8- §7" + tntSpawn.getPosition().getX(), clickType -> { - changePosition(player, x -> { + changePosition(player, tntSpawn.getPosition().getX(), x -> { tntSpawn.getPosition().setX(clamp(x)); editTNT(player, tntSpawn); }, () -> editTNT(player, tntSpawn)); @@ -213,7 +221,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); swInventory.setItem(24, new SWItem(Material.PAPER, "§ePosition-Y §8- §7" + tntSpawn.getPosition().getY(), clickType -> { - changePosition(player, y -> { + changePosition(player, tntSpawn.getPosition().getY(), y -> { tntSpawn.getPosition().setY(clamp(y)); editTNT(player, tntSpawn); }, () -> editTNT(player, tntSpawn)); @@ -229,7 +237,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); swInventory.setItem(25, new SWItem(Material.PAPER, "§ePosition-Z §8- §7" + tntSpawn.getPosition().getZ(), clickType -> { - changePosition(player, z -> { + changePosition(player, tntSpawn.getPosition().getZ(), z -> { tntSpawn.getPosition().setZ(clamp(z)); editTNT(player, tntSpawn); }, () -> editTNT(player, tntSpawn)); @@ -241,12 +249,25 @@ public class TNTSimulator { swInventory.open(); } + static void startSimulation(Player player) { + if (tntSimulatorMap.containsKey(player)) { + tntSimulatorMap.get(player).start(); + } + } + + static void addTNT(Player player, TNTSpawn tntSpawn) { + if (!tntSimulatorMap.containsKey(player)) { + tntSimulatorMap.put(player, new TNTSimulator()); + } + tntSimulatorMap.get(player).tntSpawns.add(tntSpawn); + } + private static String active(boolean b) { return b ? "§aan" : "§caus"; } - private static void changeCount(Player player, Consumer result, Runnable failure) { - SWAnvilInv swAnvilInv = new SWAnvilInv(player, "Zahl"); + private static void changeCount(Player player, int defaultValue, Consumer result, Runnable failure) { + TNTSimulatorAnvil swAnvilInv = new TNTSimulatorAnvil(player, "Zahl", defaultValue + ""); swAnvilInv.setItem(Material.PAPER); swAnvilInv.setCallback(s -> { try { @@ -258,8 +279,8 @@ public class TNTSimulator { swAnvilInv.open(); } - private static void changePosition(Player player, Consumer result, Runnable failure) { - SWAnvilInv swAnvilInv = new SWAnvilInv(player, "Position"); + private static void changePosition(Player player, double defaultValue, Consumer result, Runnable failure) { + TNTSimulatorAnvil swAnvilInv = new TNTSimulatorAnvil(player, "Position", defaultValue + ""); swAnvilInv.setItem(Material.PAPER); swAnvilInv.setCallback(s -> { try { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulatorAnvil.java b/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulatorAnvil.java new file mode 100644 index 0000000..b155778 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulatorAnvil.java @@ -0,0 +1,61 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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.canonsimulator; + +import de.steamwar.core.Core; +import de.steamwar.inventory.SWItem; +import net.wesjd.anvilgui.AnvilGUI; +import net.wesjd.anvilgui.AnvilGUI.Response; +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import java.util.function.Consumer; + +public class TNTSimulatorAnvil { + + private final AnvilGUI.Builder builder; + private final Player player; + private Consumer callback; + + public TNTSimulatorAnvil(Player p, String t, String defaultValue) { + this.builder = (new AnvilGUI.Builder()).plugin(Core.getInstance()).title(t).text("" + defaultValue).onComplete(this::onResult); + this.player = p; + } + + public void setItem(Material m) { + this.builder.itemLeft((new SWItem(m, "").getItemStack())); + } + + public void setCallback(Consumer callback) { + this.callback = callback; + } + + public void open() { + this.builder.open(this.player); + } + + private Response onResult(Player player, String s) { + this.callback.accept(s); + return Response.close(); + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulatorListener.java b/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulatorListener.java new file mode 100644 index 0000000..7df0fe8 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSimulatorListener.java @@ -0,0 +1,59 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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.canonsimulator; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.util.Vector; + +public class TNTSimulatorListener implements Listener { + + private static final Vector HALF = new Vector(0.5, 0.5, 0.5); + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (event.getItem() == null) return; + if (!event.getItem().isSimilar(TNTSimulator.WAND)) return; + event.setCancelled(true); + if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) { + TNTSimulator.startSimulation(event.getPlayer()); + return; + } + if (event.getClickedBlock() == null) { + TNTSimulator.openSimulator(event.getPlayer()); + return; + } + Vector location = event.getClickedBlock().getLocation().toVector().add(event.getBlockFace().getDirection()).add(HALF); + TNTSpawn tntSpawn = new TNTSpawn(location); + TNTSimulator.addTNT(event.getPlayer(), tntSpawn); + TNTSimulator.editTNT(event.getPlayer(), tntSpawn); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent event) { + TNTSimulator.tntSimulatorMap.remove(event.getPlayer()); + } + +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSpawn.java b/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSpawn.java index fdbe831..d625e2a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSpawn.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/canonsimulator/TNTSpawn.java @@ -26,8 +26,6 @@ import org.bukkit.World; import org.bukkit.entity.TNTPrimed; import org.bukkit.util.Vector; -import java.util.Objects; - public class TNTSpawn implements Comparable { private static final World WORLD = Bukkit.getWorlds().get(0); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java index 5c48636..35cfb36 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java @@ -33,8 +33,13 @@ public class CommandSimulator implements CommandExecutor { public boolean onCommand(CommandSender commandSender, Command command, String s, String[] args) { if(!(commandSender instanceof Player)) return false; - Player p = (Player) commandSender; - TNTSimulator.openSimulator(p); + Player player = (Player) commandSender; + if (args.length == 1 && args[0].equalsIgnoreCase("wand")) { + player.getInventory().setItemInMainHand(TNTSimulator.WAND); + player.updateInventory(); + return false; + } + TNTSimulator.openSimulator(player); return false; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulatorTabCompleter.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulatorTabCompleter.java new file mode 100644 index 0000000..6ef43f4 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulatorTabCompleter.java @@ -0,0 +1,59 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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.commands; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class CommandSimulatorTabCompleter implements TabCompleter { + + @Override + public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + if(!(sender instanceof Player)) return new ArrayList<>(); + return detonaterTabComplete((Player) sender, args); + } + + private List detonaterTabComplete(Player player, String[] args) { + List tabComplete = new ArrayList<>(); + tabComplete.add("wand"); + + if (args.length >= 2) { + return new ArrayList<>(); + } + return manageList(tabComplete, args, 0); + } + + private List manageList(List strings, String[] args, int index) { + for (int i = strings.size() - 1; i >= 0; i--) { + if (!strings.get(i).startsWith(args[index])) { + strings.remove(i); + } + } + return strings; + } + +}