Add loading and saving methods and fix many QOL things
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Dieser Commit ist enthalten in:
yoyosource 2023-10-26 18:07:25 +02:00
Ursprung 61f7218fc0
Commit 61d1f0bd45
18 geänderte Dateien mit 564 neuen und 39 gelöschten Zeilen

Datei anzeigen

@ -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();
}

Datei anzeigen

@ -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<Simulator> simulatorPageGui = new SimulatorPageGui<Simulator>(player, null, 6*9, new ArrayList<>(simulatorMap.values())) {
SimulatorPageGui<Simulator> simulatorPageGui = new SimulatorPageGui<Simulator>(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);
}
}

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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<T extends SimulatorPhase> {
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<T extends SimulatorPhase> {
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<Integer, List<SimulatorAction>> actions) {
@ -115,4 +116,8 @@ public abstract class SimulatorElement<T extends SimulatorPhase> {
.findFirst()
.orElse(null);
}
public abstract String getType();
public void saveExtra(YAPIONObject elementObject) {}
public void loadExtra(YAPIONObject elementObject) {}
}

Datei anzeigen

@ -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<SimulatorElement<?>> elements = new ArrayList<>();
public SimulatorGroup add(SimulatorElement<?> element) {

Datei anzeigen

@ -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<Integer, List<SimulatorAction>> actions, Vector position);
public void saveExtra(YAPIONObject phaseObject) {}
public void loadExtra(YAPIONObject phaseObject) {}
}

Datei anzeigen

@ -28,14 +28,14 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
public class RedstoneElement extends SimulatorElement<RedstonePhase> {
public final class RedstoneElement extends SimulatorElement<RedstonePhase> {
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<RedstonePhase> {
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";
}
}

Datei anzeigen

@ -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;

Datei anzeigen

@ -30,14 +30,14 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
public class TNTElement extends SimulatorElement<TNTPhase> {
public final class TNTElement extends SimulatorElement<TNTPhase> {
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<TNTPhase> {
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<TNTPhase> {
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";
}
}

Datei anzeigen

@ -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");
}
}

Datei anzeigen

@ -57,6 +57,10 @@ public class SimulatorGroupChooserGui extends SimulatorPageGui<SimulatorGroup> {
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<SimulatorGroup> {
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);
};

Datei anzeigen

@ -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;

Datei anzeigen

@ -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();

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<List<Simulator>> 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<SimulatorPhase> 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<SimulatorPhase> 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);
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<List<Simulator>> 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;
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<List<Simulator>> load(File file);
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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
}
}
}

Datei anzeigen

@ -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 <https://www.gnu.org/licenses/>.
*/
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<List<Simulator>> 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<Simulator> 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);
}
}