Make FixedGlobalFlagStorage load the options from file

This commit is contained in:
2025-08-02 08:31:38 +02:00
parent 811bd00ed2
commit 5550aa4930
7 changed files with 63 additions and 30 deletions
@@ -21,6 +21,7 @@ package de.steamwar.bausystem.region.flags;
import de.steamwar.bausystem.shared.EnumDisplay; import de.steamwar.bausystem.shared.EnumDisplay;
import lombok.Getter; import lombok.Getter;
import lombok.NonNull;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@@ -70,6 +71,24 @@ public final class Flag<T extends Enum<T> & Flag.Value<T>> implements EnumDispla
this.values = defaultValue.getValues(); this.values = defaultValue.getValues();
} }
public static Flag<?> valueOf(@NonNull String name) {
for (Flag<?> flag : flags) {
if (flag.name.equalsIgnoreCase(name)) {
return flag;
}
}
throw new IllegalArgumentException("No enum constant Flag." + name);
}
public Flag.Value<?> valueOfValue(@NonNull String name) {
for (Flag.Value<?> value : values) {
if (value.name().equalsIgnoreCase(name)) {
return value;
}
}
throw new IllegalArgumentException("No enum constant Flag." + this.name + "." + name);
}
public String name() { public String name() {
return name; return name;
} }
@@ -20,6 +20,8 @@
package de.steamwar.bausystem.region; package de.steamwar.bausystem.region;
import de.steamwar.bausystem.region.fixed.FixedGlobalRegion; import de.steamwar.bausystem.region.fixed.FixedGlobalRegion;
import de.steamwar.bausystem.region.fixed.loader.PrototypeLoader;
import de.steamwar.bausystem.region.fixed.loader.RegionLoader;
import lombok.NonNull; import lombok.NonNull;
import org.bukkit.Location; import org.bukkit.Location;
@@ -35,12 +37,12 @@ public class FixedRegionSystem implements RegionSystem {
@Override @Override
public void load() { public void load() {
// TODO: Implement PrototypeLoader.load();
RegionLoader.load();
} }
@Override @Override
public void save() { public void save() {
// TODO: Implement
} }
@Override @Override
@@ -26,8 +26,10 @@ import de.steamwar.bausystem.region.flags.ColorMode;
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;
import de.steamwar.bausystem.region.flags.TNTMode; import de.steamwar.bausystem.region.flags.TNTMode;
import de.steamwar.bausystem.worlddata.WorldData;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import lombok.NonNull; import lombok.NonNull;
import yapion.hierarchy.types.YAPIONObject;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -35,9 +37,20 @@ import java.util.Map;
public class FixedGlobalFlagStorage implements FlagStorage { public class FixedGlobalFlagStorage implements FlagStorage {
private Map<Flag<?>, Flag.Value<?>> flagMap = new HashMap<>(); private Map<Flag<?>, Flag.Value<?>> flagMap = new HashMap<>();
private YAPIONObject data;
public FixedGlobalFlagStorage() { public FixedGlobalFlagStorage(YAPIONObject data) {
flagMap.put(Flag.TNT, TNTMode.DENY); 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 @Override
@@ -57,6 +70,8 @@ public class FixedGlobalFlagStorage implements FlagStorage {
@Override @Override
public <T extends Enum<T> & Flag.Value<T>> boolean set(@NonNull Flag<T> flag, @NonNull T value) { public <T extends Enum<T> & Flag.Value<T>> boolean set(@NonNull Flag<T> flag, @NonNull T value) {
if (has(flag).isWritable()) { if (has(flag).isWritable()) {
data.put(flag.name(), value.name());
WorldData.write();
return flagMap.put(flag, value) != value; return flagMap.put(flag, value) != value;
} else { } else {
return false; return false;
@@ -23,6 +23,7 @@ import com.sk89q.worldedit.EditSession;
import de.steamwar.bausystem.region.*; import de.steamwar.bausystem.region.*;
import de.steamwar.bausystem.utils.PasteBuilder; import de.steamwar.bausystem.utils.PasteBuilder;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter;
import org.bukkit.Location; import org.bukkit.Location;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@@ -37,7 +38,8 @@ public final class FixedGlobalRegion implements Region {
private static final Point MIN_POINT = new Point(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE); 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 final Point MAX_POINT = new Point(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
private static final FlagStorage FLAG_STORAGE = new FixedGlobalFlagStorage(); @Setter
private static FlagStorage FLAG_STORAGE;
private static final UUID GLOBAL_REGION_ID = new UUID(0, 0); private static final UUID GLOBAL_REGION_ID = new UUID(0, 0);
@@ -127,11 +127,11 @@ public class Prototype {
skinMap.put(displayName, new Skin(defaultSkin, null, schematicFile, testblockSchematicFile, buildSchematicFile)); skinMap.put(displayName, new Skin(defaultSkin, null, schematicFile, testblockSchematicFile, buildSchematicFile));
} }
if (PROTOTYPE_MAP.containsKey(name)) { // if (PROTOTYPE_MAP.containsKey(name)) {
Region.getRegion(PROTOTYPE_MAP.remove(name)).forEach(region -> { // Region.getRegion(PROTOTYPE_MAP.remove(name)).forEach(region -> {
region._setPrototype(this); // region._setPrototype(this);
}); // });
} // }
PROTOTYPE_MAP.put(name, this); PROTOTYPE_MAP.put(name, this);
} }
@@ -210,19 +210,20 @@ public class Prototype {
} }
} }
public static Region generateRegion(String name, YAPIONObject regionConfig, YAPIONObject regionData) { public static void generateRegion(String name, YAPIONObject regionConfig, YAPIONObject regionData) {
Prototype prototype; Prototype prototype;
if (regionData.containsKey("prototype", String.class)) { if (regionData.containsKey("prototype", String.class)) {
prototype = PROTOTYPE_MAP.get(regionData.getPlainValue("prototype")); prototype = PROTOTYPE_MAP.get(regionData.getPlainValue("prototype"));
} else { } else {
prototype = PROTOTYPE_MAP.get(regionConfig.getPlainValue("prototype")); prototype = PROTOTYPE_MAP.get(regionConfig.getPlainValue("prototype"));
} }
FlagStorage flagStorage; System.out.println(name + " " + prototype);
if (regionData.containsKey("flagStorage", YAPIONType.OBJECT)) { // FlagStorage flagStorage;
flagStorage = FlagStorage.createStorage(regionData.getObject("flagStorage")); // if (regionData.containsKey("flagStorage", YAPIONType.OBJECT)) {
} else { // flagStorage = FlagStorage.createStorage(regionData.getObject("flagStorage"));
flagStorage = new FlagStorage(); // } else {
} // flagStorage = new FlagStorage();
return new Region(name, prototype, regionConfig, flagStorage, regionData); // }
// return new Region(name, prototype, regionConfig, flagStorage, regionData);
} }
} }
@@ -1,7 +1,7 @@
/* /*
* This file is a part of the SteamWar software. * This file is a part of the SteamWar software.
* *
* Copyright (C) 2021 SteamWar.de-Serverteam * Copyright (C) 2020 SteamWar.de-Serverteam
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package de.steamwar.bausystem.region.loader; package de.steamwar.bausystem.region.fixed.loader;
import de.steamwar.bausystem.region.fixed.Prototype; import de.steamwar.bausystem.region.fixed.Prototype;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
@@ -1,7 +1,7 @@
/* /*
* This file is a part of the SteamWar software. * This file is a part of the SteamWar software.
* *
* Copyright (C) 2021 SteamWar.de-Serverteam * Copyright (C) 2020 SteamWar.de-Serverteam
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@@ -17,10 +17,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
package de.steamwar.bausystem.region.loader; package de.steamwar.bausystem.region.fixed.loader;
import de.steamwar.bausystem.region.FlagStorage; import de.steamwar.bausystem.region.fixed.FixedGlobalFlagStorage;
import de.steamwar.bausystem.region.GlobalRegion; import de.steamwar.bausystem.region.fixed.FixedGlobalRegion;
import de.steamwar.bausystem.region.fixed.Prototype; import de.steamwar.bausystem.region.fixed.Prototype;
import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.bausystem.worlddata.WorldData;
import lombok.experimental.UtilityClass; import lombok.experimental.UtilityClass;
@@ -80,12 +80,6 @@ public class RegionLoader {
globalOptions = new YAPIONObject(); globalOptions = new YAPIONObject();
optionsYapionObject.add("global", globalOptions); optionsYapionObject.add("global", globalOptions);
} }
FlagStorage flagStorage; FixedGlobalRegion.setFLAG_STORAGE(new FixedGlobalFlagStorage(globalOptions.getObjectOrSetDefault("flagStorage", new YAPIONObject())));
if (globalOptions.containsKey("flagStorage", YAPIONType.OBJECT)) {
flagStorage = FlagStorage.createStorage(globalOptions.getObject("flagStorage"));
} else {
flagStorage = new FlagStorage();
}
new GlobalRegion(flagStorage, globalOptions);
} }
} }