Improve RegionData

Fix BauScoreboard
Fix TestblockCommand
This commit is contained in:
2025-12-16 13:20:42 +01:00
parent c467c3db1f
commit f4d08ad1f9
3 changed files with 42 additions and 42 deletions
@@ -175,15 +175,15 @@ public class TestblockCommand extends SWCommand {
private Region regionCheck(Player player) { private Region regionCheck(Player player) {
Region region = Region.getRegion(player.getLocation()); Region region = Region.getRegion(player.getLocation());
if (region.getFlags().has(Flag.TESTBLOCK).isWritable() && region.getFlags().get(Flag.TESTBLOCK).isWithDefault(TestblockMode.NO_VALUE)) { if (region.getRegionData().has(Flag.TESTBLOCK).isWritable() && region.getRegionData().get(Flag.TESTBLOCK).isWithDefault(TestblockMode.NO_VALUE)) {
Point minPoint = region.getArea().getMinPoint(false); Point minPoint = region.getArea().getMinPoint(false);
Point maxPoint = region.getArea().getMaxPoint(false); Point maxPoint = region.getArea().getMaxPoint(false);
// TODO: Check if empty! // TODO: Check if empty!
int half = minPoint.getZ() + (maxPoint.getZ() - minPoint.getZ()) / 2; int half = minPoint.getZ() + (maxPoint.getZ() - minPoint.getZ()) / 2;
if (player.getLocation().getBlockZ() <= half) { if (player.getLocation().getBlockZ() <= half) {
region.getFlags().set(Flag.TESTBLOCK, TestblockMode.SOUTH); region.getRegionData().set(Flag.TESTBLOCK, TestblockMode.SOUTH);
} else { } else {
region.getFlags().set(Flag.TESTBLOCK, TestblockMode.NORTH); region.getRegionData().set(Flag.TESTBLOCK, TestblockMode.NORTH);
} }
} }
if (region.getTestblockArea().isEmpty()) { if (region.getTestblockArea().isEmpty()) {
@@ -121,8 +121,8 @@ public class BauScoreboard implements Listener {
Region region = Region.getRegion(player.getLocation()); Region region = Region.getRegion(player.getLocation());
if (region.getType().isGlobal()) return "§eSteam§8War"; if (region.getType().isGlobal()) return "§eSteam§8War";
String colorCode = "§e"; String colorCode = "§e";
if (region.getFlags().has(Flag.COLOR).isReadable()) { if (region.getRegionData().has(Flag.COLOR).isReadable()) {
colorCode = "§" + region.getFlags().get(Flag.COLOR).getWithDefault().getColorCode(); colorCode = "§" + region.getRegionData().get(Flag.COLOR).getWithDefault().getColorCode();
} }
return colorCode + "■ §eSteam§8War " + colorCode + ""; // ■ return colorCode + "■ §eSteam§8War " + colorCode + ""; // ■
} }
@@ -37,44 +37,8 @@ public abstract class RegionData {
protected final YAPIONObject data; protected final YAPIONObject data;
protected final YAPIONObject flagData; protected final YAPIONObject flagData;
protected final Runnable onChange; protected final Runnable onChange;
protected final Map<Flag<?>, Flag.Value<?>> flagMap = new HashMap<>(); protected final Map<Flag<?>, Flag.Value<?>> flagMap = new HashMap<>();
private final class Property<T, K> {
private final String field;
private final Function<K, T> loader;
private final Function<T, K> writer;
private T value;
public 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));
}
}
}
private Property<SchematicNode, Integer> testblockSchematic = new Property<>("testblockSchematic", SchematicNode::byId, SchematicNode::getId); private Property<SchematicNode, Integer> testblockSchematic = new Property<>("testblockSchematic", SchematicNode::byId, SchematicNode::getId);
protected RegionData(YAPIONObject data, Runnable onChange) { protected RegionData(YAPIONObject data, Runnable onChange) {
@@ -149,4 +113,40 @@ public abstract class RegionData {
"flagMap=" + flagMap + "flagMap=" + flagMap +
'}'; '}';
} }
private final class Property<T, K> {
private final String field;
private final Function<K, T> loader;
private final Function<T, K> writer;
private T value;
public 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));
}
}
}
} }