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; 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.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import net.minecraft.client.MinecraftClient;
import org.saturnclient.saturnclient.SaturnClient; 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 { 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; private String namespace;
public ConfigManager(String namespace) { public ConfigManager(String namespace) {
this.namespace = namespace; this.namespace = namespace;
properties.put(namespace, new HashMap<>()); properties.put(namespace, new LinkedHashMap<>());
} }
// Generic method to store any type of property // Generic method to store any type of property
@@ -40,41 +43,50 @@ public class ConfigManager {
configFile.createNewFile(); configFile.createNewFile();
} }
JsonObject jsonObject = JsonParser.parseString(new String(Files.readAllBytes(configFile.toPath()))) JsonObject jsonObject = JsonParser.parseString(
.getAsJsonObject(); new String(Files.readAllBytes(configFile.toPath()))
).getAsJsonObject();
for (String namespace : properties.keySet()) { for (String namespace : properties.keySet()) {
JsonElement configElement = jsonObject.get(namespace); JsonElement configElement = jsonObject.get(namespace);
if (configElement == null) if (configElement == null) continue;
continue;
JsonObject config = jsonObject.get(namespace).getAsJsonObject(); JsonObject config = jsonObject.get(namespace).getAsJsonObject();
if (config == null) { if (config == null) {
SaturnClient.LOGGER.warn("No config found for namespace: " + namespace); SaturnClient.LOGGER.warn(
"No config found for namespace: " + namespace
);
continue; continue;
} }
Map<String, Property<?>> propertyMap = properties.get(namespace); Map<String, Property<?>> propertyMap = properties.get(
namespace
);
for (String propertyName : propertyMap.keySet()) { for (String propertyName : propertyMap.keySet()) {
JsonElement c = config.get(propertyName); JsonElement c = config.get(propertyName);
if (c == null) { if (c == null) {
SaturnClient.LOGGER.warn("No property found in config for " + propertyName); SaturnClient.LOGGER.warn(
"No property found in config for " + propertyName
);
continue; continue;
} }
SaturnClient.LOGGER.info("got prop " + c); SaturnClient.LOGGER.info("got prop " + c);
Property<?> p = propertyMap.get(propertyName); 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()); SaturnClient.LOGGER.info("Property type: " + p.getType());
if (p.matchesJson(c)) { if (p.matchesJson(c)) {
switch (p.getType()) { switch (p.getType()) {
case BOOLEAN: case BOOLEAN:
((Property<Boolean>) p).value = c.getAsBoolean(); ((Property<Boolean>) p).value =
c.getAsBoolean();
break; break;
case INTEGER: case INTEGER:
((Property<Integer>) p).value = c.getAsInt(); ((Property<Integer>) p).value = c.getAsInt();
@@ -89,7 +101,9 @@ public class ConfigManager {
break; break;
} }
} else { } 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 // Iterate through all namespaces and their properties
for (String namespace : properties.keySet()) { for (String namespace : properties.keySet()) {
JsonObject namespaceConfig = new JsonObject(); 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 // Iterate through each property in the namespace
for (String propertyName : propertyMap.keySet()) { for (String propertyName : propertyMap.keySet()) {
@@ -117,19 +133,29 @@ public class ConfigManager {
// Convert the property value based on its type // Convert the property value based on its type
switch (property.getType()) { switch (property.getType()) {
case BOOLEAN: case BOOLEAN:
propertyValue = new JsonPrimitive((Boolean) property.value); propertyValue = new JsonPrimitive(
(Boolean) property.value
);
break; break;
case INTEGER: case INTEGER:
propertyValue = new JsonPrimitive((Integer) property.value); propertyValue = new JsonPrimitive(
(Integer) property.value
);
break; break;
case FLOAT: case FLOAT:
propertyValue = new JsonPrimitive((Float) property.value); propertyValue = new JsonPrimitive(
(Float) property.value
);
break; break;
case STRING: case STRING:
propertyValue = new JsonPrimitive((String) property.value); propertyValue = new JsonPrimitive(
(String) property.value
);
break; break;
default: default:
SaturnClient.LOGGER.warn("Unknown property type for: " + propertyName); SaturnClient.LOGGER.warn(
"Unknown property type for: " + propertyName
);
break; break;
} }
@@ -1,7 +1,7 @@
package org.saturnclient.saturnclient.menus; package org.saturnclient.saturnclient.menus;
import java.util.Map; import java.util.Map;
import net.minecraft.text.Text;
import org.saturnclient.saturnclient.config.ConfigManager; import org.saturnclient.saturnclient.config.ConfigManager;
import org.saturnclient.saturnclient.config.Property; import org.saturnclient.saturnclient.config.Property;
import org.saturnclient.ui.SaturnUi; 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.SaturnSprite;
import org.saturnclient.ui.widgets.SaturnText; import org.saturnclient.ui.widgets.SaturnText;
import net.minecraft.text.Text;
public class ConfigEditor extends SaturnUi { public class ConfigEditor extends SaturnUi {
private ConfigManager config; private ConfigManager config;
public ConfigEditor(ConfigManager config) { public ConfigEditor(ConfigManager config) {
@@ -30,53 +29,112 @@ public class ConfigEditor extends SaturnUi {
int rectX = (width - rectWidth) / 2; int rectX = (width - rectWidth) / 2;
int rectY = (height - rectHeight + 15) / 2; int rectY = (height - rectHeight + 15) / 2;
draw(new SaturnSprite(Textures.SETTINGS_BG) draw(
new SaturnSprite(Textures.SETTINGS_BG)
.setX(rectX) .setX(rectX)
.setY(rectY) .setY(rectY)
.setWidth(rectWidth) .setWidth(rectWidth)
.setHeight(rectHeight) .setHeight(rectHeight)
.setAnimations(new Slide(1, 10))); .setAnimations(new Slide(1, 10))
);
int tabsX = (width - 43) / 2; int tabsX = (width - 43) / 2;
int tabsY = rectY - 17; int tabsY = rectY - 17;
draw(new SaturnSprite(Textures.TABS) draw(
new SaturnSprite(Textures.TABS)
.setX(tabsX) .setX(tabsX)
.setY(tabsY) .setY(tabsY)
.setWidth(43) .setWidth(43)
.setHeight(15) .setHeight(15)
.setAnimations(new Slide(1, 10))); .setAnimations(new Slide(1, 10))
);
int modX = 0;
int modY = 0;
int col = 0;
int propWidth = (rectWidth - 15) / 2; int propWidth = (rectWidth - 15) / 2;
SaturnScroll configScroll = new SaturnScroll(); 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(); Property<?> prop = propEntry.getValue();
String propName = propEntry.getKey(); String propName = propEntry.getKey();
if (prop.value instanceof Boolean) {
configScroll.draw(new SaturnToggle((Property<Boolean>) prop).setX(modX).setY(modY)); boolean full = isFull(prop);
configScroll.draw(
new SaturnText(propName.substring(0, 1).toUpperCase() + propName.substring(1)) if (full && col > 0) {
.setX(modX + 18).setY(modY)); col = 0;
col++; row++;
if (col < 2) {
modX += propWidth + 21;
} else {
modX = 0;
modY += 20 + 20;
col = 0;
}
} }
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(); 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; 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.MinecraftClient;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.util.math.MatrixStack; 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.ItemStack;
import net.minecraft.item.Items; import net.minecraft.item.Items;
import net.minecraft.util.Identifier; 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 { public class ArmorDisplay implements SaturnMod, HudMod {
private static ConfigManager config = new ConfigManager("armor"); 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(
public static Property<Boolean> enabled = config.property("enabled", new Property<>(false)); "Enabled",
new Property<>(false)
);
private static ModDimensions dimensions = new ModDimensions(
config,
0,
0,
34,
75
);
private boolean renderItem(DrawContext context, ItemStack item, int y) { private boolean renderItem(DrawContext context, ItemStack item, int y) {
if (item.isEmpty()) { if (item.isEmpty()) {
@@ -38,8 +47,14 @@ public class ArmorDisplay implements SaturnMod, HudMod {
matrices.scale(0.8f, 0.8f, 0f); matrices.scale(0.8f, 0.8f, 0f);
context.drawText(SaturnClient.textRenderer, "" + (item.getMaxDamage() - item.getDamage()), context.drawText(
0, 0, SaturnClient.WHITE, false); SaturnClient.textRenderer,
"" + (item.getMaxDamage() - item.getDamage()),
0,
0,
SaturnClient.WHITE,
false
);
matrices.pop(); matrices.pop();
} }
@@ -51,11 +66,11 @@ public class ArmorDisplay implements SaturnMod, HudMod {
public void render(DrawContext context) { public void render(DrawContext context) {
PlayerEntity player = MinecraftClient.getInstance().player; PlayerEntity player = MinecraftClient.getInstance().player;
ItemStack[] items = { ItemStack[] items = {
player.getEquippedStack(EquipmentSlot.MAINHAND), player.getEquippedStack(EquipmentSlot.MAINHAND),
player.getEquippedStack(EquipmentSlot.FEET), player.getEquippedStack(EquipmentSlot.FEET),
player.getEquippedStack(EquipmentSlot.LEGS), player.getEquippedStack(EquipmentSlot.LEGS),
player.getEquippedStack(EquipmentSlot.CHEST), player.getEquippedStack(EquipmentSlot.CHEST),
player.getEquippedStack(EquipmentSlot.HEAD), player.getEquippedStack(EquipmentSlot.HEAD),
}; };
int y = 60; int y = 60;
@@ -70,11 +85,11 @@ public class ArmorDisplay implements SaturnMod, HudMod {
@Override @Override
public void renderDummy(DrawContext context) { public void renderDummy(DrawContext context) {
ItemStack[] items = { ItemStack[] items = {
new ItemStack(Items.DIAMOND_SWORD), new ItemStack(Items.DIAMOND_SWORD),
new ItemStack(Items.DIAMOND_BOOTS), new ItemStack(Items.DIAMOND_BOOTS),
new ItemStack(Items.DIAMOND_LEGGINGS), new ItemStack(Items.DIAMOND_LEGGINGS),
new ItemStack(Items.DIAMOND_CHESTPLATE), new ItemStack(Items.DIAMOND_CHESTPLATE),
new ItemStack(Items.DIAMOND_HELMET), new ItemStack(Items.DIAMOND_HELMET),
}; };
int y = 60; int y = 60;
@@ -1,5 +1,9 @@
package org.saturnclient.saturnmods.mods; 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.SaturnClient;
import org.saturnclient.saturnclient.config.ConfigManager; import org.saturnclient.saturnclient.config.ConfigManager;
import org.saturnclient.saturnclient.config.Property; import org.saturnclient.saturnclient.config.Property;
@@ -8,28 +12,34 @@ import org.saturnclient.saturnmods.ModDimensions;
import org.saturnclient.saturnmods.SaturnMod; import org.saturnclient.saturnmods.SaturnMod;
import org.saturnclient.ui.Textures; 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 { 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) { public void renderDummy(DrawContext context) {
int playerX = 124; int playerX = 124;
int playerY = 69; int playerY = 69;
int playerZ = 83; int playerZ = 83;
context.drawText(SaturnClient.textRenderer, playerX + " " + playerY + " " + playerZ, context.drawText(
0, 0, SaturnClient.WHITE, false); 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; dimensions.height = SaturnClient.textRenderer.fontHeight;
} }
@@ -39,8 +49,14 @@ public class Coordinates implements SaturnMod, HudMod {
int playerY = (int) player.getY(); int playerY = (int) player.getY();
int playerZ = (int) player.getZ(); int playerZ = (int) player.getZ();
context.drawText(SaturnClient.textRenderer, playerX + " " + playerY + " " + playerZ, context.drawText(
0, 0, SaturnClient.WHITE, false); SaturnClient.textRenderer,
playerX + " " + playerY + " " + playerZ,
0,
0,
SaturnClient.WHITE,
false
);
} }
public String getName() { public String getName() {