diff --git a/src/main/java/net/minecraftforge/client/ConfigGuiHandler.java b/src/main/java/net/minecraftforge/client/ConfigGuiHandler.java index 4d92822c12..dbaee650e5 100644 --- a/src/main/java/net/minecraftforge/client/ConfigGuiHandler.java +++ b/src/main/java/net/minecraftforge/client/ConfigGuiHandler.java @@ -13,10 +13,30 @@ import net.minecraftforge.forgespi.language.IModInfo; import java.util.Optional; import java.util.function.BiFunction; +import java.util.function.Function; public class ConfigGuiHandler { - public record ConfigGuiFactory(BiFunction screenFunction) implements IExtensionPoint {} + /** + * @param screenFunction A function that takes the {@link Minecraft} client instance and the mods screen as + * arguments and returns your config screen to show when the player clicks the config button + * for your mod on the mods screen. + *

You should call {@link Minecraft#setScreen(Screen)} with the provided client instance + * and mods screen for the action of your close button.

+ */ + public record ConfigGuiFactory(BiFunction screenFunction) implements IExtensionPoint { + /** + * @param screenFunction A function that takes the mods screen as an argument and returns your config screen to + * show when the player clicks the config button for your mod on the mods screen. + *

You should call {@link Minecraft#setScreen(Screen)} with the provided mods screen + * for the action of your close button, using {@link Screen#minecraft} to get the client + * instance.

+ */ + public ConfigGuiFactory(Function screenFunction) { + this((mcClient, modsScreen) -> screenFunction.apply(modsScreen)); + } + } + public static Optional> getGuiFactoryFor(IModInfo selectedMod) { return ModList.get().getModContainerById(selectedMod.getModId()). diff --git a/src/main/java/net/minecraftforge/common/MinecraftForge.java b/src/main/java/net/minecraftforge/common/MinecraftForge.java index 7cd76ecb05..1d6c9a0c53 100644 --- a/src/main/java/net/minecraftforge/common/MinecraftForge.java +++ b/src/main/java/net/minecraftforge/common/MinecraftForge.java @@ -5,10 +5,14 @@ package net.minecraftforge.common; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.Screen; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.client.ClientCommandHandler; +import net.minecraftforge.client.ConfigGuiHandler; import net.minecraftforge.eventbus.api.BusBuilder; import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.loading.FMLEnvironment; import net.minecraftforge.network.DualStackUtils; import net.minecraftforge.versions.forge.ForgeVersion; @@ -17,6 +21,9 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.MarkerManager; +import java.util.function.BiFunction; +import java.util.function.Function; + public class MinecraftForge { /** @@ -46,6 +53,36 @@ public class MinecraftForge DualStackUtils.initialise(); } + /** + * Register a config screen for the active mod container. + * @param screenFunction A function that takes the mods screen as an argument and returns your config screen to + * show when the player clicks the config button for your mod on the mods screen. + *

You should call {@link Minecraft#setScreen(Screen)} with the provided mods screen for the + * action of your close button, using {@link Screen#minecraft} to get the client instance.

+ * @see ModLoadingContext#registerExtensionPoint(Class, Supplier) + * @see ModLoadingContext#registerConfig(ModConfig.Type, IConfigSpec) + */ + public static void registerConfigScreen(Function screenFunction) { + registerConfigScreen((mcClient, modsScreen) -> screenFunction.apply(modsScreen)); + } + + /** + * Register a config screen for the active mod container. + * @param screenFunction A function that takes the {@link Minecraft} client instance and the mods screen as + * arguments and returns your config screen to show when the player clicks the config button + * for your mod on the mods screen. + *

You should call {@link Minecraft#setScreen(Screen)} with the provided client instance + * and mods screen for the action of your close button.

+ * @see ModLoadingContext#registerExtensionPoint(Class, Supplier) + * @see ModLoadingContext#registerConfig(ModConfig.Type, IConfigSpec) + */ + public static void registerConfigScreen(BiFunction screenFunction) { + ModLoadingContext.get().registerExtensionPoint( + ConfigGuiHandler.ConfigGuiFactory.class, + () -> new ConfigGuiHandler.ConfigGuiFactory(screenFunction) + ); + } + /* public static void preloadCrashClasses(ASMDataTable table, String modID, Set classes) {