Re structured property

This commit is contained in:
2026-03-20 20:28:24 +01:00
parent 1a79309d04
commit 61accce977
9 changed files with 114 additions and 101 deletions
@@ -1,7 +1,7 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
public class BoolProperty extends Property { public class BoolProperty extends Property {
public final boolean value; public boolean value;
public final boolean defaultValue; public final boolean defaultValue;
public BoolProperty(boolean defaultValue) { public BoolProperty(boolean defaultValue) {
@@ -1,11 +1,25 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
public class ColorProperty extends Property { public class ColorProperty extends Property {
public final int value; public int value;
public final int defaultValue; public final int defaultValue;
public ColorProperty(int defaultValue) { public ColorProperty(int defaultValue) {
this.value = defaultValue; this.value = defaultValue;
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
} }
public static int parseHexToInt(String hex) {
hex = hex.replace("#", "");
if (hex.length() == 6)
hex = "FF" + hex;
if (hex.length() != 8)
throw new IllegalArgumentException(
"Hex must be 6 or 8 chars long, got '" + hex + "'");
return (int) Long.parseLong(hex, 16);
}
} }
@@ -1,7 +1,7 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
public class FloatProperty { public class FloatProperty extends Property {
public final float value; public float value;
public final float defaultValue; public final float defaultValue;
public FloatProperty(float defaultValue) { public FloatProperty(float defaultValue) {
@@ -1,7 +1,7 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
public class IntProperty extends Property { public class IntProperty extends Property {
public final int value; public int value;
public final int defaultValue; public final int defaultValue;
public IntProperty(int defaultValue) { public IntProperty(int defaultValue) {
@@ -2,8 +2,8 @@ package org.saturnclient.config.property;
import org.saturnclient.common.provider.Providers; import org.saturnclient.common.provider.Providers;
public class KeybindingProperty { public class KeybindingProperty extends Property {
public final int value; public int value;
public final int defaultValue; public final int defaultValue;
public boolean wasPressedLastTick = false; public boolean wasPressedLastTick = false;
@@ -2,7 +2,7 @@ package org.saturnclient.config.property;
import java.util.Map; import java.util.Map;
public class NamespaceProperty { public class NamespaceProperty extends Property {
public final Map<String, Property> value; public final Map<String, Property> value;
public NamespaceProperty(Map<String, Property> value) { public NamespaceProperty(Map<String, Property> value) {
@@ -6,8 +6,6 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Map; import java.util.Map;
import org.saturnclient.common.provider.Providers;
public abstract class Property { public abstract class Property {
public enum PropertyType { public enum PropertyType {
@@ -75,41 +73,46 @@ public abstract class Property {
public JsonNode toJson() { public JsonNode toJson() {
switch (type) { if (this instanceof BoolProperty p) {
return JsonNodeFactory.instance.booleanNode(p.value);
}
case BOOLEAN: if (this instanceof IntProperty p) {
return JsonNodeFactory.instance.booleanNode((Boolean) value); return JsonNodeFactory.instance.numberNode(p.value);
}
case INTEGER: if (this instanceof FloatProperty p) {
return JsonNodeFactory.instance.numberNode((Integer) value); return JsonNodeFactory.instance.numberNode(p.value);
}
case FLOAT: if (this instanceof StringProperty p) {
return JsonNodeFactory.instance.numberNode((Float) value); return JsonNodeFactory.instance.textNode(p.value);
}
case STRING: if (this instanceof ColorProperty p) {
return JsonNodeFactory.instance.textNode((String) value);
case HEX:
return JsonNodeFactory.instance.textNode( return JsonNodeFactory.instance.textNode(
String.format("#%08X", (Integer) value)); String.format("#%08X", p.value));
}
case KEYBINDING: if (this instanceof KeybindingProperty p) {
case SELECT: return JsonNodeFactory.instance.numberNode(p.value);
return JsonNodeFactory.instance.numberNode((Integer) value); }
case NAMESPACE: if (this instanceof SelectProperty p) {
return JsonNodeFactory.instance.numberNode(p.value);
}
if (this instanceof NamespaceProperty p) {
ObjectNode nested = JsonNodeFactory.instance.objectNode(); ObjectNode nested = JsonNodeFactory.instance.objectNode();
getNamespaceValue().forEach((k, v) -> { p.value.forEach((k, v) -> {
nested.set(k, v.toJson()); nested.set(k, v.toJson());
}); });
return nested; return nested;
default:
return JsonNodeFactory.instance.textNode(String.valueOf(value));
} }
return null;
} }
public void loadFromJson(JsonNode element) { public void loadFromJson(JsonNode element) {
@@ -117,58 +120,58 @@ public abstract class Property {
if (element == null) if (element == null)
return; return;
switch (type) { if (this instanceof BoolProperty p) {
case BOOLEAN:
if (element.isBoolean()) if (element.isBoolean())
setValue(element.booleanValue()); p.value = element.booleanValue();
break; return;
}
case INTEGER: if (this instanceof IntProperty p) {
if (element.isInt()) if (element.isInt())
setValue(element.intValue()); p.value = element.intValue();
else if (element.isTextual()) else if (element.isTextual())
setValue(parseHexToInt(element.textValue())); p.value = ColorProperty.parseHexToInt(element.textValue());
break; return;
}
case FLOAT: if (this instanceof FloatProperty p) {
if (element.isNumber()) if (element.isNumber())
setValue(element.floatValue()); p.value = element.floatValue();
break; return;
}
case STRING: if (this instanceof StringProperty p) {
if (element.isTextual()) if (element.isTextual())
setValue(element.textValue()); p.value = element.textValue();
break; return;
}
case HEX: if (this instanceof ColorProperty p) {
if (element.isTextual()) if (element.isTextual())
setValue(parseHexToInt(element.textValue())); p.value = ColorProperty.parseHexToInt(element.textValue());
break; return;
}
case NAMESPACE:
if (this instanceof NamespaceProperty p) {
if (element.isObject()) { if (element.isObject()) {
ObjectNode obj = (ObjectNode) element; ObjectNode obj = (ObjectNode) element;
getNamespaceValue().forEach((k, v) -> { p.value.forEach((k, v) -> {
v.loadFromJson(obj.get(k)); v.loadFromJson(obj.get(k));
}); });
}
return;
} }
break; if (this instanceof SelectProperty p) {
case SELECT:
if (element.isInt()) if (element.isInt())
setSelection(element.intValue()); p.setSelection(element.intValue());
break; return;
}
case KEYBINDING: if (this instanceof KeybindingProperty p) {
if (element.isInt()) if (element.isInt())
setValue(element.intValue()); p.value = element.intValue();
break;
} }
} }
} }
@@ -1,9 +1,7 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
import org.saturnclient.config.property.Property.PropertyType;
public class SelectProperty extends Property { public class SelectProperty extends Property {
public final int value; public int value;
public final int defaultValue; public final int defaultValue;
public final String[] availableValues; public final String[] availableValues;
@@ -15,23 +13,21 @@ public class SelectProperty extends Property {
public void next() { public void next() {
int i = (Integer) value; int i = (Integer) value;
setValue((i < availableValues.length - 1) ? i + 1 : 0); this.value = (i < availableValues.length - 1) ? i + 1 : 0;
} }
public void prev() { public void prev() {
if (type == PropertyType.SELECT) {
int i = (Integer) value; int i = (Integer) value;
setValue((i > 0) ? i - 1 : availableValues.length - 1); this.value = (i > 0) ? i - 1 : availableValues.length - 1;
}
} }
public void setSelection(int selection) { public void setSelection(int selection) {
if (type == PropertyType.SELECT && selection >= 0 && selection < availableValues.length) { if (selection >= 0 && selection < availableValues.length) {
setValue(selection); this.value = selection;
} }
} }
public String getSelection() { public String getSelection() {
return (type == PropertyType.SELECT) ? availableValues[(Integer) value] : null; return availableValues[(Integer) value];
} }
} }
@@ -1,7 +1,7 @@
package org.saturnclient.config.property; package org.saturnclient.config.property;
public class StringProperty { public class StringProperty extends Property {
public final String value; public String value;
public final String defaultValue; public final String defaultValue;
public StringProperty(String defaultValue) { public StringProperty(String defaultValue) {