better config menu stuff

This commit is contained in:
2025-03-25 00:30:04 +01:00
parent 9a49a24cac
commit 2ff66b796f
4 changed files with 205 additions and 90 deletions
@@ -1,29 +1,32 @@
package org.saturnclient.saturnclient.config;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import net.minecraft.client.MinecraftClient;
import org.saturnclient.saturnclient.SaturnClient;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonElement;
import net.minecraft.client.MinecraftClient;
public class ConfigManager {
private static File configFile = new File(MinecraftClient.getInstance().runDirectory, "saturn.json");
private static Map<String, Map<String, Property<?>>> properties = new HashMap<>();
private static File configFile = new File(
MinecraftClient.getInstance().runDirectory,
"saturn.json"
);
private static Map<String, Map<String, Property<?>>> properties =
new HashMap<>();
private String namespace;
public ConfigManager(String namespace) {
this.namespace = namespace;
properties.put(namespace, new HashMap<>());
properties.put(namespace, new LinkedHashMap<>());
}
// Generic method to store any type of property
@@ -40,41 +43,50 @@ public class ConfigManager {
configFile.createNewFile();
}
JsonObject jsonObject = JsonParser.parseString(new String(Files.readAllBytes(configFile.toPath())))
.getAsJsonObject();
JsonObject jsonObject = JsonParser.parseString(
new String(Files.readAllBytes(configFile.toPath()))
).getAsJsonObject();
for (String namespace : properties.keySet()) {
JsonElement configElement = jsonObject.get(namespace);
if (configElement == null)
continue;
if (configElement == null) continue;
JsonObject config = jsonObject.get(namespace).getAsJsonObject();
if (config == null) {
SaturnClient.LOGGER.warn("No config found for namespace: " + namespace);
SaturnClient.LOGGER.warn(
"No config found for namespace: " + namespace
);
continue;
}
Map<String, Property<?>> propertyMap = properties.get(namespace);
Map<String, Property<?>> propertyMap = properties.get(
namespace
);
for (String propertyName : propertyMap.keySet()) {
JsonElement c = config.get(propertyName);
if (c == null) {
SaturnClient.LOGGER.warn("No property found in config for " + propertyName);
SaturnClient.LOGGER.warn(
"No property found in config for " + propertyName
);
continue;
}
SaturnClient.LOGGER.info("got prop " + c);
Property<?> p = propertyMap.get(propertyName);
SaturnClient.LOGGER.info("setting prop " + propertyName + " to " + p.value);
SaturnClient.LOGGER.info(
"setting prop " + propertyName + " to " + p.value
);
SaturnClient.LOGGER.info("Property type: " + p.getType());
if (p.matchesJson(c)) {
switch (p.getType()) {
case BOOLEAN:
((Property<Boolean>) p).value = c.getAsBoolean();
((Property<Boolean>) p).value =
c.getAsBoolean();
break;
case INTEGER:
((Property<Integer>) p).value = c.getAsInt();
@@ -89,7 +101,9 @@ public class ConfigManager {
break;
}
} else {
SaturnClient.LOGGER.warn("Property does not match JSON: " + propertyName);
SaturnClient.LOGGER.warn(
"Property does not match JSON: " + propertyName
);
}
}
}
@@ -107,7 +121,9 @@ public class ConfigManager {
// Iterate through all namespaces and their properties
for (String namespace : properties.keySet()) {
JsonObject namespaceConfig = new JsonObject();
Map<String, Property<?>> propertyMap = properties.get(namespace);
Map<String, Property<?>> propertyMap = properties.get(
namespace
);
// Iterate through each property in the namespace
for (String propertyName : propertyMap.keySet()) {
@@ -117,19 +133,29 @@ public class ConfigManager {
// Convert the property value based on its type
switch (property.getType()) {
case BOOLEAN:
propertyValue = new JsonPrimitive((Boolean) property.value);
propertyValue = new JsonPrimitive(
(Boolean) property.value
);
break;
case INTEGER:
propertyValue = new JsonPrimitive((Integer) property.value);
propertyValue = new JsonPrimitive(
(Integer) property.value
);
break;
case FLOAT:
propertyValue = new JsonPrimitive((Float) property.value);
propertyValue = new JsonPrimitive(
(Float) property.value
);
break;
case STRING:
propertyValue = new JsonPrimitive((String) property.value);
propertyValue = new JsonPrimitive(
(String) property.value
);
break;
default:
SaturnClient.LOGGER.warn("Unknown property type for: " + propertyName);
SaturnClient.LOGGER.warn(
"Unknown property type for: " + propertyName
);
break;
}
@@ -1,7 +1,7 @@
package org.saturnclient.saturnclient.menus;
import java.util.Map;
import net.minecraft.text.Text;
import org.saturnclient.saturnclient.config.ConfigManager;
import org.saturnclient.saturnclient.config.Property;
import org.saturnclient.ui.SaturnUi;
@@ -12,9 +12,8 @@ import org.saturnclient.ui.widgets.SaturnScroll;
import org.saturnclient.ui.widgets.SaturnSprite;
import org.saturnclient.ui.widgets.SaturnText;
import net.minecraft.text.Text;
public class ConfigEditor extends SaturnUi {
private ConfigManager config;
public ConfigEditor(ConfigManager config) {
@@ -30,53 +29,112 @@ public class ConfigEditor extends SaturnUi {
int rectX = (width - rectWidth) / 2;
int rectY = (height - rectHeight + 15) / 2;
draw(new SaturnSprite(Textures.SETTINGS_BG)
draw(
new SaturnSprite(Textures.SETTINGS_BG)
.setX(rectX)
.setY(rectY)
.setWidth(rectWidth)
.setHeight(rectHeight)
.setAnimations(new Slide(1, 10)));
.setAnimations(new Slide(1, 10))
);
int tabsX = (width - 43) / 2;
int tabsY = rectY - 17;
draw(new SaturnSprite(Textures.TABS)
draw(
new SaturnSprite(Textures.TABS)
.setX(tabsX)
.setY(tabsY)
.setWidth(43)
.setHeight(15)
.setAnimations(new Slide(1, 10)));
int modX = 0;
int modY = 0;
int col = 0;
.setAnimations(new Slide(1, 10))
);
int propWidth = (rectWidth - 15) / 2;
SaturnScroll configScroll = new SaturnScroll();
for (Map.Entry<String, Property<?>> propEntry : config.getProperties().entrySet()) {
int row = 0;
int col = 0;
for (Map.Entry<String, Property<?>> propEntry : config
.getProperties()
.entrySet()) {
Property<?> prop = propEntry.getValue();
String propName = propEntry.getKey();
if (prop.value instanceof Boolean) {
configScroll.draw(new SaturnToggle((Property<Boolean>) prop).setX(modX).setY(modY));
configScroll.draw(
new SaturnText(propName.substring(0, 1).toUpperCase() + propName.substring(1))
.setX(modX + 18).setY(modY));
col++;
if (col < 2) {
modX += propWidth + 21;
} else {
modX = 0;
modY += 20 + 20;
boolean full = isFull(prop);
if (full && col > 0) {
col = 0;
row++;
}
int modX = ((rectWidth / 2) - 17) * col;
int modY = 12 * row;
switch (prop.getType()) {
case BOOLEAN:
configScroll.draw(
new SaturnToggle((Property<Boolean>) prop)
.setX(modX)
.setY(modY)
);
configScroll.draw(
new SaturnText(
propName.substring(0, 1).toUpperCase() +
propName.substring(1)
)
.setX(modX + 18)
.setY(modY)
);
break;
case INTEGER:
configScroll.draw(
new SaturnText(
propName.substring(0, 1).toUpperCase() +
propName.substring(1)
)
.setX(modX)
.setY(modY)
);
configScroll.draw(
new SaturnText("" + prop.value)
.setX((rectWidth / 2) - 17)
.setY(modY)
);
break;
}
if (full) {
row++;
} else {
col++;
if (col > 1) {
col = 0;
row++;
}
}
}
}
draw(configScroll.setX(rectX + 17).setY(rectY + 10).setWidth(rectWidth - 17).setHeight(rectHeight - 10));
draw(
configScroll
.setX(rectX + 17)
.setY(rectY + 10)
.setWidth(rectWidth - 17)
.setHeight(rectHeight - 10)
);
super.init();
}
public static boolean isFull(Property<?> prop) {
switch (prop.getType()) {
case BOOLEAN:
return false;
default:
return true;
}
}
}
@@ -1,13 +1,5 @@
package org.saturnclient.saturnmods.mods;
import org.saturnclient.saturnclient.SaturnClient;
import org.saturnclient.saturnclient.config.ConfigManager;
import org.saturnclient.saturnclient.config.Property;
import org.saturnclient.saturnmods.HudMod;
import org.saturnclient.saturnmods.ModDimensions;
import org.saturnclient.saturnmods.SaturnMod;
import org.saturnclient.ui.Textures;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack;
@@ -16,11 +8,28 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
import org.saturnclient.saturnclient.SaturnClient;
import org.saturnclient.saturnclient.config.ConfigManager;
import org.saturnclient.saturnclient.config.Property;
import org.saturnclient.saturnmods.HudMod;
import org.saturnclient.saturnmods.ModDimensions;
import org.saturnclient.saturnmods.SaturnMod;
import org.saturnclient.ui.Textures;
public class ArmorDisplay implements SaturnMod, HudMod {
private static ConfigManager config = new ConfigManager("armor");
private static ModDimensions dimensions = new ModDimensions(config, 0, 0, 34, 75);
public static Property<Boolean> enabled = config.property("enabled", new Property<>(false));
public static Property<Boolean> enabled = config.property(
"Enabled",
new Property<>(false)
);
private static ModDimensions dimensions = new ModDimensions(
config,
0,
0,
34,
75
);
private boolean renderItem(DrawContext context, ItemStack item, int y) {
if (item.isEmpty()) {
@@ -38,8 +47,14 @@ public class ArmorDisplay implements SaturnMod, HudMod {
matrices.scale(0.8f, 0.8f, 0f);
context.drawText(SaturnClient.textRenderer, "" + (item.getMaxDamage() - item.getDamage()),
0, 0, SaturnClient.WHITE, false);
context.drawText(
SaturnClient.textRenderer,
"" + (item.getMaxDamage() - item.getDamage()),
0,
0,
SaturnClient.WHITE,
false
);
matrices.pop();
}
@@ -1,5 +1,9 @@
package org.saturnclient.saturnmods.mods;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
import org.saturnclient.saturnclient.SaturnClient;
import org.saturnclient.saturnclient.config.ConfigManager;
import org.saturnclient.saturnclient.config.Property;
@@ -8,28 +12,34 @@ import org.saturnclient.saturnmods.ModDimensions;
import org.saturnclient.saturnmods.SaturnMod;
import org.saturnclient.ui.Textures;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.Identifier;
public class Coordinates implements SaturnMod, HudMod {
private static ConfigManager config = new ConfigManager("coords");
private static ModDimensions dimensions = new ModDimensions(config);
public static Property<Boolean> enabled = config.property("enabled", new Property<>(false));
public Coordinates() {
}
private static ConfigManager config = new ConfigManager("coords");
public static Property<Boolean> enabled = config.property(
"Enabled",
new Property<>(false)
);
private static ModDimensions dimensions = new ModDimensions(config);
public Coordinates() {}
public void renderDummy(DrawContext context) {
int playerX = 124;
int playerY = 69;
int playerZ = 83;
context.drawText(SaturnClient.textRenderer, playerX + " " + playerY + " " + playerZ,
0, 0, SaturnClient.WHITE, false);
context.drawText(
SaturnClient.textRenderer,
playerX + " " + playerY + " " + playerZ,
0,
0,
SaturnClient.WHITE,
false
);
dimensions.width = SaturnClient.textRenderer.getWidth(playerX + " " + playerY + " " + playerZ);
dimensions.width = SaturnClient.textRenderer.getWidth(
playerX + " " + playerY + " " + playerZ
);
dimensions.height = SaturnClient.textRenderer.fontHeight;
}
@@ -39,8 +49,14 @@ public class Coordinates implements SaturnMod, HudMod {
int playerY = (int) player.getY();
int playerZ = (int) player.getZ();
context.drawText(SaturnClient.textRenderer, playerX + " " + playerY + " " + playerZ,
0, 0, SaturnClient.WHITE, false);
context.drawText(
SaturnClient.textRenderer,
playerX + " " + playerY + " " + playerZ,
0,
0,
SaturnClient.WHITE,
false
);
}
public String getName() {