diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorTestCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorTestCommand.java index e66d7f21..d7f8a766 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorTestCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorTestCommand.java @@ -60,4 +60,14 @@ public class SimulatorTestCommand extends SWCommand { public void runCommand(Player player) { SimulatorExecutor.run(SIMULATOR); } + + @Register("show") + public void showCommand(Player player) { + SimulatorWatcher.show(SIMULATOR, player); + } + + @Register("hide") + public void hideCommand(Player player) { + SimulatorWatcher.hide(SIMULATOR, player); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorWatcher.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorWatcher.java index 2c6f8cbf..5c1e8ddb 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorWatcher.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorWatcher.java @@ -20,21 +20,32 @@ package de.steamwar.bausystem.features.simulator2; import de.steamwar.bausystem.features.simulator2.data.Simulator; +import de.steamwar.bausystem.features.simulator2.data.SimulatorElement; +import de.steamwar.bausystem.features.simulator2.data.SimulatorGroup; +import de.steamwar.bausystem.features.simulator2.data.tnt.TNTElement; import de.steamwar.bausystem.shared.Pair; +import de.steamwar.entity.REntity; +import de.steamwar.entity.REntityServer; +import de.steamwar.entity.RFallingBlockEntity; import de.steamwar.linkage.Linked; import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.util.Vector; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; +import java.util.*; @UtilityClass public class SimulatorWatcher { + private final World WORLD = Bukkit.getWorlds().get(0); + private Map entityServers = new HashMap<>(); private Map> watchers = new HashMap<>(); public void watch(Player player, Simulator simulator, Runnable watcher) { @@ -46,6 +57,10 @@ public class SimulatorWatcher { } public void update(Simulator simulator) { + REntityServer rEntityServer = entityServers.get(simulator); + rEntityServer.getEntities().forEach(REntity::die); + createSim(rEntityServer, simulator); + new ArrayList<>(watchers.values()).forEach(simulatorRunnablePair -> { if (simulatorRunnablePair.getKey() == simulator) { simulatorRunnablePair.getValue().run(); @@ -59,6 +74,39 @@ public class SimulatorWatcher { @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { watchers.remove(event.getPlayer()); + hide(event.getPlayer()); } } + + private REntityServer createSim(REntityServer server, Simulator simulator) { + Map>> positionCache = new HashMap<>(); + simulator.getElements().stream().map(SimulatorGroup::getElements).flatMap(List::stream).forEach(simulatorElement -> { + boolean wasNotPresent = positionCache.computeIfAbsent(simulatorElement.getPosition(), __ -> new HashSet<>()) + .add(simulatorElement.getClass()); + if (!wasNotPresent) return; + Material material = simulatorElement.getWorldMaterial(); + Location location = simulatorElement.getWorldPos().toLocation(WORLD); + RFallingBlockEntity rFallingBlockEntity = new RFallingBlockEntity(server, location, material); + rFallingBlockEntity.setNoGravity(true); + }); + return server; + } + + public void show(Simulator sim, Player player) { + entityServers.computeIfAbsent(sim, __ -> createSim(new REntityServer(), sim)).addPlayer(player); + } + + public void hide(Player player) { + entityServers.replaceAll((simulator, rEntityServer) -> { + rEntityServer.removePlayer(player); + return rEntityServer.getPlayers().isEmpty() ? null : rEntityServer; + }); + } + + public void hide(Simulator sim, Player player) { + entityServers.computeIfPresent(sim, (simulator, rEntityServer) -> { + rEntityServer.removePlayer(player); + return rEntityServer.getPlayers().isEmpty() ? null : rEntityServer; + }); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorElement.java index 3f5136c0..c61e33d9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorElement.java @@ -76,6 +76,12 @@ public abstract class SimulatorElement { position.setZ(position.getZ() + z); } + public abstract Material getWorldMaterial(); + + public Vector getWorldPos() { + return position; + } + public SWItem toItem(Player player, InvCallback invCallback) { List lore = new ArrayList<>(); lore.add("§7Phase count§8:§e " + phases.size()); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstoneElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstoneElement.java index f6acc458..85158939 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstoneElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstoneElement.java @@ -33,4 +33,14 @@ public class RedstoneElement extends SimulatorElement { public String getName() { return "Redstone"; } + + @Override + public Material getWorldMaterial() { + return Material.REDSTONE_BLOCK; + } + + @Override + public Vector getWorldPos() { + return position.clone().add(new Vector(0.5, 0, 0.5)); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTElement.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTElement.java index 3eb27a82..f5335903 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTElement.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTElement.java @@ -20,7 +20,10 @@ package de.steamwar.bausystem.features.simulator2.data.tnt; import de.steamwar.bausystem.features.simulator2.data.SimulatorElement; +import de.steamwar.inventory.InvCallback; +import de.steamwar.inventory.SWItem; import org.bukkit.Material; +import org.bukkit.entity.Player; import org.bukkit.util.Vector; public class TNTElement extends SimulatorElement { @@ -61,4 +64,17 @@ public class TNTElement extends SimulatorElement { position.setZ(position.getBlockZ() + offset.getZ()); } } + + @Override + public SWItem toItem(Player player, InvCallback invCallback) { + long sum = phases.stream().mapToInt(TNTPhase::getCount).sum(); + SWItem swItem = super.toItem(player, invCallback); + swItem.getItemStack().setAmount((int) Math.min(64, Math.max(1, sum))); + return swItem; + } + + @Override + public Material getWorldMaterial() { + return Material.TNT; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGroupGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGroupGui.java index 23a2354c..2cab54cc 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGroupGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGroupGui.java @@ -50,6 +50,7 @@ public class SimulatorGroupGui extends SimulatorPageGui> { @Override public void headerAndFooter() { + simulatorGroup.getElements().removeIf(element -> element.getPhases().isEmpty()); if (simulatorGroup.getElements().size() < 2) { back.open(); return; @@ -63,7 +64,8 @@ public class SimulatorGroupGui extends SimulatorPageGui> { inventory.setItem(4, simulatorGroup.toItem(player, clickType -> { new SimulatorMaterialGui(player, simulator, simulatorGroup::getMaterial, simulatorGroup::setMaterial, this).open(); - }, clickType -> {})); + }, clickType -> { + })); inventory.setItem(48, new SWItem(Material.REPEATER, "§eSettings", clickType -> { new SimulatorGroupSettingsGui(player, simulator, simulatorGroup, this).open(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGui.java index 932fc4a5..757918a3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGui.java @@ -43,7 +43,7 @@ public class SimulatorGui extends SimulatorPageGui { @Override public void headerAndFooter() { - simulator.getElements().removeIf(element -> element.getElements().isEmpty()); + simulator.getElements().removeIf(element -> element.getElements().isEmpty() || element.getElements().stream().allMatch(simulatorElement -> simulatorElement.getPhases().isEmpty())); inventory.setItem(4, simulator.toItem(player, clickType -> { new SimulatorMaterialGui(player, simulator, simulator::getMaterial, simulator::setMaterial, this).open(); @@ -60,7 +60,7 @@ public class SimulatorGui extends SimulatorPageGui { }, clickType -> { SimulatorElement element = simulatorGroup.getElements().get(0); if (element instanceof TNTElement) { - new SimulatorTNTGui(player, simulator, (TNTElement) element, simulatorGroup,this).open(); + new SimulatorTNTGui(player, simulator, (TNTElement) element, simulatorGroup, this).open(); } else if (element instanceof RedstoneElement) { new SimulatorRedstoneGui(player, simulator, simulatorGroup, (RedstoneElement) element, this).open(); }