From a0859923f01ea10f842cd09e1d24fe07f529b046 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 27 Oct 2023 17:41:10 +0200 Subject: [PATCH] Add SimulatorAnvilGui and add anvil gui's to everything --- .../simulator/execute/SimulatorExecutor.java | 21 ++++++ .../gui/SimulatorGroupSettingsGui.java | 7 ++ .../SimulatorRedstonePhaseSettingsGui.java | 20 +++++ .../gui/SimulatorRedstoneSettingsGui.java | 22 ++++++ .../gui/SimulatorTNTPhaseSettingsGui.java | 26 +++++++ .../gui/SimulatorTNTSettingsGui.java | 22 ++++++ .../simulator/gui/base/SimulatorAnvilGui.java | 73 +++++++++++++++++++ .../features/tracer/record/Recorder.java | 4 + .../tracer/record/SingleTraceRecorder.java | 2 +- 9 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/base/SimulatorAnvilGui.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/execute/SimulatorExecutor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/execute/SimulatorExecutor.java index cbc848e4..893161f4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/execute/SimulatorExecutor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/execute/SimulatorExecutor.java @@ -20,7 +20,12 @@ package de.steamwar.bausystem.features.simulator.execute; import de.steamwar.bausystem.features.simulator.data.Simulator; +import de.steamwar.bausystem.features.simulator.data.SimulatorElement; +import de.steamwar.bausystem.features.simulator.data.SimulatorGroup; import de.steamwar.bausystem.features.tpslimit.TPSUtils; +import de.steamwar.bausystem.features.tracer.record.Recorder; +import de.steamwar.bausystem.features.tracer.record.SingleTraceRecorder; +import de.steamwar.bausystem.region.Region; import de.steamwar.bausystem.utils.TickEndEvent; import de.steamwar.bausystem.utils.TickStartEvent; import de.steamwar.linkage.Linked; @@ -66,6 +71,22 @@ public class SimulatorExecutor implements Listener { currentlyRunning.remove(simulator); } }); + + if (simulator.isAutoTrace()) { + simulator.getGroups() + .stream() + .map(SimulatorGroup::getElements) + .flatMap(List::stream) + .map(SimulatorElement::getPosition) + .map(pos -> pos.toLocation(WORLD)) + .map(Region::getRegion) + .distinct() + .forEach(region -> { + if (Recorder.INSTANCE.isDisabled(region)) { + Recorder.INSTANCE.set(region, new SingleTraceRecorder(region)); + } + }); + } return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGroupSettingsGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGroupSettingsGui.java index 2aa11e98..4c1e8d74 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGroupSettingsGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGroupSettingsGui.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.simulator.gui; import de.steamwar.bausystem.features.simulator.SimulatorWatcher; import de.steamwar.bausystem.features.simulator.data.Simulator; import de.steamwar.bausystem.features.simulator.data.SimulatorGroup; +import de.steamwar.bausystem.features.simulator.gui.base.SimulatorAnvilGui; import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui; import de.steamwar.inventory.SWItem; import org.bukkit.Material; @@ -70,6 +71,12 @@ public class SimulatorGroupSettingsGui extends SimulatorBaseGui { SimulatorWatcher.update(simulator); }); SWItem baseTick = new SWItem(Material.REPEATER, "§eTicks§8:§7 " + baseTicks, clickType -> { + new SimulatorAnvilGui<>(player, "Ticks", baseTicks + "", Integer::parseInt, integer -> { + if (integer < 0) return false; + simulatorGroup.changeBaseTicks(integer - baseTicks); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(Material.REPEATER).open(); }); baseTick.getItemStack().setAmount(Math.max(1, Math.min(baseTicks, 64))); inventory.setItem(18, baseTick); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorRedstonePhaseSettingsGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorRedstonePhaseSettingsGui.java index 7103cb59..07f55a73 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorRedstonePhaseSettingsGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorRedstonePhaseSettingsGui.java @@ -24,6 +24,7 @@ import de.steamwar.bausystem.features.simulator.data.Simulator; import de.steamwar.bausystem.features.simulator.data.SimulatorPhase; import de.steamwar.bausystem.features.simulator.data.redstone.RedstoneElement; import de.steamwar.bausystem.features.simulator.data.redstone.RedstonePhase; +import de.steamwar.bausystem.features.simulator.gui.base.SimulatorAnvilGui; import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui; import de.steamwar.core.Core; import de.steamwar.inventory.SWItem; @@ -81,6 +82,12 @@ public class SimulatorRedstonePhaseSettingsGui extends SimulatorBaseGui { }); SWItem offsetItem = new SWItem(Material.REPEATER, "§eStart at§8:§7 " + offset, clickType -> { + new SimulatorAnvilGui<>(player, "Start at", offset + "", Integer::parseInt, integer -> { + if (integer < 0) return false; + redstone.setTickOffset(integer); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(Material.REPEATER).open(); }); offsetItem.getItemStack().setAmount(Math.max(1, Math.min(offset, 64))); inventory.setItem(20, offsetItem); @@ -98,6 +105,12 @@ public class SimulatorRedstonePhaseSettingsGui extends SimulatorBaseGui { }); SWItem lifetimeItem = new SWItem(Material.CLOCK, "§eActivation Time§8:§7 " + lifetime, clickType -> { + new SimulatorAnvilGui<>(player, "Activation Time", lifetime + "", Integer::parseInt, integer -> { + if (integer < 0) return false; + redstone.setLifetime(integer); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(Material.CLOCK).open(); }); lifetimeItem.getItemStack().setAmount(Math.max(1, Math.min(lifetime, 64))); inventory.setItem(21, lifetimeItem); @@ -116,6 +129,13 @@ public class SimulatorRedstonePhaseSettingsGui extends SimulatorBaseGui { Material negativeNumbers = Material.getMaterial(Core.getVersion() >= 19 ? "RECOVERY_COMPASS" : "FIREWORK_STAR"); SWItem orderItem = new SWItem(order >= 0 ? Material.COMPASS : negativeNumbers, "§eActivation Order§8:§7 " + order, clickType -> { + new SimulatorAnvilGui<>(player, "Activation Order", order + "", Integer::parseInt, integer -> { + if (integer < -SimulatorPhase.ORDER_LIMIT) return false; + if (integer > SimulatorPhase.ORDER_LIMIT) return false; + redstone.setOrder(integer); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(order >= 0 ? Material.COMPASS : negativeNumbers).open(); }); orderItem.getItemStack().setAmount(Math.max(1, Math.min(Math.abs(order), 30))); inventory.setItem(22, orderItem); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorRedstoneSettingsGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorRedstoneSettingsGui.java index eb0ea99e..184d73a1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorRedstoneSettingsGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorRedstoneSettingsGui.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.simulator.gui; import de.steamwar.bausystem.features.simulator.SimulatorWatcher; import de.steamwar.bausystem.features.simulator.data.Simulator; import de.steamwar.bausystem.features.simulator.data.redstone.RedstoneElement; +import de.steamwar.bausystem.features.simulator.gui.base.SimulatorAnvilGui; import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui; import de.steamwar.inventory.SWItem; import org.bukkit.Material; @@ -68,6 +69,12 @@ public class SimulatorRedstoneSettingsGui extends SimulatorBaseGui { SimulatorWatcher.update(simulator); }); SWItem baseTick = new SWItem(Material.REPEATER, "§eTicks§8:§7 " + baseTicks, clickType -> { + new SimulatorAnvilGui<>(player, "Ticks", baseTicks + "", Integer::parseInt, integer -> { + if (integer < 0) return false; + redstone.changeBaseTicks(integer - baseTicks); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(Material.REPEATER).open(); }); baseTick.getItemStack().setAmount(Math.max(1, Math.min(baseTicks, 64))); inventory.setItem(18, baseTick); @@ -86,6 +93,11 @@ public class SimulatorRedstoneSettingsGui extends SimulatorBaseGui { SimulatorWatcher.update(simulator); }); inventory.setItem(24, new SWItem(Material.PAPER, "§eX§8:§7 " + redstone.getPosition().getBlockX(), clickType -> { + new SimulatorAnvilGui<>(player, "X", redstone.getPosition().getBlockX() + "", Integer::parseInt, i -> { + redstone.getPosition().setX(i); + SimulatorWatcher.update(simulator); + return true; + }, this).open(); })); inventory.setItem(33, SWItem.getDye(1), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> { redstone.move(clickType.isShiftClick() ? -5 : -1, 0, 0); @@ -98,6 +110,11 @@ public class SimulatorRedstoneSettingsGui extends SimulatorBaseGui { SimulatorWatcher.update(simulator); }); inventory.setItem(25, new SWItem(Material.PAPER, "§eY§8:§7 " + redstone.getPosition().getBlockY(), clickType -> { + new SimulatorAnvilGui<>(player, "Y", redstone.getPosition().getBlockY() + "", Integer::parseInt, i -> { + redstone.getPosition().setY(i); + SimulatorWatcher.update(simulator); + return true; + }, this).open(); })); inventory.setItem(34, SWItem.getDye(1), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> { redstone.move(0, clickType.isShiftClick() ? -5 : -1, 0); @@ -110,6 +127,11 @@ public class SimulatorRedstoneSettingsGui extends SimulatorBaseGui { SimulatorWatcher.update(simulator); }); inventory.setItem(26, new SWItem(Material.PAPER, "§eZ§8:§7 " + redstone.getPosition().getBlockZ(), clickType -> { + new SimulatorAnvilGui<>(player, "Z", redstone.getPosition().getBlockZ() + "", Integer::parseInt, i -> { + redstone.getPosition().setZ(i); + SimulatorWatcher.update(simulator); + return true; + }, this).open(); })); inventory.setItem(35, SWItem.getDye(1), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> { redstone.move(0, 0, clickType.isShiftClick() ? -5 : -1); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorTNTPhaseSettingsGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorTNTPhaseSettingsGui.java index 4022c82b..6541c112 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorTNTPhaseSettingsGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorTNTPhaseSettingsGui.java @@ -24,6 +24,7 @@ import de.steamwar.bausystem.features.simulator.data.Simulator; import de.steamwar.bausystem.features.simulator.data.SimulatorPhase; import de.steamwar.bausystem.features.simulator.data.tnt.TNTElement; import de.steamwar.bausystem.features.simulator.data.tnt.TNTPhase; +import de.steamwar.bausystem.features.simulator.gui.base.SimulatorAnvilGui; import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui; import de.steamwar.core.Core; import de.steamwar.inventory.SWItem; @@ -81,6 +82,12 @@ public class SimulatorTNTPhaseSettingsGui extends SimulatorBaseGui { }); SWItem countItem = new SWItem(Material.TNT, "§eCount§8:§7 " + count, clickType -> { + new SimulatorAnvilGui<>(player, "Count", count + "", Integer::parseInt, integer -> { + if (integer < 0) return false; + tnt.setCount(integer); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(Material.TNT).open(); }); countItem.getItemStack().setAmount(Math.max(1, Math.min(count, 64))); inventory.setItem(18, countItem); @@ -98,6 +105,12 @@ public class SimulatorTNTPhaseSettingsGui extends SimulatorBaseGui { }); SWItem offsetItem = new SWItem(Material.REPEATER, "§eStart at§8:§7 " + offset, clickType -> { + new SimulatorAnvilGui<>(player, "Start at", offset + "", Integer::parseInt, integer -> { + if (integer < 0) return false; + tnt.setTickOffset(integer); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(Material.REPEATER).open(); }); offsetItem.getItemStack().setAmount(Math.max(1, Math.min(offset, 64))); inventory.setItem(19, offsetItem); @@ -115,6 +128,12 @@ public class SimulatorTNTPhaseSettingsGui extends SimulatorBaseGui { }); SWItem lifetimeItem = new SWItem(Material.CLOCK, "§eLifetime§8:§7 " + lifetime, clickType -> { + new SimulatorAnvilGui<>(player, "Lifetime", lifetime + "", Integer::parseInt, integer -> { + if (integer < 0) return false; + tnt.setLifetime(integer); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(Material.CLOCK).open(); }); lifetimeItem.getItemStack().setAmount(Math.max(1, Math.min(lifetime, 64))); inventory.setItem(20, lifetimeItem); @@ -133,6 +152,13 @@ public class SimulatorTNTPhaseSettingsGui extends SimulatorBaseGui { Material negativeNumbers = Material.getMaterial(Core.getVersion() >= 19 ? "RECOVERY_COMPASS" : "FIREWORK_STAR"); SWItem orderItem = new SWItem(order >= 0 ? Material.COMPASS : negativeNumbers, "§eCalculation Order§8:§7 " + order, clickType -> { + new SimulatorAnvilGui<>(player, "Calculation Order", order + "", Integer::parseInt, integer -> { + if (integer < -SimulatorPhase.ORDER_LIMIT) return false; + if (integer > SimulatorPhase.ORDER_LIMIT) return false; + tnt.setOrder(integer); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(order >= 0 ? Material.COMPASS : negativeNumbers).open(); }); orderItem.getItemStack().setAmount(Math.max(1, Math.min(Math.abs(order), 30))); inventory.setItem(22, orderItem); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorTNTSettingsGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorTNTSettingsGui.java index d880ca5e..20f695e4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorTNTSettingsGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorTNTSettingsGui.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.simulator.gui; import de.steamwar.bausystem.features.simulator.SimulatorWatcher; import de.steamwar.bausystem.features.simulator.data.Simulator; import de.steamwar.bausystem.features.simulator.data.tnt.TNTElement; +import de.steamwar.bausystem.features.simulator.gui.base.SimulatorAnvilGui; import de.steamwar.bausystem.features.simulator.gui.base.SimulatorBaseGui; import de.steamwar.inventory.SWItem; import org.bukkit.Material; @@ -78,6 +79,12 @@ public class SimulatorTNTSettingsGui extends SimulatorBaseGui { // TODO: Anvil G SimulatorWatcher.update(simulator); }); SWItem baseTick = new SWItem(Material.REPEATER, "§eTicks§8:§7 " + baseTicks, clickType -> { + new SimulatorAnvilGui<>(player, "Ticks", baseTicks + "", Integer::parseInt, integer -> { + if (integer < 0) return false; + tnt.changeBaseTicks(integer - baseTicks); + SimulatorWatcher.update(simulator); + return true; + }, this).setItem(Material.REPEATER).open(); }); baseTick.getItemStack().setAmount(Math.max(1, Math.min(baseTicks, 64))); inventory.setItem(18, baseTick); @@ -124,6 +131,11 @@ public class SimulatorTNTSettingsGui extends SimulatorBaseGui { // TODO: Anvil G SimulatorWatcher.update(simulator); }); inventory.setItem(24, new SWItem(Material.PAPER, "§eX§8:§7 " + tnt.getPosition().getX(), clickType -> { + new SimulatorAnvilGui<>(player, "X", tnt.getPosition().getX() + "", Double::parseDouble, d -> { + tnt.getPosition().setX(d); + SimulatorWatcher.update(simulator); + return true; + }, this).open(); })); inventory.setItem(33, SWItem.getDye(1), "§e-1", Arrays.asList("§7Shift§8: §e-0.0625"), false, clickType -> { tnt.move(clickType.isShiftClick() ? -0.0625 : -1, 0, 0); @@ -136,6 +148,11 @@ public class SimulatorTNTSettingsGui extends SimulatorBaseGui { // TODO: Anvil G SimulatorWatcher.update(simulator); }); inventory.setItem(25, new SWItem(Material.PAPER, "§eY§8:§7 " + tnt.getPosition().getY(), clickType -> { + new SimulatorAnvilGui<>(player, "Y", tnt.getPosition().getY() + "", Double::parseDouble, d -> { + tnt.getPosition().setY(d); + SimulatorWatcher.update(simulator); + return true; + }, this).open(); })); inventory.setItem(34, SWItem.getDye(1), "§e-1", Arrays.asList("§7Shift§8: §e-0.0625"), false, clickType -> { tnt.move(0, clickType.isShiftClick() ? -0.0625 : -1, 0); @@ -148,6 +165,11 @@ public class SimulatorTNTSettingsGui extends SimulatorBaseGui { // TODO: Anvil G SimulatorWatcher.update(simulator); }); inventory.setItem(26, new SWItem(Material.PAPER, "§eZ§8:§7 " + tnt.getPosition().getZ(), clickType -> { + new SimulatorAnvilGui<>(player, "Z", tnt.getPosition().getZ() + "", Double::parseDouble, d -> { + tnt.getPosition().setZ(d); + SimulatorWatcher.update(simulator); + return true; + }, this).open(); })); inventory.setItem(35, SWItem.getDye(1), "§e-1", Arrays.asList("§7Shift§8: §e-0.0625"), false, clickType -> { tnt.move(0, 0, clickType.isShiftClick() ? -0.0625 : -1); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/base/SimulatorAnvilGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/base/SimulatorAnvilGui.java new file mode 100644 index 00000000..e492a304 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/base/SimulatorAnvilGui.java @@ -0,0 +1,73 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.base; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.inventory.SWAnvilInv; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; + +import java.util.Objects; +import java.util.function.Consumer; +import java.util.function.Function; + +public class SimulatorAnvilGui { + + private SWAnvilInv anvilInv; + + public SimulatorAnvilGui(Player player, String title, String defaultText, Function mapper, Function value, SimulatorBaseGui back) { + if (defaultText == null) { + anvilInv = new SWAnvilInv(player, title); + } else { + anvilInv = new SWAnvilInv(player, title, defaultText); + } + anvilInv.addCloseCallback(() -> { + Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { + back.open(); + }, 0); + }); + anvilInv.setCallback(s -> { + T t; + try { + t = mapper.apply(s); + } catch (NumberFormatException e) { + new SimulatorAnvilGui<>(player, title, s, mapper, value, back).open(); + return; + } + try { + if (!value.apply(t)) { + new SimulatorAnvilGui<>(player, title, s, mapper, value, back).open(); + } + } finally { + back.open(); + } + }); + } + + public SimulatorAnvilGui setItem(Material material) { + anvilInv.setItem(material); + return this; + } + + public void open() { + anvilInv.open(); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/record/Recorder.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/record/Recorder.java index 1bd04377..1a310d35 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/record/Recorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/record/Recorder.java @@ -86,6 +86,10 @@ public class Recorder implements Listener { return get(tntTraceRecorderMap.computeIfAbsent(tntPrimed, e -> Region.getRegion(e.getLocation()))); } + public boolean isDisabled(Region region) { + return !regionTraceRecorderMap.containsKey(region); + } + public TraceRecorder get(Region region) { return regionTraceRecorderMap.getOrDefault(region, DISABLED); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/record/SingleTraceRecorder.java b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/record/SingleTraceRecorder.java index 6eae0b06..046f0368 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/record/SingleTraceRecorder.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/tracer/record/SingleTraceRecorder.java @@ -36,7 +36,7 @@ public class SingleTraceRecorder extends AutoTraceRecorder { @Override protected boolean shouldStartRecording(StartType startType) { - return startType == StartType.EXPLODE; + return startType == StartType.IGNITE; } @Override