From a9a3d3fe34ebd47c5614262ce8fee10b7c13cb6c Mon Sep 17 00:00:00 2001 From: Mehrad Nayyeri Date: Fri, 30 Jun 2023 12:34:36 +0330 Subject: [PATCH] Added support for ModMenu --- build.gradle | 4 +- gradle.properties | 1 + .../entrypoint/ModMenuEntrypoint.java | 44 +++++++++++++++++++ src/main/resources/fabric.mod.json | 3 ++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuEntrypoint.java diff --git a/build.gradle b/build.gradle index 935e412..f038635 100644 --- a/build.gradle +++ b/build.gradle @@ -16,7 +16,7 @@ repositories { url 'https://maven.parchmentmc.org' } maven { - url 'https://jitpack.io' + url 'https://maven.terraformersmc.com/releases' } } @@ -41,6 +41,8 @@ dependencies { modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" + + modImplementation "com.terraformersmc:modmenu:${project.modmenu_version}" } processResources { diff --git a/gradle.properties b/gradle.properties index 8b6ebea..b8faf86 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,3 +15,4 @@ archives_base_name=mehrad-config # Dependencies fabric_version=0.83.0+1.19.4 +modmenu_version=6.2.2 diff --git a/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuEntrypoint.java b/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuEntrypoint.java new file mode 100644 index 0000000..74e903c --- /dev/null +++ b/src/client/java/ir/mehradn/mehradconfig/entrypoint/ModMenuEntrypoint.java @@ -0,0 +1,44 @@ +package ir.mehradn.mehradconfig.entrypoint; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import ir.mehradn.mehradconfig.MehradConfig; +import ir.mehradn.mehradconfig.gui.ConfigScreenBuilder; +import ir.mehradn.mehradconfig.gui.screen.MehradConfigScreen; +import net.minecraft.client.gui.screens.Screen; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Supplier; + +/** + * This is the entrypoint used by ModMenu to create the config buttons. Use the {@link #register} to register your configs. + */ +public class ModMenuEntrypoint implements ModMenuApi { + private 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)); + } + + @Override + public Map> getProvidedConfigScreenFactories() { + Map> factories = new HashMap<>(); + for (Map.Entry entry : screenBuilders.entrySet()) + factories.put(entry.getKey(), (parent) -> entry.getValue().build(parent)); + return factories; + } + + private record ConfigScreenLoader(Supplier configConstructor, ConfigScreenBuilder configScreenBuilder) { + MehradConfigScreen build(Screen parentScreen) { + return this.configScreenBuilder.buildAndLoad(this.configConstructor, parentScreen); + } + } +} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 9d23af7..1ca5f85 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -17,6 +17,9 @@ "entrypoints": { "main": [ "ir.mehradn.mehradconfig.entrypoint.MehradConfigEntrypoint" + ], + "modmenu": [ + "ir.mehradn.mehradconfig.entrypoint.ModMenuEntrypoint" ] }, "depends": {