Skip to content

Commit 8d0c377

Browse files
committed
almost working
1 parent bda0caa commit 8d0c377

7 files changed

Lines changed: 266 additions & 156 deletions

File tree

src/main/java/dsns/betterhud/BetterHUD.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dsns.betterhud;
22

3+
import dsns.betterhud.ModMenu;
34
import dsns.betterhud.mods.*;
45
import dsns.betterhud.util.BaseMod;
56
import java.util.ArrayList;
@@ -21,7 +22,7 @@ public class BetterHUD implements ClientModInitializer {
2122
// That way, it's clear which mod wrote info, warnings, and errors.
2223
public static final Logger LOGGER = LoggerFactory.getLogger("betterhud");
2324

24-
ArrayList<BaseMod> mods = new ArrayList<>(
25+
public static ArrayList<BaseMod> mods = new ArrayList<>(
2526
Arrays.asList(
2627
new FPS(),
2728
new Ping(),
@@ -35,9 +36,10 @@ public class BetterHUD implements ClientModInitializer {
3536

3637
@Override
3738
public void onInitializeClient() {
38-
Config.configure(mods);
39+
Config.configure();
3940

4041
BetterHUDGUI betterHUDGUI = new BetterHUDGUI();
42+
4143
HudElementRegistry.addLast(
4244
Identifier.of("betterhud", "hud"),
4345
betterHUDGUI::onHudRender

src/main/java/dsns/betterhud/BetterHUDGUI.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ public class BetterHUDGUI implements ClientTickEvents.StartTick {
2828
private final List<CustomText> bottomRightText = new ObjectArrayList<>();
2929
private final List<CustomText> customPositionText = new ObjectArrayList<>();
3030

31-
private ArrayList<BaseMod> mods = new ArrayList<>();
32-
33-
public void instantiateMods(ArrayList<BaseMod> mods) {
34-
this.mods = mods;
35-
}
36-
3731
@Override
3832
public void onStartTick(MinecraftClient client) {
3933
this.topLeftText.clear();
@@ -42,24 +36,26 @@ public void onStartTick(MinecraftClient client) {
4236
this.bottomRightText.clear();
4337
this.customPositionText.clear();
4438

45-
for (BaseMod mod : mods) {
39+
for (BaseMod mod : BetterHUD.mods) {
4640
ModSettings modSettings = mod.getModSettings();
47-
if (!modSettings.getSetting("enabled").getBooleanValue()) continue;
41+
if (!modSettings.getSetting("Enabled").getBooleanValue()) continue;
42+
System.out.println(modSettings.getSetting("Enabled").getBooleanValue());
4843

4944
CustomText modText = mod.onStartTick(client);
45+
System.out.println(modText);
5046
if (modText == null) continue;
5147

5248
String orientation = modSettings
53-
.getSetting("orientation")
49+
.getSetting("Orientation")
5450
.getStringValue();
5551

56-
if (modSettings.getSetting("customPosition").getBooleanValue()) {
52+
if (modSettings.getSetting("Custom Position").getBooleanValue()) {
5753
modText.customPosition = true;
5854
modText.customX = modSettings
59-
.getSetting("customX")
55+
.getSetting("Custom X")
6056
.getIntValue();
6157
modText.customY = modSettings
62-
.getSetting("customY")
58+
.getSetting("Custom Y")
6359
.getIntValue();
6460
this.customPositionText.add(modText);
6561
} else if (orientation.equals("top-left")) {

src/main/java/dsns/betterhud/Config.java

Lines changed: 101 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,47 @@ public class Config {
1919

2020
private static HashMap<String, ModSettings> settings = new HashMap<>();
2121

22-
public static void configure(ArrayList<BaseMod> mods) {
23-
if (settings.size() == 0) {
24-
for (BaseMod mod : mods) {
25-
settings.put(mod.getModID(), mod.getModSettings());
26-
}
27-
}
28-
22+
public static void configure() {
2923
if (Files.exists(configPath)) {
30-
deserialize(mods);
24+
deserialize();
3125
} else {
3226
serialize();
3327
}
3428
}
3529

3630
public static void serialize() {
31+
HashMap<String, ModSettings> settings = new HashMap<>();
32+
33+
if (settings.size() == 0) {
34+
for (BaseMod mod : BetterHUD.mods) {
35+
settings.put(mod.getModID(), mod.getModSettings());
36+
}
37+
}
38+
3739
Properties prop = new Properties();
38-
prop.putAll(settings);
40+
41+
HashMap<String, String> serialized = new HashMap<String, String>();
42+
43+
for (Map.Entry<String, ModSettings> entry : settings.entrySet()) {
44+
ModSettings modSettings = entry.getValue();
45+
46+
for (Map.Entry<String, Setting> settingEntry : modSettings
47+
.getSettings()
48+
.entrySet()) {
49+
System.out.println(settingEntry);
50+
System.out.println(settingEntry.getValue());
51+
System.out.println(settingEntry.getValue().getStringValue());
52+
53+
serialized.put(
54+
entry.getKey() + "." + settingEntry.getKey(),
55+
settingEntry.getValue().getStringValue()
56+
);
57+
}
58+
}
59+
60+
System.out.println("Serialized settings: " + serialized);
61+
62+
prop.putAll(serialized);
3963

4064
try {
4165
prop.store(Files.newOutputStream(configPath), null);
@@ -44,29 +68,91 @@ public static void serialize() {
4468
}
4569
}
4670

47-
public static void deserialize(ArrayList<BaseMod> mods) {
71+
public static void deserialize() {
4872
Properties prop = new Properties();
4973
try {
5074
prop.load(Files.newInputStream(configPath));
75+
System.out.println(
76+
"Loaded config with " + prop.size() + " properties."
77+
);
5178
} catch (Exception e) {
79+
System.err.println("Failed to load config: " + e.getMessage());
5280
e.printStackTrace();
81+
return;
5382
}
5483

5584
Map<String, String> map = new HashMap<String, String>();
56-
5785
for (final String name : prop.stringPropertyNames()) {
5886
map.put(name, prop.getProperty(name));
5987
}
6088

61-
// set each setting
62-
for (ModSettings modSetting : settings.values()) {
89+
// Load per-mod prefixed settings
90+
int updatedCount = 0;
91+
for (Map.Entry<String, ModSettings> modEntry : settings.entrySet()) {
92+
String modID = modEntry.getKey();
93+
ModSettings modSetting = modEntry.getValue();
94+
6395
for (Map.Entry<String, Setting> entry : modSetting
6496
.getSettings()
6597
.entrySet()) {
66-
entry.getValue().setValue(map.get(entry.getKey()));
98+
String fullKey = modID + "." + entry.getKey();
99+
String val = map.get(fullKey);
100+
if (val != null) {
101+
entry.getValue().setValue(val);
102+
updatedCount++;
103+
System.out.println(
104+
"Deserialized: " + fullKey + " = " + val
105+
); // Debug (remove after)
106+
} else {
107+
System.out.println(
108+
"No value found for " + fullKey + "; keeping default."
109+
); // Debug (remove after)
110+
}
67111
}
68112
}
69113

70-
serialize();
114+
// Backwards compatibility: Migrate old global settings to all mods (one-time)
115+
String globalBg = map.get("backgroundColor");
116+
if (globalBg != null) {
117+
for (Map.Entry<
118+
String,
119+
ModSettings
120+
> modEntry : settings.entrySet()) {
121+
Setting bgSetting = modEntry
122+
.getValue()
123+
.getSetting("Background Color");
124+
if (bgSetting != null) {
125+
bgSetting.setValue(globalBg);
126+
updatedCount++;
127+
}
128+
}
129+
System.out.println(
130+
"Migrated global backgroundColor to all mods: " + globalBg
131+
);
132+
}
133+
134+
String globalText = map.get("textColor");
135+
if (globalText != null) {
136+
for (Map.Entry<
137+
String,
138+
ModSettings
139+
> modEntry : settings.entrySet()) {
140+
Setting textSetting = modEntry
141+
.getValue()
142+
.getSetting("Text Color");
143+
if (textSetting != null) {
144+
textSetting.setValue(globalText);
145+
updatedCount++;
146+
}
147+
}
148+
System.out.println(
149+
"Migrated global textColor to all mods: " + globalText
150+
);
151+
}
152+
153+
// Ignore other old globals (e.g., horizontalMargin) unless you map them to specific settings
154+
155+
System.out.println("Deserialized " + updatedCount + " settings.");
156+
serialize(); // Re-save with migrated values
71157
}
72158
}

0 commit comments

Comments
 (0)