From 61d1f0bd4552257aaf0c89e4d668c3b6076b0a76 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 26 Oct 2023 18:07:25 +0200 Subject: [PATCH] Add loading and saving methods and fix many QOL things --- .../features/simulator2/SimulatorCursor.java | 8 +- .../features/simulator2/SimulatorStorage.java | 48 ++++--- .../features/simulator2/data/Simulator.java | 7 +- .../simulator2/data/SimulatorElement.java | 9 +- .../simulator2/data/SimulatorGroup.java | 4 +- .../simulator2/data/SimulatorPhase.java | 3 + .../data/redstone/RedstoneElement.java | 9 +- .../data/redstone/RedstonePhase.java | 3 +- .../simulator2/data/tnt/TNTElement.java | 10 +- .../simulator2/data/tnt/TNTPhase.java | 19 ++- .../gui/SimulatorGroupChooserGui.java | 8 ++ .../simulator2/gui/SimulatorGroupGui.java | 2 - .../simulator2/gui/base/SimulatorBaseGui.java | 5 +- .../storage/SimFormatSimulatorLoader.java | 130 +++++++++++++++++ .../SimulatorFormatSimulatorLoader.java | 136 ++++++++++++++++++ .../simulator2/storage/SimulatorLoader.java | 31 ++++ .../simulator2/storage/SimulatorSaver.java | 81 +++++++++++ .../storage/YAPIONFormatSimulatorLoader.java | 90 ++++++++++++ 18 files changed, 564 insertions(+), 39 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimFormatSimulatorLoader.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorFormatSimulatorLoader.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorLoader.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorSaver.java create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/YAPIONFormatSimulatorLoader.java 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 2b1f2f87..90feb161 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorCursor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorCursor.java @@ -34,14 +34,12 @@ 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; @@ -59,7 +57,10 @@ import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; -import java.util.*; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.function.BiFunction; import java.util.function.Function; import java.util.stream.Collectors; @@ -343,6 +344,7 @@ public class SimulatorCursor implements Listener { sim = new Simulator(s); SimulatorStorage.addSimulator(s, sim); createElement(player, rayTraceResult, sim); + SimulatorStorage.setSimulator(player, sim); }); anvilInv.open(); } 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 c81b55f2..8c49a66a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorStorage.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/SimulatorStorage.java @@ -22,6 +22,7 @@ package de.steamwar.bausystem.features.simulator2; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.SWUtils; import de.steamwar.bausystem.features.simulator2.data.Simulator; +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.inventory.SWItem; @@ -53,11 +54,11 @@ public class SimulatorStorage { if (selection == null) { return null; } - return simulatorMap.computeIfAbsent(selection, SimulatorStorage::loadSimulator); + return simulatorMap.get(selection); } public static Simulator getSimulator(String name) { - return simulatorMap.computeIfAbsent(name, SimulatorStorage::loadSimulator); + return simulatorMap.get(name); } public static void addSimulator(String name, Simulator simulator) { @@ -69,7 +70,7 @@ public class SimulatorStorage { } public static void openSimulatorSelector(Player player) { - SimulatorPageGui simulatorPageGui = new SimulatorPageGui(player, null, 6*9, new ArrayList<>(simulatorMap.values())) { + SimulatorPageGui simulatorPageGui = new SimulatorPageGui(player, null, 6 * 9, new ArrayList<>(simulatorMap.values())) { @Override public String baseTitle() { return "Simulators"; @@ -78,29 +79,32 @@ public class SimulatorStorage { @Override public SWItem convert(Simulator simulator) { return simulator.toItem(player, clickType -> { - ItemStack mainHand = player.getInventory().getItemInMainHand(); - ItemStack offHand = player.getInventory().getItemInOffHand(); - ItemStack itemStack; - if (SimulatorCursor.isSimulatorItem(mainHand)) { - itemStack = mainHand; - } else if (SimulatorCursor.isSimulatorItem(offHand)) { - itemStack = offHand; - } else { - itemStack = null; - } - if (itemStack == null) { - return; - } - - // TODO: Dynamic Item Lore, and remove if simulator does not exist anymore (On Join for every Player Inventory Slot with Simulator...) - ItemUtils.setTag(itemStack, simulatorSelection, simulator.getName()); - ItemMeta itemMeta = itemStack.getItemMeta(); - itemMeta.setDisplayName(BauSystem.MESSAGE.parse("SIMULATOR_WAND_NAME_SELECTED", player, simulator.getName())); - itemStack.setItemMeta(itemMeta); + setSimulator(player, simulator); player.closeInventory(); }); } }; simulatorPageGui.open(); } + + public static void setSimulator(Player player, Simulator simulator) { + ItemStack mainHand = player.getInventory().getItemInMainHand(); + ItemStack offHand = player.getInventory().getItemInOffHand(); + ItemStack itemStack; + if (SimulatorCursor.isSimulatorItem(mainHand)) { + itemStack = mainHand; + } else if (SimulatorCursor.isSimulatorItem(offHand)) { + itemStack = offHand; + } else { + itemStack = null; + } + if (itemStack == null) { + return; + } + + ItemUtils.setTag(itemStack, simulatorSelection, simulator.getName()); + ItemMeta itemMeta = itemStack.getItemMeta(); + itemMeta.setDisplayName(BauSystem.MESSAGE.parse("SIMULATOR_WAND_NAME_SELECTED", player, simulator.getName())); + itemStack.setItemMeta(itemMeta); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/Simulator.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/Simulator.java index 73f0ab1d..b1f6c6f6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/Simulator.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/Simulator.java @@ -35,7 +35,7 @@ import java.util.Map; @Getter @Setter @RequiredArgsConstructor -public class Simulator { +public final class Simulator { private Material material = Material.BARREL; private final String name; private boolean autoTrace = false; @@ -56,4 +56,9 @@ public class Simulator { simulatorGroup.toSimulatorActions(actions); }); } + + public Simulator add(SimulatorGroup group) { + elements.add(group); + return this; + } } 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 e01ef88c..aeb99c42 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 @@ -28,6 +28,7 @@ import lombok.Setter; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.util.Vector; +import yapion.hierarchy.types.YAPIONObject; import java.util.ArrayList; import java.util.Comparator; @@ -56,7 +57,7 @@ public abstract class SimulatorElement { phases.sort(Comparator.comparingInt(SimulatorPhase::getTickOffset)); } - public abstract String getName(); + public abstract String getName(Player player); public int getBaseTick() { return phases.stream() @@ -97,7 +98,7 @@ public abstract class SimulatorElement { lore.add(""); lore.add("§cDisabled"); } - return new SWItem(material, "§e" + getName(), lore, disabled, invCallback); + return new SWItem(material, "§e" + getName(player), lore, disabled, invCallback); } public void toSimulatorActions(Map> actions) { @@ -115,4 +116,8 @@ public abstract class SimulatorElement { .findFirst() .orElse(null); } + + public abstract String getType(); + public void saveExtra(YAPIONObject elementObject) {} + public void loadExtra(YAPIONObject elementObject) {} } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorGroup.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorGroup.java index 4e643fda..a4b6d031 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorGroup.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorGroup.java @@ -34,9 +34,9 @@ import java.util.Map; @Getter @Setter -public class SimulatorGroup { +public final class SimulatorGroup { private Material material = Material.CHEST; - protected boolean disabled = false; + private boolean disabled = false; private final List> elements = new ArrayList<>(); public SimulatorGroup add(SimulatorElement element) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorPhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorPhase.java index d70acee5..09e174fd 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorPhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/SimulatorPhase.java @@ -23,6 +23,7 @@ import de.steamwar.bausystem.features.simulator2.execute.SimulatorAction; import lombok.Getter; import lombok.Setter; import org.bukkit.util.Vector; +import yapion.hierarchy.types.YAPIONObject; import java.util.List; import java.util.Map; @@ -37,4 +38,6 @@ public abstract class SimulatorPhase { protected int order = 0; public abstract void toSimulatorActions(Map> actions, Vector position); + public void saveExtra(YAPIONObject phaseObject) {} + public void loadExtra(YAPIONObject phaseObject) {} } 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 4ba337a8..4394f27f 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 @@ -28,14 +28,14 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.util.Vector; -public class RedstoneElement extends SimulatorElement { +public final class RedstoneElement extends SimulatorElement { public RedstoneElement(Vector position) { super(Material.REDSTONE_BLOCK, position); } @Override - public String getName() { + public String getName(Player player) { return "Redstone"; } @@ -58,4 +58,9 @@ public class RedstoneElement extends SimulatorElement { public void open(Player player, Simulator simulator, SimulatorGroup group, SimulatorBaseGui back) { new SimulatorRedstoneGui(player, simulator, group, this, back).open(); } + + @Override + public String getType() { + return "Redstone"; + } } 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 df2e06e2..0972c4ca 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 @@ -1,3 +1,4 @@ + /* * This file is a part of the SteamWar software. * @@ -34,7 +35,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicReference; @NoArgsConstructor -public class RedstonePhase extends SimulatorPhase { +public final class RedstonePhase extends SimulatorPhase { public RedstonePhase(int tickOffset) { this.tickOffset = tickOffset; 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 62982a22..69b7c67f 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 @@ -30,14 +30,14 @@ import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.util.Vector; -public class TNTElement extends SimulatorElement { +public final class TNTElement extends SimulatorElement { public TNTElement(Vector position) { super(Material.TNT, position); } @Override - public String getName() { + public String getName(Player player) { return "TNT"; } @@ -74,6 +74,7 @@ public class TNTElement extends SimulatorElement { 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))); + swItem.setName("§e" + getName(player) + "§8:§7 " + sum); return swItem; } @@ -91,4 +92,9 @@ public class TNTElement extends SimulatorElement { public void open(Player player, Simulator simulator, SimulatorGroup group, SimulatorBaseGui back) { new SimulatorTNTGui(player, simulator, this, group, back).open(); } + + @Override + public String getType() { + return "TNT"; + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTPhase.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTPhase.java index 36c2d2f6..7becfec9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTPhase.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/data/tnt/TNTPhase.java @@ -27,6 +27,7 @@ import lombok.Setter; import org.bukkit.World; import org.bukkit.entity.TNTPrimed; import org.bukkit.util.Vector; +import yapion.hierarchy.types.YAPIONObject; import java.util.ArrayList; import java.util.List; @@ -35,7 +36,7 @@ import java.util.Map; @Getter @Setter @NoArgsConstructor -public class TNTPhase extends SimulatorPhase { +public final class TNTPhase extends SimulatorPhase { private int count = 1; private boolean xJump = false; private boolean yJump = false; @@ -68,4 +69,20 @@ public class TNTPhase extends SimulatorPhase { } }); } + + @Override + public void saveExtra(YAPIONObject phaseObject) { + phaseObject.add("count", count); + phaseObject.add("xJump", xJump); + phaseObject.add("yJump", yJump); + phaseObject.add("zJump", zJump); + } + + @Override + public void loadExtra(YAPIONObject phaseObject) { + count = phaseObject.getPlainValue("count"); + xJump = phaseObject.getPlainValue("xJump"); + yJump = phaseObject.getPlainValue("yJump"); + zJump = phaseObject.getPlainValue("zJump"); + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGroupChooserGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGroupChooserGui.java index 503feb86..f71e7f4c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGroupChooserGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/SimulatorGroupChooserGui.java @@ -57,6 +57,10 @@ public class SimulatorGroupChooserGui extends SimulatorPageGui { parent.getElements().remove(subject); back.open(); SimulatorWatcher.update(simulator); + if (parent.getElements().size() == 1) { + parent.setDisabled(false); + parent.setMaterial(Material.BARREL); + } })); } } @@ -71,6 +75,10 @@ public class SimulatorGroupChooserGui extends SimulatorPageGui { InvCallback invCallback = clickType -> { simulatorGroup.add(subject); parent.getElements().remove(subject); + if (parent.getElements().size() == 1) { + parent.setDisabled(false); + parent.setMaterial(Material.BARREL); + } back.open(); SimulatorWatcher.update(simulator); }; 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 33d87d76..7fe8e6b4 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 @@ -23,8 +23,6 @@ import de.steamwar.bausystem.features.simulator2.SimulatorWatcher; 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.redstone.RedstoneElement; -import de.steamwar.bausystem.features.simulator2.data.tnt.TNTElement; import de.steamwar.bausystem.features.simulator2.gui.base.SimulatorBaseGui; import de.steamwar.bausystem.features.simulator2.gui.base.SimulatorPageGui; import de.steamwar.inventory.SWItem; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/base/SimulatorBaseGui.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/base/SimulatorBaseGui.java index b15ba629..326425de 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/base/SimulatorBaseGui.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/gui/base/SimulatorBaseGui.java @@ -49,7 +49,10 @@ public abstract class SimulatorBaseGui { String originalTitle = player.getOpenInventory().getTitle(); if (inv != null && (Core.getVersion() > 19 || newTitle.equals(originalTitle))) { - inv.clear(); + // TODO: Flickering is better but not gone! + for (int i = 9; i < size - 9; i++) { + inv.setItem(i, null); + } setup(); if (player.getOpenInventory().getTopInventory() != inv) { inventory.open(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimFormatSimulatorLoader.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimFormatSimulatorLoader.java new file mode 100644 index 00000000..4082850f --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimFormatSimulatorLoader.java @@ -0,0 +1,130 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.features.simulator2.storage; + +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.SimulatorPhase; +import de.steamwar.bausystem.features.simulator2.data.redstone.RedstoneElement; +import de.steamwar.bausystem.features.simulator2.data.redstone.RedstonePhase; +import de.steamwar.bausystem.features.simulator2.data.tnt.TNTElement; +import de.steamwar.bausystem.features.simulator2.data.tnt.TNTPhase; +import org.bukkit.Material; +import org.bukkit.util.Vector; +import yapion.exceptions.YAPIONException; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONObject; +import yapion.parser.YAPIONParser; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +public class SimFormatSimulatorLoader implements SimulatorLoader { + + @Override + public Optional> load(File file) { + if (!file.getName().endsWith(".sim")) { + return Optional.empty(); + } + + YAPIONObject yapionObject; + try { + yapionObject = YAPIONParser.parse(file); + } catch (YAPIONException | IOException e) { + return Optional.empty(); + } + + String name = file.getName().substring(0, file.getName().length() - ".sim".length()); + + Simulator simulator = new Simulator(name); + loadSimulator(yapionObject, simulator); + return Optional.of(Collections.singletonList(simulator)); + } + + private void loadSimulator(YAPIONObject simulatorObject, Simulator simulator) { + simulator.setMaterial(Material.valueOf(simulatorObject.getPlainValue("material"))); + simulator.setAutoTrace(simulatorObject.getPlainValue("autoTrace")); + + YAPIONArray groups = simulatorObject.getArray("groups"); + groups.streamObject().forEach(groupObject -> { + SimulatorGroup simulatorGroup = new SimulatorGroup(); + loadGroup(groupObject, simulatorGroup); + simulator.add(simulatorGroup); + }); + } + + private void loadGroup(YAPIONObject groupObject, SimulatorGroup group) { + group.setMaterial(Material.valueOf(groupObject.getPlainValue("material"))); + group.setDisabled(groupObject.getPlainValue("disabled")); + + YAPIONArray elements = groupObject.getArray("elements"); + elements.streamObject().forEach(elementObject -> { + SimulatorElement element; + Supplier phaseConstructor; + Vector position = new Vector(groupObject.getDouble("x"), groupObject.getDouble("y"), groupObject.getDouble("z")); + String type = groupObject.getPlainValue("type"); + switch (type) { + case "TNT": + element = new TNTElement(position); + phaseConstructor = TNTPhase::new; + break; + case "Redstone": + element = new RedstoneElement(position); + phaseConstructor = RedstonePhase::new; + break; + default: + element = null; + phaseConstructor = null; + break; + } + if (element == null) { + return; + } + loadElement(elementObject, element, phaseConstructor); + group.add(element); + }); + } + + private void loadElement(YAPIONObject elementObject, SimulatorElement element, Supplier phaseConstructor) { + element.setMaterial(Material.valueOf(elementObject.getPlainValue("material"))); + element.setDisabled(elementObject.getPlainValue("disabled")); + element.loadExtra(elementObject); + + YAPIONArray phases = elementObject.getArray("phases"); + phases.streamObject().forEach(phaseObject -> { + SimulatorPhase phase = phaseConstructor.get(); + loadPhase(phaseObject, phase); + ((SimulatorElement) element).add(phase); + }); + } + + private void loadPhase(YAPIONObject phaseObject, SimulatorPhase phase) { + phase.setTickOffset(phaseObject.getPlainValue("tickOffset")); + phase.setLifetime(phaseObject.getPlainValue("lifetime")); + phase.setOrder(phaseObject.getPlainValue("order")); + phase.loadExtra(phaseObject); + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorFormatSimulatorLoader.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorFormatSimulatorLoader.java new file mode 100644 index 00000000..86bc406e --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorFormatSimulatorLoader.java @@ -0,0 +1,136 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.features.simulator2.storage; + +import de.steamwar.bausystem.features.simulator2.data.Simulator; +import de.steamwar.bausystem.features.simulator2.data.SimulatorGroup; +import de.steamwar.bausystem.features.simulator2.data.tnt.TNTElement; +import de.steamwar.bausystem.features.simulator2.data.tnt.TNTPhase; +import org.bukkit.Material; +import org.bukkit.util.Vector; +import yapion.exceptions.YAPIONException; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONObject; +import yapion.parser.YAPIONParser; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +public class SimulatorFormatSimulatorLoader implements SimulatorLoader { + + @Override + public Optional> load(File file) { + if (!file.getName().endsWith(".simulator")) { + return Optional.empty(); + } + + YAPIONObject yapionObject; + try { + yapionObject = YAPIONParser.parse(file); + } catch (YAPIONException | IOException e) { + return Optional.empty(); + } + + String name = file.getName().substring(0, file.getName().length() - ".simulator".length()); + + Simulator simulator = new Simulator(name); + simulator.setMaterial(Material.valueOf(yapionObject.getStringOrDefault("material", "BARREL"))); + + YAPIONArray tntElements = yapionObject.getArrayOrDefault("tntElements", new YAPIONArray()); + for (YAPIONObject element : tntElements.streamObject().collect(Collectors.toList())) { + if (element.containsKey("elements")) { + simulator.add(loadGroup(element)); + } else { + simulator.add(loadElement(element)); + } + } + + file.delete(); + return Optional.of(Collections.singletonList(simulator)); + } + + private SimulatorGroup loadGroup(YAPIONObject element) { + double x = element.getDoubleOrDefault("x", 0); + double y = element.getDoubleOrDefault("y", 0); + double z = element.getDoubleOrDefault("z", 0); + int tickOffset = element.getIntOrDefault("tickOffset", 0); + + SimulatorGroup simulatorGroup = new SimulatorGroup(); + simulatorGroup.setDisabled(element.getBooleanOrDefault("disabled", false)); + + YAPIONArray elements = element.getArrayOrDefault("elements", new YAPIONArray()); + for (YAPIONObject e : elements.streamObject().collect(Collectors.toList())) { + simulatorGroup.add(loadElement(e, x, y, z, tickOffset)); + } + + return simulatorGroup; + } + + private TNTElement loadElement(YAPIONObject element, double dx, double dy, double dz, int dTickOffset) { + TNTElement tntElement = new TNTElement(new Vector(element.getDouble("x") + dx, element.getDouble("y") + dy, element.getDouble("z") + dz)); + tntElement.setDisabled(element.getBooleanOrDefault("disabled", false)); + tntElement.setMaterial(Material.valueOf(element.getStringOrDefault("material", "TNT"))); + + TNTPhase tntPhase = loadPhase(element); + tntPhase.setTickOffset(tntPhase.getTickOffset() + dTickOffset); + tntElement.add(tntPhase); + + return tntElement; + } + + private SimulatorGroup loadElement(YAPIONObject element) { + SimulatorGroup simulatorGroup = new SimulatorGroup(); + + TNTElement tntElement = new TNTElement(new Vector(element.getDouble("x"), element.getDouble("y"), element.getDouble("z"))); + tntElement.setDisabled(element.getBooleanOrDefault("disabled", false)); + tntElement.setMaterial(Material.valueOf(element.getStringOrDefault("material", "TNT"))); + simulatorGroup.add(tntElement); + + tntElement.add(loadPhase(element)); + + return simulatorGroup; + } + + private TNTPhase loadPhase(YAPIONObject element) { + TNTPhase tntPhase = new TNTPhase(); + tntPhase.setLifetime(element.getIntOrDefault("fuseTicks", 80)); + tntPhase.setCount(element.getIntOrDefault("count", 1)); + tntPhase.setTickOffset(element.getIntOrDefault("tickOffset", 0)); + tntPhase.setXJump(element.getBooleanOrDefault("xVelocity", false)); + tntPhase.setYJump(element.getBooleanOrDefault("yVelocity", false)); + tntPhase.setZJump(element.getBooleanOrDefault("zVelocity", false)); + switch (element.getStringOrDefault("order", "REPEATER")) { + case "REPEATER": + tntPhase.setOrder(0); + break; + case "OBSERVER": + tntPhase.setOrder(1); + break; + case "COMPARATOR": + tntPhase.setOrder(2); + break; + } + return tntPhase; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorLoader.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorLoader.java new file mode 100644 index 00000000..70659936 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorLoader.java @@ -0,0 +1,31 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.features.simulator2.storage; + +import de.steamwar.bausystem.features.simulator2.data.Simulator; + +import java.io.File; +import java.util.List; +import java.util.Optional; + +public interface SimulatorLoader { + + Optional> load(File file); +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorSaver.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorSaver.java new file mode 100644 index 00000000..bb19c48d --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/SimulatorSaver.java @@ -0,0 +1,81 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.features.simulator2.storage; + +import de.steamwar.bausystem.features.simulator2.data.Simulator; +import lombok.experimental.UtilityClass; +import yapion.hierarchy.output.FileOutput; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONObject; + +import java.io.File; +import java.io.IOException; + +@UtilityClass +public class SimulatorSaver { + + public void saveSimulator(File directory, Simulator simulator) { + YAPIONObject simulatorObject = new YAPIONObject(); + simulatorObject.add("material", simulator.getMaterial().name()); + simulatorObject.add("autoTrace", simulator.isAutoTrace()); + + YAPIONArray groups = new YAPIONArray(); + simulator.getElements().forEach(group -> { + YAPIONObject groupObject = new YAPIONObject(); + groupObject.add("material", group.getMaterial().name()); + groupObject.add("disabled", group.isDisabled()); + groups.add(groupObject); + + YAPIONArray elements = new YAPIONArray(); + group.getElements().forEach(element -> { + YAPIONObject elementObject = new YAPIONObject(); + elementObject.add("type", element.getType()); + elementObject.add("material", element.getMaterial()); + elementObject.add("disabled", element.isDisabled()); + elementObject.add("x", element.getPosition().getX()); + elementObject.add("y", element.getPosition().getY()); + elementObject.add("z", element.getPosition().getZ()); + element.saveExtra(elementObject); + + YAPIONArray phases = new YAPIONArray(); + element.getPhases().forEach(phase -> { + YAPIONObject phaseObject = new YAPIONObject(); + phaseObject.add("tickOffset", phase.getTickOffset()); + phaseObject.add("lifetime", phase.getLifetime()); + phaseObject.add("order", phase.getOrder()); + phase.saveExtra(phaseObject); + phases.add(phaseObject); + }); + elementObject.add("phases", phases); + elements.add(elementObject); + }); + groupObject.add("elements", elements); + groups.add(groupObject); + }); + simulatorObject.add("groups", groups); + + File file = new File(directory, simulator.getName() + ".sim"); + try { + simulatorObject.toJSON(new FileOutput(file)).close(); + } catch (IOException e) { + // Ignore + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/YAPIONFormatSimulatorLoader.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/YAPIONFormatSimulatorLoader.java new file mode 100644 index 00000000..6434fcc0 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator2/storage/YAPIONFormatSimulatorLoader.java @@ -0,0 +1,90 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2023 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.features.simulator2.storage; + +import de.steamwar.bausystem.features.simulator2.data.Simulator; +import de.steamwar.bausystem.features.simulator2.data.SimulatorGroup; +import de.steamwar.bausystem.features.simulator2.data.tnt.TNTElement; +import de.steamwar.bausystem.features.simulator2.data.tnt.TNTPhase; +import de.steamwar.sql.SteamwarUser; +import org.bukkit.Material; +import org.bukkit.util.Vector; +import yapion.exceptions.YAPIONException; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONObject; +import yapion.parser.YAPIONParser; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +public class YAPIONFormatSimulatorLoader implements SimulatorLoader { + + @Override + public Optional> load(File file) { + if (!file.getName().endsWith(".yapion")) { + return Optional.empty(); + } + + YAPIONObject yapionObject; + try { + yapionObject = YAPIONParser.parse(file); + } catch (YAPIONException | IOException e) { + return Optional.empty(); + } + + String name = file.getName().substring(0, file.getName().length() - 7); + SteamwarUser steamwarUser = SteamwarUser.get(Integer.parseInt(name)); + + List simulators = new ArrayList<>(); + for (String s : yapionObject.getKeys()) { + String newName = steamwarUser.getUserName() + (s.isEmpty() ? "" : "_" + s); + YAPIONArray content = yapionObject.getArray(s); + if (content.isEmpty()) continue; + Simulator simulator = new Simulator(newName); + for (YAPIONObject element : content.streamObject().collect(Collectors.toList())) { + SimulatorGroup simulatorGroup = new SimulatorGroup(); + simulator.add(simulatorGroup); + + TNTElement tntElement = new TNTElement(new Vector(element.getDouble("positionX"), element.getDouble("positionY"), element.getDouble("positionZ"))); + tntElement.setDisabled(element.getBooleanOrDefault("disabled", false)); // IDK if this existed back then? + tntElement.setMaterial(Material.valueOf(element.getStringOrDefault("material", "TNT"))); + simulatorGroup.add(tntElement); + + TNTPhase tntPhase = new TNTPhase(); + tntPhase.setLifetime(element.getIntOrDefault("fuseTicks", 80)); + tntPhase.setCount(element.getIntOrDefault("count", 1)); + tntPhase.setTickOffset(element.getIntOrDefault("tickOffset", 0)); + tntPhase.setXJump(element.getBooleanOrDefault("xVelocity", false)); + tntPhase.setYJump(element.getBooleanOrDefault("yVelocity", false)); + tntPhase.setZJump(element.getBooleanOrDefault("zVelocity", false)); + tntPhase.setOrder(element.getBooleanOrDefault("comparator", false) ? 2 : 0); + tntElement.add(tntPhase); + } + simulators.add(simulator); + } + + file.delete(); + return Optional.of(simulators); + } +}