From 5788ae70ef1f29bf1905d11ba256c8deaa7aee67 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 10 Jun 2022 17:25:49 +0200 Subject: [PATCH] Add all tnt menus Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 2 + .../features/simulatorn/TNTSimulator.java | 21 ++++- .../simulatorn/gui/TNTElementGUI.java | 88 +++++++++++++++++-- .../simulatorn/tnt/SimulatorElement.java | 15 +++- .../features/simulatorn/tnt/TNTElement.java | 22 ++++- .../features/simulatorn/tnt/TNTGroup.java | 17 +++- 6 files changed, 152 insertions(+), 13 deletions(-) diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index f4b018be..90836666 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -603,6 +603,8 @@ SIMULATOR_TNT_SPAWN_MATERIAL = §eMaterial SIMULATOR_TNT_SPAWN_MATERIAL_LORE_1 = §7Current material§8: §e{0} SIMULATOR_TNT_SPAWN_MATERIAL_LORE_2 = §eLeft-Click §7to change SIMULATOR_TNT_SPAWN_MATERIAL_LORE_3 = §eRight-Click §7to reset +SIMULATOR_TNT_SPAWN_ENABLED = §aEnabled +SIMULATOR_TNT_SPAWN_DISABLED = §cDisabled SIMULATOR_MATERIAL_GUI_NAME = Change material SIMULATOR_MATERIAL_NAME = §e{0} SIMULATOR_MATERIAL_NAME_LORE = §7Material §8- §e{0} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/TNTSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/TNTSimulator.java index 3f073c06..44c48a58 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/TNTSimulator.java @@ -34,10 +34,7 @@ import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONType; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; @Getter @@ -137,6 +134,22 @@ public class TNTSimulator { return tntSpawns; } + public void remove(TNTElement tntElement) { + hide(tntElement); + tntElementList.remove(tntElement); + Set toRemove = new HashSet<>(); + for (SimulatorElement spawn : tntElementList) { + spawn.remove(tntElement); + if (spawn instanceof TNTGroup) { + if (((TNTGroup) spawn).getElements().isEmpty()) { + toRemove.add(spawn); + } + } + } + tntElementList.removeAll(toRemove); + tntElement.close(); + } + public void edit(Player player, RayTraceResult result) { if (result == null) { TNTSimulatorGui.open(player, vector -> { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/gui/TNTElementGUI.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/gui/TNTElementGUI.java index ff020a86..1f07cb45 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/gui/TNTElementGUI.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/gui/TNTElementGUI.java @@ -24,9 +24,11 @@ import de.steamwar.bausystem.features.simulatorn.OrderUtils; import de.steamwar.bausystem.features.simulatorn.SimulatorStorage; import de.steamwar.bausystem.features.simulatorn.TNTSimulator; import de.steamwar.bausystem.features.simulatorn.tnt.TNTElement; +import de.steamwar.bausystem.features.simulatorn.tnt.TNTGroup; import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; +import de.steamwar.inventory.SWListInv; import lombok.experimental.UtilityClass; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -104,7 +106,7 @@ public class TNTElementGUI { })); }; editObserver.run(); - tntElement.register(editObserver); + tntElement.register(editObserver, player::closeInventory); inv.addCloseRunnable(() -> tntElement.unregister(editObserver)); inv.open(); @@ -201,7 +203,7 @@ public class TNTElementGUI { */ }; editObserver.run(); - tntElement.register(editObserver); + tntElement.register(editObserver, player::closeInventory); inv.addCloseRunnable(() -> tntElement.unregister(editObserver)); inv.open(); @@ -298,7 +300,7 @@ public class TNTElementGUI { })); }; editObserver.run(); - tntElement.register(editObserver); + tntElement.register(editObserver, player::closeInventory); inv.addCloseRunnable(() -> tntElement.unregister(editObserver)); inv.open(); @@ -311,7 +313,9 @@ public class TNTElementGUI { } Runnable editObserver = () -> { - inv.setItem(42, new SWItem(tntElement.getOrder(), BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_ACTIVATED_NAME", player), OrderUtils.orderList(tntElement.getOrder(), player), false, clickType -> { + TNTSimulator tntSimulator = SimulatorStorage.getSimulator(player.getInventory().getItemInMainHand()); + + inv.setItem(19, new SWItem(tntElement.getOrder(), BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_ACTIVATED_NAME", player), OrderUtils.orderList(tntElement.getOrder(), player), false, clickType -> { if (clickType.isShiftClick()) { tntElement.setOrder(OrderUtils.previous(tntElement.getOrder())); } else { @@ -319,9 +323,83 @@ public class TNTElementGUI { } tntElement.change(); })); + + inv.setItem(21, new SWItem(tntElement.getMaterial(), BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_MATERIAL", player), Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_MATERIAL_LORE_1", player, tntElement.getMaterial().name().toLowerCase()), BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_MATERIAL_LORE_2", player), BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_MATERIAL_LORE_3", player)), false, clickType -> { + if (clickType.isLeftClick()) { + List> swListEntries = new ArrayList<>(); + MATERIALS.forEach(current -> { + swListEntries.add(new SWListInv.SWListEntry<>(new SWItem(current, BauSystem.MESSAGE.parse("SIMULATOR_MATERIAL_NAME", player, current.name().toLowerCase()), Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_MATERIAL_CLICK", player)), false, ignored -> { + }), current)); + }); + SWListInv swListInv = new SWListInv<>(player, BauSystem.MESSAGE.parse("SIMULATOR_MATERIAL_GUI_NAME", player), false, swListEntries, (invClickType, material) -> { + tntElement.setMaterial(material); + tntElement.change(); + editOther(player, tntElement, back); + }); + swListInv.open(); + } else { + tntElement.setMaterial(Material.TNT); + tntElement.change(); + editOther(player, tntElement, back); + } + })); + + inv.setItem(22, new SWItem(tntElement.isDisabled() ? Material.ENDER_PEARL : Material.ENDER_EYE, BauSystem.MESSAGE.parse(tntElement.isDisabled() ? "SIMULATOR_TNT_SPAWN_DISABLED" : "SIMULATOR_TNT_SPAWN_ENABLED", player), new ArrayList<>(), !tntElement.isDisabled(), clickType -> { + if (!tntElement.isDisabled()) { + tntSimulator.hide(tntElement); + } + tntElement.setDisabled(!tntElement.isDisabled()); + if (!tntElement.isDisabled()) { + tntSimulator.show(tntElement); + } + tntElement.change(); + })); + + if (!tntElement.hasParent()) { + inv.setItem(24, new SWItem(Material.TNT, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_ADD_IGNITION_PHASE", player), Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_ADD_IGNITION_PHASE_SHIFT", player)), false, clickType -> { + // Create TNTGroup + tntSimulator.getTntElementList().remove(tntElement); + Vector vector = tntElement.getOwnPosition().clone(); + int tickOffset = tntElement.getOwnTickOffset(); + TNTGroup tntGroup = new TNTGroup(vector); + tntGroup.setTickOffset(tickOffset); + tntElement.setTickOffset(0); + tntElement.getOwnPosition().setX(0); + tntElement.getOwnPosition().setY(0); + tntElement.getOwnPosition().setZ(0); + tntGroup.add(tntElement); + tntSimulator.getTntElementList().add(tntGroup); + + // Add new TNT + TNTElement newElement = new TNTElement(new Vector(0, 0, 0)); + newElement.setTickOffset(1); + tntGroup.add(newElement); + + tntElement.change(); + })); + } else { + inv.setItem(24, new SWItem()); + } + + inv.setItem(25, new SWItem(Material.DISPENSER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_ADD_TNT", player), clickType -> { + Vector vector = tntElement.getOwnPosition().clone(); + TNTElement newElement = new TNTElement(vector); + if (tntElement.hasParent()) { + newElement.setTickOffset(tntElement.getOwnTickOffset() + 1); + tntElement.getParent().add(newElement); + } else { + tntSimulator.getTntElementList().add(newElement); + } + })); + + // Delete tnt + inv.setItem(44, new SWItem(Material.BARRIER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_REMOVE_TNT", player), clickType -> { + tntSimulator.remove(tntElement); + player.closeInventory(); + })); }; editObserver.run(); - tntElement.register(editObserver); + tntElement.register(editObserver, player::closeInventory); inv.addCloseRunnable(() -> tntElement.unregister(editObserver)); inv.open(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/SimulatorElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/SimulatorElement.java index 4804f8c3..96685764 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/SimulatorElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/SimulatorElement.java @@ -32,6 +32,7 @@ import java.util.*; public interface SimulatorElement { Map> observer = new HashMap<>(); + Map closeObserver = new HashMap<>(); YAPIONObject toYAPION(); List getEntities(); @@ -40,6 +41,7 @@ public interface SimulatorElement { return new Vector(0, 0, 0); } + void remove(TNTElement tntElement); void show(SimulatorEntityShowMode showMode); void hide(SimulatorEntityShowMode showMode); @@ -50,13 +52,24 @@ public interface SimulatorElement { default void change() { observer.getOrDefault(this, new HashSet<>()).forEach(Runnable::run); } - default void register(Runnable observer) { + default void register(Runnable observer, Runnable close) { SimulatorElement.observer.computeIfAbsent(this, k -> new HashSet<>()).add(observer); + SimulatorElement.closeObserver.put(observer, close); } default void unregister(Runnable observer) { SimulatorElement.observer.computeIfPresent(this, (k, v) -> { v.remove(observer); return v.isEmpty() ? null : v; }); + SimulatorElement.closeObserver.remove(observer); + } + default void close() { + Set allRunnables = observer.remove(this); + if (allRunnables == null) return; + allRunnables.forEach(runnable -> { + Runnable closeRunnable = closeObserver.remove(runnable); + if (closeRunnable == null) return; + closeRunnable.run(); + }); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/TNTElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/TNTElement.java index ca98e215..33130b35 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/TNTElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/TNTElement.java @@ -131,6 +131,10 @@ public class TNTElement implements SimulatorElement { return tickOffset; } + @Override + public void remove(TNTElement tntElement) { + } + @Override public void show(SimulatorEntityShowMode showMode) { if (disabled) return; @@ -158,7 +162,7 @@ public class TNTElement implements SimulatorElement { lore.add(""); lore.add(BauSystem.MESSAGE.parse("SIMULATOR_GUI_TNT_DISABLED", p)); } - return new SWItem(material, BauSystem.MESSAGE.parse("SIMULATOR_GUI_TNT_SPAWN_NAME", p), lore, false, null); + return new SWItem(material, BauSystem.MESSAGE.parse("SIMULATOR_GUI_TNT_SPAWN_NAME", p), lore, disabled, null); } @Override @@ -218,4 +222,20 @@ public class TNTElement implements SimulatorElement { public void setOrder(Material material) { this.order = material; } + + public void setMaterial(Material material) { + this.material = material; + } + + public boolean hasParent() { + return tntGroup != null; + } + + public TNTGroup getParent() { + return tntGroup; + } + + public void setDisabled(boolean disabled) { + this.disabled = disabled; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/TNTGroup.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/TNTGroup.java index 1669c018..6809aa9f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/TNTGroup.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulatorn/tnt/TNTGroup.java @@ -32,7 +32,6 @@ import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONObject; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -80,6 +79,11 @@ public class TNTGroup implements SimulatorElement { return yapionObject; } + public void add(TNTElement tntElement) { + tntElement.tntGroup = this; + elements.add(tntElement); + } + @Override public List getEntities() { if (disabled) new ArrayList<>(); @@ -94,6 +98,11 @@ public class TNTGroup implements SimulatorElement { } } + @Override + public void remove(TNTElement tntElement) { + elements.remove(tntElement); + } + @Override public void show(SimulatorEntityShowMode showMode) { if (disabled) return; @@ -118,7 +127,7 @@ public class TNTGroup implements SimulatorElement { lore.add(""); lore.add(BauSystem.MESSAGE.parse("SIMULATOR_GUI_TNT_DISABLED", p)); } - return new SWItem(material, BauSystem.MESSAGE.parse("SIMULATOR_GUI_TNT_GROUP_NAME", p), lore, false, null); + return new SWItem(material, BauSystem.MESSAGE.parse("SIMULATOR_GUI_TNT_GROUP_NAME", p), lore, disabled, null); } @Override @@ -128,4 +137,8 @@ public class TNTGroup implements SimulatorElement { tntElement.locations(result); }); } + + public void setTickOffset(int tickOffset) { + this.tickOffset = tickOffset; + } }