From a219bfc7b01198b27341973f34b717b61cf216f8 Mon Sep 17 00:00:00 2001 From: jojo Date: Thu, 28 Jan 2021 10:40:33 +0100 Subject: [PATCH] Simplify TNTSimulator --- .../src/de/steamwar/bausystem/BauSystem.java | 2 +- .../bausystem/cannonsimulator/TNTSpawn.java | 133 --------------- .../bausystem/commands/CommandSimulator.java | 11 +- .../TNTSimulator.java | 160 +++++++++++++++--- .../TNTSimulatorListener.java | 17 +- 5 files changed, 153 insertions(+), 170 deletions(-) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSpawn.java rename BauSystem_Main/src/de/steamwar/bausystem/{cannonsimulator => world}/TNTSimulator.java (76%) rename BauSystem_Main/src/de/steamwar/bausystem/{cannonsimulator => world}/TNTSimulatorListener.java (83%) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index e1191a2..cd69556 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -19,7 +19,7 @@ package de.steamwar.bausystem; -import de.steamwar.bausystem.cannonsimulator.TNTSimulatorListener; +import de.steamwar.bausystem.world.TNTSimulatorListener; import de.steamwar.bausystem.commands.*; import de.steamwar.bausystem.world.*; import de.steamwar.core.CommandRemover; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSpawn.java b/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSpawn.java deleted file mode 100644 index 3a27cc3..0000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSpawn.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * - * 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.cannonsimulator; - -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.entity.TNTPrimed; -import org.bukkit.util.Vector; - -public class TNTSpawn implements Comparable { - - private static final World WORLD = Bukkit.getWorlds().get(0); - - private String name = ""; - private Vector position; - private int fuseTicks = 80; - private int count = 1; - private int tickOffset = 0; - private boolean xVelocity = true; - private boolean yVelocity = true; - private boolean zVelocity = true; - private boolean comparator = false; - - public TNTSpawn(Vector position) { - this.position = position; - } - - public void spawn() { - WORLD.spawn(position.toLocation(WORLD), TNTPrimed.class, tntPrimed -> { - tntPrimed.setFuseTicks(fuseTicks); - if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0)); - if (!yVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setY(0)); - if (!zVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setZ(0)); - }); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Vector getPosition() { - return position; - } - - public void setPosition(Vector position) { - this.position = position; - } - - public int getFuseTicks() { - return fuseTicks; - } - - public void setFuseTicks(int fuseTicks) { - this.fuseTicks = fuseTicks; - } - - public int getCount() { - return count; - } - - public void setCount(int count) { - this.count = count; - } - - public int getTickOffset() { - return tickOffset; - } - - public void setTickOffset(int tickOffset) { - this.tickOffset = tickOffset; - } - - public boolean isxVelocity() { - return xVelocity; - } - - public void setxVelocity(boolean xVelocity) { - this.xVelocity = xVelocity; - } - - public boolean isyVelocity() { - return yVelocity; - } - - public void setyVelocity(boolean yVelocity) { - this.yVelocity = yVelocity; - } - - public boolean iszVelocity() { - return zVelocity; - } - - public void setzVelocity(boolean zVelocity) { - this.zVelocity = zVelocity; - } - - public boolean isComparator() { - return comparator; - } - - public void setComparator(boolean comparator) { - this.comparator = comparator; - } - - @Override - public int compareTo(TNTSpawn tntSpawn) { - return -Integer.compare(tickOffset, tntSpawn.tickOffset); - } - -} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java index 8e86f61..e6fe493 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/commands/CommandSimulator.java @@ -23,7 +23,7 @@ package de.steamwar.bausystem.commands; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; -import de.steamwar.bausystem.cannonsimulator.TNTSimulator; +import de.steamwar.bausystem.world.TNTSimulator; import de.steamwar.bausystem.world.Welt; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -32,6 +32,12 @@ import org.bukkit.entity.Player; public class CommandSimulator implements CommandExecutor { + private void help(Player player) { + player.sendMessage("§8/§esimulator §8- §7Öffnet die Simulations GUI"); + player.sendMessage("§8/§esimulator start §8- §7Startet die Simulation"); + player.sendMessage("§8/§esimulator wand §8- §7Legt den Simulator ins Inventar"); + } + private boolean permissionCheck(Player player) { if (Welt.noPermission(player, Permission.world)) { player.sendMessage(BauSystem.PREFIX + "§cDu darfst hier nicht den Simulator nutzen"); @@ -58,6 +64,9 @@ public class CommandSimulator implements CommandExecutor { case "start": TNTSimulator.get(player).start(); break; + default: + help(player); + break; } return false; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulator.java similarity index 76% rename from BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulator.java rename to BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulator.java index ee7be08..b1e5495 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulator.java @@ -19,7 +19,7 @@ * / */ -package de.steamwar.bausystem.cannonsimulator; +package de.steamwar.bausystem.world; import de.steamwar.bausystem.BauSystem; import de.steamwar.inventory.SWAnvilInv; @@ -28,13 +28,14 @@ import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; import org.bukkit.Bukkit; import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.entity.Player; +import org.bukkit.entity.TNTPrimed; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Consumer; import org.bukkit.util.Vector; import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; public class TNTSimulator { @@ -46,19 +47,20 @@ public class TNTSimulator { private static final Vector NZ_VECTOR = new Vector(0, 0, -0.0625); private static final List LORE = Collections.singletonList("§7Zum ändern klicken"); - static Map tntSimulatorMap = new HashMap<>(); + static final Map TNT_SIMULATOR_MAP = new HashMap<>(); + private final Set TNT_SPAWNS = new HashSet<>(); public static final ItemStack WAND = new SWItem(Material.TRIPWIRE_HOOK, "§eKanonen Simulator", Arrays.asList("§eRechtsklick Block §8- §7Füge einen TNT hinzu", "§eRechtsklick Luft §8- §7Öffne den Simulator", "§eLinksklick §8- §7Starte die Simulation"), false, null).getItemStack(); public static TNTSimulator get(Player player) { - return tntSimulatorMap.computeIfAbsent(player, p -> new TNTSimulator()); + return TNT_SIMULATOR_MAP.computeIfAbsent(player, p -> new TNTSimulator()); } public static void openSimulator(Player player) { TNTSimulator tntSimulator = get(player); List> swListEntryList = new ArrayList<>(); - tntSimulator.tntSpawns.forEach(tntSpawn -> { + tntSimulator.TNT_SPAWNS.forEach(tntSpawn -> { List lore = new ArrayList<>(); lore.add("§7Klicken zum einstellen"); lore.add(""); @@ -77,7 +79,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); }); swListInv.setItem(51, new SWItem(Material.BARRIER, "§cLösche alle TNT", clickType -> { - tntSimulator.tntSpawns.clear(); + tntSimulator.TNT_SPAWNS.clear(); openSimulator(player); })); swListInv.setItem(49, new SWItem(Material.TNT, "§aNeues TNT", clickType -> { @@ -88,10 +90,10 @@ public class TNTSimulator { vector.multiply(0.0625); TNTSpawn tntSpawn = new TNTSpawn(vector); - if (tntSimulator.tntSpawns.contains(tntSpawn)) { + if (tntSimulator.TNT_SPAWNS.contains(tntSpawn)) { return; } - tntSimulator.tntSpawns.add(tntSpawn); + tntSimulator.TNT_SPAWNS.add(tntSpawn); editTNT(player, tntSpawn); })); swListInv.setItem(47, new SWItem(Material.FLINT_AND_STEEL, "§eSimulation starten", clickType -> { @@ -111,7 +113,7 @@ public class TNTSimulator { // Delete tnt swInventory.setItem(53, new SWItem(Material.BARRIER, "§cEntfernen", clickType -> { - tntSimulator.tntSpawns.remove(tntSpawn); + tntSimulator.TNT_SPAWNS.remove(tntSpawn); openSimulator(player); })); @@ -135,7 +137,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); swInventory.setItem(19, new SWItem(Material.TNT, "§eAnzahl §8- §7" + tntSpawn.getCount(), LORE, false, clickType -> { - changeCount(player, tntSpawn.getCount(), count -> { + changeCount(player, "Anzahl TNT", tntSpawn.getCount(), count -> { if (count < 1) count = 1; if (count > 400) count = 400; tntSpawn.setCount(count); @@ -159,7 +161,7 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); swInventory.setItem(20, new SWItem(Material.CLOCK, "§eTick §8- §7" + tntSpawn.getTickOffset(), LORE, false, clickType -> { - changeCount(player, tntSpawn.getTickOffset(), tick -> { + changeCount(player, "Tick Offset", tntSpawn.getTickOffset(), tick -> { if (tick < 0) tick = 0; if (tick > 8000) tick = 8000; tntSpawn.setTickOffset(tick); @@ -182,8 +184,8 @@ public class TNTSimulator { } editTNT(player, tntSpawn); })); - swInventory.setItem(21, new SWItem(Material.CLOCK, "§eFuseTicks §8- §7" + tntSpawn.getFuseTicks(), LORE, false, clickType -> { - changeCount(player, tntSpawn.getFuseTicks(), tick -> { + swInventory.setItem(21, new SWItem(Material.CLOCK, "§eFuse-Ticks §8- §7" + tntSpawn.getFuseTicks(), LORE, false, clickType -> { + changeCount(player, "Fuse-Ticks", tntSpawn.getFuseTicks(), tick -> { if (tick < 0) tick = 0; if (tick > 80) tick = 80; tntSpawn.setFuseTicks(tick); @@ -269,24 +271,24 @@ public class TNTSimulator { } static void startSimulation(Player player) { - if (tntSimulatorMap.containsKey(player)) { - tntSimulatorMap.get(player).start(); + if (TNT_SIMULATOR_MAP.containsKey(player)) { + TNT_SIMULATOR_MAP.get(player).start(); } } static void addTNT(Player player, TNTSpawn tntSpawn) { - if (!tntSimulatorMap.containsKey(player)) { - tntSimulatorMap.put(player, new TNTSimulator()); + if (!TNT_SIMULATOR_MAP.containsKey(player)) { + TNT_SIMULATOR_MAP.put(player, new TNTSimulator()); } - tntSimulatorMap.get(player).tntSpawns.add(tntSpawn); + TNT_SIMULATOR_MAP.get(player).TNT_SPAWNS.add(tntSpawn); } private static String active(boolean b) { return b ? "§aan" : "§caus"; } - private static void changeCount(Player player, int defaultValue, Consumer result, Runnable failure) { - SWAnvilInv swAnvilInv = new SWAnvilInv(player, "Zahl", defaultValue + ""); + private static void changeCount(Player player, String name, int defaultValue, Consumer result, Runnable failure) { + SWAnvilInv swAnvilInv = new SWAnvilInv(player, name, defaultValue + ""); swAnvilInv.setItem(Material.PAPER); swAnvilInv.setCallback(s -> { try { @@ -315,8 +317,6 @@ public class TNTSimulator { return (int)(d * 10000) * 0.0001; } - private Set tntSpawns = new HashSet<>(); - private static class TNTSpawnPair { private int countLeft; @@ -336,7 +336,7 @@ public class TNTSimulator { public void start() { Map[]> tntSpawnMap = new HashMap<>(); - tntSpawns.forEach(tntSpawn -> { + TNT_SPAWNS.forEach(tntSpawn -> { tntSpawnMap.computeIfAbsent(tntSpawn.getTickOffset(), integer -> new ArrayList[]{new ArrayList<>(), new ArrayList<>()})[tntSpawn.isComparator() ? 1 : 0].add(new TNTSpawnPair(tntSpawn.getCount(), tntSpawn)); }); tntSpawnMap.forEach((integer, tntSpawnPairs) -> { @@ -348,15 +348,121 @@ public class TNTSimulator { } private void spawnRandomList(List tntSpawnPairs) { - AtomicInteger index = new AtomicInteger(tntSpawnPairs.size() - 1); + int index = tntSpawnPairs.size() - 1; while (!tntSpawnPairs.isEmpty()) { - TNTSpawnPair tntSpawnPair = tntSpawnPairs.get(index.get()); + TNTSpawnPair tntSpawnPair = tntSpawnPairs.get(index); tntSpawnPair.spawn(); if (tntSpawnPair.countLeft <= 0) { - tntSpawnPairs.remove(index.get()); + tntSpawnPairs.remove(index); } - if (index.decrementAndGet() < 0) index.set(tntSpawnPairs.size() - 1); + if (--index < 0) index = tntSpawnPairs.size() - 1; } } + public static class TNTSpawn implements Comparable { + + private static final World WORLD = Bukkit.getWorlds().get(0); + + private String name = ""; + private Vector position; + private int fuseTicks = 80; + private int count = 1; + private int tickOffset = 0; + private boolean xVelocity = true; + private boolean yVelocity = true; + private boolean zVelocity = true; + private boolean comparator = false; + + public TNTSpawn(Vector position) { + this.position = position; + } + + public void spawn() { + WORLD.spawn(position.toLocation(WORLD), TNTPrimed.class, tntPrimed -> { + tntPrimed.setFuseTicks(fuseTicks); + if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0)); + if (!yVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setY(0)); + if (!zVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setZ(0)); + }); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Vector getPosition() { + return position; + } + + public void setPosition(Vector position) { + this.position = position; + } + + public int getFuseTicks() { + return fuseTicks; + } + + public void setFuseTicks(int fuseTicks) { + this.fuseTicks = fuseTicks; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public int getTickOffset() { + return tickOffset; + } + + public void setTickOffset(int tickOffset) { + this.tickOffset = tickOffset; + } + + public boolean isxVelocity() { + return xVelocity; + } + + public void setxVelocity(boolean xVelocity) { + this.xVelocity = xVelocity; + } + + public boolean isyVelocity() { + return yVelocity; + } + + public void setyVelocity(boolean yVelocity) { + this.yVelocity = yVelocity; + } + + public boolean iszVelocity() { + return zVelocity; + } + + public void setzVelocity(boolean zVelocity) { + this.zVelocity = zVelocity; + } + + public boolean isComparator() { + return comparator; + } + + public void setComparator(boolean comparator) { + this.comparator = comparator; + } + + @Override + public int compareTo(TNTSpawn tntSpawn) { + return -Integer.compare(tickOffset, tntSpawn.tickOffset); + } + + } + } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulatorListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulatorListener.java similarity index 83% rename from BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulatorListener.java rename to BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulatorListener.java index 49dc743..b6e26b6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulatorListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulatorListener.java @@ -19,11 +19,10 @@ * / */ -package de.steamwar.bausystem.cannonsimulator; +package de.steamwar.bausystem.world; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; -import de.steamwar.bausystem.world.Welt; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -32,6 +31,8 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.util.Vector; +import static de.steamwar.bausystem.world.TNTSimulator.*; + public class TNTSimulatorListener implements Listener { private static final Vector HALF = new Vector(0.5, 0, 0.5); @@ -47,29 +48,29 @@ public class TNTSimulatorListener implements Listener { @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { if (event.getItem() == null) return; - if (!event.getItem().isSimilar(TNTSimulator.WAND)) return; + if (!event.getItem().isSimilar(WAND)) return; event.setCancelled(true); if (!permissionCheck(event.getPlayer())) { return; } if (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.LEFT_CLICK_AIR) { - TNTSimulator.startSimulation(event.getPlayer()); + startSimulation(event.getPlayer()); return; } if (event.getClickedBlock() == null) { - TNTSimulator.openSimulator(event.getPlayer()); + 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); + addTNT(event.getPlayer(), tntSpawn); + editTNT(event.getPlayer(), tntSpawn); } @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { - TNTSimulator.tntSimulatorMap.remove(event.getPlayer()); + TNT_SIMULATOR_MAP.remove(event.getPlayer()); } }