diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java index 822a48f6..f7db47b2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java @@ -25,10 +25,12 @@ import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.config.ColorConfig; import de.steamwar.bausystem.features.simulator.show.SimulatorEntityShowMode; +import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWInventory; import de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; +import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.World; @@ -40,6 +42,8 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.util.Consumer; import org.bukkit.util.RayTraceResult; import org.bukkit.util.Vector; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONObject; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -56,14 +60,39 @@ public class TNTSimulator { private static final List EMPTY = new ArrayList<>(); public static final ItemStack WAND = new SWItem(Material.BLAZE_ROD, "§eKanonensimulator", Arrays.asList("§eRechtsklick §8- §7Füge eine Position hinzu", "§eSneaken §8- §7Auf den Block zentrieren", "§eLinksklick §8- §7Starte die Simulation", "§eRechtsklick Luft §8- §7Öffne die GUI"), false, null).getItemStack(); - private static final Map TNT_SIMULATOR_MAP = new HashMap<>(); + static final Map TNT_SIMULATOR_MAP = new HashMap<>(); public static TNTSimulator get(Player player) { - return TNT_SIMULATOR_MAP.computeIfAbsent(player, p -> new TNTSimulator(player)); + return TNT_SIMULATOR_MAP.computeIfAbsent(player, p -> { + YAPIONArray yapionArray = WorldData.getSimulatorData().getYAPIONArrayOrSetDefault(SteamwarUser.get(player.getUniqueId()).getId() + "", new YAPIONArray()); + TNTSimulator simulator = new TNTSimulator(player); + if (!yapionArray.isEmpty()) { + yapionArray.forEach(yapionAnyType -> { + simulator.spawns.add(simulator.new TNTSpawn((YAPIONObject) yapionAnyType)); + }); + } + return simulator; + }); + } + + public void saveAndDelete() { + TNTSimulator simulator = TNT_SIMULATOR_MAP.remove(player); + YAPIONArray yapionArray = new YAPIONArray(); + simulator.spawns.forEach(tntSpawn -> { + yapionArray.add(tntSpawn.output()); + }); + String userID = SteamwarUser.get(player.getUniqueId()).getId() + ""; + if (yapionArray.isEmpty()) { + WorldData.getSimulatorData().remove(userID); + } else { + WorldData.getSimulatorData().add(userID, yapionArray); + } + simulator.hide(); } public void delete() { TNTSimulator simulator = TNT_SIMULATOR_MAP.remove(player); + WorldData.getSimulatorData().remove(SteamwarUser.get(player.getUniqueId()).getId() + ""); simulator.hide(); } @@ -294,6 +323,33 @@ public class TNTSimulator { editTNT(); } + private TNTSpawn(YAPIONObject yapionObject) { + this.position = new Vector(yapionObject.getPlainValueOrDefault("positionX", 0.0), yapionObject.getPlainValueOrDefault("positionY", 0.0), yapionObject.getPlainValueOrDefault("positionZ", 0.0)); + this.entity = SimulatorEntityShowMode.createEntity(player, position, false); + this.fuseTicks = yapionObject.getPlainValue("fuseTicks"); + this.count = yapionObject.getPlainValue("count"); + this.tickOffset = yapionObject.getPlainValue("tickOffset"); + this.xVelocity = yapionObject.getPlainValue("xVelocity"); + this.yVelocity = yapionObject.getPlainValue("yVelocity"); + this.zVelocity = yapionObject.getPlainValue("zVelocity"); + this.comparator = yapionObject.getPlainValue("comparator"); + } + + private YAPIONObject output() { + YAPIONObject yapionObject = new YAPIONObject(); + yapionObject.add("positionX", position.getX()); + yapionObject.add("positionY", position.getY()); + yapionObject.add("positionZ", position.getZ()); + yapionObject.add("fuseTicks", fuseTicks); + yapionObject.add("count", count); + yapionObject.add("tickOffset", tickOffset); + yapionObject.add("xVelocity", xVelocity); + yapionObject.add("yVelocity", yVelocity); + yapionObject.add("zVelocity", zVelocity); + yapionObject.add("comparator", comparator); + return yapionObject; + } + private void show() { if (printed) return; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java index 58289a9b..8a93e29f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java @@ -23,6 +23,7 @@ package de.steamwar.bausystem.features.simulator; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.Permission; +import de.steamwar.bausystem.linkage.Disable; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import org.bukkit.FluidCollisionMode; @@ -47,7 +48,8 @@ import static de.steamwar.bausystem.features.simulator.TNTSimulator.WAND; import static de.steamwar.bausystem.features.simulator.TNTSimulator.get; @Linked(LinkageType.LISTENER) -public class TNTSimulatorListener implements Listener { +@Linked(LinkageType.DISABLE_LINK) +public class TNTSimulatorListener implements Listener, Disable { private boolean permissionCheck(Player player) { if (!Permission.hasPermission(player, Permission.WORLD)) { @@ -139,7 +141,13 @@ public class TNTSimulatorListener implements Listener { @EventHandler public void onPlayerQuit(PlayerQuitEvent event) { - get(event.getPlayer()).delete(); + get(event.getPlayer()).saveAndDelete(); } + @Override + public void disable() { + for (TNTSimulator simulator : TNTSimulator.TNT_SIMULATOR_MAP.values()) { + simulator.saveAndDelete(); + } + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java index 7d033602..30c1d5b5 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java @@ -47,8 +47,8 @@ public enum LinkageType { // NORMAL COMMAND(-1, false, SWCommand.class::isAssignableFrom), - ENABLE_LINK(0, false, Enable.class::isAssignableFrom, o -> ((Enable) o).enable()), - DISABLE_LINK(0, true, Disable.class::isAssignableFrom, o -> ((Disable) o).disable()), + ENABLE_LINK(0, false, Enable.class::isInstance, o -> ((Enable) o).enable()), + DISABLE_LINK(0, true, Disable.class::isInstance, o -> ((Disable) o).disable()), PLAIN(1, false, clazz -> true), LISTENER(2, false, Listener.class::isAssignableFrom, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())), UNLINK_LISTENER(2, true, Listener.class::isAssignableFrom, o -> HandlerList.unregisterAll((Listener) o)), diff --git a/BauSystem_Main/src/de/steamwar/bausystem/worlddata/WorldData.java b/BauSystem_Main/src/de/steamwar/bausystem/worlddata/WorldData.java index 5332f19e..f320d894 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/worlddata/WorldData.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/worlddata/WorldData.java @@ -47,6 +47,10 @@ public class WorldData { return getWorldData().getYAPIONObjectOrSetDefault("regions", new YAPIONObject()); } + public YAPIONObject getSimulatorData() { + return getWorldData().getYAPIONObjectOrSetDefault("simulators", new YAPIONObject()); + } + private void read() { worldData = new YAPIONObject(); if (optionsFile.length() != 0) {