forked from SteamWar/SteamWar
Add FixedRegionDataUtils
This commit is contained in:
@@ -33,7 +33,7 @@ import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public interface Region {
|
||||
public interface Region extends RegionDataStore {
|
||||
|
||||
static Stream<Region> getRegions() {
|
||||
return RegionSystem.INSTANCE.getRegions();
|
||||
|
||||
@@ -41,7 +41,7 @@ public interface RegionBackups {
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
abstract class Backup implements Comparable<Backup> {
|
||||
abstract class Backup implements RegionDataStore, Comparable<Backup> {
|
||||
@NonNull
|
||||
private final BackupType type;
|
||||
|
||||
|
||||
@@ -22,8 +22,6 @@ package de.steamwar.bausystem.region;
|
||||
import de.steamwar.bausystem.region.flags.Flag;
|
||||
import de.steamwar.sql.SchematicNode;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
import yapion.hierarchy.types.YAPIONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -33,33 +31,22 @@ import java.util.function.Function;
|
||||
|
||||
public abstract class RegionData {
|
||||
|
||||
@Setter
|
||||
protected RegionDataStore store;
|
||||
|
||||
private final List<Property<?, ?>> properties = new ArrayList<>();
|
||||
|
||||
protected final YAPIONObject data;
|
||||
protected final YAPIONObject flagData;
|
||||
protected final Runnable onChange;
|
||||
|
||||
protected final Map<Flag<?>, Flag.Value<?>> flagMap = new HashMap<>();
|
||||
private Property<SchematicNode, Integer> testblockSchematic = new Property<>("testblockSchematic", SchematicNode::byId, SchematicNode::getId);
|
||||
protected final Property<SchematicNode, Integer> testblockSchematic = new Property<>("testblockSchematic", SchematicNode::byId, SchematicNode::getId);
|
||||
|
||||
protected RegionData(YAPIONObject data, Runnable onChange) {
|
||||
this.data = data;
|
||||
this.flagData = data.getObjectOrSetDefault("flagStorage", new YAPIONObject());
|
||||
this.onChange = onChange;
|
||||
protected RegionData(RegionDataStore store) {
|
||||
this.store = store;
|
||||
initialize();
|
||||
for (final Flag flag : Flag.getFlags()) {
|
||||
if (!has(flag).isWritable()) continue;
|
||||
try {
|
||||
String s = flagData.getPlainValue(flag.name());
|
||||
flagMap.put(flag, flag.valueOfValue(s));
|
||||
} catch (Exception e) {
|
||||
flagMap.put(flag, (Flag.Value<?>) flag.getDefaultValue());
|
||||
}
|
||||
}
|
||||
properties.forEach(Property::load);
|
||||
store.loadRegionData(this);
|
||||
}
|
||||
|
||||
public void setStore(RegionDataStore store) {
|
||||
this.store = store;
|
||||
store.loadRegionData(this);
|
||||
}
|
||||
|
||||
protected void initialize() {
|
||||
@@ -74,8 +61,7 @@ public abstract class RegionData {
|
||||
public final <T extends Enum<T> & Flag.Value<T>> boolean set(@NonNull Flag<T> flag, @NonNull T value) {
|
||||
if (has(flag).isWritable()) {
|
||||
if (flagMap.put(flag, value) != value) {
|
||||
flagData.put(flag.name(), value.name());
|
||||
onChange.run();
|
||||
store.saveRegionData(this);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -91,24 +77,27 @@ public abstract class RegionData {
|
||||
for (Flag flag : Flag.getFlags()) {
|
||||
if (has(flag).isWritable()) {
|
||||
flagMap.remove(flag);
|
||||
flagData.remove(flag.name());
|
||||
}
|
||||
}
|
||||
properties.forEach(property -> property.set(null));
|
||||
onChange.run();
|
||||
store.saveRegionData(this);
|
||||
}
|
||||
|
||||
public final Map<Flag<?>, Flag.Value<?>> getBackedMap() {
|
||||
return flagMap;
|
||||
}
|
||||
|
||||
public final List<Property<Object, Object>> getBackedProperties() {
|
||||
return (List) properties;
|
||||
}
|
||||
|
||||
public SchematicNode getTestblockSchematic() {
|
||||
return testblockSchematic.get();
|
||||
}
|
||||
|
||||
public void setTestblockSchematic(SchematicNode schematic) {
|
||||
testblockSchematic.set(schematic);
|
||||
onChange.run();
|
||||
store.saveRegionData(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -123,39 +112,26 @@ public abstract class RegionData {
|
||||
return st.toString();
|
||||
}
|
||||
|
||||
private final class Property<T, K> {
|
||||
private final String field;
|
||||
private final Function<K, T> loader;
|
||||
private final Function<T, K> writer;
|
||||
public final class Property<T, K> {
|
||||
public final String field;
|
||||
public final Function<K, T> loader;
|
||||
public final Function<T, K> writer;
|
||||
|
||||
private T value;
|
||||
|
||||
public Property(String field, Function<K, T> loader, Function<T, K> writer) {
|
||||
private Property(String field, Function<K, T> loader, Function<T, K> writer) {
|
||||
this.field = field;
|
||||
this.loader = loader;
|
||||
this.writer = writer;
|
||||
properties.add(this);
|
||||
}
|
||||
|
||||
public void load() {
|
||||
if (flagData.containsKey(field)) {
|
||||
value = loader.apply(flagData.getPlainValue(field));
|
||||
} else {
|
||||
value = null;
|
||||
}
|
||||
}
|
||||
|
||||
public T get() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void set(T value) {
|
||||
this.value = value;
|
||||
if (value == null) {
|
||||
flagData.remove(field);
|
||||
} else {
|
||||
flagData.put(field, writer.apply(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user