From 1d42f04e9ae97dd6e3de7a9ba68836d8acc40dff Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Sun, 8 Mar 2026 08:19:46 +0100 Subject: [PATCH] Initial rudimentary implementation (not done) of PlotRegionBackups --- .../bausystem/region/RegionBackups.java | 14 +++- .../steamwar/bausystem/region/RegionData.java | 13 +++ .../dynamic/modes/PlotRegionBackups.java | 81 +++++++++++++++++-- .../bausystem/region/fixed/FixedRegion.java | 4 +- 4 files changed, 98 insertions(+), 14 deletions(-) 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 29318e42..eda6cd2e 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionBackups.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionBackups.java @@ -27,6 +27,7 @@ import javax.annotation.CheckReturnValue; import javax.annotation.Nullable; import java.util.List; import java.util.Optional; +import java.util.function.Function; public interface RegionBackups { @@ -39,7 +40,6 @@ public interface RegionBackups { public final int maxBackups; } - @RequiredArgsConstructor @Getter abstract class Backup implements RegionDataStore, Comparable { @NonNull @@ -49,7 +49,13 @@ public interface RegionBackups { private final String name; @NonNull - private final RegionData regionData; + protected final RegionData regionData; + + protected Backup(@NonNull BackupType type, @NonNull String name, @NonNull Function regionDataConstructor) { + this.type = type; + this.name = name; + regionData = regionDataConstructor.apply(this); + } @CheckReturnValue public abstract boolean load(); @@ -73,7 +79,7 @@ public interface RegionBackups { List list(); @Nullable - Backup get(String name); + Backup get(@NonNull String name); RegionBackups EMPTY = new RegionBackups() { @Override @@ -88,7 +94,7 @@ public interface RegionBackups { @Nullable @Override - public Backup get(String name) { + public Backup get(@NonNull String name) { return null; } }; 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 36e4b5cd..fdc66505 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionData.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/region/RegionData.java @@ -34,6 +34,7 @@ public abstract class RegionData { private final List> properties = new ArrayList<>(); protected final Map, Flag.Value> flagMap = new HashMap<>(); + // TODO: These should be turned into an ECS like System because not every Region has them or should have them! protected final Property testblockSchematic = new Property<>("testblockSchematic", SchematicNode::byId, SchematicNode::getId); protected RegionData(RegionDataStore store) { @@ -103,6 +104,18 @@ public abstract class RegionData { }); } + /** + * This method only copies all flags and properties from this into other without saving other afterward. + */ + public final void copyInto(RegionData other) { + if (this == other) return; + other.flagMap.clear(); + other.flagMap.putAll(flagMap); + // TODO: This might not be correct, needs to be investigated + other.properties.clear(); + other.properties.addAll(properties); + } + public final Map, Flag.Value> getBackedMap() { return flagMap; } diff --git a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/modes/PlotRegionBackups.java b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/modes/PlotRegionBackups.java index 62b71501..7c1c6624 100644 --- a/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/modes/PlotRegionBackups.java +++ b/BauSystem/BauSystem_RegionDynamic/src/de/steamwar/bausystem/region/dynamic/modes/PlotRegionBackups.java @@ -21,32 +21,97 @@ package de.steamwar.bausystem.region.dynamic.modes; import de.steamwar.bausystem.region.RegionBackups; import de.steamwar.bausystem.region.RegionData; -import de.steamwar.bausystem.region.RegionDataStore; import de.steamwar.bausystem.region.dynamic.DynamicRegion; import lombok.NonNull; import org.jetbrains.annotations.Nullable; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.function.Function; +import java.util.stream.Collectors; public class PlotRegionBackups implements RegionBackups { - public PlotRegionBackups(DynamicRegion region, Function regionDataConstructor) { + private final DynamicRegion region; + private final Function regionDataConstructor; + private final Map> backups = new HashMap<>(); + + /** + * @param region + * @param regionDataConstructor construct the regionData copying the values from the region into the returned value. + */ + public PlotRegionBackups(DynamicRegion region, Function regionDataConstructor) { + this.region = region; + this.regionDataConstructor = regionDataConstructor; + + // DynamicRegionRepository.loadRegionData(); } @Override public Optional create(BackupType backupType) { - return Optional.empty(); + List backupList = backups.computeIfAbsent(backupType, __ -> new ArrayList<>()); + + // Cleanup backups if there are too many! + backupList.sort(Backup::compareTo); + while (backupList.size() >= backupType.maxBackups) { + backupList.removeFirst().delete(); + } + + // Create backup and save! + Backup backup = new BackupImpl(backupType, "", regionDataConstructor); + // Create schematic and stuff? + backup.save(); + backupList.add(backup); + + return Optional.of(backup); } @Override public @NonNull List list() { - return List.of(); + return backups.values() + .stream() + .flatMap(List::stream) + .collect(Collectors.toList()); } @Override - public @Nullable Backup get(String name) { - return null; + public @Nullable Backup get(@NonNull String name) { + return backups.values() + .stream() + .flatMap(List::stream) + .filter(backup -> backup.getName().equals(name)) + .findFirst() + .orElse(null); + } + + private final class BackupImpl extends Backup { + + public BackupImpl(@NonNull BackupType type, @NonNull String name, @NonNull Function regionDataConstructor) { + super(type, name, regionDataConstructor); + } + + @Override + public boolean load() { + throw new UnsupportedOperationException(); + } + + @Override + public long getCreationTime() { + throw new UnsupportedOperationException(); + } + + @Override + public void delete() { + throw new UnsupportedOperationException(); + } + + @Override + public void save() { + throw new UnsupportedOperationException(); + } + + @Override + public void load(RegionData regionData) { + throw new UnsupportedOperationException(); + } } } 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 5b897f4b..8117420d 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 @@ -101,7 +101,7 @@ public class FixedRegion implements Region { @Nullable @Override - public Backup get(String name) { + public Backup get(@NonNull String name) { final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), FixedRegion.this.name); //noinspection ResultOfMethodCallIgnored definedBackupFolder.mkdirs(); @@ -116,7 +116,7 @@ public class FixedRegion implements Region { private final File file; public BackupImpl(File file) { - super(RegionBackups.BackupType.AUTOMATIC, file.getName().replace(' ', '_').replace(".schem", ""), flagStorage); + super(RegionBackups.BackupType.AUTOMATIC, file.getName().replace(' ', '_').replace(".schem", ""), __ -> flagStorage); this.file = file; }