Skip to content

Commit

Permalink
fix(config): allow configs registered after modmenu is initialized to…
Browse files Browse the repository at this point in the history
… still have screens
  • Loading branch information
Jamalam360 committed Mar 25, 2024
1 parent 3d87351 commit 86d5cfc
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
6 changes: 1 addition & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
- Fix a typo in the mod renaming warning.
- Add a few new methods to the `JamLibPlatform.Platform` enum.
- `JamLib` is no longer entirely `ApiStatus.Internal` (there are some stable API methods in there. Non-stable public internal methods are annotated as such).
- Build system improvements.
- Update the icon again.
- Fix an issue with configs being registered after the ModMenu entrypoint.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -69,6 +70,23 @@ public ConfigManager(String modId, String configName, Class<T> configClass) {
this.reloadFromDisk();
// There is an extra save here in-case the config schema was updated.
this.save();
tryNotifyModMenuCompatibilityOfNewConfigManager();
}

// Slightly hacky but here we are
private static void tryNotifyModMenuCompatibilityOfNewConfigManager() {
try {
Class<?> clazz = Class.forName("io.github.jamalam360.jamlib.fabriclike.config.ModMenuCompatibility");
Method method = clazz.getDeclaredMethod("repopulateFactories");
method.setAccessible(true);
method.invoke(null);
} catch (Exception e) {
JamLib.LOGGER.warn("Failed to update ModMenu compatibility with new config manager; config may not show in ModMenu");

//if (Platform.isDevelopmentEnvironment()) {
e.printStackTrace();
//}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.github.jamalam360.jamlib.fabriclike.config;

import com.google.common.base.Suppliers;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import io.github.jamalam360.jamlib.JamLib;
import io.github.jamalam360.jamlib.config.ConfigManager;
import io.github.jamalam360.jamlib.config.gui.ConfigScreen;
import io.github.jamalam360.jamlib.config.gui.SelectConfigScreen;
Expand All @@ -11,15 +11,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

@ApiStatus.Internal
public class ModMenuCompatibility implements ModMenuApi {
private final Supplier<Map<String, ConfigScreenFactory<?>>> factories = Suppliers.memoize(() -> {
Map<String, ConfigScreenFactory<?>> factories = new HashMap<>();
ConfigManager.MANAGERS.values().stream().map(ConfigManager::getModId).distinct().forEach(modId -> factories.put(modId, createScreenFactoryForMod(modId)));
return factories;
});
private static final Map<String, ConfigScreenFactory<?>> FACTORIES = new HashMap<>();

private static void repopulateFactories() {
FACTORIES.clear();
ConfigManager.MANAGERS.values().stream().map(ConfigManager::getModId).distinct().forEach(modId -> FACTORIES.put(modId, createScreenFactoryForMod(modId)));
}

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

@Override
public Map<String, ConfigScreenFactory<?>> getProvidedConfigScreenFactories() {
return this.factories.get();
repopulateFactories();
JamLib.LOGGER.info("Registering config screens with ModMenu: " + String.join(", ", FACTORIES.keySet()));
return FACTORIES;
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org.gradle.jvmargs=-Xmx3G
org.gradle.daemon=false
org.gradle.parallel=true
version=1.0.6+1.20.4
version=1.0.7+1.20.4
minecraft_version=1.20.4
branch=main
group=io.github.jamalam360
Expand Down

0 comments on commit 86d5cfc

Please sign in to comment.