From 6b67b7bf8e9ca2a76577f71631bde21422aaba4c Mon Sep 17 00:00:00 2001 From: Leo dev Date: Thu, 3 Apr 2025 22:19:31 +0200 Subject: [PATCH] namespace load fix --- .../saturnclient/config/ConfigManager.java | 10 +- .../saturnclient/config/Property.java | 32 +- .../saturnclient/menus/ConfigEditor.java | 9 +- .../menus/SaturnConfigEditor.java | 304 +++++++++--------- .../ui/components/SaturnFloat.java | 2 + .../saturnclient/ui/components/SaturnHex.java | 2 + .../ui/components/SaturnInteger.java | 2 + .../ui/components/SaturnToggle.java | 60 ++-- 8 files changed, 214 insertions(+), 207 deletions(-) diff --git a/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java b/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java index ccf332d..1894f02 100644 --- a/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java +++ b/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java @@ -1,5 +1,7 @@ package org.saturnclient.saturnclient.config; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -87,7 +89,7 @@ public class ConfigManager { if (p.getType() == Property.PropertyType.NAMESPACE) { // Handle nested namespace JsonObject nestedConfig = c.getAsJsonObject(); - Map> nestedProperties = (Map>) p.value; + Map> nestedProperties = p.getNamespaceValue(); loadProperties(nestedConfig, nestedProperties); } else { // Handle regular properties @@ -136,8 +138,12 @@ public class ConfigManager { jsonObject.add(namespace, namespaceConfig); } + // Format the json so it can easily be edited + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + String formattedJson = gson.toJson(jsonObject); + // Write the JSON object to the config file - Files.write(configFile.toPath(), jsonObject.toString().getBytes()); + Files.write(configFile.toPath(), formattedJson.getBytes()); SaturnClient.LOGGER.info("Config saved successfully."); } catch (IOException e) { SaturnClient.LOGGER.error("Error saving the config file", e); diff --git a/src/main/java/org/saturnclient/saturnclient/config/Property.java b/src/main/java/org/saturnclient/saturnclient/config/Property.java index 0de955e..bf123b5 100644 --- a/src/main/java/org/saturnclient/saturnclient/config/Property.java +++ b/src/main/java/org/saturnclient/saturnclient/config/Property.java @@ -29,7 +29,7 @@ public class Property { type = PropertyType.FLOAT; } else if (value instanceof String) { type = PropertyType.STRING; - } else if (isMapOfStringToProperty(value)) { + } else if (valueIsNamespace(value)) { type = PropertyType.NAMESPACE; } } @@ -87,6 +87,14 @@ public class Property { 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"); + } + public boolean matchesJson(JsonElement element) { if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); @@ -98,14 +106,14 @@ public class Property { return true; if (value instanceof String && primitive.isString()) return true; - if (isMapOfStringToProperty(value) && primitive.isJsonObject()) - return true; // Check if JSON string represents a valid hex integer if (value instanceof String && primitive.isString()) { String str = primitive.getAsString(); return str.matches("0x[0-9A-Fa-f]+"); } + } else if (element.isJsonObject()) { + return isNamespace(value); } return false; } @@ -115,19 +123,11 @@ public class Property { return String.valueOf(value); } - public static boolean isMapOfStringToProperty(Object obj) { - if (!(obj instanceof Map map)) { - return false; - } + public boolean isNamespace(Object obj) { + return type == PropertyType.NAMESPACE && value instanceof Map; + } - for (Map.Entry entry : map.entrySet()) { - if (!(entry.getKey() instanceof String)) { - return false; - } - if (!(entry.getValue() instanceof Property)) { - return false; - } - } - return true; + public static boolean valueIsNamespace(Object obj) { + return obj instanceof Map; } } diff --git a/src/main/java/org/saturnclient/saturnclient/menus/ConfigEditor.java b/src/main/java/org/saturnclient/saturnclient/menus/ConfigEditor.java index 89a511d..fe32ffd 100644 --- a/src/main/java/org/saturnclient/saturnclient/menus/ConfigEditor.java +++ b/src/main/java/org/saturnclient/saturnclient/menus/ConfigEditor.java @@ -176,7 +176,8 @@ public class ConfigEditor extends SaturnUi { // Recursively draw nested properties and update total rows Map> nestedProperties = (Map>) prop.value; - totalRows = drawProperties(configScroll, nestedProperties, rectWidth, totalRows + 1, 0); + totalRows = drawProperties(configScroll, nestedProperties, rectWidth, + totalRows + 1, 0); } if (full) { @@ -200,4 +201,10 @@ public class ConfigEditor extends SaturnUi { return true; } } + + @Override + public void close() { + ConfigManager.save(); + super.close(); + } } diff --git a/src/main/java/org/saturnclient/saturnclient/menus/SaturnConfigEditor.java b/src/main/java/org/saturnclient/saturnclient/menus/SaturnConfigEditor.java index af0b242..6e433d4 100644 --- a/src/main/java/org/saturnclient/saturnclient/menus/SaturnConfigEditor.java +++ b/src/main/java/org/saturnclient/saturnclient/menus/SaturnConfigEditor.java @@ -11,189 +11,179 @@ import org.saturnclient.ui.components.SaturnHex; import org.saturnclient.ui.components.SaturnInteger; import org.saturnclient.ui.components.SaturnString; import org.saturnclient.ui.components.SaturnToggle; -import org.saturnclient.ui.widgets.SaturnClickableImage; import org.saturnclient.ui.widgets.SaturnScroll; import org.saturnclient.ui.widgets.SaturnSprite; import org.saturnclient.ui.widgets.SaturnText; public class SaturnConfigEditor extends SaturnUi { - public SaturnConfigEditor() { - super(Text.of("Config editor")); - } + public SaturnConfigEditor() { + super(Text.of("Config editor")); + } - @Override - protected void init() { - int rectWidth = 330; - int rectHeight = 228; - int rectX = (width - rectWidth) / 2; - int rectY = (height - rectHeight + 15) / 2; + @Override + protected void init() { + int rectWidth = 330; + int rectHeight = 228; + int rectX = (width - rectWidth) / 2; + int rectY = (height - rectHeight + 15) / 2; - draw( - new SaturnSprite(Textures.SETTINGS_BG) - .setX(rectX) - .setY(rectY) - .setWidth(rectWidth) - .setHeight(rectHeight) - .setAnimations(SaturnClient.getAnimations())); + draw( + new SaturnSprite(Textures.SETTINGS_BG) + .setX(rectX) + .setY(rectY) + .setWidth(rectWidth) + .setHeight(rectHeight) + .setAnimations(SaturnClient.getAnimations())); - draw( - new SaturnClickableImage(Textures.CLOSE, () -> { - this.close(); - }) - .setX(rectX) - .setY(rectY - 15) - .setWidth(13) - .setHeight(13) - .setAnimations(SaturnClient.getAnimations())); + SaturnScroll configScroll = new SaturnScroll(); - SaturnScroll configScroll = new SaturnScroll(); + int row = 0; + int col = 0; - int row = 0; - int col = 0; + drawProperties(configScroll, SaturnClient.config.getProperties(), rectWidth, row, col); - drawProperties(configScroll, SaturnClient.config.getProperties(), rectWidth, row, col); + draw( + configScroll + .setX(rectX + 17) + .setY(rectY + 10) + .setWidth(rectWidth - 17) + .setHeight(rectHeight - 10) + .setAnimations(SaturnClient.getAnimations())); - draw( - configScroll - .setX(rectX + 17) - .setY(rectY + 10) - .setWidth(rectWidth - 17) - .setHeight(rectHeight - 10) - .setAnimations(SaturnClient.getAnimations())); + super.init(); + } - super.init(); - } + @SuppressWarnings("unchecked") + private int drawProperties(SaturnScroll configScroll, Map> properties, int rectWidth, + int row, int col) { + int totalRows = row; + for (Map.Entry> propEntry : properties.entrySet()) { + Property prop = propEntry.getValue(); + String propName = propEntry.getKey(); - @SuppressWarnings("unchecked") - private int drawProperties(SaturnScroll configScroll, Map> properties, int rectWidth, - int row, int col) { - int totalRows = row; - for (Map.Entry> propEntry : properties.entrySet()) { - Property prop = propEntry.getValue(); - String propName = propEntry.getKey(); + boolean full = isFull(prop); - boolean full = isFull(prop); + if (full && col > 0) { + col = 0; + totalRows++; + } - if (full && col > 0) { - col = 0; - totalRows++; - } + int modX = ((rectWidth / 2) - 17) * col; + int modY = 14 * totalRows; - int modX = ((rectWidth / 2) - 17) * col; - int modY = 14 * totalRows; + switch (prop.getType()) { + case BOOLEAN: + configScroll.draw( + new SaturnToggle((Property) prop) + .setX(modX) + .setY(modY)); + configScroll.draw( + new SaturnText( + propName.substring(0, 1).toUpperCase() + + propName.substring(1)) + .setX(modX + 18) + .setY(modY) + .setScale(0.8f)); + break; + case INTEGER: + configScroll.draw( + new SaturnText( + propName.substring(0, 1).toUpperCase() + + propName.substring(1)) + .setX(modX) + .setY(modY) + .setScale(0.8f)); - switch (prop.getType()) { - case BOOLEAN: - configScroll.draw( - new SaturnToggle((Property) prop) - .setX(modX) - .setY(modY)); - configScroll.draw( - new SaturnText( - propName.substring(0, 1).toUpperCase() + - propName.substring(1)) - .setX(modX + 18) - .setY(modY) - .setScale(0.8f)); - break; - case INTEGER: - configScroll.draw( - new SaturnText( - propName.substring(0, 1).toUpperCase() + - propName.substring(1)) - .setX(modX) - .setY(modY) - .setScale(0.8f)); + configScroll.draw( + new SaturnInteger( + (Property) prop, + (rectWidth / 2) - 17, + modY, + 70)); + break; + case FLOAT: + configScroll.draw( + new SaturnText( + propName.substring(0, 1).toUpperCase() + + propName.substring(1)) + .setX(modX) + .setY(modY) + .setScale(0.8f)); - configScroll.draw( - new SaturnInteger( - (Property) prop, - (rectWidth / 2) - 17, - modY, - 70)); - break; - case FLOAT: - configScroll.draw( - new SaturnText( - propName.substring(0, 1).toUpperCase() + - propName.substring(1)) - .setX(modX) - .setY(modY) - .setScale(0.8f)); + configScroll.draw( + new SaturnFloat( + (Property) prop, + (rectWidth / 2) - 17, + modY, + 70)); + break; + case STRING: + configScroll.draw( + new SaturnText( + propName.substring(0, 1).toUpperCase() + + propName.substring(1)) + .setX(modX) + .setY(modY) + .setScale(0.8f)); - configScroll.draw( - new SaturnFloat( - (Property) prop, - (rectWidth / 2) - 17, - modY, - 70)); - break; - case STRING: - configScroll.draw( - new SaturnText( - propName.substring(0, 1).toUpperCase() + - propName.substring(1)) - .setX(modX) - .setY(modY) - .setScale(0.8f)); + configScroll.draw( + new SaturnString( + (Property) prop, + (rectWidth / 2) - 17, + modY, + 70)); + break; + case HEX: + configScroll.draw( + new SaturnText( + propName.substring(0, 1).toUpperCase() + + propName.substring(1)) + .setX(modX) + .setY(modY) + .setScale(0.8f)); - configScroll.draw( - new SaturnString( - (Property) prop, - (rectWidth / 2) - 17, - modY, - 70)); - break; - case HEX: - configScroll.draw( - new SaturnText( - propName.substring(0, 1).toUpperCase() + - propName.substring(1)) - .setX(modX) - .setY(modY) - .setScale(0.8f)); + configScroll.draw( + new SaturnHex( + (Property) prop, + (rectWidth / 2) - 17, + modY, + 70)); + break; + case NAMESPACE: + configScroll.draw( + new SaturnText( + propName.substring(0, 1).toUpperCase() + + propName.substring(1)) + .setX(modX) + .setY(modY) + .setScale(0.8f)); - configScroll.draw( - new SaturnHex( - (Property) prop, - (rectWidth / 2) - 17, - modY, - 70)); - break; - case NAMESPACE: - configScroll.draw( - new SaturnText( - propName.substring(0, 1).toUpperCase() + - propName.substring(1)) - .setX(modX) - .setY(modY) - .setScale(0.8f)); + // Recursively draw nested properties and update total rows + Map> nestedProperties = (Map>) prop.value; + totalRows = drawProperties(configScroll, nestedProperties, rectWidth, + totalRows + 1, 0); + } - // Recursively draw nested properties and update total rows - Map> nestedProperties = (Map>) prop.value; - totalRows = drawProperties(configScroll, nestedProperties, rectWidth, totalRows + 1, 0); - } - - if (full) { - totalRows++; - } else { - col++; - if (col > 1) { - col = 0; - totalRows++; + if (full) { + totalRows++; + } else { + col++; + if (col > 1) { + col = 0; + totalRows++; + } + } } - } + return totalRows; } - return totalRows; - } - public static boolean isFull(Property prop) { - switch (prop.getType()) { - case BOOLEAN: - return false; - default: - return true; + public static boolean isFull(Property prop) { + switch (prop.getType()) { + case BOOLEAN: + return false; + default: + return true; + } } - } } diff --git a/src/main/java/org/saturnclient/ui/components/SaturnFloat.java b/src/main/java/org/saturnclient/ui/components/SaturnFloat.java index 229507d..9dd1b79 100644 --- a/src/main/java/org/saturnclient/ui/components/SaturnFloat.java +++ b/src/main/java/org/saturnclient/ui/components/SaturnFloat.java @@ -59,6 +59,7 @@ public class SaturnFloat extends SaturnWidget { text.substring(cursorPosition); prop.value = text.isEmpty() ? 0 : Float.parseFloat(text); + org.saturnclient.saturnclient.config.ConfigManager.save(); cursorPosition++; } @@ -77,6 +78,7 @@ public class SaturnFloat extends SaturnWidget { cursorPosition = 1; } prop.value = Float.parseFloat(text); + org.saturnclient.saturnclient.config.ConfigManager.save(); } } else if (keyCode == GLFW.GLFW_KEY_LEFT) { if (cursorPosition > 0) { diff --git a/src/main/java/org/saturnclient/ui/components/SaturnHex.java b/src/main/java/org/saturnclient/ui/components/SaturnHex.java index 3b1fce6..a7dc526 100644 --- a/src/main/java/org/saturnclient/ui/components/SaturnHex.java +++ b/src/main/java/org/saturnclient/ui/components/SaturnHex.java @@ -42,6 +42,7 @@ public class SaturnHex extends SaturnWidget { if (text.length() == 7) { // Only update value when full hex is entered prop.value = hexToInt(text); + org.saturnclient.saturnclient.config.ConfigManager.save(); } } } @@ -65,6 +66,7 @@ public class SaturnHex extends SaturnWidget { if (text.length() == 7) { // Update only when valid hex prop.value = hexToInt(text); + org.saturnclient.saturnclient.config.ConfigManager.save(); } } diff --git a/src/main/java/org/saturnclient/ui/components/SaturnInteger.java b/src/main/java/org/saturnclient/ui/components/SaturnInteger.java index 82b2a7c..f18b44b 100644 --- a/src/main/java/org/saturnclient/ui/components/SaturnInteger.java +++ b/src/main/java/org/saturnclient/ui/components/SaturnInteger.java @@ -59,6 +59,7 @@ public class SaturnInteger extends SaturnWidget { text.substring(cursorPosition); prop.value = text.isEmpty() ? 0 : Integer.parseInt(text); + org.saturnclient.saturnclient.config.ConfigManager.save(); cursorPosition++; } @@ -77,6 +78,7 @@ public class SaturnInteger extends SaturnWidget { cursorPosition = 1; } prop.value = Integer.parseInt(text); + org.saturnclient.saturnclient.config.ConfigManager.save(); } } else if (keyCode == GLFW.GLFW_KEY_LEFT) { if (cursorPosition > 0) { diff --git a/src/main/java/org/saturnclient/ui/components/SaturnToggle.java b/src/main/java/org/saturnclient/ui/components/SaturnToggle.java index 6e45f0d..ff25552 100644 --- a/src/main/java/org/saturnclient/ui/components/SaturnToggle.java +++ b/src/main/java/org/saturnclient/ui/components/SaturnToggle.java @@ -19,49 +19,47 @@ public class SaturnToggle extends SaturnWidget { @Override public void render( - DrawContext context, - boolean hovering, - int mouseX, - int mouseY - ) { + DrawContext context, + boolean hovering, + int mouseX, + int mouseY) { int color = prop.value - ? SaturnClient.COLOR.value - : SaturnClient.getWhite(alpha); + ? SaturnClient.COLOR.value + : SaturnClient.getWhite(alpha); int w = 13; int h = 6; int s = 8; context.drawTexture( - RenderLayer::getGuiTextured, - Textures.TOGGLE_BG, - 0, - 1, - 0, - 0, - w, - h, - w, - h, - color - ); + RenderLayer::getGuiTextured, + Textures.TOGGLE_BG, + 0, + 1, + 0, + 0, + w, + h, + w, + h, + color); context.drawTexture( - RenderLayer::getGuiTextured, - Textures.TOGGLE_INDICATOR, - prop.value ? 5 : 0, - 0, - 0, - 0, - s, - s, - s, - s, - color - ); + RenderLayer::getGuiTextured, + Textures.TOGGLE_INDICATOR, + prop.value ? 5 : 0, + 0, + 0, + 0, + s, + s, + s, + s, + color); } @Override public void click(int mouseX, int mouseY) { prop.value = !prop.value; + org.saturnclient.saturnclient.config.ConfigManager.save(); } }