From 002996afbceb240d8a67dc90f6034e3ad3e8c4ed Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 11 Jun 2022 14:34:35 +0200 Subject: [PATCH] Add phase gui Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 4 +- BauSystem_Main/src/BauSystem_de.properties | 2 + .../features/simulator/TNTSimulator.java | 32 +++- .../simulator/gui/SimulatorSelectionGUI.java | 2 - .../features/simulator/gui/TNTElementGUI.java | 130 +--------------- .../simulator/gui/TNTGroupEditGUI.java | 105 +++++++++++++ .../simulator/gui/TNTSimulatorGui.java | 123 +++++++++++++-- .../gui/components/ChangeMaterial.java | 66 ++++++++ .../gui/components/ChangePosition.java | 144 ++++++++++++++++++ .../simulator/gui/components/Disabled.java | 48 ++++++ .../simulator/tnt/SimulatorElement.java | 11 ++ .../features/simulator/tnt/TNTGroup.java | 8 + 12 files changed, 530 insertions(+), 145 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTGroupEditGUI.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/ChangeMaterial.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/ChangePosition.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/Disabled.java diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index f13c0c42..6252d304 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -515,9 +515,11 @@ SIMULATOR_EDIT_PROPERTIES = §7Edit properties SIMULATOR_EDIT_OTHER = §7Edit other SIMULATOR_EDIT_GROUP = §7Edit group +SIMULATOR_EDIT_GROUP_MENU = §eEdit group + SIMULATOR_WAND_NAME = §eSimulator SIMULATOR_WAND_NAME_SELECTED = §7Simulator §8- §e{0} -SIMULATOR_WAND_LORE_1 = §eRigth click §8- §7adds a position +SIMULATOR_WAND_LORE_1 = §eRight click §8- §7adds a position SIMULATOR_WAND_LORE_2 = §eSneaking §8- §7Free movement SIMULATOR_WAND_LORE_3 = §eLeft click §8- §7Start the simulation SIMULATOR_WAND_LORE_4 = §eRight click in air §8- §7Opens the gui diff --git a/BauSystem_Main/src/BauSystem_de.properties b/BauSystem_Main/src/BauSystem_de.properties index 5cd44ad3..cdd330b5 100644 --- a/BauSystem_Main/src/BauSystem_de.properties +++ b/BauSystem_Main/src/BauSystem_de.properties @@ -516,6 +516,8 @@ SIMULATOR_EDIT_PROPERTIES = §7Editiere Eigenschaften SIMULATOR_EDIT_OTHER = §7Editiere Andere SIMULATOR_EDIT_GROUP = §7Editiere Gruppe +SIMULATOR_EDIT_GROUP_MENU = §eEditiere Gruppe + SIMULATOR_WAND_NAME = §eKanonensimulator SIMULATOR_WAND_NAME_SELECTED = §7Kanonensimulator §8- §e{0} SIMULATOR_WAND_LORE_1 = §eRechtsklick §8- §7Füge eine Position hinzu diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java index ee73a8c3..57af3266 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java @@ -73,6 +73,20 @@ public class TNTSimulator { return yapionObject; } + public void hide() { + playerShowMode.forEach((player, simulatorEntityShowMode) -> { + simulatorEntityShowMode.hide(); + }); + } + + public void show() { + playerShowMode.forEach((player, simulatorEntityShowMode) -> { + for (SimulatorElement element : tntElementList) { + element.show(simulatorEntityShowMode); + } + }); + } + public void hide(Player player) { SimulatorEntityShowMode showMode = playerShowMode.get(player); if (showMode == null) { @@ -103,9 +117,9 @@ public class TNTSimulator { }); } - public void hide(TNTElement tntElement) { + public void hide(SimulatorElement simulatorElement) { playerShowMode.forEach((player, simulatorEntityShowMode) -> { - tntElement.hide(simulatorEntityShowMode); + simulatorElement.hide(simulatorEntityShowMode); }); } @@ -116,9 +130,9 @@ public class TNTSimulator { }); } - public void show(TNTElement tntElement) { + public void show(SimulatorElement simulatorElement) { playerShowMode.forEach((player, simulatorEntityShowMode) -> { - tntElement.show(simulatorEntityShowMode); + simulatorElement.show(simulatorEntityShowMode); }); } @@ -143,6 +157,7 @@ public class TNTSimulator { if (spawn instanceof TNTGroup) { if (((TNTGroup) spawn).getElements().isEmpty()) { toRemove.add(spawn); + spawn.close(); } } } @@ -150,6 +165,15 @@ public class TNTSimulator { tntElement.close(); } + public void change() { + tntElementList.forEach(simulatorElement -> { + simulatorElement.change(); + if (simulatorElement instanceof TNTGroup) { + ((TNTGroup) simulatorElement).getElements().forEach(SimulatorElement::change); + } + }); + } + public void edit(Player player, RayTraceResult result) { if (result == null) { TNTSimulatorGui.open(player, this, null, this::getTntElementList, null); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorSelectionGUI.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorSelectionGUI.java index 69bf9b82..ae0e822a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorSelectionGUI.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorSelectionGUI.java @@ -53,8 +53,6 @@ public class SimulatorSelectionGUI { player.closeInventory(); }); - // TODO: Add button to create new simulator with AnvilGUI - inv.open(); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTElementGUI.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTElementGUI.java index ae40232f..d572e282 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTElementGUI.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTElementGUI.java @@ -23,12 +23,14 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.simulator.OrderUtils; import de.steamwar.bausystem.features.simulator.SimulatorStorage; import de.steamwar.bausystem.features.simulator.TNTSimulator; +import de.steamwar.bausystem.features.simulator.gui.components.ChangeMaterial; +import de.steamwar.bausystem.features.simulator.gui.components.ChangePosition; +import de.steamwar.bausystem.features.simulator.gui.components.Disabled; import de.steamwar.bausystem.features.simulator.tnt.TNTElement; import de.steamwar.bausystem.features.simulator.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; @@ -50,14 +52,6 @@ public class TNTElementGUI { private static final Vector FY_VECTOR = new Vector(0, 1, 0); private static final Vector FZ_VECTOR = new Vector(0, 0, 1); - private static final List MATERIALS = new ArrayList<>(); - static { - Arrays.stream(Material.values()) - .filter(Material::isItem) - .filter(material -> !material.isLegacy()) - .forEach(MATERIALS::add); - } - private SWInventory open(Player player, String name) { SWInventory inv = new SWInventory(player, 45, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_GUI_NAME", player, name)); SWItem gray = new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§f", clickType -> {}); @@ -131,74 +125,7 @@ public class TNTElementGUI { Runnable editObserver = () -> { TNTSimulator tntSimulator = SimulatorStorage.getSimulator(player.getInventory().getItemInMainHand()); - // X Position - inv.setItem(10, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().add(clickType.isShiftClick() ? X_VECTOR : FX_VECTOR); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - })); - inv.setItem(19, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_X", player, tntElement.getPosition().getX()), lore, false, clickType -> { - changePosition(player, tntElement.getPosition().getX(), x -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().setX(clamp(x - tntElement.getParentPosition().getX())); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - editLocation(player, tntElement, back); - }, () -> editLocation(player, tntElement, back)); - })); - inv.setItem(28, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().subtract(clickType.isShiftClick() ? X_VECTOR : FX_VECTOR); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - })); - - // Y Position - inv.setItem(11, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().add(clickType.isShiftClick() ? Y_VECTOR : FY_VECTOR); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - })); - inv.setItem(20, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_Y", player, tntElement.getPosition().getY()), lore, false, clickType -> { - changePosition(player, tntElement.getPosition().getY(), y -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().setY(clamp(y - tntElement.getParentPosition().getY())); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - editLocation(player, tntElement, back); - }, () -> editLocation(player, tntElement, back)); - })); - inv.setItem(29, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().subtract(clickType.isShiftClick() ? Y_VECTOR : FY_VECTOR); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - })); - - // Z Position - inv.setItem(12, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().add(clickType.isShiftClick() ? Z_VECTOR : FZ_VECTOR); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - })); - inv.setItem(21, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_Z", player, tntElement.getPosition().getZ()), lore, false, clickType -> { - changePosition(player, tntElement.getPosition().getZ(), z -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().setZ(clamp(z - tntElement.getParentPosition().getZ())); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - editLocation(player, tntElement, back); - }, () -> editLocation(player, tntElement, back)); - })); - inv.setItem(30, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { - if (tntSimulator != null) tntSimulator.hide(tntElement); - tntElement.getOwnPosition().subtract(clickType.isShiftClick() ? Z_VECTOR : FZ_VECTOR); - if (tntSimulator != null) tntSimulator.show(tntElement); - tntElement.change(); - })); + ChangePosition.show(inv, player, tntSimulator, tntElement, tntElement::getOwnPosition, x -> x - tntElement.getParentPosition().getX(), y -> y - tntElement.getParentPosition().getY(), z -> z - tntElement.getParentPosition().getZ(), () -> editLocation(player, tntElement, back)); // Alignment /* @@ -328,36 +255,8 @@ 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(); - })); + ChangeMaterial.show(inv, player, 21, tntElement, Material.BARREL, () -> editOther(player, tntElement, back)); + Disabled.show(inv, player, 22, tntSimulator, tntElement); if (!tntElement.hasParent()) { inv.setItem(24, new SWItem(Material.TNT, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_ADD_IGNITION_PHASE", player), Arrays.asList(), false, clickType -> { @@ -421,10 +320,6 @@ public class TNTElementGUI { return (byte) (b ? 10 : 1); } - private double clamp(double d) { - return (int) (d * 100) * 0.01; - } - private void changeCount(Player player, String name, int defaultValue, Consumer result, Runnable failure) { SWAnvilInv swAnvilInv = new SWAnvilInv(player, name, defaultValue + ""); swAnvilInv.setItem(Material.PAPER); @@ -437,17 +332,4 @@ public class TNTElementGUI { }); swAnvilInv.open(); } - - private void changePosition(Player player, double defaultValue, Consumer result, Runnable failure) { - SWAnvilInv swAnvilInv = new SWAnvilInv(player, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_ANVIL_GUI_NAME", player), defaultValue + ""); - swAnvilInv.setItem(Material.PAPER); - swAnvilInv.setCallback(s -> { - try { - result.accept(Double.parseDouble(s.replace(',', '.'))); - } catch (NumberFormatException e) { - failure.run(); - } - }); - swAnvilInv.open(); - } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTGroupEditGUI.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTGroupEditGUI.java new file mode 100644 index 00000000..7cfb6754 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTGroupEditGUI.java @@ -0,0 +1,105 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.simulator.gui; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.simulator.SimulatorStorage; +import de.steamwar.bausystem.features.simulator.TNTSimulator; +import de.steamwar.bausystem.features.simulator.gui.components.ChangeMaterial; +import de.steamwar.bausystem.features.simulator.gui.components.ChangePosition; +import de.steamwar.bausystem.features.simulator.gui.components.Disabled; +import de.steamwar.bausystem.features.simulator.tnt.TNTGroup; +import de.steamwar.inventory.SWAnvilInv; +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWItem; +import lombok.experimental.UtilityClass; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.util.Consumer; + +import java.util.Arrays; +import java.util.List; +import java.util.function.UnaryOperator; + +@UtilityClass +public class TNTGroupEditGUI { + + public void open(Player player, TNTGroup tntGroup, Runnable back) { + SWInventory inv = new SWInventory(player, 45, BauSystem.MESSAGE.parse("SIMULATOR_EDIT_GROUP_MENU", player)); + SWItem gray = new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§f", clickType -> {}); + for (int i = 0; i < 9; i++) { + inv.setItem(i, gray); + inv.setItem(i + 36, gray); + } + + if (back != null) { + inv.setItem(36, new SWItem(Material.ARROW, BauSystem.MESSAGE.parse("SIMULATOR_BACK", player), clickType -> back.run())); + } + + String plusOneName = BauSystem.MESSAGE.parse("SIMULATOR_PLUS_ONE", player); + String minusOneName = BauSystem.MESSAGE.parse("SIMULATOR_MINUS_ONE", player); + List plusOneFiveShiftLore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_PLUS_FIVE_SHIFT", player)); + List minusOneFiveShiftLore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_MINUS_FIVE_SHIFT", player)); + List lore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_LORE", player)); + + Runnable editObserver = () -> { + TNTSimulator tntSimulator = SimulatorStorage.getSimulator(player.getInventory().getItemInMainHand()); + + ChangePosition.show(inv, player, tntSimulator, tntGroup, tntGroup::getPosition, UnaryOperator.identity(), UnaryOperator.identity(), UnaryOperator.identity(), () -> open(player, tntGroup, back)); + ChangeMaterial.show(inv, player, 14, tntGroup, Material.BARREL, () -> open(player, tntGroup, back)); + Disabled.show(inv, player, 32, tntSimulator, tntGroup); + + // Change TickOffset + inv.setItem(16, new SWItem(SWItem.getDye(10), plusOneName, plusOneFiveShiftLore, false, clickType -> { + tntGroup.setTickOffset(tntGroup.getTickOffset() + (clickType.isShiftClick() ? 5 : 1)); + tntGroup.change(); + })); + SWItem tickItem = new SWItem(SWItem.getMaterial("DIODE"), BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_TICK", player, tntGroup.getTickOffset()), lore, false, clickType -> changeCount(player, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_TICK_ANVIL_GUI_NAME", player), tntGroup.getTickOffset(), tick -> { + tntGroup.setTickOffset(tick); + tntGroup.change(); + open(player, tntGroup, back); + }, () -> open(player, tntGroup, back))); + tickItem.getItemStack().setAmount(Math.max(tntGroup.getTickOffset(), 1)); + inv.setItem(25, tickItem); + inv.setItem(34, new SWItem(SWItem.getDye(1), minusOneName, minusOneFiveShiftLore, false, clickType -> { + tntGroup.setTickOffset(tntGroup.getTickOffset() - (clickType.isShiftClick() ? 5 : 1)); + tntGroup.change(); + })); + }; + editObserver.run(); + tntGroup.register(editObserver, player::closeInventory); + inv.addCloseRunnable(() -> tntGroup.unregister(editObserver)); + + inv.open(); + } + + private 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 { + result.accept(Integer.parseInt(s)); + } catch (NumberFormatException e) { + failure.run(); + } + }); + swAnvilInv.open(); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTSimulatorGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTSimulatorGui.java index 083fe4a3..d959aae0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTSimulatorGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/TNTSimulatorGui.java @@ -24,19 +24,30 @@ import de.steamwar.bausystem.features.simulator.TNTSimulator; import de.steamwar.bausystem.features.simulator.tnt.SimulatorElement; import de.steamwar.bausystem.features.simulator.tnt.TNTElement; import de.steamwar.bausystem.features.simulator.tnt.TNTGroup; +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; +import org.bukkit.util.Vector; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.function.Supplier; @UtilityClass public class TNTSimulatorGui { + private static final Vector X_VECTOR = new Vector(0.0625, 0, 0); + private static final Vector Y_VECTOR = new Vector(0, 0.0625, 0); + private static final Vector Z_VECTOR = new Vector(0, 0, 0.0625); + + private static final Vector FX_VECTOR = new Vector(1, 0, 0); + private static final Vector FY_VECTOR = new Vector(0, 1, 0); + private static final Vector FZ_VECTOR = new Vector(0, 0, 1); + public void open(Player player, TNTSimulator currentTntSimulator, TNTGroup currentTntGroup, Supplier> simulatorElements, Runnable back) { List> swListEntryList = new ArrayList<>(); @@ -57,26 +68,110 @@ public class TNTSimulatorGui { } if (currentTntSimulator != null) { inv.setItem(49, new SWItem(Material.MAGENTA_GLAZED_TERRACOTTA, BauSystem.MESSAGE.parse("SIMULATOR_GUI_MOVE_ALL", player), clickType -> { + moveAll(player, currentTntSimulator, () -> open(player, currentTntSimulator, currentTntGroup, simulatorElements, back)); })); } if (currentTntGroup != null) { - List otherLore = new ArrayList<>(); - otherLore.add(""); - otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_X", player, currentTntGroup.getPosition().getX())); - otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_Y", player, currentTntGroup.getPosition().getY())); - otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_Z", player, currentTntGroup.getPosition().getZ())); - otherLore.add(""); - otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_MATERIAL_NAME_LORE", player, currentTntGroup.getMaterial().name())); - otherLore.add(""); - otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_TICK", player, currentTntGroup.getTickOffset())); - if (currentTntGroup.isDisabled()) { + Runnable editObserver = () -> { + List otherLore = new ArrayList<>(); otherLore.add(""); - otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_GUI_TNT_DISABLED", player)); - } - inv.setItem(51, new SWItem(Material.ANVIL, BauSystem.MESSAGE.parse("SIMULATOR_EDIT_GROUP", player), otherLore, false, clickType -> { - })); + otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_X", player, currentTntGroup.getPosition().getX())); + otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_Y", player, currentTntGroup.getPosition().getY())); + otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_Z", player, currentTntGroup.getPosition().getZ())); + otherLore.add(""); + otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_MATERIAL_NAME_LORE", player, currentTntGroup.getMaterial().name())); + otherLore.add(""); + otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_TICK", player, currentTntGroup.getTickOffset())); + if (currentTntGroup.isDisabled()) { + otherLore.add(""); + otherLore.add(BauSystem.MESSAGE.parse("SIMULATOR_GUI_TNT_DISABLED", player)); + } + inv.setItem(51, new SWItem(Material.ANVIL, BauSystem.MESSAGE.parse("SIMULATOR_EDIT_GROUP", player), otherLore, false, clickType -> { + TNTGroupEditGUI.open(player, currentTntGroup, () -> open(player, currentTntSimulator, currentTntGroup, simulatorElements, back)); + })); + }; + editObserver.run(); + currentTntGroup.register(editObserver, player::closeInventory); + inv.addCloseRunnable(() -> currentTntGroup.unregister(editObserver)); } inv.open(); } + + public void moveAll(Player player, TNTSimulator tntSimulator, Runnable back) { + SWInventory inv = new SWInventory(player, 45, BauSystem.MESSAGE.parse("SIMULATOR_MOVE_ALL_GUI_NAME", player)); + SWItem gray = new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§f", clickType -> {}); + for (int i = 0; i < 9; i++) { + inv.setItem(i, gray); + inv.setItem(i + 36, gray); + } + + if (back != null) { + inv.setItem(36, new SWItem(Material.ARROW, BauSystem.MESSAGE.parse("SIMULATOR_BACK", player), clickType -> back.run())); + } + + String plusOneName = BauSystem.MESSAGE.parse("SIMULATOR_PLUS_ONE", player); + String minusOneName = BauSystem.MESSAGE.parse("SIMULATOR_MINUS_ONE", player); + List plusOnePixelShiftLore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_PLUS_PIXEL_SHIFT", player)); + List minusOnePixelShiftLore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_MINUS_PIXEL_SHIFT", player)); + List lore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_LORE", player)); + + // X Position + inv.setItem(12, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { + tntSimulator.hide(); + moveAll(tntSimulator, clickType.isShiftClick() ? X_VECTOR : FX_VECTOR); + tntSimulator.show(); + tntSimulator.change(); + })); + inv.setItem(21, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_POSITION_X", player), lore, false, clickType -> {})); + inv.setItem(30, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { + tntSimulator.hide(); + moveAll(tntSimulator, (clickType.isShiftClick() ? X_VECTOR : FX_VECTOR).clone().multiply(-1)); + tntSimulator.show(); + tntSimulator.change(); + })); + + // Y Position + inv.setItem(13, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { + tntSimulator.hide(); + moveAll(tntSimulator, clickType.isShiftClick() ? Y_VECTOR : FY_VECTOR); + tntSimulator.show(); + tntSimulator.change(); + })); + inv.setItem(22, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_POSITION_Y", player), lore, false, clickType -> {})); + inv.setItem(31, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { + tntSimulator.hide(); + moveAll(tntSimulator, (clickType.isShiftClick() ? Y_VECTOR : FY_VECTOR).clone().multiply(-1)); + tntSimulator.show(); + tntSimulator.change(); + })); + + // Z Position + inv.setItem(14, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { + tntSimulator.hide(); + moveAll(tntSimulator, clickType.isShiftClick() ? Z_VECTOR : FZ_VECTOR); + tntSimulator.show(); + tntSimulator.change(); + })); + inv.setItem(23, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_POSITION_Z", player), lore, false, clickType -> {})); + inv.setItem(32, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { + tntSimulator.hide(); + moveAll(tntSimulator, (clickType.isShiftClick() ? Z_VECTOR : FZ_VECTOR).clone().multiply(-1)); + tntSimulator.show(); + })); + + inv.open(); + } + + public void moveAll(TNTSimulator tntSimulator, Vector vector) { + for (SimulatorElement element : tntSimulator.getTntElementList()) { + if (element instanceof TNTGroup) { + TNTGroup group = (TNTGroup) element; + group.getPosition().add(vector); + } else if (element instanceof TNTElement) { + TNTElement tntElement = (TNTElement) element; + tntElement.getOwnPosition().add(vector); + } + } + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/ChangeMaterial.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/ChangeMaterial.java new file mode 100644 index 00000000..5c6750fc --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/ChangeMaterial.java @@ -0,0 +1,66 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.simulator.gui.components; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.simulator.tnt.SimulatorElement; +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; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@UtilityClass +public class ChangeMaterial { + + private static final List MATERIALS = new ArrayList<>(); + static { + Arrays.stream(Material.values()) + .filter(Material::isItem) + .filter(material -> !material.isLegacy()) + .forEach(MATERIALS::add); + } + + public void show(SWInventory inv, Player player, int position, SimulatorElement simulatorElement, Material defaultMaterial, Runnable back) { + inv.setItem(position, new SWItem(simulatorElement.getMaterial(), BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_MATERIAL", player), Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_MATERIAL_LORE_1", player, simulatorElement.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) -> { + simulatorElement.setMaterial(material); + simulatorElement.change(); + back.run(); + }); + swListInv.open(); + } else { + simulatorElement.setMaterial(defaultMaterial); + simulatorElement.change(); + } + })); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/ChangePosition.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/ChangePosition.java new file mode 100644 index 00000000..19156b9f --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/ChangePosition.java @@ -0,0 +1,144 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.simulator.gui.components; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.simulator.TNTSimulator; +import de.steamwar.bausystem.features.simulator.tnt.SimulatorElement; +import de.steamwar.inventory.SWAnvilInv; +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWItem; +import lombok.experimental.UtilityClass; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.util.Consumer; +import org.bukkit.util.Vector; + +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +@UtilityClass +public class ChangePosition { + + private static final Vector X_VECTOR = new Vector(0.0625, 0, 0); + private static final Vector Y_VECTOR = new Vector(0, 0.0625, 0); + private static final Vector Z_VECTOR = new Vector(0, 0, 0.0625); + + private static final Vector FX_VECTOR = new Vector(1, 0, 0); + private static final Vector FY_VECTOR = new Vector(0, 1, 0); + private static final Vector FZ_VECTOR = new Vector(0, 0, 1); + + public void show(SWInventory inv, Player player, TNTSimulator tntSimulator, SimulatorElement simulatorElement, Supplier toChangeVector, UnaryOperator calculatePositionX, UnaryOperator calculatePositionY, UnaryOperator calculatePositionZ, Runnable back) { + String plusOneName = BauSystem.MESSAGE.parse("SIMULATOR_PLUS_ONE", player); + String minusOneName = BauSystem.MESSAGE.parse("SIMULATOR_MINUS_ONE", player); + List plusOnePixelShiftLore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_PLUS_PIXEL_SHIFT", player)); + List minusOnePixelShiftLore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_MINUS_PIXEL_SHIFT", player)); + List lore = Arrays.asList(BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_LORE", player)); + + // X Position + inv.setItem(10, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().add(clickType.isShiftClick() ? X_VECTOR : FX_VECTOR); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + })); + inv.setItem(19, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_X", player, simulatorElement.getPosition().getX()), lore, false, clickType -> { + changePosition(player, simulatorElement.getPosition().getX(), x -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().setX(clamp(calculatePositionX.apply(x))); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + back.run(); + }, back); + })); + inv.setItem(28, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().subtract(clickType.isShiftClick() ? X_VECTOR : FX_VECTOR); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + })); + + // Y Position + inv.setItem(11, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().add(clickType.isShiftClick() ? Y_VECTOR : FY_VECTOR); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + })); + inv.setItem(20, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_Y", player, simulatorElement.getPosition().getY()), lore, false, clickType -> { + changePosition(player, simulatorElement.getPosition().getY(), y -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().setY(clamp(calculatePositionY.apply(y))); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + back.run(); + }, back); + })); + inv.setItem(29, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().subtract(clickType.isShiftClick() ? Y_VECTOR : FY_VECTOR); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + })); + + // Z Position + inv.setItem(12, new SWItem(SWItem.getDye(10), plusOneName, plusOnePixelShiftLore, false, clickType -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().add(clickType.isShiftClick() ? Z_VECTOR : FZ_VECTOR); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + })); + inv.setItem(21, new SWItem(Material.PAPER, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_Z", player, simulatorElement.getPosition().getZ()), lore, false, clickType -> { + changePosition(player, simulatorElement.getPosition().getZ(), z -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().setZ(clamp(calculatePositionZ.apply(z))); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + back.run(); + }, back); + })); + inv.setItem(30, new SWItem(SWItem.getDye(1), minusOneName, minusOnePixelShiftLore, false, clickType -> { + if (tntSimulator != null) tntSimulator.hide(simulatorElement); + toChangeVector.get().subtract(clickType.isShiftClick() ? Z_VECTOR : FZ_VECTOR); + if (tntSimulator != null) tntSimulator.show(simulatorElement); + simulatorElement.change(); + })); + } + + private double clamp(double d) { + return (int) (d * 100) * 0.01; + } + + private void changePosition(Player player, double defaultValue, Consumer result, Runnable failure) { + SWAnvilInv swAnvilInv = new SWAnvilInv(player, BauSystem.MESSAGE.parse("SIMULATOR_TNT_SPAWN_POSITION_ANVIL_GUI_NAME", player), defaultValue + ""); + swAnvilInv.setItem(Material.PAPER); + swAnvilInv.setCallback(s -> { + try { + result.accept(Double.parseDouble(s.replace(',', '.'))); + } catch (NumberFormatException e) { + failure.run(); + } + }); + swAnvilInv.open(); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/Disabled.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/Disabled.java new file mode 100644 index 00000000..3ac98483 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/components/Disabled.java @@ -0,0 +1,48 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 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.simulator.gui.components; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.simulator.TNTSimulator; +import de.steamwar.bausystem.features.simulator.tnt.SimulatorElement; +import de.steamwar.inventory.SWInventory; +import de.steamwar.inventory.SWItem; +import lombok.experimental.UtilityClass; +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import java.util.ArrayList; + +@UtilityClass +public class Disabled { + + public void show(SWInventory inv, Player player, int position, TNTSimulator tntSimulator, SimulatorElement simulatorElement) { + inv.setItem(position, new SWItem(simulatorElement.isDisabled() ? Material.ENDER_PEARL : Material.ENDER_EYE, BauSystem.MESSAGE.parse(simulatorElement.isDisabled() ? "SIMULATOR_TNT_SPAWN_DISABLED" : "SIMULATOR_TNT_SPAWN_ENABLED", player), new ArrayList<>(), !simulatorElement.isDisabled(), clickType -> { + if (!simulatorElement.isDisabled()) { + tntSimulator.hide(simulatorElement); + } + simulatorElement.setDisabled(!simulatorElement.isDisabled()); + if (!simulatorElement.isDisabled()) { + tntSimulator.show(simulatorElement); + } + simulatorElement.change(); + })); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/SimulatorElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/SimulatorElement.java index 3147f208..fe44129c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/SimulatorElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/SimulatorElement.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.simulator.tnt; import de.steamwar.bausystem.features.simulator.show.SimulatorEntityShowMode; import de.steamwar.bausystem.shared.Pair; import de.steamwar.inventory.SWItem; +import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.bukkit.util.Vector; @@ -51,6 +52,9 @@ public interface SimulatorElement { // Observer default void change() { observer.getOrDefault(this, new HashSet<>()).forEach(Runnable::run); + if (this instanceof TNTGroup) { + ((TNTGroup) this).getElements().forEach(SimulatorElement::change); + } } default void register(Runnable observer, Runnable close) { SimulatorElement.observer.computeIfAbsent(this, k -> new HashSet<>()).add(observer); @@ -72,4 +76,11 @@ public interface SimulatorElement { closeRunnable.run(); }); } + + // API + Material getMaterial(); + void setMaterial(Material material); + + boolean isDisabled(); + void setDisabled(boolean disabled); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/TNTGroup.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/TNTGroup.java index f660f6c7..fddf63ad 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/TNTGroup.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/TNTGroup.java @@ -141,4 +141,12 @@ public class TNTGroup implements SimulatorElement { public void setTickOffset(int tickOffset) { this.tickOffset = tickOffset; } + + public void setMaterial(Material material) { + this.material = material; + } + + public void setDisabled(boolean disabled) { + this.disabled = disabled; + } }