Add RegionDataRepository

This commit is contained in:
2025-08-03 13:05:52 +02:00
parent 7c61e305b0
commit 2709de3297
4 changed files with 106 additions and 4 deletions

View File

@@ -28,6 +28,11 @@ public enum RegionType {
GLOBAL(true),
NORMAL(false),
SPAWN(false),
SPAWN_PATH(false),
SPAWN_EXTENSION(false),
PATH(false),
;
private final boolean global;

View File

@@ -43,5 +43,4 @@ dependencies {
compileOnly(libs.fawe18)
implementation(libs.luaj)
implementation(files("$projectDir/../libs/YAPION-SNAPSHOT.jar"))
}

View File

@@ -66,9 +66,7 @@ public class DynamicRegionSystem implements RegionSystem {
if (tile.isEmpty()) return DynamicGlobalRegion.INSTANCE;
UUID uuid = tileMap.get(tile.get());
if (uuid == null) return DynamicGlobalRegion.INSTANCE;
Region region = regionMap.get(uuid);
if (region == null) return DynamicGlobalRegion.INSTANCE;
return region;
return regionMap.getOrDefault(uuid, DynamicGlobalRegion.INSTANCE);
}
@Override

View File

@@ -0,0 +1,100 @@
/*
* 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 com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import de.steamwar.bausystem.region.FlagStorage;
import de.steamwar.bausystem.region.RegionBackups;
import de.steamwar.bausystem.region.flags.Flag;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileReader;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
@UtilityClass
public class RegionDataRepository {
private static File regionDataFolder = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions");
static {
regionDataFolder.mkdirs();
}
public File getRegionDirectory(DynamicRegion region) {
File file = new File(regionDataFolder, region.getID().toString());
file.mkdirs();
return file;
}
public void loadFlagStorage(DynamicRegion region, FlagStorage storage) {
File file = getRegionDirectory(region);
file = new File(file, "flags.json");
loadFlagStorage(file, storage);
}
@SneakyThrows
private void loadFlagStorage(File file, FlagStorage storage) {
JsonObject jsonObject = JsonParser.parseReader(new FileReader(file)).getAsJsonObject();
for (Flag flag : Flag.getFlags()) {
Flag.Value<?> value;
try {
value = flag.valueOfValue(jsonObject.get(flag.name()).getAsString());
} catch (IllegalArgumentException e) {
continue;
}
storage.getBackedMap().put(flag, value);
}
}
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd'_'HH:mm:ss");
public RegionBackups getBackups(DynamicRegion region) {
if (region.getType().isGlobal()) {
return RegionBackups.EMPTY;
}
File directory = new File(getRegionDirectory(region), "backup");
directory.mkdirs();
return new RegionBackups() {
@Override
public Optional<Backup> create(BackupType backupType) {
return Optional.empty();
}
@Override
public @NonNull List<Backup> list() {
return List.of();
}
@Nullable
@Override
public Backup get(String name) {
return null;
}
};
}
}