Fix RegionDataRepository

Add DefaultFlagStorage
This commit is contained in:
2025-08-03 14:02:37 +02:00
parent 4148d42695
commit c06ae73788
10 changed files with 316 additions and 73 deletions

View File

@@ -25,6 +25,7 @@ import lombok.RequiredArgsConstructor;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
@@ -41,7 +42,7 @@ public interface RegionBackups {
@RequiredArgsConstructor
@Getter
abstract class Backup {
abstract class Backup implements Comparator<Backup> {
@NonNull
private final BackupType type;
@@ -55,6 +56,13 @@ public interface RegionBackups {
public abstract boolean load();
public abstract void delete();
public abstract long getCreationTime();
@Override
public int compare(Backup o1, Backup o2) {
return Long.compare(o1.getCreationTime(), o2.getCreationTime());
}
}
@CheckReturnValue

View File

@@ -26,14 +26,15 @@ import lombok.RequiredArgsConstructor;
@Getter
public enum RegionType {
GLOBAL(true),
NORMAL(false),
GLOBAL(true, false),
NORMAL(false, true),
SPAWN(false),
SPAWN_PATH(false),
SPAWN_EXTENSION(false),
PATH(false),
SPAWN(false, false),
SPAWN_PATH(false, false),
SPAWN_EXTENSION(false, false),
PATH(false, false),
;
private final boolean global;
private final boolean createBackup;
}

View File

@@ -22,7 +22,7 @@ package de.steamwar.bausystem.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.dynamic.MovementListener;
import de.steamwar.bausystem.region.dynamic.Tile;
import de.steamwar.bausystem.region.dynamic.global.DynamicGlobalRegion;
import de.steamwar.bausystem.region.dynamic.global.GlobalRegion;
import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@@ -57,16 +57,16 @@ public class DynamicRegionSystem implements RegionSystem {
@Override
public @NonNull Region getGlobalRegion() {
return DynamicGlobalRegion.INSTANCE;
return GlobalRegion.INSTANCE;
}
@Override
public @NonNull Region get(@NonNull Location location) {
Optional<Tile> tile = Tile.fromLocation(location);
if (tile.isEmpty()) return DynamicGlobalRegion.INSTANCE;
if (tile.isEmpty()) return GlobalRegion.INSTANCE;
UUID uuid = tileMap.get(tile.get());
if (uuid == null) return DynamicGlobalRegion.INSTANCE;
return regionMap.getOrDefault(uuid, DynamicGlobalRegion.INSTANCE);
if (uuid == null) return GlobalRegion.INSTANCE;
return regionMap.getOrDefault(uuid, GlobalRegion.INSTANCE);
}
@Override

View File

@@ -0,0 +1,63 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.region.dynamic;
import de.steamwar.bausystem.region.FlagOptional;
import de.steamwar.bausystem.region.FlagStorage;
import de.steamwar.bausystem.region.flags.Flag;
import lombok.NonNull;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
public abstract class DefaultFlagStorage implements FlagStorage {
protected Map<Flag<?>, Flag.Value<?>> flagMap = new HashMap<>();
private Consumer<FlagStorage> operation;
@Override
public <T extends Enum<T> & Flag.Value<T>> boolean set(@NonNull Flag<T> flag, @NonNull T value) {
if (has(flag).isWritable()) {
boolean result = flagMap.put(flag, value) != value;
if (operation != null) {
operation.accept(this);
}
return result;
} else {
return false;
}
}
@Override
public @NonNull <T extends Enum<T> & Flag.Value<T>> FlagOptional<T> get(@NonNull Flag<T> flag) {
return FlagOptional.of(flag, (T) flagMap.get(flag));
}
@Override
public Map<Flag<?>, Flag.Value<?>> getBackedMap() {
return flagMap;
}
@Override
public void setSaveOperation(Consumer<FlagStorage> operation) {
this.operation = operation;
}
}

View File

@@ -19,6 +19,7 @@
package de.steamwar.bausystem.region.dynamic;
import de.steamwar.bausystem.region.FlagStorage;
import de.steamwar.bausystem.region.Region;
public interface DynamicRegion extends Region {
@@ -26,4 +27,6 @@ public interface DynamicRegion extends Region {
default void update(Tile updateFrom) {
System.out.println("Updating from " + updateFrom);
}
void setFlags(FlagStorage flags);
}

View File

@@ -21,9 +21,16 @@ package de.steamwar.bausystem.region.dynamic;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonWriter;
import com.sk89q.worldedit.EditSession;
import de.steamwar.bausystem.region.FlagStorage;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.RegionBackups;
import de.steamwar.bausystem.region.dynamic.normal.NormalFlagStorage;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.bausystem.utils.PasteBuilder;
import lombok.Cleanup;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
@@ -32,20 +39,32 @@ import org.bukkit.Bukkit;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@UtilityClass
public class RegionDataRepository {
public static final String FLAGS_FILE_NAME = "flags.json";
public static final String REGION_SCHEM_FILE_NAME = "region.schem";
private static File regionDataFolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions");
static {
regionDataFolder.mkdirs();
}
public File getRegionDirectory(DynamicRegion region) {
private File getRegionDirectory(DynamicRegion region) {
File file = new File(regionDataFolder, region.getID().toString());
file.mkdirs();
return file;
@@ -53,12 +72,16 @@ public class RegionDataRepository {
public void loadFlagStorage(DynamicRegion region, FlagStorage storage) {
File file = getRegionDirectory(region);
file = new File(file, "flags.json");
file = new File(file, FLAGS_FILE_NAME);
loadFlagStorage(file, storage);
storage.setSaveOperation(currentStorage -> {
saveFlagStorage(region, currentStorage);
});
}
@SneakyThrows
private void loadFlagStorage(File file, FlagStorage storage) {
if (file == null || !file.exists()) return;
JsonObject jsonObject = JsonParser.parseReader(new FileReader(file)).getAsJsonObject();
for (Flag flag : Flag.getFlags()) {
Flag.Value<?> value;
@@ -71,30 +94,164 @@ public class RegionDataRepository {
}
}
private void saveFlagStorage(DynamicRegion region, FlagStorage storage) {
File file = getRegionDirectory(region);
file = new File(file, FLAGS_FILE_NAME);
saveFlagStorage(file, storage);
}
@SneakyThrows
public void saveFlagStorage(File file, FlagStorage storage) {
file.getParentFile().mkdirs();
@Cleanup
JsonWriter jsonWriter = new JsonWriter(new FileWriter(file));
jsonWriter.setIndent(" ");
jsonWriter.beginObject();
for (Map.Entry<Flag<?>, Flag.Value<?>> entry : storage.getBackedMap().entrySet()) {
jsonWriter.name(entry.getKey().name());
jsonWriter.value(entry.getValue().name());
}
jsonWriter.endObject();
}
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd'_'HH:mm:ss");
public RegionBackups getBackups(DynamicRegion region) {
if (region.getType().isGlobal()) {
if (!region.getType().isCreateBackup()) {
return RegionBackups.EMPTY;
}
File directory = new File(getRegionDirectory(region), "backup");
directory.mkdirs();
return new RegionBackups() {
private List<Backup> backups = new ArrayList<>();
{
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
backups.add(new BackupImpl(file, region));
}
}
backups.sort(null);
}
@Override
@SneakyThrows
public Optional<Backup> create(BackupType backupType) {
return Optional.empty();
String name = LocalDateTime.now().format(formatter);
File backupDirectory = new File(directory, name);
backupDirectory.mkdirs();
Point minPoint = region.getArea().getMinPoint(false);
Point maxPoint = region.getArea().getMaxPoint(false);
boolean success = FlatteningWrapper.impl.backup(minPoint, maxPoint, new File(backupDirectory, REGION_SCHEM_FILE_NAME));
if (!success) {
deleteDir(backupDirectory);
return Optional.empty();
}
int count = 0;
for (int i = backups.size() - 1; i >= 0; i--) {
Backup backup = backups.get(i);
if (backup.getType() == backupType) {
if (count >= backupType.maxBackups - 1) {
backup.delete();
backups.remove(i);
continue;
}
count++;
}
}
new File(backupDirectory, backupType.name()).createNewFile();
saveFlagStorage(new File(backupDirectory, FLAGS_FILE_NAME), region.getFlags());
Backup backup = new BackupImpl(backupDirectory, region);
backups.add(backup);
return Optional.of(backup);
}
@Override
public @NonNull List<Backup> list() {
return List.of();
return backups;
}
@Nullable
@Override
public Backup get(String name) {
for (Backup backup : backups) {
if (backup.getName().equals(name)) {
return backup;
}
}
return null;
}
};
}
private static class BackupImpl extends RegionBackups.Backup {
private final File file;
private final DynamicRegion region;
public BackupImpl(File file, DynamicRegion region) {
super(getType(file), file.getName(), getFlags(file));
this.file = file;
this.region = region;
}
private static RegionBackups.BackupType getType(File file) {
for (RegionBackups.BackupType type : RegionBackups.BackupType.values()) {
if (new File(file, type.name()).exists()) return type;
}
throw new IllegalArgumentException("Unknown backup type");
}
private static FlagStorage getFlags(File file) {
NormalFlagStorage storage = new NormalFlagStorage();
loadFlagStorage(new File(file, FLAGS_FILE_NAME), storage);
return storage;
}
@Override
public boolean load() {
if (!file.exists()) return false;
EditSession editSession = new PasteBuilder(new PasteBuilder.FileProvider(new File(file, REGION_SCHEM_FILE_NAME)))
.pastePoint(region.getArea().getMinPoint(false))
.minPoint(region.getArea().getMinPoint(false))
.maxPoint(region.getArea().getMaxPoint(false))
.run();
region.getHistory().remember(editSession);
region.getFlags().setSaveOperation(null);
region.setFlags(getFlags());
region.getFlags().setSaveOperation(storage -> saveFlagStorage(region, storage));
return true;
}
@Override
public void delete() {
deleteDir(file);
}
@Override
public long getCreationTime() {
return file.lastModified();
}
}
@SneakyThrows
private static void deleteDir(File file) {
Files.walkFileTree(file.toPath(), new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
}

View File

@@ -20,37 +20,21 @@
package de.steamwar.bausystem.region.dynamic.global;
import de.steamwar.bausystem.region.FlagOptional;
import de.steamwar.bausystem.region.FlagStorage;
import de.steamwar.bausystem.region.RegionFlagPolicy;
import de.steamwar.bausystem.region.dynamic.DefaultFlagStorage;
import de.steamwar.bausystem.region.flags.ColorMode;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.ProtectMode;
import de.steamwar.bausystem.region.flags.TNTMode;
import de.steamwar.bausystem.worlddata.WorldData;
import de.steamwar.core.Core;
import lombok.NonNull;
import yapion.hierarchy.types.YAPIONObject;
import lombok.ToString;
import java.util.HashMap;
import java.util.Map;
@ToString
public class GlobalFlagStorage extends DefaultFlagStorage {
public class DynamicGlobalFlagStorage implements FlagStorage {
private Map<Flag<?>, Flag.Value<?>> flagMap = new HashMap<>();
private YAPIONObject data;
public DynamicGlobalFlagStorage(YAPIONObject data) {
public GlobalFlagStorage() {
flagMap.put(Flag.TNT, TNTMode.DENY);
this.data = data;
for (final Flag flag : Flag.getFlags()) {
if (!has(flag).isWritable()) continue;
try {
String s = data.getPlainValue(flag.name());
flagMap.put(flag, flag.valueOfValue(s));
} catch (Exception e) {
flagMap.put(flag, (Flag.Value<?>) flag.getDefaultValue());
}
}
}
@Override
@@ -67,17 +51,6 @@ public class DynamicGlobalFlagStorage implements FlagStorage {
return RegionFlagPolicy.NOT_APPLICABLE;
}
@Override
public <T extends Enum<T> & Flag.Value<T>> boolean set(@NonNull Flag<T> flag, @NonNull T value) {
if (has(flag).isWritable()) {
data.put(flag.name(), value.name());
WorldData.write();
return flagMap.put(flag, value) != value;
} else {
return false;
}
}
@Override
public @NonNull <T extends Enum<T> & Flag.Value<T>> FlagOptional<T> get(@NonNull Flag<T> flag) {
if (flag.oneOf(Flag.COLOR)) {
@@ -86,23 +59,6 @@ public class DynamicGlobalFlagStorage implements FlagStorage {
if (flag.oneOf(Flag.PROTECT)) {
return FlagOptional.of((Flag) flag, ProtectMode.INACTIVE);
}
return FlagOptional.of(flag, (T) flagMap.get(flag));
}
@Override
public void clear() {
flagMap.clear();
}
@Override
public Map<Flag<?>, Flag.Value<?>> getBackedMap() {
return flagMap;
}
@Override
public String toString() {
return "FixedGlobalFlagStorage{" +
"flagMap=" + flagMap +
'}';
return super.get(flag);
}
}

View File

@@ -22,25 +22,23 @@ package de.steamwar.bausystem.region.dynamic.global;
import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.bausystem.region.*;
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
import de.steamwar.bausystem.region.dynamic.RegionDataRepository;
import de.steamwar.bausystem.utils.PasteBuilder;
import lombok.NonNull;
import org.bukkit.Location;
import yapion.hierarchy.types.YAPIONObject;
import javax.annotation.Nullable;
import java.io.File;
import java.util.UUID;
import java.util.function.BiConsumer;
public class DynamicGlobalRegion implements DynamicRegion {
public class GlobalRegion implements DynamicRegion {
public static final DynamicGlobalRegion INSTANCE = new DynamicGlobalRegion();
public static final GlobalRegion INSTANCE = new GlobalRegion();
private static final Point MIN_POINT = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
private static final Point MAX_POINT = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
private static FlagStorage FLAG_STORAGE = new DynamicGlobalFlagStorage(new YAPIONObject());
private static final UUID GLOBAL_REGION_ID = new UUID(0, 0);
private static final Region.Area GLOBAL_AREA = new Region.Area() {
@@ -92,7 +90,12 @@ public class DynamicGlobalRegion implements DynamicRegion {
private static final RegionConfig GLOBAL_CONFIG = new RegionConfig(null);
private DynamicGlobalRegion() {
private static FlagStorage FLAG_STORAGE = new GlobalFlagStorage();
static {
RegionDataRepository.loadFlagStorage(INSTANCE, FLAG_STORAGE);
}
private GlobalRegion() {
}
@Override
@@ -110,6 +113,11 @@ public class DynamicGlobalRegion implements DynamicRegion {
return FLAG_STORAGE;
}
@Override
public void setFlags(FlagStorage flags) {
throw new UnsupportedOperationException();
}
@Override
public @NonNull Region.Area getArea() {
return GLOBAL_AREA;

View File

@@ -0,0 +1,42 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.region.dynamic.normal;
import de.steamwar.bausystem.region.RegionFlagPolicy;
import de.steamwar.bausystem.region.dynamic.DefaultFlagStorage;
import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.core.Core;
import lombok.NonNull;
import lombok.ToString;
@ToString
public class NormalFlagStorage extends DefaultFlagStorage {
@Override
public @NonNull <T extends Enum<T> & Flag.Value<T>> RegionFlagPolicy has(@NonNull Flag<T> flag) {
if (flag.oneOf(Flag.COLOR, Flag.TNT, Flag.FIRE, Flag.FREEZE, Flag.PROTECT, Flag.NO_GRAVITY, Flag.TESTBLOCK, Flag.CHANGED)) {
return RegionFlagPolicy.WRITABLE;
}
if (flag.oneOf(Flag.ITEMS) && Core.getVersion() >= 20) {
return RegionFlagPolicy.WRITABLE;
}
return RegionFlagPolicy.NOT_APPLICABLE;
}
}

View File

@@ -137,6 +137,11 @@ public class FixedRegion implements Region {
public void delete() {
file.delete();
}
@Override
public long getCreationTime() {
return file.lastModified();
}
}
public FixedRegion(String name, FixedRegionData flagStorage, Prototype prototype, YAPIONObject regionConfig, YAPIONObject regionData) {