From 191499e2b7f212d17479e23e0c6488e2b986364a Mon Sep 17 00:00:00 2001 From: Mehrad Nayyeri Date: Sun, 2 Jul 2023 16:19:42 +0330 Subject: [PATCH] Fixed a problem with ModMenu registration --- gradle.properties | 2 +- .../entrypoint/ModMenuConfigScreen.java | 26 ++++++++++++ .../entrypoint/ModMenuEntrypoint.java | 13 +++++- .../entrypoint/ModMenuSupport.java | 40 ------------------- .../entrypoint/ModMenuConfig.java | 26 ++++++++++++ .../mehradconfigtest/MehradConfigTest.java | 6 ++- 6 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuConfigScreen.java delete mode 100644 src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuSupport.java create mode 100644 src/main/java/ir/mehradn/mehradconfig/entrypoint/ModMenuConfig.java diff --git a/gradle.properties b/gradle.properties index e6d1f1a..305ba8b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ parchment_version=1.20.1:2023.06.26 loader_version=0.14.21 # Mod Properties -mod_version=2.0.0 +mod_version=2.1.0 maven_group=ir.mehradn archives_base_name=mehrad-config diff --git a/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuConfigScreen.java b/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuConfigScreen.java new file mode 100644 index 0000000..3b8283c --- /dev/null +++ b/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuConfigScreen.java @@ -0,0 +1,26 @@ +package ir.mehradn.mehradconfig.entrypoint; + +import ir.mehradn.mehradconfig.gui.ConfigScreenBuilder; +import java.util.HashMap; +import java.util.Map; + +/** + * This class is used for registering your config screen builders for ModMenu. + * + * @see #register + */ +public class ModMenuConfigScreen { + static final Map modMenuScreenBuilders = new HashMap<>(); + + /** + * Registers a config screen builder to build screens for ModMenu. The screen will be created by {@link ConfigScreenBuilder#buildAndLoad}. + * This method by itself won't register your config, and you need to call {@link ModMenuConfig#register} before this in the main entrypoint of + * your mod. + * + * @param modId the mod id of your mod + * @param configScreenBuilder the config screen builder to build the screen with + */ + public static void register(String modId, ConfigScreenBuilder configScreenBuilder) { + modMenuScreenBuilders.put(modId, configScreenBuilder); + } +} diff --git a/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuEntrypoint.java b/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuEntrypoint.java index 39048b4..3a2c5c3 100644 --- a/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuEntrypoint.java +++ b/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuEntrypoint.java @@ -2,18 +2,27 @@ import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ModMenuApi; +import ir.mehradn.mehradconfig.MehradConfig; +import ir.mehradn.mehradconfig.gui.ConfigScreenBuilder; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import java.util.HashMap; import java.util.Map; +import java.util.function.Supplier; @Environment(EnvType.CLIENT) public class ModMenuEntrypoint implements ModMenuApi { + private static final ConfigScreenBuilder DEFAULT = new ConfigScreenBuilder().setScreenType(ConfigScreenBuilder.DefaultScreens.COMPACT); + @Override public Map> getProvidedConfigScreenFactories() { Map> factories = new HashMap<>(); - for (Map.Entry entry : ModMenuSupport.screenBuilders.entrySet()) - factories.put(entry.getKey(), (parent) -> entry.getValue().build(parent)); + for (Map.Entry> entry : ModMenuConfig.modMenuConfigs.entrySet()) { + String modId = entry.getKey(); + Supplier configConstructor = entry.getValue(); + ConfigScreenBuilder configScreenBuilder = ModMenuConfigScreen.modMenuScreenBuilders.getOrDefault(modId, DEFAULT); + factories.put(entry.getKey(), (parent) -> configScreenBuilder.buildAndLoad(configConstructor, parent)); + } return factories; } } diff --git a/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuSupport.java b/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuSupport.java deleted file mode 100644 index 98733cd..0000000 --- a/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuSupport.java +++ /dev/null @@ -1,40 +0,0 @@ -package ir.mehradn.mehradconfig.entrypoint; - -import ir.mehradn.mehradconfig.MehradConfig; -import ir.mehradn.mehradconfig.gui.ConfigScreenBuilder; -import ir.mehradn.mehradconfig.gui.screen.MehradConfigScreen; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.minecraft.client.gui.screens.Screen; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Supplier; - -/** - * This class is used for registering your configs for ModMenu. - * - * @see #register - */ -@Environment(EnvType.CLIENT) -public class ModMenuSupport { - static final Map screenBuilders = new HashMap<>(); - - /** - * Registers a config constructor and a config screen builder to build screens for ModMenu. The screen will be created by - * {@link ConfigScreenBuilder#buildAndLoad}. - * - * @param modId the mod id of your mod - * @param configConstructor a constructor for the type of the config to load, modify and save - * @param configScreenBuilder the config screen builder to build the screen with - */ - public static void register(String modId, Supplier configConstructor, ConfigScreenBuilder configScreenBuilder) { - screenBuilders.put(modId, new ConfigScreenLoader(configConstructor, configScreenBuilder)); - } - - @Environment(EnvType.CLIENT) - record ConfigScreenLoader(Supplier configConstructor, ConfigScreenBuilder configScreenBuilder) { - MehradConfigScreen build(Screen parentScreen) { - return this.configScreenBuilder.buildAndLoad(this.configConstructor, parentScreen); - } - } -} diff --git a/src/main/java/ir/mehradn/mehradconfig/entrypoint/ModMenuConfig.java b/src/main/java/ir/mehradn/mehradconfig/entrypoint/ModMenuConfig.java new file mode 100644 index 0000000..ef79cb4 --- /dev/null +++ b/src/main/java/ir/mehradn/mehradconfig/entrypoint/ModMenuConfig.java @@ -0,0 +1,26 @@ +package ir.mehradn.mehradconfig.entrypoint; + +import ir.mehradn.mehradconfig.MehradConfig; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + +/** + * This class is used for registering your configs for ModMenu. + * + * @see #register + */ +public final class ModMenuConfig { + static final Map> modMenuConfigs = new HashMap<>(); + + /** + * Registers a config constructor for ModMenu. The screen will by default use the compact default screen type. + * Checkout {@code ModMenuConfigScreen} for customizing the config screen. + * + * @param modId the mod id of your mod + * @param configConstructor a constructor for the type of the config to load, modify and save + */ + public static void register(String modId, Supplier configConstructor) { + modMenuConfigs.put(modId, configConstructor); + } +} diff --git a/src/testmod/java/ir/mehradn/mehradconfigtest/MehradConfigTest.java b/src/testmod/java/ir/mehradn/mehradconfigtest/MehradConfigTest.java index c46e1d1..e119f4e 100644 --- a/src/testmod/java/ir/mehradn/mehradconfigtest/MehradConfigTest.java +++ b/src/testmod/java/ir/mehradn/mehradconfigtest/MehradConfigTest.java @@ -1,6 +1,7 @@ package ir.mehradn.mehradconfigtest; -import ir.mehradn.mehradconfig.entrypoint.ModMenuSupport; +import ir.mehradn.mehradconfig.entrypoint.ModMenuConfig; +import ir.mehradn.mehradconfig.entrypoint.ModMenuConfigScreen; import ir.mehradn.mehradconfig.gui.ConfigScreenBuilder; import ir.mehradn.mehradconfigtest.config.TestConfig; import net.fabricmc.api.ModInitializer; @@ -25,6 +26,7 @@ public void onInitialize() { //.setOnSave((minecraft, thisScreen, parentScreen) -> { LOGGER.info("SAVED!"); minecraft.setScreen(parentScreen); }) //.setOnCancel((minecraft, thisScreen, parentScreen) -> { LOGGER.info("CANCELED!"); minecraft.setScreen(parentScreen); }) ; - ModMenuSupport.register(MOD_ID, TestConfig::new, builder); + ModMenuConfig.register(MOD_ID, TestConfig::new); + ModMenuConfigScreen.register(MOD_ID, builder); } }