From c05724efda4d2a802a8c4e0905b6142a50aaa47f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 27 Oct 2023 16:34:54 +0200 Subject: [PATCH] Update SimulatorExecutor and fix many bugs --- .../bausystem/utils/TickListener15.java | 23 ++++ BauSystem_19/build.gradle | 1 + .../bausystem/utils/TickListener19.java | 51 ++++++++ .../src/de/steamwar/bausystem/BauSystem.java | 2 + .../features/simulator/SimulatorCommand.java | 2 + .../features/simulator/SimulatorCursor.java | 6 +- .../features/simulator/SimulatorStorage.java | 2 + .../features/simulator/SimulatorWatcher.java | 4 +- .../features/simulator/data/Simulator.java | 13 ++- .../simulator/data/SimulatorElement.java | 7 +- .../simulator/data/SimulatorGroup.java | 9 +- .../simulator/data/SimulatorPhase.java | 3 +- .../data/redstone/RedstonePhase.java | 50 ++++---- .../features/simulator/data/tnt/TNTPhase.java | 5 +- .../simulator/execute/SimulatorExecutor.java | 109 +++++++++++------- .../gui/SimulatorGroupChooserGui.java | 4 +- .../features/simulator/gui/SimulatorGui.java | 4 +- .../gui/SimulatorTNTSettingsGui.java | 2 +- .../storage/SimFormatSimulatorLoader.java | 4 +- .../simulator/storage/SimulatorSaver.java | 2 +- .../bausystem/utils/TickEndEvent.java | 36 ++++++ .../bausystem/utils/TickListener.java | 31 +++++ .../bausystem/utils/TickStartEvent.java | 36 ++++++ build.gradle | 4 + 24 files changed, 316 insertions(+), 94 deletions(-) create mode 100644 BauSystem_15/src/de/steamwar/bausystem/utils/TickListener15.java create mode 100644 BauSystem_19/src/de/steamwar/bausystem/utils/TickListener19.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/utils/TickEndEvent.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/utils/TickListener.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/utils/TickStartEvent.java diff --git a/BauSystem_15/src/de/steamwar/bausystem/utils/TickListener15.java b/BauSystem_15/src/de/steamwar/bausystem/utils/TickListener15.java new file mode 100644 index 00000000..f2b604ac --- /dev/null +++ b/BauSystem_15/src/de/steamwar/bausystem/utils/TickListener15.java @@ -0,0 +1,23 @@ +/* + * 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.utils; + +public class TickListener15 implements TickListener { +} diff --git a/BauSystem_19/build.gradle b/BauSystem_19/build.gradle index e7be349d..b1c37d01 100644 --- a/BauSystem_19/build.gradle +++ b/BauSystem_19/build.gradle @@ -51,6 +51,7 @@ dependencies { implementation project(":BauSystem_Main") compileOnly 'org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT' + compileOnly 'io.papermc.paper:paper-api:1.19.2-R0.1-SNAPSHOT' compileOnly 'it.unimi.dsi:fastutil:8.5.6' compileOnly 'com.mojang:datafixerupper:4.0.26' compileOnly 'io.netty:netty-all:4.1.68.Final' diff --git a/BauSystem_19/src/de/steamwar/bausystem/utils/TickListener19.java b/BauSystem_19/src/de/steamwar/bausystem/utils/TickListener19.java new file mode 100644 index 00000000..5191b38b --- /dev/null +++ b/BauSystem_19/src/de/steamwar/bausystem/utils/TickListener19.java @@ -0,0 +1,51 @@ +/* + * 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.utils; + +import com.destroystokyo.paper.event.server.ServerTickEndEvent; +import com.destroystokyo.paper.event.server.ServerTickStartEvent; +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.tpslimit.TPSFreezeUtils; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +public class TickListener19 implements TickListener, Listener { + + private boolean tickStartRan = false; + + public TickListener19() { + Bukkit.getPluginManager().registerEvents(this, BauSystem.getInstance()); + } + + @EventHandler + public void onServerTickStart(ServerTickStartEvent event) { + if (TPSFreezeUtils.isFrozen()) return; + Bukkit.getPluginManager().callEvent(new TickStartEvent()); + tickStartRan = true; + } + + @EventHandler + public void onServerTickEnd(ServerTickEndEvent event) { + if (!tickStartRan) return; + Bukkit.getPluginManager().callEvent(new TickEndEvent()); + tickStartRan = false; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index bb60156f..10b36297 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -27,6 +27,7 @@ import de.steamwar.bausystem.linkage.LinkageUtils; import de.steamwar.bausystem.region.loader.PrototypeLoader; import de.steamwar.bausystem.region.loader.RegionLoader; import de.steamwar.bausystem.region.loader.Updater; +import de.steamwar.bausystem.utils.TickListener; import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.message.Message; import lombok.Getter; @@ -76,6 +77,7 @@ public class BauSystem extends JavaPlugin implements Listener { LinkageUtils.link(); RamUsage.init(); + TickListener.impl.init(); // This could disable any watchdog stuff. We need to investigate if this is a problem. /* diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java index 5f320fc5..56991431 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java @@ -30,12 +30,14 @@ import de.steamwar.command.TypeMapper; import de.steamwar.command.TypeValidator; import de.steamwar.linkage.Linked; import de.steamwar.linkage.LinkedInstance; +import de.steamwar.linkage.MinVersion; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import java.util.Collection; @Linked +@MinVersion(19) public class SimulatorCommand extends SWCommand { @LinkedInstance diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCursor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCursor.java index 39564fa6..ff545a86 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCursor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCursor.java @@ -41,6 +41,7 @@ import de.steamwar.entity.REntityServer; import de.steamwar.entity.RFallingBlockEntity; import de.steamwar.inventory.SWAnvilInv; import de.steamwar.linkage.Linked; +import de.steamwar.linkage.MinVersion; import lombok.AllArgsConstructor; import lombok.Getter; import org.bukkit.Bukkit; @@ -66,6 +67,7 @@ import java.util.function.Function; import java.util.stream.Collectors; @Linked +@MinVersion(19) public class SimulatorCursor implements Listener { private final World WORLD = Bukkit.getWorlds().get(0); @@ -366,7 +368,7 @@ public class SimulatorCursor implements Listener { if (rayTraceResult.getHitEntity() != null) { REntity hitEntity = rayTraceResult.getHitEntity(); Vector vector = new Vector(hitEntity.getX(), hitEntity.getY(), hitEntity.getZ()); - List> elements = simulator.getElements().stream().map(SimulatorGroup::getElements).flatMap(List::stream).filter(element -> { + List> elements = simulator.getGroups().stream().map(SimulatorGroup::getElements).flatMap(List::stream).filter(element -> { return element.getWorldPos().distanceSquared(vector) < (1 / 16.0) * (1 / 16.0); }).collect(Collectors.toList()); @@ -414,7 +416,7 @@ public class SimulatorCursor implements Listener { } SimulatorElement element = type.elementFunction.apply(vector); SimulatorGroup group = new SimulatorGroup().add(element); - simulator.getElements().add(group); + simulator.getGroups().add(group); SimulatorGui simulatorGui = new SimulatorGui(player, simulator); element.open(player, simulator, group, simulatorGui); SimulatorWatcher.update(simulator); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorStorage.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorStorage.java index 071a2acd..9ad4701d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorStorage.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorStorage.java @@ -31,6 +31,7 @@ import de.steamwar.bausystem.features.simulator.storage.YAPIONFormatSimulatorLoa import de.steamwar.bausystem.utils.ItemUtils; import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; +import de.steamwar.linkage.MinVersion; import de.steamwar.linkage.api.Enable; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -44,6 +45,7 @@ import java.io.IOException; import java.util.*; @Linked +@MinVersion(19) public class SimulatorStorage implements Enable { public static final File simulatorsDir = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "simulators"); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorWatcher.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorWatcher.java index 1a3f3987..4ea413d8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorWatcher.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorWatcher.java @@ -28,6 +28,7 @@ import de.steamwar.entity.REntity; import de.steamwar.entity.REntityServer; import de.steamwar.entity.RFallingBlockEntity; import de.steamwar.linkage.Linked; +import de.steamwar.linkage.MinVersion; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import org.bukkit.Location; @@ -73,6 +74,7 @@ public class SimulatorWatcher { } @Linked + @MinVersion(19) public static class QuitListener implements Listener { @EventHandler @@ -87,7 +89,7 @@ public class SimulatorWatcher { return null; } Map>> positionCache = new HashMap<>(); - simulator.getElements().stream().map(group -> group.getElements().stream().map(element -> new Pair<>(group, element)).collect(Collectors.toList())).flatMap(List::stream).forEach(pair -> { + simulator.getGroups().stream().map(group -> group.getElements().stream().map(element -> new Pair<>(group, element)).collect(Collectors.toList())).flatMap(List::stream).forEach(pair -> { SimulatorGroup group = pair.getKey(); SimulatorElement element = pair.getValue(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/Simulator.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/Simulator.java index 75e79765..91b7a8e1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/Simulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/Simulator.java @@ -31,6 +31,7 @@ import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; @Getter @Setter @@ -39,10 +40,10 @@ public final class Simulator { private Material material = Material.BARREL; private final String name; private boolean autoTrace = false; - private final List elements = new ArrayList<>(); + private final List groups = new ArrayList<>(); public void move(int x, int y, int z) { - elements.forEach(simulatorGroup -> { + groups.forEach(simulatorGroup -> { simulatorGroup.move(x, y, z); }); } @@ -51,14 +52,14 @@ public final class Simulator { return new SWItem(material, "§e" + name, invCallback); } - public void toSimulatorActions(Map> actions) { - elements.forEach(simulatorGroup -> { - simulatorGroup.toSimulatorActions(actions); + public void toSimulatorActions(BiConsumer tickStart, BiConsumer tickEnd) { + groups.forEach(simulatorGroup -> { + simulatorGroup.toSimulatorActions(tickStart, tickEnd); }); } public Simulator add(SimulatorGroup group) { - elements.add(group); + groups.add(group); return this; } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorElement.java index a79a7dad..df5b13a4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorElement.java @@ -34,6 +34,7 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; @Getter @Setter @@ -101,17 +102,17 @@ public abstract class SimulatorElement { return new SWItem(material, "§e" + getName(player), lore, disabled, invCallback); } - public void toSimulatorActions(Map> actions) { + public void toSimulatorActions(BiConsumer tickStart, BiConsumer tickEnd) { if (disabled) return; phases.forEach(phase -> { - phase.toSimulatorActions(actions, position.clone()); + phase.toSimulatorActions(position.clone(), tickStart, tickEnd); }); } public abstract void open(Player player, Simulator simulator, SimulatorGroup group, SimulatorBaseGui back); public SimulatorGroup getGroup(Simulator simulator) { - return simulator.getElements().stream() + return simulator.getGroups().stream() .filter(simulatorGroup -> simulatorGroup.getElements().contains(this)) .findFirst() .orElse(null); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorGroup.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorGroup.java index 3b09e8b3..0e90903c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorGroup.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorGroup.java @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; @Getter @Setter @@ -40,7 +41,9 @@ public final class SimulatorGroup { private final List> elements = new ArrayList<>(); public SimulatorGroup add(SimulatorElement element) { - elements.add(element); + if (!elements.contains(element)) { + elements.add(element); + } return this; } @@ -82,10 +85,10 @@ public final class SimulatorGroup { } } - public void toSimulatorActions(Map> actions) { + public void toSimulatorActions(BiConsumer tickStart, BiConsumer tickEnd) { if (disabled) return; elements.forEach(simulatorElement -> { - simulatorElement.toSimulatorActions(actions); + simulatorElement.toSimulatorActions(tickStart, tickEnd); }); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorPhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorPhase.java index 33009044..b226d124 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorPhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/SimulatorPhase.java @@ -27,6 +27,7 @@ import yapion.hierarchy.types.YAPIONObject; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; @Getter @Setter @@ -37,7 +38,7 @@ public abstract class SimulatorPhase { protected int lifetime = 80; protected int order = 0; - public abstract void toSimulatorActions(Map> actions, Vector position); + public abstract void toSimulatorActions(Vector position, BiConsumer tickStart, BiConsumer tickEnd); public void saveExtra(YAPIONObject phaseObject) {} public void loadExtra(YAPIONObject phaseObject) {} } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/redstone/RedstonePhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/redstone/RedstonePhase.java index f67dfb45..e54abf62 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/redstone/RedstonePhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/redstone/RedstonePhase.java @@ -20,6 +20,7 @@ package de.steamwar.bausystem.features.simulator.data.redstone; +import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.simulator.data.SimulatorPhase; import de.steamwar.bausystem.features.simulator.execute.SimulatorAction; import lombok.NoArgsConstructor; @@ -33,6 +34,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BiConsumer; @NoArgsConstructor public final class RedstonePhase extends SimulatorPhase { @@ -46,31 +48,27 @@ public final class RedstonePhase extends SimulatorPhase { } @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) { - // TODO: 0 Tick Pistons not working - Block block = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); - previousBlockState.set(block.getState()); - block.setType(Material.REDSTONE_BLOCK); - if (lifetime == 0) { - previousBlockState.get().update(true, true); - } - } - }); - - if (lifetime == 0) { - return; - } - actions.computeIfAbsent(tickOffset + lifetime, __ -> new ArrayList<>()) - .add(new SimulatorAction(0, 1) { - @Override - public void accept(World world) { - previousBlockState.get().update(true, true); - } - }); + public void toSimulatorActions(Vector position, BiConsumer tickStart, BiConsumer tickEnd) { + AtomicReference blockState = new AtomicReference<>(); + tickStart.accept(tickOffset, new SimulatorAction(order, 1) { + @Override + public void accept(World world) { + Block block = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); + blockState.set(block.getState()); + block.setType(Material.REDSTONE_BLOCK); + } + }); + tickEnd.accept(tickOffset + lifetime, new SimulatorAction(0, 1) { + @Override + public void accept(World world) { + BlockState state = blockState.get(); + if (state != null) { + state.update(true, true); + } else { + Block block = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); + block.setType(Material.AIR); + } + } + }); } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/tnt/TNTPhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/tnt/TNTPhase.java index d776edb8..7583834b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/tnt/TNTPhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/data/tnt/TNTPhase.java @@ -32,6 +32,7 @@ import yapion.hierarchy.types.YAPIONObject; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; @Getter @Setter @@ -57,8 +58,8 @@ public final class TNTPhase extends SimulatorPhase { } @Override - public void toSimulatorActions(Map> actions, Vector position) { - actions.computeIfAbsent(tickOffset, __ -> new ArrayList<>()).add(new SimulatorAction(order, count) { + public void toSimulatorActions(Vector position, BiConsumer tickStart, BiConsumer tickEnd) { + tickStart.accept(tickOffset, new SimulatorAction(order, count) { @Override public void accept(World world) { TNTPrimed tnt = world.spawn(position.toLocation(world), TNTPrimed.class); 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 65f15009..cbc848e4 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 @@ -19,63 +19,88 @@ package de.steamwar.bausystem.features.simulator.execute; -import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.simulator.data.Simulator; -import lombok.experimental.UtilityClass; +import de.steamwar.bausystem.features.tpslimit.TPSUtils; +import de.steamwar.bausystem.utils.TickEndEvent; +import de.steamwar.bausystem.utils.TickStartEvent; +import de.steamwar.linkage.Linked; +import de.steamwar.linkage.MinVersion; import org.bukkit.Bukkit; import org.bukkit.World; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import java.util.*; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; -@UtilityClass -public class SimulatorExecutor { +@Linked +@MinVersion(19) +public class SimulatorExecutor implements Listener { private static final World WORLD = Bukkit.getWorlds().get(0); private static Set currentlyRunning = new HashSet<>(); + private static Map>> tickStartActions = new HashMap<>(); + private static Map> tickEndActions = new HashMap<>(); - public boolean run(Simulator simulator) { + public static boolean run(Simulator simulator) { if (currentlyRunning.contains(simulator)) return false; + currentlyRunning.add(simulator); - 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); - }); + long currentTick = TPSUtils.currentRealTick.get(); + AtomicLong lastTick = new AtomicLong(); + simulator.toSimulatorActions((tickOffset, simulatorAction) -> { + lastTick.set(Math.max(lastTick.get(), tickOffset)); + tickStartActions.computeIfAbsent(currentTick + tickOffset, __ -> new HashMap<>()) + .computeIfAbsent(simulatorAction.getOrder(), __ -> new ArrayList<>()) + .add(simulatorAction); + }, (tickOffset, simulatorAction) -> { + lastTick.set(Math.max(lastTick.get(), tickOffset)); + tickEndActions.computeIfAbsent(currentTick + tickOffset, __ -> 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); - } + tickEndActions.computeIfAbsent(currentTick + lastTick.get() + 4, __ -> new ArrayList<>()) + .add(new SimulatorAction(0, 1) { + @Override + public void accept(World world) { + currentlyRunning.remove(simulator); } - } - } - }, 0, 1); + }); return true; } + + @EventHandler + public void onTickStart(TickStartEvent event) { + long currentTick = TPSUtils.currentRealTick.get(); + Map> actionsToRun = tickStartActions.remove(currentTick); + if (actionsToRun == null) return; + + List keys = new ArrayList<>(actionsToRun.keySet()); + keys.sort(null); + for (int actionKey : keys) { + runActions(actionsToRun.get(actionKey)); + } + } + + @EventHandler + public void onTickEnd(TickEndEvent event) { + long currentTick = TPSUtils.currentRealTick.get() - 1; + List actionsToRun = tickEndActions.remove(currentTick); + if (actionsToRun == null) return; + runActions(actionsToRun); + } + + private void runActions(List actionsToRun) { + while (!actionsToRun.isEmpty()) { + Collections.shuffle(actionsToRun); + for (int i = actionsToRun.size() - 1 ; i >= 0; i--) { + SimulatorAction action = actionsToRun.get(i); + action.accept(WORLD); + action.setCount(action.getCount() - 1); + if (action.getCount() == 0) { + actionsToRun.remove(i); + } + } + } + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGroupChooserGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGroupChooserGui.java index c431420c..9778c6c6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGroupChooserGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGroupChooserGui.java @@ -39,7 +39,7 @@ public class SimulatorGroupChooserGui extends SimulatorPageGui { private final SimulatorBaseGui back; public SimulatorGroupChooserGui(Player player, Simulator simulator, SimulatorElement subject, SimulatorGroup parent, SimulatorBaseGui back) { - super(player, simulator, 6 * 9, simulator.getElements().stream().filter(e -> e != parent).collect(Collectors.toList())); + super(player, simulator, 6 * 9, simulator.getGroups().stream().filter(e -> e != parent).collect(Collectors.toList())); this.subject = subject; this.parent = parent; this.back = back; @@ -53,7 +53,7 @@ public class SimulatorGroupChooserGui extends SimulatorPageGui { inventory.setItem(49, new SWItem(Material.BARRIER, "§cRemove from Group", clickType -> { SimulatorGroup newParent = new SimulatorGroup(); newParent.add(subject); - simulator.getElements().add(newParent); + simulator.getGroups().add(newParent); parent.getElements().remove(subject); back.open(); SimulatorWatcher.update(simulator); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGui.java index dfc612ec..e35f0eaa 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/gui/SimulatorGui.java @@ -31,7 +31,7 @@ import org.bukkit.entity.Player; public class SimulatorGui extends SimulatorPageGui { public SimulatorGui(Player player, Simulator simulator) { - super(player, simulator, 6 * 9, simulator.getElements()); + super(player, simulator, 6 * 9, simulator.getGroups()); } @Override @@ -41,7 +41,7 @@ public class SimulatorGui extends SimulatorPageGui { @Override public void headerAndFooter() { - if (simulator.getElements().removeIf(element -> element.getElements().isEmpty() || element.getElements().stream().allMatch(simulatorElement -> simulatorElement.getPhases().isEmpty()))) { + if (simulator.getGroups().removeIf(element -> element.getElements().isEmpty() || element.getElements().stream().allMatch(simulatorElement -> simulatorElement.getPhases().isEmpty()))) { SimulatorWatcher.update(simulator); } 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 e1698c6f..d880ca5e 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 @@ -32,7 +32,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class SimulatorTNTSettingsGui extends SimulatorBaseGui { +public class SimulatorTNTSettingsGui extends SimulatorBaseGui { // TODO: Anvil GUI's! private final TNTElement tnt; private final SimulatorBaseGui back; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/storage/SimFormatSimulatorLoader.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/storage/SimFormatSimulatorLoader.java index 18089002..15af2e4a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/storage/SimFormatSimulatorLoader.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/storage/SimFormatSimulatorLoader.java @@ -83,8 +83,8 @@ public class SimFormatSimulatorLoader implements SimulatorLoader { elements.streamObject().forEach(elementObject -> { SimulatorElement element; Supplier phaseConstructor; - Vector position = new Vector(groupObject.getDouble("x"), groupObject.getDouble("y"), groupObject.getDouble("z")); - String type = groupObject.getPlainValue("type"); + Vector position = new Vector(elementObject.getDouble("x"), elementObject.getDouble("y"), elementObject.getDouble("z")); + String type = elementObject.getPlainValue("type"); switch (type) { case "TNT": element = new TNTElement(position); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/storage/SimulatorSaver.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/storage/SimulatorSaver.java index 67260e4f..285be22f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/storage/SimulatorSaver.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/storage/SimulatorSaver.java @@ -37,7 +37,7 @@ public class SimulatorSaver { simulatorObject.add("autoTrace", simulator.isAutoTrace()); YAPIONArray groups = new YAPIONArray(); - simulator.getElements().forEach(group -> { + simulator.getGroups().forEach(group -> { YAPIONObject groupObject = new YAPIONObject(); groupObject.add("material", group.getMaterial().name()); groupObject.add("disabled", group.isDisabled()); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/TickEndEvent.java b/BauSystem_Main/src/de/steamwar/bausystem/utils/TickEndEvent.java new file mode 100644 index 00000000..11b90f80 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/utils/TickEndEvent.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.utils; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class TickEndEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/TickListener.java b/BauSystem_Main/src/de/steamwar/bausystem/utils/TickListener.java new file mode 100644 index 00000000..029a2e5c --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/utils/TickListener.java @@ -0,0 +1,31 @@ +/* + * 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.utils; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.core.VersionDependent; + +public interface TickListener { + + TickListener impl = VersionDependent.getVersionImpl(BauSystem.getInstance()); + + default void init() { + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/utils/TickStartEvent.java b/BauSystem_Main/src/de/steamwar/bausystem/utils/TickStartEvent.java new file mode 100644 index 00000000..1134a832 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/utils/TickStartEvent.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.utils; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class TickStartEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/build.gradle b/build.gradle index 48c8df51..5ce3ebaf 100644 --- a/build.gradle +++ b/build.gradle @@ -61,6 +61,10 @@ allprojects { maven { url = uri('https://libraries.minecraft.net') } + + maven { + url = uri("https://repo.papermc.io/repository/maven-public/") + } } }