From 86d5cfc90ed877d9ff0c56745fbd2acd746ec3fc Mon Sep 17 00:00:00 2001 From: "James (Jamalam)" Date: Mon, 25 Mar 2024 19:19:34 +0000 Subject: [PATCH] fix(config): allow configs registered after modmenu is initialized to still have screens --- CHANGELOG.md | 6 +----- .../jamlib/config/ConfigManager.java | 18 ++++++++++++++++++ .../config/ModMenuCompatibility.java | 18 ++++++++++-------- gradle.properties | 2 +- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb3346f..a56a05d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/common/src/main/java/io/github/jamalam360/jamlib/config/ConfigManager.java b/common/src/main/java/io/github/jamalam360/jamlib/config/ConfigManager.java index e1b5ad9..f123aa7 100644 --- a/common/src/main/java/io/github/jamalam360/jamlib/config/ConfigManager.java +++ b/common/src/main/java/io/github/jamalam360/jamlib/config/ConfigManager.java @@ -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; @@ -69,6 +70,23 @@ public ConfigManager(String modId, String configName, Class 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(); + //} + } } /** diff --git a/fabric-like/src/main/java/io/github/jamalam360/jamlib/fabriclike/config/ModMenuCompatibility.java b/fabric-like/src/main/java/io/github/jamalam360/jamlib/fabriclike/config/ModMenuCompatibility.java index 6d8c9ec..d5c9270 100644 --- a/fabric-like/src/main/java/io/github/jamalam360/jamlib/fabriclike/config/ModMenuCompatibility.java +++ b/fabric-like/src/main/java/io/github/jamalam360/jamlib/fabriclike/config/ModMenuCompatibility.java @@ -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; @@ -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>> factories = Suppliers.memoize(() -> { - Map> factories = new HashMap<>(); - ConfigManager.MANAGERS.values().stream().map(ConfigManager::getModId).distinct().forEach(modId -> factories.put(modId, createScreenFactoryForMod(modId))); - return factories; - }); + private static final Map> 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> managers = ConfigManager.MANAGERS.values().stream().filter(m -> m.getModId().equals(modId)).toList(); @@ -33,6 +33,8 @@ private static ConfigScreenFactory createScreenFactoryForMod(String modId) { @Override public Map> getProvidedConfigScreenFactories() { - return this.factories.get(); + repopulateFactories(); + JamLib.LOGGER.info("Registering config screens with ModMenu: " + String.join(", ", FACTORIES.keySet())); + return FACTORIES; } } diff --git a/gradle.properties b/gradle.properties index 8f744b0..f803598 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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