Implement PathRegionData as singleton

This commit is contained in:
2026-03-29 14:14:29 +02:00
parent e7cd1f883b
commit fbafd05ae2
6 changed files with 201 additions and 9 deletions
@@ -122,6 +122,8 @@ public class DynamicRegionEditor implements SWPlayer.Component, Listener {
entityServer.close(); entityServer.close();
task.cancel(); task.cancel();
PlayerMoveEvent.getHandlerList().unregister(this); PlayerMoveEvent.getHandlerList().unregister(this);
PlayerSwapHandItemsEvent.getHandlerList().unregister(this);
PlayerInteractEvent.getHandlerList().unregister(this);
} }
@EventHandler @EventHandler
@@ -23,6 +23,7 @@ import com.google.gson.*;
import com.google.gson.stream.JsonWriter; import com.google.gson.stream.JsonWriter;
import de.steamwar.bausystem.region.*; import de.steamwar.bausystem.region.*;
import de.steamwar.bausystem.region.dynamic.path.PathRegion; import de.steamwar.bausystem.region.dynamic.path.PathRegion;
import de.steamwar.bausystem.region.dynamic.path.PathRegionData;
import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.Flag;
import lombok.Cleanup; import lombok.Cleanup;
import lombok.SneakyThrows; import lombok.SneakyThrows;
@@ -94,7 +95,7 @@ public class DynamicRegionRepository {
RegionSystem.LOGGER.log(Level.WARNING, "Failed to resolve region id: " + region.getName()); RegionSystem.LOGGER.log(Level.WARNING, "Failed to resolve region id: " + region.getName());
continue; continue;
} }
if (regionUUID.equals(RegionSystem.GLOBAL_REGION_ID)) { if (regionUUID.equals(RegionSystem.GLOBAL_REGION_ID) || regionUUID.equals(PathRegionData.PATH_REGION_ID)) {
continue; continue;
} }
@@ -179,13 +180,18 @@ public class DynamicRegionRepository {
return regionConstructor.newInstance(uuid, tileData); return regionConstructor.newInstance(uuid, tileData);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | } catch (InstantiationException | IllegalAccessException | IllegalArgumentException |
InvocationTargetException e) { InvocationTargetException e) {
e.printStackTrace();
RegionSystem.LOGGER.log(Level.SEVERE, "Failed to read region metadata file (invalid data)"); RegionSystem.LOGGER.log(Level.SEVERE, "Failed to read region metadata file (invalid data)");
return null; return null;
} }
} }
private static File getRegionDirectory(UUID uuid) {
return new File(REGION_DATA_FOLDER, uuid.toString());
}
public static File getRegionDirectory(Region region) { public static File getRegionDirectory(Region region) {
return new File(REGION_DATA_FOLDER, region.getID().toString()); return getRegionDirectory(region.getID());
} }
public static File getBackupsTypeDirectory(Region region, RegionBackups.BackupType backupType) { public static File getBackupsTypeDirectory(Region region, RegionBackups.BackupType backupType) {
@@ -198,7 +204,17 @@ public class DynamicRegionRepository {
return new File(getBackupsTypeDirectory(region, backup.getType()), backup.getName()); return new File(getBackupsTypeDirectory(region, backup.getType()), backup.getName());
} }
public static void loadRegionData(UUID uuid, RegionData regionData) {
File regionDirectory = getRegionDirectory(uuid);
if (!regionDirectory.exists()) return;
loadRegionData(regionDirectory, regionData);
}
public static void loadRegionData(Region region, RegionData regionData) { public static void loadRegionData(Region region, RegionData regionData) {
if (region.getRegionData() instanceof PathRegionData) {
return;
}
File regionDirectory = getRegionDirectory(region); File regionDirectory = getRegionDirectory(region);
if (!regionDirectory.exists()) return; if (!regionDirectory.exists()) return;
loadRegionData(regionDirectory, regionData); loadRegionData(regionDirectory, regionData);
@@ -252,16 +268,17 @@ public class DynamicRegionRepository {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
File regionDirectory = new File(REGION_DATA_FOLDER, region.getID().toString()); File regionDirectory = getRegionDirectory(region);
if (!regionDirectory.exists()) { if (!regionDirectory.exists()) regionDirectory.mkdir();
regionDirectory.mkdir();
}
if (region instanceof DynamicRegion dynamicRegion) { if (region instanceof DynamicRegion dynamicRegion) {
RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(region.getClass()); RegionConstructorData constructorData = DynamicRegionSystem.constructorDataMap.get(region.getClass());
writeMetaFile(regionDirectory, constructorData, dynamicRegion); writeMetaFile(regionDirectory, constructorData, dynamicRegion);
} }
if (region.getRegionData() instanceof PathRegionData) {
return;
}
writeFlagsFile(regionDirectory, region.getRegionData()); writeFlagsFile(regionDirectory, region.getRegionData());
} }
@@ -273,6 +290,11 @@ public class DynamicRegionRepository {
writeFlagsFile(backupDirectory, backup.getRegionData()); writeFlagsFile(backupDirectory, backup.getRegionData());
} }
public static void saveData(UUID uuid, RegionData regionData) {
File regionDirectory = getRegionDirectory(uuid);
writeFlagsFile(regionDirectory, regionData);
}
@SneakyThrows @SneakyThrows
private static void writeMetaFile(File regionDirectory, RegionConstructorData constructorData, DynamicRegion dynamicRegion) { private static void writeMetaFile(File regionDirectory, RegionConstructorData constructorData, DynamicRegion dynamicRegion) {
@Cleanup @Cleanup
@@ -63,7 +63,7 @@ public class PathRegion extends DynamicRegion {
super(id, null); super(id, null);
this.tile = tile; this.tile = tile;
area = new PathArea(tile, this); area = new PathArea(tile, this);
regionData = new PathRegionData(this); regionData = PathRegionData.INSTANCE;
calculateGardenState(); calculateGardenState();
} }
@@ -20,7 +20,9 @@
package de.steamwar.bausystem.region.dynamic.path; package de.steamwar.bausystem.region.dynamic.path;
import de.steamwar.bausystem.region.RegionData; import de.steamwar.bausystem.region.RegionData;
import de.steamwar.bausystem.region.RegionDataStore;
import de.steamwar.bausystem.region.RegionFlagPolicy; import de.steamwar.bausystem.region.RegionFlagPolicy;
import de.steamwar.bausystem.region.dynamic.DynamicRegionRepository;
import de.steamwar.bausystem.region.flags.FireMode; import de.steamwar.bausystem.region.flags.FireMode;
import de.steamwar.bausystem.region.flags.Flag; import de.steamwar.bausystem.region.flags.Flag;
import de.steamwar.bausystem.region.flags.ProtectMode; import de.steamwar.bausystem.region.flags.ProtectMode;
@@ -28,10 +30,26 @@ import de.steamwar.bausystem.region.flags.TNTMode;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import lombok.NonNull; import lombok.NonNull;
import java.util.UUID;
public class PathRegionData extends RegionData { public class PathRegionData extends RegionData {
public PathRegionData(PathRegion pathRegion) { public static final UUID PATH_REGION_ID = new UUID(Long.MAX_VALUE, Long.MAX_VALUE);
super(pathRegion);
public static final PathRegionData INSTANCE = new PathRegionData();
private PathRegionData() {
super(new RegionDataStore() {
@Override
public void save() {
DynamicRegionRepository.saveData(PATH_REGION_ID, INSTANCE);
}
@Override
public void load(RegionData regionData) {
DynamicRegionRepository.loadRegionData(PATH_REGION_ID, regionData);
}
});
} }
@Override @Override
@@ -0,0 +1,54 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2026 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.spawn;
import de.steamwar.bausystem.region.Point;
import de.steamwar.bausystem.region.Region;
import de.steamwar.bausystem.utils.PasteBuilder;
import lombok.NonNull;
public class SpawnArea implements Region.Area {
@Override
public @NonNull Point getMinPoint(boolean extension) {
return null;
}
@Override
public @NonNull Point getMaxPoint(boolean extension) {
return null;
}
@Override
public @NonNull Point getCopyPoint() {
return null;
}
@Override
public void reset(PasteBuilder pasteBuilder, boolean extension) {
// TODO: Implement!
Region.Area.super.reset(pasteBuilder, extension);
}
@Override
public void place(PasteBuilder pasteBuilder, boolean extension) {
}
}
@@ -0,0 +1,96 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2026 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.spawn;
import com.google.gson.JsonArray;
import com.google.gson.stream.JsonWriter;
import de.steamwar.bausystem.region.RegionBackups;
import de.steamwar.bausystem.region.RegionHistory;
import de.steamwar.bausystem.region.RegionType;
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
import de.steamwar.bausystem.region.dynamic.Tile;
import de.steamwar.bausystem.region.dynamic.TileUtils;
import de.steamwar.sql.GameModeConfig;
import lombok.NonNull;
import org.bukkit.Material;
import java.io.IOException;
import java.util.UUID;
public class SpawnRegion extends DynamicRegion {
private final Tile tile;
public SpawnRegion(Tile tile) {
this(UUID.randomUUID(), tile);
finishCreate();
}
public SpawnRegion(UUID id, JsonArray tileData) {
this(id, TileUtils.readTile(tileData));
finishLoad();
}
private SpawnRegion(UUID id, Tile tile) {
super(id, null);
this.tile = tile;
// TODO: Initialize
}
@Override
public void writeTileData(JsonWriter writer) throws IOException {
TileUtils.writeTile(writer, tile);
}
@Override
public @NonNull RegionType getType() {
return RegionType.SPAWN;
}
@Override
public @NonNull Area getArea() {
return null;
}
@Override
public @NonNull Area getBuildArea() {
return Area.EMPTY;
}
@Override
public @NonNull Area getTestblockArea() {
return Area.EMPTY;
}
@Override
public @NonNull GameModeConfig<Material, String> getGameModeConfig() {
return GameModeConfig.getDefaults();
}
@Override
public @NonNull RegionHistory getHistory() {
return RegionHistory.EMPTY;
}
@Override
public @NonNull RegionBackups getBackups() {
return RegionBackups.EMPTY;
}
}