Implement more in PlotRegionBackups

This commit is contained in:
2026-03-15 17:29:44 +01:00
parent 6b7939a586
commit 24029b795c
5 changed files with 38 additions and 14 deletions
@@ -43,10 +43,10 @@ public interface RegionBackups {
@Getter
abstract class Backup implements RegionDataStore, Comparable<Backup> {
@NonNull
private final BackupType type;
protected final BackupType type;
@NonNull
private final String name;
protected final String name;
@NonNull
protected final RegionData regionData;
@@ -106,6 +106,7 @@ public abstract class RegionData {
/**
* This method only copies all flags and properties from this into other without saving other afterward.
* TODO: If {@link #connectedRegions()} is overridden this method will not work correctly!
*/
public final void copyInto(RegionData other) {
if (this == other) return;
@@ -290,7 +290,7 @@ public class DynamicRegionRepository {
}
@SneakyThrows
private static void deleteDir(File file) {
public static void deleteDir(File file) {
Files.walkFileTree(file.toPath(), new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
@@ -50,10 +50,13 @@ public class PasteUtils {
editSession.close();
}
public static void paste(File file, Point minPoint, int rotate) {
Clipboard clipboard = FlatteningWrapper.impl.loadSchematic(file);
public static EditSession paste(File file, Point minPoint, int rotate) {
try (Clipboard clipboard = FlatteningWrapper.impl.loadSchematic(file)) {
BlockVector3 offset = clipboard.getRegion().getMinimumPoint().subtract(clipboard.getOrigin());
BlockVector3 to = minPoint.toBlockVector3().subtract(offset);
clipboard.paste(BukkitAdapter.adapt(Bukkit.getWorlds().get(0)), to, false, true, new AffineTransform().rotateY(rotate));
return clipboard.paste(BukkitAdapter.adapt(Bukkit.getWorlds().get(0)), to, false, true, new AffineTransform().rotateY(rotate));
} catch (SecurityException exception) {
return null;
}
}
}
@@ -19,15 +19,18 @@
package de.steamwar.bausystem.region.dynamic.modes;
import com.sk89q.worldedit.EditSession;
import de.steamwar.bausystem.region.RegionBackups;
import de.steamwar.bausystem.region.RegionData;
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
import de.steamwar.bausystem.region.dynamic.DynamicRegionRepository;
import de.steamwar.bausystem.region.dynamic.PasteUtils;
import lombok.NonNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class PlotRegionBackups implements RegionBackups {
@@ -57,7 +60,7 @@ public class PlotRegionBackups implements RegionBackups {
}
// Create backup and save!
Backup backup = new BackupImpl(backupType, "", regionDataConstructor);
Backup backup = new BackupImpl(this, backupType, "", regionDataConstructor, region);
// Create schematic and stuff?
backup.save();
backupList.add(backup);
@@ -70,7 +73,7 @@ public class PlotRegionBackups implements RegionBackups {
return backups.values()
.stream()
.flatMap(List::stream)
.collect(Collectors.toList());
.toList();
}
@Override
@@ -85,13 +88,28 @@ public class PlotRegionBackups implements RegionBackups {
private final class BackupImpl extends Backup {
public BackupImpl(@NonNull BackupType type, @NonNull String name, @NonNull Function<Backup, RegionData> regionDataConstructor) {
private static final String SCHEM_FILE = "backup.schem";
private final PlotRegionBackups parent;
private final File folder;
private final DynamicRegion region;
public BackupImpl(PlotRegionBackups parent, @NonNull BackupType type, @NonNull String name, @NonNull Function<Backup, RegionData> regionDataConstructor, DynamicRegion region) {
super(type, name, regionDataConstructor);
this.parent = parent;
folder = new File("");
this.region = region;
}
@Override
public boolean load() {
throw new UnsupportedOperationException();
File file = new File(folder, SCHEM_FILE);
if (!file.exists()) return false;
EditSession editSession = PasteUtils.paste(file, region.getArea().getMinPoint(false), 0);
if (editSession == null) return false;
region.getHistory().remember(editSession);
regionData.copyInto(region.getRegionData());
return true;
}
@Override
@@ -101,7 +119,9 @@ public class PlotRegionBackups implements RegionBackups {
@Override
public void delete() {
throw new UnsupportedOperationException();
parent.backups.getOrDefault(type, Collections.emptyList())
.remove(this);
DynamicRegionRepository.deleteDir(folder);
}
@Override