diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulator.java index fc2a2d4..73130b8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/TNTSimulator.java @@ -46,6 +46,7 @@ public class TNTSimulator { private static final Vector Z_VECTOR = new Vector(0, 0, 0.0625); private static final Vector NZ_VECTOR = new Vector(0, 0, -0.0625); private static final List LORE = Collections.singletonList("§7Zum ändern klicken"); + private static final List EMPTY = new ArrayList<>(); static final Map TNT_SIMULATOR_MAP = new HashMap<>(); private final Set TNT_SPAWNS = new HashSet<>(); @@ -298,45 +299,30 @@ public class TNTSimulator { return (int)(d * 10000) * 0.0001; } - private static class TNTSpawnPair { - - private int countLeft; - private TNTSpawn tntSpawn; - - public TNTSpawnPair(int countLeft, TNTSpawn tntSpawn) { - this.countLeft = countLeft; - this.tntSpawn = tntSpawn; - } - - private void spawn() { - tntSpawn.spawn(); - countLeft--; - } - - } - public void start() { - Map[]> tntSpawnMap = new HashMap<>(); + Map> first = new HashMap<>(); + Map> second = new HashMap<>(); TNT_SPAWNS.forEach(tntSpawn -> { - tntSpawnMap.computeIfAbsent(tntSpawn.getTickOffset(), integer -> new ArrayList[]{new ArrayList<>(), new ArrayList<>()})[tntSpawn.isComparator() ? 1 : 0].add(new TNTSpawnPair(tntSpawn.getCount(), tntSpawn)); + Map> list = tntSpawn.isComparator() ? second : first; + for (int i = 0; i < tntSpawn.getCount(); i++) { + list.computeIfAbsent(tntSpawn.getTickOffset(), integer -> new ArrayList<>()).add(tntSpawn); + } }); - tntSpawnMap.forEach((integer, tntSpawnPairs) -> { + Set ticks = new HashSet<>(first.keySet()); + ticks.addAll(second.keySet()); + for (int tick : ticks) { Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), () -> { - spawnRandomList(tntSpawnPairs[0]); - spawnRandomList(tntSpawnPairs[1]); - }, integer + 1L); - }); + spawnRandomList(first.getOrDefault(tick, EMPTY)); + spawnRandomList(second.getOrDefault(tick, EMPTY)); + }, tick + 1L); + } } - private void spawnRandomList(List tntSpawnPairs) { - int index = tntSpawnPairs.size() - 1; - while (!tntSpawnPairs.isEmpty()) { - TNTSpawnPair tntSpawnPair = tntSpawnPairs.get(index); - tntSpawnPair.spawn(); - if (tntSpawnPair.countLeft <= 0) { - tntSpawnPairs.remove(index); - } - if (--index < 0) index = tntSpawnPairs.size() - 1; + private void spawnRandomList(List tntSpawns) { + if (tntSpawns.isEmpty()) return; + Collections.shuffle(tntSpawns); + for (TNTSpawn tntSpawn : tntSpawns) { + tntSpawn.spawn(); } }