Implement FixedRegion.regionBackups

This commit is contained in:
2025-08-02 14:31:46 +02:00
parent 0a3ae7117d
commit b14c39683a
2 changed files with 92 additions and 4 deletions
@@ -24,19 +24,28 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.*;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.TestblockMode;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.core.Core;
import de.steamwar.sql.SchematicType;
import lombok.NonNull;
import org.bukkit.Bukkit;
import yapion.hierarchy.types.YAPIONObject;
import javax.annotation.Nullable;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
public class FixedRegion implements Region {
private static final File backupFolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "backup");
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd' 'HH:mm:ss");
private final String name;
private final UUID uuid;
private final FixedFlagStorage flagStorage;
private final Prototype prototype;
@@ -50,7 +59,86 @@ public class FixedRegion implements Region {
private final RegionConfig regionConfig;
private final RegionHistory regionHistory = new RegionHistory.Impl(20);
private final RegionBackups regionBackups = new RegionBackups() {
@Override
public Optional<Backup> create(BackupType backupType) {
final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), name);
//noinspection ResultOfMethodCallIgnored
definedBackupFolder.mkdirs();
File[] currentBackups = definedBackupFolder.listFiles();
if (currentBackups != null && currentBackups.length >= 20) {
List<File> files = new ArrayList<>(Arrays.asList(currentBackups));
files.sort(Comparator.comparingLong(File::lastModified));
while (files.size() >= 20) files.remove(0).delete();
}
final File backupFile = new File(definedBackupFolder, LocalDateTime.now().format(formatter) + ".schem");
Point minPoint = area.getMinPoint(false);
Point maxPoint = area.getMaxPoint(false);
if (!FlatteningWrapper.impl.backup(minPoint, maxPoint, backupFile)) {
return Optional.empty();
}
return Optional.of(new BackupImpl(backupFile));
}
@Override
public @NonNull List<Backup> list() {
final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), name);
//noinspection ResultOfMethodCallIgnored
definedBackupFolder.mkdirs();
File[] currentBackups = definedBackupFolder.listFiles();
if (currentBackups == null || currentBackups.length == 0) {
return Collections.emptyList();
}
List<File> files = new ArrayList<>(Arrays.asList(currentBackups));
files.sort(Comparator.comparingLong(File::lastModified));
return files.stream().map(BackupImpl::new).collect(Collectors.toList());
}
@Nullable
@Override
public Backup get(String name) {
final File definedBackupFolder = new File(new File(backupFolder, prototype.getName()), name);
//noinspection ResultOfMethodCallIgnored
definedBackupFolder.mkdirs();
File[] files = definedBackupFolder.listFiles((dir, s) -> s.equals(name + ".schem"));
if (files == null || files.length == 0) return null;
return new BackupImpl(files[0]);
}
};
private class BackupImpl extends RegionBackups.Backup {
private final File file;
public BackupImpl(File file) {
super(RegionBackups.BackupType.AUTOMATIC, file.getName(), flagStorage);
this.file = file;
}
@Override
public boolean load() {
if (!file.exists()) return false;
EditSession editSession = new PasteBuilder(new PasteBuilder.FileProvider(file))
.pastePoint(area.getMinPoint(false).add(prototype.getSizeX() / 2, 0, prototype.getSizeZ() / 2))
.minPoint(area.getMinPoint(false))
.maxPoint(area.getMaxPoint(false))
.waterLevel(waterLevel)
.run();
regionHistory.remember(editSession);
return true;
}
@Override
public void delete() {
file.delete();
}
}
public FixedRegion(String name, FixedFlagStorage flagStorage, Prototype prototype, YAPIONObject regionConfig, YAPIONObject regionData) {
this.name = name;
uuid = UUID.nameUUIDFromBytes(name.getBytes(StandardCharsets.UTF_8));
this.flagStorage = flagStorage;
this.prototype = prototype;
@@ -293,6 +381,6 @@ public class FixedRegion implements Region {
@Override
public @NonNull RegionBackups getBackups() {
return RegionBackups.EMPTY;
return regionBackups;
}
}