From 186d75fccac7c93bd0afc2e79a79b4a6d6d1ec52 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 23 Nov 2021 20:24:57 +0100 Subject: [PATCH] Update TNTSimulator Add SimulatorData Signed-off-by: yoyosource --- .../features/simulator/TNTSimulator.java | 10 +-- .../bausystem/worlddata/SimulatorData.java | 74 +++++++++++++++++++ .../bausystem/worlddata/WorldData.java | 15 +++- 3 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/worlddata/SimulatorData.java 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 29d4ea38..78f38475 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulator.java @@ -25,6 +25,7 @@ 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.SimulatorData; import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.inventory.SWAnvilInv; import de.steamwar.inventory.SWInventory; @@ -64,7 +65,7 @@ public class TNTSimulator { public static TNTSimulator get(Player player) { return TNT_SIMULATOR_MAP.computeIfAbsent(player, p -> { - YAPIONArray yapionArray = WorldData.getSimulatorData().getYAPIONArrayOrSetDefault(SteamwarUser.get(player.getUniqueId()).getId() + "", new YAPIONArray()); + YAPIONArray yapionArray = SimulatorData.getSimulator(player).getYAPIONArrayOrSetDefault("", new YAPIONArray()); TNTSimulator simulator = new TNTSimulator(player); if (!yapionArray.isEmpty()) { yapionArray.forEach(yapionAnyType -> { @@ -81,11 +82,10 @@ public class TNTSimulator { simulator.spawns.forEach(tntSpawn -> { yapionArray.add(tntSpawn.output()); }); - String userID = SteamwarUser.get(player.getUniqueId()).getId() + ""; if (yapionArray.isEmpty()) { - WorldData.getSimulatorData().remove(userID); + SimulatorData.removeSimulator(player); } else { - WorldData.getSimulatorData().add(userID, yapionArray); + SimulatorData.saveSimulator(player, new YAPIONObject().add("", yapionArray)); } simulator.hide(); } @@ -97,7 +97,7 @@ public class TNTSimulator { public void delete() { TNTSimulator simulator = TNT_SIMULATOR_MAP.remove(player); - WorldData.getSimulatorData().remove(SteamwarUser.get(player.getUniqueId()).getId() + ""); + SimulatorData.removeSimulator(player); simulator.hide(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/worlddata/SimulatorData.java b/BauSystem_Main/src/de/steamwar/bausystem/worlddata/SimulatorData.java new file mode 100644 index 00000000..dc2695e3 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/worlddata/SimulatorData.java @@ -0,0 +1,74 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.worlddata; + +import de.steamwar.sql.SteamwarUser; +import lombok.SneakyThrows; +import lombok.experimental.UtilityClass; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import yapion.hierarchy.output.FileOutput; +import yapion.hierarchy.types.YAPIONObject; +import yapion.parser.YAPIONParser; + +import java.io.File; + +@UtilityClass +public class SimulatorData { + + private final File simulatorsDir = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "simulators"); + + static { + simulatorsDir.mkdirs(); + } + + @SneakyThrows + public YAPIONObject getSimulator(Player player) { + File file = getFile(player); + if (!file.exists()) { + return new YAPIONObject(); + } + return YAPIONParser.parse(file); + } + + @SneakyThrows + public void saveSimulator(Player player, YAPIONObject yapionObject) { + File file = getFile(player); + yapionObject.toYAPION(new FileOutput(file)).close(); + } + + @SneakyThrows + void saveSimulator(SteamwarUser steamwarUser, YAPIONObject yapionObject) { + File file = new File(simulatorsDir, steamwarUser.getId() + ".yapion"); + yapionObject.toYAPION(new FileOutput(file)).close(); + } + + public void removeSimulator(Player player) { + File file = getFile(player); + if (file.exists()) { + file.delete(); + } + } + + private File getFile(Player player) { + SteamwarUser steamwarUser = SteamwarUser.get(player.getUniqueId()); + return new File(simulatorsDir, steamwarUser.getId() + ".yapion"); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/worlddata/WorldData.java b/BauSystem_Main/src/de/steamwar/bausystem/worlddata/WorldData.java index a07729e0..539aaa5a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/worlddata/WorldData.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/worlddata/WorldData.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.worlddata; +import de.steamwar.sql.SteamwarUser; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import yapion.hierarchy.output.FileOutput; @@ -52,10 +53,6 @@ public class WorldData { return getWorldData().getYAPIONObjectOrSetDefault("warps", new YAPIONObject()); } - public YAPIONObject getSimulatorData() { - return getWorldData().getYAPIONObjectOrSetDefault("simulators", new YAPIONObject()); - } - private void read() { worldData = new YAPIONObject(); if (optionsFile.length() != 0) { @@ -71,6 +68,16 @@ public class WorldData { // Ignored } } + + // Conversion from old simulator saving to new one + if (worldData.containsKey("simulators")) { + YAPIONObject yapionObject = worldData.getObject("simulators"); + worldData.remove("simulators"); + yapionObject.forEach((s, yapionAnyType) -> { + SteamwarUser steamwarUser = SteamwarUser.get(Integer.parseInt(s)); + SimulatorData.saveSimulator(steamwarUser, (YAPIONObject) yapionAnyType); + }); + } } public void write() {