Initial rudimentary implementation (not done) of PlotRegionBackups

This commit is contained in:
2026-03-08 08:19:46 +01:00
parent 8bbb1239a6
commit 3296620119
4 changed files with 98 additions and 14 deletions

View File

@@ -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<Backup> {
@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<Backup, RegionData> regionDataConstructor) {
this.type = type;
this.name = name;
regionData = regionDataConstructor.apply(this);
}
@CheckReturnValue
public abstract boolean load();
@@ -73,7 +79,7 @@ public interface RegionBackups {
List<Backup> 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;
}
};

View File

@@ -34,6 +34,7 @@ public abstract class RegionData {
private final List<Property<?, ?>> properties = new ArrayList<>();
protected final Map<Flag<?>, 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<SchematicNode, Integer> 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<?>, Flag.Value<?>> getBackedMap() {
return flagMap;
}

View File

@@ -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<RegionDataStore, RegionData> regionDataConstructor) {
private final DynamicRegion region;
private final Function<Backup, RegionData> regionDataConstructor;
private final Map<BackupType, List<Backup>> 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<Backup, RegionData> regionDataConstructor) {
this.region = region;
this.regionDataConstructor = regionDataConstructor;
// DynamicRegionRepository.loadRegionData();
}
@Override
public Optional<Backup> create(BackupType backupType) {
return Optional.empty();
List<Backup> 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<Backup> 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<Backup, RegionData> 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();
}
}
}

View File

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