diff --git a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulator.java index 498807a..c716b98 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSimulator.java @@ -22,7 +22,6 @@ package de.steamwar.bausystem.cannonsimulator; import de.steamwar.bausystem.BauSystem; -import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; @@ -34,6 +33,7 @@ import org.bukkit.util.Consumer; import org.bukkit.util.Vector; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; public class TNTSimulator { @@ -197,6 +197,12 @@ public class TNTSimulator { editTNT(player, tntSpawn); })); + // Repeater before Comparator + swInventory.setItem(4, new SWItem(tntSpawn.isComparator() ? Material.COMPARATOR : Material.REPEATER, "§7Gezündet durch §8- §e" + (tntSpawn.isComparator() ? "Comparator" : "Repeater"), clickType -> { + tntSpawn.setComparator(!tntSpawn.isComparator()); + editTNT(player, tntSpawn); + })); + // Velocity Settings swInventory.setItem(13, new SWItem(tntSpawn.isxVelocity() ? Material.LIME_CONCRETE : Material.RED_CONCRETE, "§7Start §eVelocity X §8- §7" + active(tntSpawn.isxVelocity()), clickType -> { tntSpawn.setxVelocity(!tntSpawn.isxVelocity()); @@ -310,10 +316,46 @@ public class TNTSimulator { private Set tntSpawns = new HashSet<>(); + 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<>(); tntSpawns.forEach(tntSpawn -> { - Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), tntSpawn::spawn, tntSpawn.getTickOffset() + 1L); + tntSpawnMap.computeIfAbsent(tntSpawn.getTickOffset(), integer -> new ArrayList[]{new ArrayList<>(), new ArrayList<>()})[tntSpawn.isComparator() ? 1 : 0].add(new TNTSpawnPair(tntSpawn.getCount(), tntSpawn)); + }); + tntSpawnMap.forEach((integer, tntSpawnPairs) -> { + Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), () -> { + spawnRandomList(tntSpawnPairs[0]); + spawnRandomList(tntSpawnPairs[1]); + }, integer + 1L); }); } + private void spawnRandomList(List tntSpawnPairs) { + AtomicInteger index = new AtomicInteger(tntSpawnPairs.size() - 1); + while (!tntSpawnPairs.isEmpty()) { + TNTSpawnPair tntSpawnPair = tntSpawnPairs.get(index.get()); + tntSpawnPair.spawn(); + if (tntSpawnPair.countLeft <= 0) { + tntSpawnPairs.remove(index.get()); + } + if (index.decrementAndGet() < 0) index.set(tntSpawnPairs.size() - 1); + } + } + } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSpawn.java b/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSpawn.java index 2c6ca4c..3a27cc3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSpawn.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/cannonsimulator/TNTSpawn.java @@ -38,20 +38,19 @@ public class TNTSpawn implements Comparable { private boolean xVelocity = true; private boolean yVelocity = true; private boolean zVelocity = true; + private boolean comparator = false; public TNTSpawn(Vector position) { this.position = position; } public void spawn() { - for (int i = 0; i < count; i++) { - WORLD.spawn(position.toLocation(WORLD), TNTPrimed.class, tntPrimed -> { - tntPrimed.setFuseTicks(fuseTicks); - if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0)); - if (!yVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setY(0)); - if (!zVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setZ(0)); - }); - } + WORLD.spawn(position.toLocation(WORLD), TNTPrimed.class, tntPrimed -> { + tntPrimed.setFuseTicks(fuseTicks); + if (!xVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setX(0)); + if (!yVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setY(0)); + if (!zVelocity) tntPrimed.setVelocity(tntPrimed.getVelocity().setZ(0)); + }); } public String getName() { @@ -118,6 +117,14 @@ public class TNTSpawn implements Comparable { this.zVelocity = zVelocity; } + public boolean isComparator() { + return comparator; + } + + public void setComparator(boolean comparator) { + this.comparator = comparator; + } + @Override public int compareTo(TNTSpawn tntSpawn) { return -Integer.compare(tickOffset, tntSpawn.tickOffset);