Re structured property
This commit is contained in:
@@ -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:
|
|
||||||
return JsonNodeFactory.instance.booleanNode((Boolean) value);
|
|
||||||
|
|
||||||
case INTEGER:
|
|
||||||
return JsonNodeFactory.instance.numberNode((Integer) value);
|
|
||||||
|
|
||||||
case FLOAT:
|
|
||||||
return JsonNodeFactory.instance.numberNode((Float) value);
|
|
||||||
|
|
||||||
case STRING:
|
|
||||||
return JsonNodeFactory.instance.textNode((String) value);
|
|
||||||
|
|
||||||
case HEX:
|
|
||||||
return JsonNodeFactory.instance.textNode(
|
|
||||||
String.format("#%08X", (Integer) value));
|
|
||||||
|
|
||||||
case KEYBINDING:
|
|
||||||
case SELECT:
|
|
||||||
return JsonNodeFactory.instance.numberNode((Integer) value);
|
|
||||||
|
|
||||||
case NAMESPACE:
|
|
||||||
|
|
||||||
ObjectNode nested = JsonNodeFactory.instance.objectNode();
|
|
||||||
|
|
||||||
getNamespaceValue().forEach((k, v) -> {
|
|
||||||
nested.set(k, v.toJson());
|
|
||||||
});
|
|
||||||
|
|
||||||
return nested;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return JsonNodeFactory.instance.textNode(String.valueOf(value));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this instanceof IntProperty p) {
|
||||||
|
return JsonNodeFactory.instance.numberNode(p.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this instanceof FloatProperty p) {
|
||||||
|
return JsonNodeFactory.instance.numberNode(p.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this instanceof StringProperty p) {
|
||||||
|
return JsonNodeFactory.instance.textNode(p.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this instanceof ColorProperty p) {
|
||||||
|
return JsonNodeFactory.instance.textNode(
|
||||||
|
String.format("#%08X", p.value));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this instanceof KeybindingProperty p) {
|
||||||
|
return JsonNodeFactory.instance.numberNode(p.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this instanceof SelectProperty p) {
|
||||||
|
return JsonNodeFactory.instance.numberNode(p.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this instanceof NamespaceProperty p) {
|
||||||
|
ObjectNode nested = JsonNodeFactory.instance.objectNode();
|
||||||
|
|
||||||
|
p.value.forEach((k, v) -> {
|
||||||
|
nested.set(k, v.toJson());
|
||||||
|
});
|
||||||
|
|
||||||
|
return nested;
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
|
if (element.isBoolean())
|
||||||
|
p.value = element.booleanValue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
case BOOLEAN:
|
if (this instanceof IntProperty p) {
|
||||||
if (element.isBoolean())
|
if (element.isInt())
|
||||||
setValue(element.booleanValue());
|
p.value = element.intValue();
|
||||||
break;
|
else if (element.isTextual())
|
||||||
|
p.value = ColorProperty.parseHexToInt(element.textValue());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
case INTEGER:
|
if (this instanceof FloatProperty p) {
|
||||||
if (element.isInt())
|
if (element.isNumber())
|
||||||
setValue(element.intValue());
|
p.value = element.floatValue();
|
||||||
else if (element.isTextual())
|
return;
|
||||||
setValue(parseHexToInt(element.textValue()));
|
}
|
||||||
break;
|
|
||||||
|
|
||||||
case FLOAT:
|
if (this instanceof StringProperty p) {
|
||||||
if (element.isNumber())
|
if (element.isTextual())
|
||||||
setValue(element.floatValue());
|
p.value = element.textValue();
|
||||||
break;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
case STRING:
|
if (this instanceof ColorProperty p) {
|
||||||
if (element.isTextual())
|
if (element.isTextual())
|
||||||
setValue(element.textValue());
|
p.value = ColorProperty.parseHexToInt(element.textValue());
|
||||||
break;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
case HEX:
|
if (this instanceof NamespaceProperty p) {
|
||||||
if (element.isTextual())
|
if (element.isObject()) {
|
||||||
setValue(parseHexToInt(element.textValue()));
|
ObjectNode obj = (ObjectNode) element;
|
||||||
break;
|
|
||||||
|
|
||||||
case NAMESPACE:
|
p.value.forEach((k, v) -> {
|
||||||
|
v.loadFromJson(obj.get(k));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (element.isObject()) {
|
if (this instanceof SelectProperty p) {
|
||||||
|
if (element.isInt())
|
||||||
|
p.setSelection(element.intValue());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ObjectNode obj = (ObjectNode) element;
|
if (this instanceof KeybindingProperty p) {
|
||||||
|
if (element.isInt())
|
||||||
getNamespaceValue().forEach((k, v) -> {
|
p.value = element.intValue();
|
||||||
v.loadFromJson(obj.get(k));
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case SELECT:
|
|
||||||
if (element.isInt())
|
|
||||||
setSelection(element.intValue());
|
|
||||||
break;
|
|
||||||
|
|
||||||
case KEYBINDING:
|
|
||||||
if (element.isInt())
|
|
||||||
setValue(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;
|
this.value = (i > 0) ? i - 1 : availableValues.length - 1;
|
||||||
setValue((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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user