diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java index b4cf6302..bd44975d 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/Region.java @@ -33,7 +33,7 @@ import java.util.UUID; import java.util.function.BiConsumer; import java.util.stream.Stream; -public interface Region { +public interface Region extends RegionDataStore { static Stream getRegions() { return RegionSystem.INSTANCE.getRegions(); diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionBackups.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionBackups.java index 5d29bde6..c21ba501 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionBackups.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionBackups.java @@ -41,7 +41,7 @@ public interface RegionBackups { @RequiredArgsConstructor @Getter - abstract class Backup implements Comparable { + abstract class Backup implements RegionDataStore, Comparable { @NonNull private final BackupType type; diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionData.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionData.java index 20254530..7a3509ce 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionData.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionData.java @@ -22,8 +22,6 @@ package de.steamwar.bausystem.region; import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.sql.SchematicNode; import lombok.NonNull; -import lombok.Setter; -import yapion.hierarchy.types.YAPIONObject; import java.util.ArrayList; import java.util.HashMap; @@ -33,33 +31,22 @@ import java.util.function.Function; public abstract class RegionData { - @Setter protected RegionDataStore store; private final List> properties = new ArrayList<>(); - protected final YAPIONObject data; - protected final YAPIONObject flagData; - protected final Runnable onChange; - protected final Map, Flag.Value> flagMap = new HashMap<>(); - private Property testblockSchematic = new Property<>("testblockSchematic", SchematicNode::byId, SchematicNode::getId); + protected final Property testblockSchematic = new Property<>("testblockSchematic", SchematicNode::byId, SchematicNode::getId); - protected RegionData(YAPIONObject data, Runnable onChange) { - this.data = data; - this.flagData = data.getObjectOrSetDefault("flagStorage", new YAPIONObject()); - this.onChange = onChange; + protected RegionData(RegionDataStore store) { + this.store = store; initialize(); - for (final Flag flag : Flag.getFlags()) { - if (!has(flag).isWritable()) continue; - try { - String s = flagData.getPlainValue(flag.name()); - flagMap.put(flag, flag.valueOfValue(s)); - } catch (Exception e) { - flagMap.put(flag, (Flag.Value) flag.getDefaultValue()); - } - } - properties.forEach(Property::load); + store.loadRegionData(this); + } + + public void setStore(RegionDataStore store) { + this.store = store; + store.loadRegionData(this); } protected void initialize() { @@ -74,8 +61,7 @@ public abstract class RegionData { public final & Flag.Value> boolean set(@NonNull Flag flag, @NonNull T value) { if (has(flag).isWritable()) { if (flagMap.put(flag, value) != value) { - flagData.put(flag.name(), value.name()); - onChange.run(); + store.saveRegionData(this); return true; } } @@ -91,24 +77,27 @@ public abstract class RegionData { for (Flag flag : Flag.getFlags()) { if (has(flag).isWritable()) { flagMap.remove(flag); - flagData.remove(flag.name()); } } properties.forEach(property -> property.set(null)); - onChange.run(); + store.saveRegionData(this); } public final Map, Flag.Value> getBackedMap() { return flagMap; } + public final List> getBackedProperties() { + return (List) properties; + } + public SchematicNode getTestblockSchematic() { return testblockSchematic.get(); } public void setTestblockSchematic(SchematicNode schematic) { testblockSchematic.set(schematic); - onChange.run(); + store.saveRegionData(this); } @Override @@ -123,39 +112,26 @@ public abstract class RegionData { return st.toString(); } - private final class Property { - private final String field; - private final Function loader; - private final Function writer; + public final class Property { + public final String field; + public final Function loader; + public final Function writer; private T value; - public Property(String field, Function loader, Function writer) { + private Property(String field, Function loader, Function writer) { this.field = field; this.loader = loader; this.writer = writer; properties.add(this); } - public void load() { - if (flagData.containsKey(field)) { - value = loader.apply(flagData.getPlainValue(field)); - } else { - value = null; - } - } - public T get() { return value; } public void set(T value) { this.value = value; - if (value == null) { - flagData.remove(field); - } else { - flagData.put(field, writer.apply(value)); - } } @Override diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/global/GlobalRegion.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/global/GlobalRegion.java index 4c44308d..063e38f7 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/global/GlobalRegion.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/global/GlobalRegion.java @@ -86,7 +86,7 @@ public class GlobalRegion implements Region { } }; - private static final GlobalRegionData REGION_DATA = new GlobalRegionData(); + private static final GlobalRegionData REGION_DATA = new GlobalRegionData(INSTANCE); @Override public @NonNull UUID getID() { @@ -132,4 +132,14 @@ public class GlobalRegion implements Region { public @NonNull RegionBackups getBackups() { return RegionBackups.EMPTY; } + + @Override + public void saveRegionData(@NonNull RegionData regionData) { + + } + + @Override + public void loadRegionData(@NonNull RegionData regionData) { + + } } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/global/GlobalRegionData.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/global/GlobalRegionData.java index 81ba2cad..12322639 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/global/GlobalRegionData.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/global/GlobalRegionData.java @@ -31,8 +31,8 @@ import yapion.hierarchy.types.YAPIONObject; public class GlobalRegionData extends RegionData { - public GlobalRegionData() { - super(new YAPIONObject(), () -> {}); // TODO: Implement loading of data and saving! + public GlobalRegionData(GlobalRegion globalRegion) { + super(globalRegion); } @Override @@ -44,7 +44,7 @@ public class GlobalRegionData extends RegionData { @Override public @NonNull & Flag.Value> RegionFlagPolicy has(@NonNull Flag flag) { - if (flag.oneOf(Flag.COLOR)) { + if (flag.oneOf(Flag.COLOR, Flag.PROTECT)) { return RegionFlagPolicy.READ_ONLY; } if (flag.oneOf(Flag.ITEMS) && Core.getVersion() >= 20) { diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegion.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegion.java index 0942831b..6b0523a9 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegion.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegion.java @@ -21,12 +21,15 @@ package de.steamwar.bausystem.region.fixed; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.bausystem.region.*; +import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.utils.PasteBuilder; +import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.sql.GameModeConfig; import lombok.NonNull; import lombok.Setter; import org.bukkit.Location; import org.bukkit.Material; +import yapion.hierarchy.types.YAPIONObject; import javax.annotation.Nullable; import java.io.File; @@ -137,4 +140,14 @@ public final class FixedGlobalRegion implements Region { public @NonNull RegionBackups getBackups() { return RegionBackups.EMPTY; } + + @Override + public void saveRegionData(@NonNull RegionData regionData) { + FixedRegionDataUtils.saveRegionData("global", regionData); + } + + @Override + public void loadRegionData(@NonNull RegionData regionData) { + FixedRegionDataUtils.loadRegionData("global", regionData); + } } diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegionData.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegionData.java index 065e2e20..f18befad 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegionData.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegionData.java @@ -20,18 +20,18 @@ package de.steamwar.bausystem.region.fixed; import de.steamwar.bausystem.region.RegionData; +import de.steamwar.bausystem.region.RegionDataStore; import de.steamwar.bausystem.region.RegionFlagPolicy; import de.steamwar.bausystem.region.flags.ColorMode; import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.ProtectMode; import de.steamwar.bausystem.region.flags.TNTMode; import lombok.NonNull; -import yapion.hierarchy.types.YAPIONObject; public class FixedGlobalRegionData extends RegionData { - public FixedGlobalRegionData(YAPIONObject data, Runnable onChange) { - super(data, onChange); + public FixedGlobalRegionData(RegionDataStore store) { + super(store); } @Override diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java index d194ced9..cc020657 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java @@ -142,12 +142,20 @@ public class FixedRegion implements Region { public long getCreationTime() { return file.lastModified(); } + + @Override + public void saveRegionData(@NonNull RegionData regionData) { + } + + @Override + public void loadRegionData(@NonNull RegionData regionData) { + } } - public FixedRegion(String name, FixedRegionData flagStorage, Prototype prototype, YAPIONObject regionConfig, YAPIONObject regionData) { + public FixedRegion(String name, Prototype prototype, YAPIONObject regionConfig) { this.name = name; uuid = UUID.nameUUIDFromBytes(name.getBytes(StandardCharsets.UTF_8)); - this.flagStorage = flagStorage; + this.flagStorage = new FixedRegionData(this); this.prototype = prototype; this.skin = prototype.getDefaultSkin(); @@ -392,4 +400,14 @@ public class FixedRegion implements Region { public @NonNull RegionBackups getBackups() { return regionBackups; } + + @Override + public void saveRegionData(@NonNull RegionData regionData) { + FixedRegionDataUtils.saveRegionData(name, regionData); + } + + @Override + public void loadRegionData(@NonNull RegionData regionData) { + FixedRegionDataUtils.loadRegionData(name, regionData); + } } diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegionData.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegionData.java index c2de7042..bf1dfdae 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegionData.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegionData.java @@ -20,15 +20,15 @@ package de.steamwar.bausystem.region.fixed; import de.steamwar.bausystem.region.RegionData; +import de.steamwar.bausystem.region.RegionDataStore; import de.steamwar.bausystem.region.RegionFlagPolicy; import de.steamwar.bausystem.region.flags.Flag; import lombok.NonNull; -import yapion.hierarchy.types.YAPIONObject; public class FixedRegionData extends RegionData { - public FixedRegionData(YAPIONObject data, Runnable onChange) { - super(data, onChange); + public FixedRegionData(RegionDataStore store) { + super(store); } @Override diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegionDataUtils.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegionDataUtils.java new file mode 100644 index 00000000..69c2612a --- /dev/null +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegionDataUtils.java @@ -0,0 +1,64 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2025 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.region.fixed; + +import de.steamwar.bausystem.region.RegionData; +import de.steamwar.bausystem.region.flags.Flag; +import de.steamwar.bausystem.worlddata.WorldData; +import lombok.NonNull; +import lombok.experimental.UtilityClass; +import yapion.hierarchy.types.YAPIONObject; + +@UtilityClass +public class FixedRegionDataUtils { + + public void saveRegionData(@NonNull String identifier, @NonNull RegionData regionData) { + YAPIONObject yapionObject = new YAPIONObject(); + YAPIONObject flagData = yapionObject.getOrSetDefault("flagStorage", new YAPIONObject()); + regionData.getBackedMap().forEach((flag, value) -> { + flagData.put(flag.name(), value.name()); + }); + regionData.getBackedProperties().forEach(property -> { + yapionObject.put(property.field, property.writer.apply(property.get())); + }); + WorldData.getRegionsData().put(identifier, yapionObject); + WorldData.write(); + } + + public void loadRegionData(@NonNull String identifier, @NonNull RegionData regionData) { + YAPIONObject values = WorldData.getRegionsData().getObject(identifier); + if (values == null) return; + YAPIONObject flagData = values.getObjectOrDefault("flagStorage", new YAPIONObject()); + for (final Flag flag : Flag.getFlags()) { + if (!regionData.has(flag).isWritable()) continue; + + try { + String s = flagData.getPlainValue(flag.name()); + regionData.getBackedMap().put(flag, flag.valueOfValue(s)); + } catch (Exception e) { + regionData.getBackedMap().put(flag, (Flag.Value) flag.getDefaultValue()); + } + } + regionData.getBackedProperties().forEach(property -> { + Object object = values.getPlainValue(property.field); + property.set(property.loader.apply(object)); + }); + } +} diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/Prototype.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/Prototype.java index 04cf7ee0..a3d14793 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/Prototype.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/Prototype.java @@ -210,14 +210,8 @@ public class Prototype { } } - public static void generateRegion(String name, YAPIONObject regionConfig, YAPIONObject regionData) { - Prototype prototype; - if (regionData.containsKey("prototype", String.class)) { - prototype = PROTOTYPE_MAP.get(regionData.getPlainValue("prototype")); - } else { - prototype = PROTOTYPE_MAP.get(regionConfig.getPlainValue("prototype")); - } - FixedRegionData flagStorage = new FixedRegionData(regionData, WorldData::write); - FixedRegionSystem.addRegion(new FixedRegion(name, flagStorage, prototype, regionConfig, regionData)); + public static void generateRegion(String name, YAPIONObject regionConfig) { + Prototype prototype = PROTOTYPE_MAP.get(regionConfig.getPlainValue("prototype")); + FixedRegionSystem.addRegion(new FixedRegion(name, prototype, regionConfig)); } } diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/loader/RegionLoader.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/loader/RegionLoader.java index 3bfe538d..f72ad6ac 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/loader/RegionLoader.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/loader/RegionLoader.java @@ -22,13 +22,11 @@ package de.steamwar.bausystem.region.fixed.loader; import de.steamwar.bausystem.region.fixed.FixedGlobalRegion; import de.steamwar.bausystem.region.fixed.FixedGlobalRegionData; import de.steamwar.bausystem.region.fixed.Prototype; -import de.steamwar.bausystem.worlddata.WorldData; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import yapion.hierarchy.diff.DiffChange; import yapion.hierarchy.diff.YAPIONDiff; import yapion.hierarchy.types.YAPIONObject; -import yapion.hierarchy.types.YAPIONType; import yapion.parser.YAPIONParser; import java.io.BufferedInputStream; @@ -55,7 +53,6 @@ public class RegionLoader { } loaded = yapionObject; - YAPIONObject optionsYapionObject = WorldData.getRegionsData(); yapionObject.forEach((key, yapionAnyType) -> { if (key.equals("global")) { return; @@ -63,23 +60,9 @@ public class RegionLoader { if (!(yapionAnyType instanceof YAPIONObject)) { return; } - - YAPIONObject regionConfig = (YAPIONObject) yapionAnyType; - YAPIONObject regionData = new YAPIONObject(); - if (optionsYapionObject.containsKey(key, YAPIONType.OBJECT)) { - regionData = optionsYapionObject.getObject(key); - } else { - optionsYapionObject.add(key, regionData); - } - - Prototype.generateRegion(key, regionConfig, regionData); + Prototype.generateRegion(key, (YAPIONObject) yapionAnyType); }); - YAPIONObject globalOptions = optionsYapionObject.getObject("global"); - if (globalOptions == null) { - globalOptions = new YAPIONObject(); - optionsYapionObject.add("global", globalOptions); - } - FixedGlobalRegion.setFLAG_STORAGE(new FixedGlobalRegionData(globalOptions, WorldData::write)); + FixedGlobalRegion.setFLAG_STORAGE(new FixedGlobalRegionData(FixedGlobalRegion.INSTANCE)); } }