Skip to content

Commit 86d5cfc

Browse files
committed
fix(config): allow configs registered after modmenu is initialized to still have screens
1 parent 3d87351 commit 86d5cfc

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
- Fix a typo in the mod renaming warning.
2-
- Add a few new methods to the `JamLibPlatform.Platform` enum.
3-
- `JamLib` is no longer entirely `ApiStatus.Internal` (there are some stable API methods in there. Non-stable public internal methods are annotated as such).
4-
- Build system improvements.
5-
- Update the icon again.
1+
- Fix an issue with configs being registered after the ModMenu entrypoint.

common/src/main/java/io/github/jamalam360/jamlib/config/ConfigManager.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.io.File;
1212
import java.io.IOException;
1313
import java.lang.reflect.Field;
14+
import java.lang.reflect.Method;
1415
import java.nio.file.Files;
1516
import java.nio.file.Path;
1617
import java.nio.file.StandardCopyOption;
@@ -69,6 +70,23 @@ public ConfigManager(String modId, String configName, Class<T> configClass) {
6970
this.reloadFromDisk();
7071
// There is an extra save here in-case the config schema was updated.
7172
this.save();
73+
tryNotifyModMenuCompatibilityOfNewConfigManager();
74+
}
75+
76+
// Slightly hacky but here we are
77+
private static void tryNotifyModMenuCompatibilityOfNewConfigManager() {
78+
try {
79+
Class<?> clazz = Class.forName("io.github.jamalam360.jamlib.fabriclike.config.ModMenuCompatibility");
80+
Method method = clazz.getDeclaredMethod("repopulateFactories");
81+
method.setAccessible(true);
82+
method.invoke(null);
83+
} catch (Exception e) {
84+
JamLib.LOGGER.warn("Failed to update ModMenu compatibility with new config manager; config may not show in ModMenu");
85+
86+
//if (Platform.isDevelopmentEnvironment()) {
87+
e.printStackTrace();
88+
//}
89+
}
7290
}
7391

7492
/**
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.github.jamalam360.jamlib.fabriclike.config;
22

3-
import com.google.common.base.Suppliers;
43
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
54
import com.terraformersmc.modmenu.api.ModMenuApi;
5+
import io.github.jamalam360.jamlib.JamLib;
66
import io.github.jamalam360.jamlib.config.ConfigManager;
77
import io.github.jamalam360.jamlib.config.gui.ConfigScreen;
88
import io.github.jamalam360.jamlib.config.gui.SelectConfigScreen;
@@ -11,15 +11,15 @@
1111
import java.util.HashMap;
1212
import java.util.List;
1313
import java.util.Map;
14-
import java.util.function.Supplier;
1514

1615
@ApiStatus.Internal
1716
public class ModMenuCompatibility implements ModMenuApi {
18-
private final Supplier<Map<String, ConfigScreenFactory<?>>> factories = Suppliers.memoize(() -> {
19-
Map<String, ConfigScreenFactory<?>> factories = new HashMap<>();
20-
ConfigManager.MANAGERS.values().stream().map(ConfigManager::getModId).distinct().forEach(modId -> factories.put(modId, createScreenFactoryForMod(modId)));
21-
return factories;
22-
});
17+
private static final Map<String, ConfigScreenFactory<?>> FACTORIES = new HashMap<>();
18+
19+
private static void repopulateFactories() {
20+
FACTORIES.clear();
21+
ConfigManager.MANAGERS.values().stream().map(ConfigManager::getModId).distinct().forEach(modId -> FACTORIES.put(modId, createScreenFactoryForMod(modId)));
22+
}
2323

2424
private static ConfigScreenFactory<?> createScreenFactoryForMod(String modId) {
2525
List<ConfigManager<?>> managers = ConfigManager.MANAGERS.values().stream().filter(m -> m.getModId().equals(modId)).toList();
@@ -33,6 +33,8 @@ private static ConfigScreenFactory<?> createScreenFactoryForMod(String modId) {
3333

3434
@Override
3535
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
36-
return this.factories.get();
36+
repopulateFactories();
37+
JamLib.LOGGER.info("Registering config screens with ModMenu: " + String.join(", ", FACTORIES.keySet()));
38+
return FACTORIES;
3739
}
3840
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
org.gradle.jvmargs=-Xmx3G
22
org.gradle.daemon=false
33
org.gradle.parallel=true
4-
version=1.0.6+1.20.4
4+
version=1.0.7+1.20.4
55
minecraft_version=1.20.4
66
branch=main
77
group=io.github.jamalam360

0 commit comments

Comments
 (0)