From 21ec66b4e2373f851be204316bf6e5fa5c2acb7d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 25 Oct 2023 14:13:45 +0200 Subject: [PATCH] Fix many things --- .../features/simulator2/SimulatorCursor.java | 25 ++++++++++++++--- .../features/simulator2/SimulatorStorage.java | 28 +++++++++++++++++++ .../features/simulator2/SimulatorWatcher.java | 21 ++++++++------ .../data/redstone/RedstonePhase.java | 4 ++- 4 files changed, 65 insertions(+), 13 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorCursor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorCursor.java index b7a38046..3f443d26 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorCursor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorCursor.java @@ -34,12 +34,14 @@ import de.steamwar.bausystem.features.simulator2.execute.SimulatorExecutor; import de.steamwar.bausystem.features.simulator2.gui.SimulatorGroupGui; import de.steamwar.bausystem.features.simulator2.gui.SimulatorGui; import de.steamwar.bausystem.features.simulator2.gui.base.SimulatorBaseGui; +import de.steamwar.bausystem.features.simulator2.gui.base.SimulatorPageGui; import de.steamwar.bausystem.utils.ItemUtils; import de.steamwar.bausystem.utils.RayTraceUtils; import de.steamwar.entity.REntity; import de.steamwar.entity.REntityServer; import de.steamwar.entity.RFallingBlockEntity; import de.steamwar.inventory.SWAnvilInv; +import de.steamwar.inventory.SWItem; import de.steamwar.linkage.Linked; import lombok.AllArgsConstructor; import lombok.Getter; @@ -137,7 +139,7 @@ public class SimulatorCursor implements Listener { } } - private void calcCursor(Player player) { + private synchronized void calcCursor(Player player) { if (!isSimulatorItem(player.getInventory().getItemInMainHand()) && !isSimulatorItem(player.getInventory().getItemInOffHand())) { removeCursor(player); SimulatorWatcher.show(null, player); @@ -151,7 +153,11 @@ public class SimulatorCursor implements Listener { RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(player, player.getLocation(), entities); if (rayTraceResult == null) { removeCursor(player); - SWUtils.sendToActionbar(player, ""); + if (simulator == null) { + SWUtils.sendToActionbar(player, "§eSelect Simulator"); + } else { + SWUtils.sendToActionbar(player, "§eOpen Simulator"); + } return; } @@ -324,11 +330,18 @@ public class SimulatorCursor implements Listener { RayTraceUtils.RRayTraceResult rayTraceResult = RayTraceUtils.traceREntity(player, player.getLocation(), SimulatorWatcher.getEntitiesOfSimulator(simulator)); if (simulator == null) { if (rayTraceResult == null) { - // TODO: Open Simulator Selection GUI + SimulatorStorage.openSimulatorSelector(player); } else { SWAnvilInv anvilInv = new SWAnvilInv(player, "Name"); anvilInv.setCallback(s -> { - // TODO: Check if Sim exists then close and message, otherwise create sim and add first tnt open that! + Simulator sim = SimulatorStorage.getSimulator(s); + if (sim != null) { + player.sendMessage("§cThe simulator " + s + " already exists"); + return; + } + sim = new Simulator(s); + SimulatorStorage.addSimulator(s, sim); + createElement(player, rayTraceResult, sim); }); anvilInv.open(); } @@ -373,6 +386,10 @@ public class SimulatorCursor implements Listener { } // Add new Element to current simulator + createElement(player, rayTraceResult, simulator); + } + + private void createElement(Player player, RayTraceUtils.RRayTraceResult rayTraceResult, Simulator simulator) { CursorType type = cursorType.getOrDefault(player, CursorType.TNT); Vector vector = type.position.apply(player, rayTraceResult); if (type == CursorType.REDSTONE_BLOCK) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorStorage.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorStorage.java index a4b7b7e6..0dca6740 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorStorage.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorStorage.java @@ -21,11 +21,14 @@ package de.steamwar.bausystem.features.simulator2; import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.features.simulator2.data.Simulator; +import de.steamwar.bausystem.features.simulator2.gui.base.SimulatorPageGui; import de.steamwar.bausystem.utils.ItemUtils; +import de.steamwar.inventory.SWItem; import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -51,7 +54,32 @@ public class SimulatorStorage { return simulatorMap.computeIfAbsent(selection, SimulatorStorage::loadSimulator); } + public static Simulator getSimulator(String name) { + return simulatorMap.computeIfAbsent(name, SimulatorStorage::loadSimulator); + } + + public static void addSimulator(String name, Simulator simulator) { + simulatorMap.putIfAbsent(name, simulator); + } + private static Simulator loadSimulator(String name) { return SimulatorTestCommand.SIMULATOR; // TODO: Implement Loading and legacy Loading } + + public static void openSimulatorSelector(Player player) { + SimulatorPageGui simulatorPageGui = new SimulatorPageGui(player, null, 6*9, new ArrayList<>(simulatorMap.values())) { + @Override + public String baseTitle() { + return "Simulators"; + } + + @Override + public SWItem convert(Simulator simulator) { + return simulator.toItem(player, clickType -> { + System.out.println(clickType); + }); + } + }; + simulatorPageGui.open(); + } } 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 dc239c0d..470f9f12 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorWatcher.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorWatcher.java @@ -40,6 +40,7 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.util.Vector; import java.util.*; +import java.util.stream.Collectors; @UtilityClass public class SimulatorWatcher { @@ -56,7 +57,7 @@ public class SimulatorWatcher { } } - public void update(Simulator simulator) { + public synchronized void update(Simulator simulator) { REntityServer rEntityServer = entityServers.get(simulator); if (rEntityServer != null) { rEntityServer.getEntities().forEach(REntity::die); @@ -85,19 +86,23 @@ public class SimulatorWatcher { return null; } 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()); + simulator.getElements().stream().map(group -> group.getElements().stream().map(element -> new Pair<>(group, element)).collect(Collectors.toList())).flatMap(List::stream).forEach(pair -> { + SimulatorGroup group = pair.getKey(); + // TODO: Disabled material + SimulatorElement element = pair.getValue(); + + boolean wasNotPresent = positionCache.computeIfAbsent(element.getPosition(), __ -> new HashSet<>()) + .add(element.getClass()); if (!wasNotPresent) return; - Material material = simulatorElement.getWorldMaterial(); - Location location = simulatorElement.getWorldPos().toLocation(WORLD); + Material material = element.getWorldMaterial(); + Location location = element.getWorldPos().toLocation(WORLD); RFallingBlockEntity rFallingBlockEntity = new RFallingBlockEntity(server, location, material); rFallingBlockEntity.setNoGravity(true); }); return server; } - public void show(Simulator sim, Player player) { + public synchronized void show(Simulator sim, Player player) { entityServers.forEach((simulator, rEntityServer) -> { if (rEntityServer == null) return; if (rEntityServer.getPlayers().contains(player) && sim != simulator) { @@ -108,7 +113,7 @@ public class SimulatorWatcher { entityServers.computeIfAbsent(sim, __ -> createSim(new REntityServer(), sim)).addPlayer(player); } - List getEntitiesOfSimulator(Simulator simulator) { + synchronized List getEntitiesOfSimulator(Simulator simulator) { REntityServer entityServer = entityServers.get(simulator); if (entityServer == null) { return Collections.emptyList(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstonePhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstonePhase.java index cd269703..df2e06e2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstonePhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/redstone/RedstonePhase.java @@ -55,7 +55,9 @@ public class RedstonePhase extends SimulatorPhase { Block block = world.getBlockAt(position.getBlockX(), position.getBlockY(), position.getBlockZ()); previousBlockState.set(block.getState()); block.setType(Material.REDSTONE_BLOCK); - previousBlockState.get().update(true, true); + if (lifetime == 0) { + previousBlockState.get().update(true, true); + } } });