From b14c39683a9365730aa1ce011010fdd3fdb219c6 Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Sat, 2 Aug 2025 14:31:46 +0200 Subject: [PATCH] Implement FixedRegion.regionBackups --- .../features/backup/BackupCommand.java | 4 +- .../bausystem/region/fixed/FixedRegion.java | 92 ++++++++++++++++++- 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/backup/BackupCommand.java b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/backup/BackupCommand.java index 6c7708c9..393549b7 100644 --- a/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/backup/BackupCommand.java +++ b/BauSystem/BauSystem_Main/src/de/steamwar/bausystem/features/backup/BackupCommand.java @@ -120,12 +120,12 @@ public class BackupCommand extends SWCommand { return new TypeMapper() { @Override public RegionBackups.Backup map(CommandSender commandSender, String[] previousArguments, String s) { - return Region.getRegion(((Player) commandSender).getLocation()).getBackups().get(s); + return Region.getRegion(((Player) commandSender).getLocation()).getBackups().get(s.replace('_', ' ')); } @Override public Collection tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) { - return listBackup((Player) sender).stream().map(RegionBackups.Backup::getName).collect(Collectors.toList()); + return listBackup((Player) sender).stream().map(RegionBackups.Backup::getName).map(name -> name.replace(' ', '_')).collect(Collectors.toList()); } }; } 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 d89095c3..ed109105 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 @@ -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 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 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 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 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; } }