From 35060834168ed1299dd6e9ca18483306a8cb0cec Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sun, 22 Oct 2023 14:25:54 +0200 Subject: [PATCH] Add SimulatorExecutor --- .../simulator2/SimulatorTestCommand.java | 17 ++-- .../features/simulator2/data/Simulator.java | 8 ++ .../simulator2/data/SimulatorElement.java | 9 +++ .../simulator2/data/SimulatorGroup.java | 9 +++ .../simulator2/data/SimulatorPhase.java | 7 ++ .../data/redstone/RedstonePhase.java | 32 ++++++++ .../simulator2/data/tnt/TNTPhase.java | 22 +++++ .../features/simulator2/execute/.gitkeep | 0 .../simulator2/execute/SimulatorAction.java | 36 +++++++++ .../simulator2/execute/SimulatorExecutor.java | 81 +++++++++++++++++++ .../gui/SimulatorTNTPhaseSettingsGui.java | 2 +- 11 files changed, 217 insertions(+), 6 deletions(-) delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/.gitkeep create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/SimulatorAction.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/SimulatorExecutor.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorTestCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorTestCommand.java index 82b7703e..e66d7f21 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorTestCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorTestCommand.java @@ -25,9 +25,11 @@ import de.steamwar.bausystem.features.simulator2.data.redstone.RedstoneElement; import de.steamwar.bausystem.features.simulator2.data.redstone.RedstonePhase; import de.steamwar.bausystem.features.simulator2.data.tnt.TNTElement; import de.steamwar.bausystem.features.simulator2.data.tnt.TNTPhase; +import de.steamwar.bausystem.features.simulator2.execute.SimulatorExecutor; import de.steamwar.bausystem.features.simulator2.gui.SimulatorGui; import de.steamwar.command.SWCommand; import de.steamwar.linkage.Linked; +import jdk.jfr.Registered; import org.bukkit.entity.Player; import org.bukkit.util.Vector; @@ -39,18 +41,23 @@ public class SimulatorTestCommand extends SWCommand { public SimulatorTestCommand() { super("simtest"); - SimulatorGroup group1 = new SimulatorGroup().add(new TNTElement(new Vector(0, 0, 0)).add(new TNTPhase())).add(new TNTElement(new Vector(0, 0, 0)).add(new TNTPhase())); + SimulatorGroup group1 = new SimulatorGroup().add(new TNTElement(new Vector(-477.5, 46, 311.5)).add(new TNTPhase())); SIMULATOR.getElements().add(group1); - SimulatorGroup group2 = new SimulatorGroup().add(new TNTElement(new Vector(0, 0, 0)).add(new TNTPhase())); + SimulatorGroup group2 = new SimulatorGroup().add(new TNTElement(new Vector(-477.5, 47, 312.5)).add(new TNTPhase())); SIMULATOR.getElements().add(group2); - SimulatorGroup group3 = new SimulatorGroup().add(new RedstoneElement(new Vector(0, 0, 0)).add(new RedstonePhase())); + SimulatorGroup group3 = new SimulatorGroup().add(new RedstoneElement(new Vector(-484, 47, 307)).add(new RedstonePhase())); SIMULATOR.getElements().add(group3); } - @Register - public void genericCommand(Player player, String... args) { + @Register("open") + public void openCommand(Player player) { new SimulatorGui(player, SIMULATOR).open(); } + + @Register("run") + public void runCommand(Player player) { + SimulatorExecutor.run(SIMULATOR); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/Simulator.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/Simulator.java index df486e6c..73f0ab1d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/Simulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/Simulator.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.features.simulator2.data; +import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction; import de.steamwar.inventory.InvCallback; import de.steamwar.inventory.SWItem; import lombok.Getter; @@ -29,6 +30,7 @@ import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; +import java.util.Map; @Getter @Setter @@ -48,4 +50,10 @@ public class Simulator { public SWItem toItem(Player player, InvCallback invCallback) { return new SWItem(material, "§e" + name, invCallback); } + + public void toSimulatorActions(Map> actions) { + elements.forEach(simulatorGroup -> { + simulatorGroup.toSimulatorActions(actions); + }); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorElement.java index 3a5cf861..3f5136c0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorElement.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.features.simulator2.data; +import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction; import de.steamwar.inventory.InvCallback; import de.steamwar.inventory.SWItem; import lombok.Getter; @@ -30,6 +31,7 @@ import org.bukkit.util.Vector; import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.Map; @Getter @Setter @@ -88,4 +90,11 @@ public abstract class SimulatorElement { } return new SWItem(material, "§e" + getName(), lore, disabled, invCallback); } + + public void toSimulatorActions(Map> actions) { + if (disabled) return; + phases.forEach(phase -> { + phase.toSimulatorActions(actions, position.clone()); + }); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorGroup.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorGroup.java index 86bcba99..1c3ac29c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorGroup.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorGroup.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.features.simulator2.data; +import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction; import de.steamwar.inventory.InvCallback; import de.steamwar.inventory.SWItem; import lombok.Getter; @@ -29,6 +30,7 @@ import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.Comparator; import java.util.List; +import java.util.Map; @Getter @Setter @@ -79,4 +81,11 @@ public class SimulatorGroup { return new SWItem(material, "§eGroup", lore, disabled, groupCallback); } } + + public void toSimulatorActions(Map> actions) { + if (disabled) return; + elements.forEach(simulatorElement -> { + simulatorElement.toSimulatorActions(actions); + }); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorPhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorPhase.java index cc9c0011..126775b5 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorPhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorPhase.java @@ -19,8 +19,13 @@ package de.steamwar.bausystem.features.simulator2.data; +import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction; import lombok.Getter; import lombok.Setter; +import org.bukkit.util.Vector; + +import java.util.List; +import java.util.Map; @Getter @Setter @@ -30,4 +35,6 @@ public abstract class SimulatorPhase { protected int tickOffset = 0; protected int lifetime = 80; protected int order = 1; + + public abstract void toSimulatorActions(Map> actions, Vector position); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstonePhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstonePhase.java index 54686631..e8b1c210 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstonePhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstonePhase.java @@ -20,8 +20,18 @@ package de.steamwar.bausystem.features.simulator2.data.redstone; import de.steamwar.bausystem.features.simulator2.data.SimulatorPhase; +import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction; import lombok.NoArgsConstructor; import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.util.Vector; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; @NoArgsConstructor public class RedstonePhase extends SimulatorPhase { @@ -33,4 +43,26 @@ public class RedstonePhase extends SimulatorPhase { { this.lifetime = 1; } + + @Override + public void toSimulatorActions(Map> actions, Vector position) { + AtomicReference previousBlockState = new AtomicReference<>(); + actions.computeIfAbsent(tickOffset, __ -> new ArrayList<>()) + .add(new SimulatorAction(0, 1) { + @Override + public void accept(World world) { + Block block = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); + previousBlockState.set(block.getState()); + block.setType(Material.REDSTONE_BLOCK); + } + }); + + actions.computeIfAbsent(tickOffset + lifetime, __ -> new ArrayList<>()) + .add(new SimulatorAction(0, 1) { + @Override + public void accept(World world) { + previousBlockState.get().update(true, true); + } + }); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTPhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTPhase.java index 0f04cdd9..36c2d2f6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTPhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTPhase.java @@ -20,9 +20,17 @@ package de.steamwar.bausystem.features.simulator2.data.tnt; import de.steamwar.bausystem.features.simulator2.data.SimulatorPhase; +import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; +import org.bukkit.World; +import org.bukkit.entity.TNTPrimed; +import org.bukkit.util.Vector; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; @Getter @Setter @@ -46,4 +54,18 @@ public class TNTPhase extends SimulatorPhase { yJump = jump; zJump = jump; } + + @Override + public void toSimulatorActions(Map> actions, Vector position) { + actions.computeIfAbsent(tickOffset, __ -> new ArrayList<>()).add(new SimulatorAction(order, count) { + @Override + public void accept(World world) { + TNTPrimed tnt = world.spawn(position.toLocation(world), TNTPrimed.class); + if (!xJump) tnt.setVelocity(tnt.getVelocity().setX(0)); + if (!yJump) tnt.setVelocity(tnt.getVelocity().setY(0)); + if (!zJump) tnt.setVelocity(tnt.getVelocity().setZ(0)); + tnt.setFuseTicks(lifetime); + } + }); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/.gitkeep b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/SimulatorAction.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/SimulatorAction.java new file mode 100644 index 00000000..bacc0ffd --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/SimulatorAction.java @@ -0,0 +1,36 @@ +/* + * 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.simulator2.execute; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.World; + +import java.util.function.Consumer; + +@Getter +@AllArgsConstructor +public abstract class SimulatorAction implements Consumer { + private int order; + + @Setter + private int count; +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/SimulatorExecutor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/SimulatorExecutor.java new file mode 100644 index 00000000..0f755aa6 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/execute/SimulatorExecutor.java @@ -0,0 +1,81 @@ +/* + * 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.simulator2.execute; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.simulator2.data.Simulator; +import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; +import org.bukkit.World; + +import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; + +@UtilityClass +public class SimulatorExecutor { + + private static final World WORLD = Bukkit.getWorlds().get(0); + private static Set currentlyRunning = new HashSet<>(); + + public boolean run(Simulator simulator) { + if (currentlyRunning.contains(simulator)) return false; + + Map> map = new HashMap<>(); + simulator.toSimulatorActions(map); + + Map>> actions = new HashMap<>(); + map.forEach((integer, simulatorActions) -> { + simulatorActions.forEach(simulatorAction -> { + actions.computeIfAbsent(integer, __ -> new HashMap<>()).computeIfAbsent(simulatorAction.getOrder(), __ -> new ArrayList<>()).add(simulatorAction); + }); + }); + + AtomicInteger tick = new AtomicInteger(); + BauSystem.runTaskTimer(BauSystem.getInstance(), bukkitTask -> { + if (actions.isEmpty()) { + bukkitTask.cancel(); + currentlyRunning.remove(simulator); + return; + } + + int currentTick = tick.getAndIncrement(); + Map> actionsToRun = actions.remove(currentTick); + if (actionsToRun == null) return; + List keys = new ArrayList<>(actionsToRun.keySet()); + keys.sort(null); + + for (int actionKey : keys) { + List keyedActions = actionsToRun.get(actionKey); + while (!keyedActions.isEmpty()) { + Collections.shuffle(keyedActions); + for (int i = keyedActions.size() - 1 ; i >= 0; i--) { + SimulatorAction action = keyedActions.get(i); + action.accept(WORLD); + action.setCount(action.getCount() - 1); + if (action.getCount() == 0) { + keyedActions.remove(i); + } + } + } + } + }, 0, 1); + return true; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorTNTPhaseSettingsGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorTNTPhaseSettingsGui.java index 2f8d548a..4e68d0dd 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorTNTPhaseSettingsGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorTNTPhaseSettingsGui.java @@ -136,7 +136,7 @@ public class SimulatorTNTPhaseSettingsGui extends SimulatorBaseGui { orderItem.getItemStack().setAmount(Math.max(1, Math.min(order, 30))); inventory.setItem(22, orderItem); - inventory.setItem(31, SWItem.getDye(order > -30 ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> { + inventory.setItem(31, SWItem.getDye(order > -SimulatorPhase.ORDER_LIMIT ? 1 : 8), "§e-1", Arrays.asList("§7Shift§8: §e-5"), false, clickType -> { tnt.setOrder(Math.max(-30, order - (clickType.isShiftClick() ? 5 : 1))); SimulatorWatcher.update(simulator); });