From 9155bd70704f625e2540d9a638dd0b6fa1517de5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 11 Jun 2022 16:53:49 +0200 Subject: [PATCH] Add starting of TNTSimulator Signed-off-by: yoyosource --- .../features/simulator/SimulatorCommand.java | 2 +- .../features/simulator/TNTSimulator.java | 55 +++++++++++++++++++ .../simulator/TNTSimulatorListener.java | 2 +- .../simulator/tnt/SimulatorElement.java | 2 +- .../features/simulator/tnt/TNTElement.java | 5 +- .../features/simulator/tnt/TNTGroup.java | 3 +- 6 files changed, 63 insertions(+), 6 deletions(-) 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 c82c3273..ec7d535f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/SimulatorCommand.java @@ -83,7 +83,7 @@ public class SimulatorCommand extends SWCommand { BauSystem.MESSAGE.send("SIMULATOR_NOT_EXISTS", p); return; } - // SimulatorStorage.start(name); + tntSimulator.start(); } @Mapper("simulators") 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 57af3266..275ecab9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java @@ -19,13 +19,18 @@ package de.steamwar.bausystem.features.simulator; +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.configplayer.Config; import de.steamwar.bausystem.features.simulator.gui.TNTElementGUI; import de.steamwar.bausystem.features.simulator.gui.TNTSimulatorGui; import de.steamwar.bausystem.features.simulator.show.SimulatorEntityShowMode; 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.bausystem.features.tracer.record.RecordStateMachine; +import de.steamwar.bausystem.shared.Pair; import lombok.Getter; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -35,6 +40,7 @@ import yapion.hierarchy.types.YAPIONObject; import yapion.hierarchy.types.YAPIONType; import java.util.*; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; @Getter @@ -201,4 +207,53 @@ public class TNTSimulator { show(p); }); } + + public void start() { + Map>>> result = new HashMap<>(); + for (SimulatorElement element : tntElementList) { + element.locations(result); + } + + playerShowMode.forEach((player, simulatorEntityShowMode) -> { + boolean simulatorAutoTrace = Config.getInstance().get(player).getPlainValueOrDefault("simulatorAutoTrace", false); + if (simulatorAutoTrace) { + RecordStateMachine.commandSingle(); + player.performCommand("trace show"); + } + }); + + result.forEach((integer, integerSetMap) -> { + List>>> elements = new ArrayList<>(); + integerSetMap.forEach((integer2, set) -> { + elements.add(new Pair<>(integer2, set)); + }); + elements.sort(Comparator.comparingInt(Pair::getKey)); + + + Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> { + for (Pair>> pair : elements) { + Map counts = new HashMap<>(); + pair.getValue().forEach(runnableIntegerPair -> { + counts.put(runnableIntegerPair.getKey(), runnableIntegerPair.getValue()); + }); + List order = new ArrayList<>(); + counts.forEach((runnable, integer1) -> { + order.add(runnable); + }); + Collections.shuffle(order); + + while (!counts.isEmpty()) { + for (Runnable runnable : order) { + if (counts.get(runnable) == 0) { + counts.remove(runnable); + continue; + } + runnable.run(); + counts.put(runnable, counts.get(runnable) - 1); + } + } + } + }, integer); + }); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java index e084493a..12e7f57f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java @@ -139,7 +139,7 @@ public class TNTSimulatorListener implements Listener { if (simulator == null) { return; } - System.out.println("Left click"); + simulator.start(); break; case RIGHT_CLICK_BLOCK: case RIGHT_CLICK_AIR: 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 fe44129c..501eb1fd 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 @@ -47,7 +47,7 @@ public interface SimulatorElement { void hide(SimulatorEntityShowMode showMode); SWItem menu(Player p); - void locations(Map>> result); // Ticks to subtick order to spawning runnable to count of activations + void locations(Map>>> result); // Ticks to subtick order to spawning runnable to count of activations // Observer default void change() { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/TNTElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/TNTElement.java index 2061a34c..011c4445 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/TNTElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/tnt/TNTElement.java @@ -163,10 +163,11 @@ public class TNTElement implements SimulatorElement { } @Override - public void locations(Map>> result) { + public void locations(Map>>> result) { if (disabled) return; result.computeIfAbsent(getTickOffset(), ignore -> new HashMap<>()) - .computeIfAbsent(OrderUtils.order(order), ignore -> new Pair<>(() -> { + .computeIfAbsent(OrderUtils.order(order), ignore -> new HashSet<>()) + .add(new Pair<>(() -> { SimulatorStorage.WORLD.spawn(getPosition().toLocation(SimulatorStorage.WORLD), TNTPrimed.class, tntPrimed -> { tntPrimed.setFuseTicks(fuseTicks); if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0)); 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 59cfa9d3..9ae10591 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 @@ -34,6 +34,7 @@ import yapion.hierarchy.types.YAPIONObject; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; @Getter @@ -136,7 +137,7 @@ public class TNTGroup implements SimulatorElement { } @Override - public void locations(Map>> result) { + public void locations(Map>>> result) { if (disabled) return; elements.forEach(tntElement -> { tntElement.locations(result);