From 1a79309d046f4b3d8345a43deaba2a4d8ddd2788 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 20 Mar 2026 20:14:24 +0100 Subject: [PATCH] Re structuring property --- .../config/property/BoolProperty.java | 11 + .../config/property/ColorProperty.java | 11 + .../config/property/FloatProperty.java | 11 + .../config/property/IntProperty.java | 11 + .../config/property/KeybindingProperty.java | 46 ++++ .../config/property/NamedProperty.java | 11 + .../config/property/NamespaceProperty.java | 15 ++ .../config/property/Property.java | 215 +++--------------- .../config/property/SelectProperty.java | 37 +++ .../config/property/StringProperty.java | 11 + 10 files changed, 198 insertions(+), 181 deletions(-) create mode 100644 src/main/java/org/saturnclient/config/property/BoolProperty.java create mode 100644 src/main/java/org/saturnclient/config/property/ColorProperty.java create mode 100644 src/main/java/org/saturnclient/config/property/FloatProperty.java create mode 100644 src/main/java/org/saturnclient/config/property/IntProperty.java create mode 100644 src/main/java/org/saturnclient/config/property/KeybindingProperty.java create mode 100644 src/main/java/org/saturnclient/config/property/NamedProperty.java create mode 100644 src/main/java/org/saturnclient/config/property/NamespaceProperty.java create mode 100644 src/main/java/org/saturnclient/config/property/SelectProperty.java create mode 100644 src/main/java/org/saturnclient/config/property/StringProperty.java diff --git a/src/main/java/org/saturnclient/config/property/BoolProperty.java b/src/main/java/org/saturnclient/config/property/BoolProperty.java new file mode 100644 index 0000000..8f3edc7 --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/BoolProperty.java @@ -0,0 +1,11 @@ +package org.saturnclient.config.property; + +public class BoolProperty extends Property { + public final boolean value; + public final boolean defaultValue; + + public BoolProperty(boolean defaultValue) { + this.value = defaultValue; + this.defaultValue = defaultValue; + } +} diff --git a/src/main/java/org/saturnclient/config/property/ColorProperty.java b/src/main/java/org/saturnclient/config/property/ColorProperty.java new file mode 100644 index 0000000..64913af --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/ColorProperty.java @@ -0,0 +1,11 @@ +package org.saturnclient.config.property; + +public class ColorProperty extends Property { + public final int value; + public final int defaultValue; + + public ColorProperty(int defaultValue) { + this.value = defaultValue; + this.defaultValue = defaultValue; + } +} diff --git a/src/main/java/org/saturnclient/config/property/FloatProperty.java b/src/main/java/org/saturnclient/config/property/FloatProperty.java new file mode 100644 index 0000000..00c7eee --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/FloatProperty.java @@ -0,0 +1,11 @@ +package org.saturnclient.config.property; + +public class FloatProperty { + public final float value; + public final float defaultValue; + + public FloatProperty(float defaultValue) { + this.value = defaultValue; + this.defaultValue = defaultValue; + } +} diff --git a/src/main/java/org/saturnclient/config/property/IntProperty.java b/src/main/java/org/saturnclient/config/property/IntProperty.java new file mode 100644 index 0000000..5e27dd4 --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/IntProperty.java @@ -0,0 +1,11 @@ +package org.saturnclient.config.property; + +public class IntProperty extends Property { + public final int value; + public final int defaultValue; + + public IntProperty(int defaultValue) { + this.value = defaultValue; + this.defaultValue = defaultValue; + } +} diff --git a/src/main/java/org/saturnclient/config/property/KeybindingProperty.java b/src/main/java/org/saturnclient/config/property/KeybindingProperty.java new file mode 100644 index 0000000..f2723f4 --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/KeybindingProperty.java @@ -0,0 +1,46 @@ +package org.saturnclient.config.property; + +import org.saturnclient.common.provider.Providers; + +public class KeybindingProperty { + public final int value; + public final int defaultValue; + public boolean wasPressedLastTick = false; + + public KeybindingProperty(int defaultValue) { + this.value = defaultValue; + this.defaultValue = defaultValue; + } + + public boolean isKeyPressed() { + + return (Integer) value != -1 && + Providers.GLFW.isKeyPressed((Integer) value); + } + + public boolean wasKeyPressed() { + boolean pressed = isKeyPressed(); + + boolean result = pressed && !wasPressedLastTick; + + wasPressedLastTick = pressed; + + return result; + } + + // ---------- HEX ---------- + + 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); + } +} diff --git a/src/main/java/org/saturnclient/config/property/NamedProperty.java b/src/main/java/org/saturnclient/config/property/NamedProperty.java new file mode 100644 index 0000000..ddf2dc2 --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/NamedProperty.java @@ -0,0 +1,11 @@ +package org.saturnclient.config.property; + +public class NamedProperty { + public String name; + public Property prop; + + public NamedProperty(String name, Property value) { + this.name = name; + this.prop = value; + } +} \ No newline at end of file diff --git a/src/main/java/org/saturnclient/config/property/NamespaceProperty.java b/src/main/java/org/saturnclient/config/property/NamespaceProperty.java new file mode 100644 index 0000000..1ec4bf4 --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/NamespaceProperty.java @@ -0,0 +1,15 @@ +package org.saturnclient.config.property; + +import java.util.Map; + +public class NamespaceProperty { + public final Map value; + + public NamespaceProperty(Map value) { + this.value = value; + } + + public Map getNamespaceMap() { + return this.value; + } +} diff --git a/src/main/java/org/saturnclient/config/property/Property.java b/src/main/java/org/saturnclient/config/property/Property.java index 1355ab2..b26ca8c 100644 --- a/src/main/java/org/saturnclient/config/property/Property.java +++ b/src/main/java/org/saturnclient/config/property/Property.java @@ -8,17 +8,7 @@ import java.util.Map; import org.saturnclient.common.provider.Providers; -public class Property { - - public static class NamedProperty { - public String name; - public Property prop; - - public NamedProperty(String name, Property value) { - this.name = name; - this.prop = value; - } - } +public abstract class Property { public enum PropertyType { BOOLEAN, @@ -31,118 +21,56 @@ public class Property { KEYBINDING, } - public T value; public boolean isReset = false; - private T defaultValue; - private String[] availableValues; - private final PropertyType type; - private boolean wasPressedLastTick = false; - - private Property(T value, PropertyType type) { - this.value = value; - this.defaultValue = value; - this.type = type; - } // ---------- Factory Methods ---------- - public static Property from(T value) { - if (value instanceof Boolean) - return new Property<>(value, PropertyType.BOOLEAN); - if (value instanceof Integer) - return new Property<>(value, PropertyType.INTEGER); - if (value instanceof Float) - return new Property<>(value, PropertyType.FLOAT); - if (value instanceof String) - return new Property<>(value, PropertyType.STRING); - if (valueIsNamespace(value)) - return new Property<>(value, PropertyType.NAMESPACE); - return null; + public static NamespaceProperty namespace(Map value) { + return new NamespaceProperty(value); } - public static Property font(int value) { + public static BoolProperty bool(boolean value) { + return new BoolProperty(value); + } + + public static IntProperty integer(int value) { + return new IntProperty(value); + } + + public static FloatProperty floatProp(float value) { + return new FloatProperty(value); + } + + public static StringProperty string(String value) { + return new StringProperty(value); + } + + public static ColorProperty color(int value) { + return new ColorProperty(value); + } + + public static KeybindingProperty keybinding(int value) { + return new KeybindingProperty(value); + } + + public static SelectProperty font(int value) { return select(value, "Default", "Inter", "Inter bold"); } - public static Property select(Integer value, String... availableValues) { - Property property = new Property<>(value, PropertyType.SELECT); - property.availableValues = availableValues; - return property; + public static SelectProperty select(Integer value, String... availableValues) { + return new SelectProperty(value, availableValues); } - public static Property>> namespace(Map> value) { - return new Property<>(value, PropertyType.NAMESPACE); + // ---------- Utility ---------- + + public NamedProperty named(String name) { + return new NamedProperty(name, this); } - public static Property bool(boolean value) { - return new Property<>(value, PropertyType.BOOLEAN); - } - - public static Property integer(int value) { - return new Property<>(value, PropertyType.INTEGER); - } - - public static Property floatProp(float value) { - return new Property<>(value, PropertyType.FLOAT); - } - - public static Property string(String value) { - return new Property<>(value, PropertyType.STRING); - } - - public static Property color(int value) { - return new Property<>(value, PropertyType.HEX); - } - - public static Property keybinding(int value) { - return new Property<>(value, PropertyType.KEYBINDING); - } - - // ---------- Select Helpers ---------- - - public void next() { - if (type == PropertyType.SELECT) { - int i = (Integer) value; - setValue((i < availableValues.length - 1) ? i + 1 : 0); - } - } - - public void prev() { - if (type == PropertyType.SELECT) { - int i = (Integer) value; - setValue((i > 0) ? i - 1 : availableValues.length - 1); - } - } - - public void setSelection(int selection) { - if (type == PropertyType.SELECT && selection >= 0 && selection < availableValues.length) { - setValue(selection); - } - } - - public String getSelection() { - return (type == PropertyType.SELECT) ? availableValues[(Integer) value] : null; - } - - // ---------- Lifecycle ---------- - public void reset() { - value = defaultValue; isReset = true; } - public PropertyType getType() { - return type; - } - - @SuppressWarnings("unchecked") - public Map> getNamespaceValue() { - if (type == PropertyType.NAMESPACE && value instanceof Map) { - return (Map>) value; - } - throw new IllegalStateException("Property is not a namespace"); - } - // ---------- JSON Serialization ---------- public JsonNode toJson() { @@ -243,79 +171,4 @@ public class Property { break; } } - - // ---------- Utility ---------- - - @Override - public String toString() { - return String.valueOf(value); - } - - public static boolean valueIsNamespace(Object obj) { - return obj instanceof Map; - } - - @SuppressWarnings("unchecked") - public void setValue(Object value) { - this.value = (T) value; - } - - @SuppressWarnings("unchecked") - public void setValue(Object value, Object defaultValue) { - this.value = (T) value; - this.defaultValue = (T) defaultValue; - } - - public NamedProperty named(String name) { - return new NamedProperty<>(name, this); - } - - // ---------- Keybindings (no SaturnClient) ---------- - - public boolean isKeyPressed() { - - return (Integer) value != -1 && - Providers.GLFW.isKeyPressed((Integer) value); - } - - public boolean wasKeyPressed() { - boolean pressed = isKeyPressed(); - - boolean result = pressed && !wasPressedLastTick; - - wasPressedLastTick = pressed; - - return result; - } - - // ---------- HEX ---------- - - 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); - } - - // ---------- Misc ---------- - - public Property copy() { - return new Property<>(this.value, this.type); - } - - public boolean isNamespace() { - return type == PropertyType.NAMESPACE; - } - - @SuppressWarnings("unchecked") - public Map> getNamespaceMap() { - return isNamespace() ? (Map>) value : null; - } } \ No newline at end of file diff --git a/src/main/java/org/saturnclient/config/property/SelectProperty.java b/src/main/java/org/saturnclient/config/property/SelectProperty.java new file mode 100644 index 0000000..d8f1642 --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/SelectProperty.java @@ -0,0 +1,37 @@ +package org.saturnclient.config.property; + +import org.saturnclient.config.property.Property.PropertyType; + +public class SelectProperty extends Property { + public final int value; + public final int defaultValue; + public final String[] availableValues; + + public SelectProperty(int defaultValue, String... availableValues) { + this.value = defaultValue; + this.defaultValue = defaultValue; + this.availableValues = availableValues; + } + + public void next() { + int i = (Integer) value; + setValue((i < availableValues.length - 1) ? i + 1 : 0); + } + + public void prev() { + if (type == PropertyType.SELECT) { + int i = (Integer) value; + setValue((i > 0) ? i - 1 : availableValues.length - 1); + } + } + + public void setSelection(int selection) { + if (type == PropertyType.SELECT && selection >= 0 && selection < availableValues.length) { + setValue(selection); + } + } + + public String getSelection() { + return (type == PropertyType.SELECT) ? availableValues[(Integer) value] : null; + } +} diff --git a/src/main/java/org/saturnclient/config/property/StringProperty.java b/src/main/java/org/saturnclient/config/property/StringProperty.java new file mode 100644 index 0000000..a81675c --- /dev/null +++ b/src/main/java/org/saturnclient/config/property/StringProperty.java @@ -0,0 +1,11 @@ +package org.saturnclient.config.property; + +public class StringProperty { + public final String value; + public final String defaultValue; + + public StringProperty(String defaultValue) { + this.value = defaultValue; + this.defaultValue = defaultValue; + } +}