From 9ee33e8dcb33b91cffa3dc13b83349c2a6271603 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 11 Jun 2022 20:51:25 +0200 Subject: [PATCH] Update TNTSimulator Signed-off-by: yoyosource --- .../features/simulator/TNTSimulator.java | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) 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 275ecab9..e0d10819 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java @@ -41,6 +41,7 @@ import yapion.hierarchy.types.YAPIONType; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @Getter @@ -222,38 +223,42 @@ public class TNTSimulator { } }); + AtomicInteger maxTick = new AtomicInteger(0); + Map>>> toSpawn = new HashMap<>(); result.forEach((integer, integerSetMap) -> { - List>>> elements = new ArrayList<>(); - integerSetMap.forEach((integer2, set) -> { - elements.add(new Pair<>(integer2, set)); + List>>> internal = new ArrayList<>(); + integerSetMap.forEach((integer1, pairs) -> { + internal.add(new Pair<>(integer1, pairs)); }); - elements.sort(Comparator.comparingInt(Pair::getKey)); + internal.sort(Comparator.comparingInt(Pair::getKey)); + toSpawn.put(integer, internal.stream().map(Pair::getValue).map(ArrayList::new).peek(Collections::shuffle).collect(Collectors.toList())); - 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); + if (maxTick.get() < integer) { + maxTick.set(integer); + } }); + + AtomicInteger currentTick = new AtomicInteger(0); + Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), bukkitTask -> { + int tick = currentTick.get(); + List>> toSpawnInTick = toSpawn.get(tick); + if (toSpawnInTick == null) return; + toSpawnInTick.forEach(pairs -> { + AtomicBoolean hasSomeLeft = new AtomicBoolean(true); + while(hasSomeLeft.get()) { + hasSomeLeft.set(false); + pairs.forEach(pair -> { + if (pair.getValue() > 0) { + hasSomeLeft.set(true); + pair.getKey().run(); + pair.setValue(pair.getValue() - 1); + } + }); + } + }); + if (tick > maxTick.get()) bukkitTask.cancel(); + currentTick.incrementAndGet(); + }, 1, 1); } }