forked from SteamWar/SteamWar
Initial rudimentary implementation (not done) of PlotRegionBackups
This commit is contained in:
+73
-8
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user