forked from SteamWar/SteamWar
Implement FixedRegion.regionBackups
This commit is contained in:
+2
-2
@@ -120,12 +120,12 @@ public class BackupCommand extends SWCommand {
|
|||||||
return new TypeMapper<RegionBackups.Backup>() {
|
return new TypeMapper<RegionBackups.Backup>() {
|
||||||
@Override
|
@Override
|
||||||
public RegionBackups.Backup map(CommandSender commandSender, String[] previousArguments, String s) {
|
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
|
@Override
|
||||||
public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
|
public Collection<String> 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());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+90
-2
@@ -24,19 +24,28 @@ import de.steamwar.bausystem.BauSystem;
|
|||||||
import de.steamwar.bausystem.region.*;
|
import de.steamwar.bausystem.region.*;
|
||||||
import de.steamwar.bausystem.region.flags.Flag;
|
import de.steamwar.bausystem.region.flags.Flag;
|
||||||
import de.steamwar.bausystem.region.flags.TestblockMode;
|
import de.steamwar.bausystem.region.flags.TestblockMode;
|
||||||
|
import de.steamwar.bausystem.utils.FlatteningWrapper;
|
||||||
import de.steamwar.bausystem.utils.PasteBuilder;
|
import de.steamwar.bausystem.utils.PasteBuilder;
|
||||||
import de.steamwar.core.Core;
|
import de.steamwar.core.Core;
|
||||||
import de.steamwar.sql.SchematicType;
|
import de.steamwar.sql.SchematicType;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
import yapion.hierarchy.types.YAPIONObject;
|
import yapion.hierarchy.types.YAPIONObject;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.charset.StandardCharsets;
|
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 {
|
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 UUID uuid;
|
||||||
private final FixedFlagStorage flagStorage;
|
private final FixedFlagStorage flagStorage;
|
||||||
private final Prototype prototype;
|
private final Prototype prototype;
|
||||||
@@ -50,7 +59,86 @@ public class FixedRegion implements Region {
|
|||||||
private final RegionConfig regionConfig;
|
private final RegionConfig regionConfig;
|
||||||
private final RegionHistory regionHistory = new RegionHistory.Impl(20);
|
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) {
|
public FixedRegion(String name, FixedFlagStorage flagStorage, Prototype prototype, YAPIONObject regionConfig, YAPIONObject regionData) {
|
||||||
|
this.name = name;
|
||||||
uuid = UUID.nameUUIDFromBytes(name.getBytes(StandardCharsets.UTF_8));
|
uuid = UUID.nameUUIDFromBytes(name.getBytes(StandardCharsets.UTF_8));
|
||||||
this.flagStorage = flagStorage;
|
this.flagStorage = flagStorage;
|
||||||
this.prototype = prototype;
|
this.prototype = prototype;
|
||||||
@@ -293,6 +381,6 @@ public class FixedRegion implements Region {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NonNull RegionBackups getBackups() {
|
public @NonNull RegionBackups getBackups() {
|
||||||
return RegionBackups.EMPTY;
|
return regionBackups;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user