SteamWar/BauSystem
Archiviert
13
0

Simplify TNTSimulator.start

Dieser Commit ist enthalten in:
jojo 2021-02-03 16:53:37 +01:00
Ursprung 4f1307cc8d
Commit f977837175

Datei anzeigen

@ -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<String> LORE = Collections.singletonList("§7Zum ändern klicken");
private static final List<TNTSpawn> EMPTY = new ArrayList<>();
static final Map<Player, TNTSimulator> TNT_SIMULATOR_MAP = new HashMap<>();
private final Set<TNTSpawn> 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<Integer, List<TNTSpawnPair>[]> tntSpawnMap = new HashMap<>();
Map<Integer, List<TNTSpawn>> first = new HashMap<>();
Map<Integer, List<TNTSpawn>> 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<Integer, List<TNTSpawn>> 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<Integer> 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<TNTSpawnPair> 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<TNTSpawn> tntSpawns) {
if (tntSpawns.isEmpty()) return;
Collections.shuffle(tntSpawns);
for (TNTSpawn tntSpawn : tntSpawns) {
tntSpawn.spawn();
}
}