From 75eedc04a1ebecf5fa8f34f23a3deabc6d8ea3ae Mon Sep 17 00:00:00 2001 From: Paint_Ninja Date: Sun, 31 Dec 2023 15:37:33 +0000 Subject: [PATCH] Make common DisplayTest registration tasks easier (#9838) Backport of #9822 to 1.19.2 --- docs/README.md | 2 +- .../minecraftforge/fml/IExtensionPoint.java | 19 +++++++++ .../net/minecraftforge/fml/ModContainer.java | 21 +++++++++- .../minecraftforge/fml/ModLoadingContext.java | 39 +++++++++++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) diff --git a/docs/README.md b/docs/README.md index 9e681597ef..ea84c574cb 100644 --- a/docs/README.md +++ b/docs/README.md @@ -63,5 +63,5 @@ Please ensure you have a valid email address associated with your GitHub account [Download]: https://files.minecraftforge.net/ [Forum]: https://www.minecraftforge.net/forum/ -[Discord]: https://discord.gg/UvedJ9m +[Discord]: https://discord.minecraftforge.net [Documentation]: https://mcforge.readthedocs.io diff --git a/fmlcore/src/main/java/net/minecraftforge/fml/IExtensionPoint.java b/fmlcore/src/main/java/net/minecraftforge/fml/IExtensionPoint.java index 731901cb2b..bffc968370 100644 --- a/fmlcore/src/main/java/net/minecraftforge/fml/IExtensionPoint.java +++ b/fmlcore/src/main/java/net/minecraftforge/fml/IExtensionPoint.java @@ -95,5 +95,24 @@ public interface IExtensionPoint @SuppressWarnings("JavadocReference") // reference to NetworkConstants, ForgeHooksClient record DisplayTest(Supplier suppliedVersion, BiPredicate remoteVersionTest) implements IExtensionPoint { public static final String IGNORESERVERONLY = "OHNOES\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31\uD83D\uDE31"; + + /** + * Ignores any version information coming from the server - use for server only mods + */ + public static final Supplier IGNORE_SERVER_VERSION = () -> new DisplayTest(IGNORESERVERONLY, (remoteVersion, isFromServer) -> true); + + /** + * Ignores all information and provides no information + */ + public static final Supplier IGNORE_ALL_VERSION = () -> new DisplayTest("", (remoteVersion, isFromServer) -> true); + + /** + * An optional alternative to {@link #DisplayTest(Supplier, BiPredicate)} which accepts a constant version string + * instead of a {@link Supplier}. + *

Internally, the provided version string is wrapped in a Supplier for you.

+ */ + public DisplayTest(String version, BiPredicate remoteVersionTest) { + this(() -> version, remoteVersionTest); + } } } diff --git a/fmlcore/src/main/java/net/minecraftforge/fml/ModContainer.java b/fmlcore/src/main/java/net/minecraftforge/fml/ModContainer.java index 6eb85d8e9a..e1e7d055c6 100644 --- a/fmlcore/src/main/java/net/minecraftforge/fml/ModContainer.java +++ b/fmlcore/src/main/java/net/minecraftforge/fml/ModContainer.java @@ -20,6 +20,7 @@ import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.function.BiFunction; +import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.function.Supplier; @@ -63,9 +64,9 @@ public abstract class ModContainer () -> new IExtensionPoint.DisplayTest(() -> this.modInfo.getVersion().toString(), (incoming, isNetwork) -> Objects.equals(incoming, this.modInfo.getVersion().toString())); case "IGNORE_SERVER_VERSION" -> // Ignores any version information coming from the server - use for server only mods - () -> new IExtensionPoint.DisplayTest(() -> IExtensionPoint.DisplayTest.IGNORESERVERONLY, (incoming, isNetwork) -> true); + IExtensionPoint.DisplayTest.IGNORE_SERVER_VERSION; case "IGNORE_ALL_VERSION" -> // Ignores all information and provides no information - () -> new IExtensionPoint.DisplayTest(() -> "", (incoming, isNetwork) -> true); + IExtensionPoint.DisplayTest.IGNORE_ALL_VERSION; case "NONE" -> null; // NO display test at all - use this if you're going to do your own display test default -> // any other value throws an exception throw new IllegalArgumentException("Invalid displayTest value supplied in mods.toml"); @@ -142,6 +143,22 @@ public abstract class ModContainer extensionPoints.put(point, extension); } + public void registerDisplayTest(IExtensionPoint.DisplayTest displayTest) { + registerExtensionPoint(IExtensionPoint.DisplayTest.class, () -> displayTest); + } + + public void registerDisplayTest(Supplier displayTest) { + registerExtensionPoint(IExtensionPoint.DisplayTest.class, displayTest); + } + + public void registerDisplayTest(String version, BiPredicate remoteVersionTest) { + registerDisplayTest(new IExtensionPoint.DisplayTest(version, remoteVersionTest)); + } + + public void registerDisplayTest(Supplier suppliedVersion, BiPredicate remoteVersionTest) { + registerDisplayTest(new IExtensionPoint.DisplayTest(suppliedVersion, remoteVersionTest)); + } + public void addConfig(final ModConfig modConfig) { configs.put(modConfig.getType(), modConfig); } diff --git a/fmlcore/src/main/java/net/minecraftforge/fml/ModLoadingContext.java b/fmlcore/src/main/java/net/minecraftforge/fml/ModLoadingContext.java index e549437fda..bfe5e4d248 100644 --- a/fmlcore/src/main/java/net/minecraftforge/fml/ModLoadingContext.java +++ b/fmlcore/src/main/java/net/minecraftforge/fml/ModLoadingContext.java @@ -12,6 +12,7 @@ import net.minecraftforge.fml.loading.FMLLoader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.function.BiPredicate; import java.util.function.Supplier; public class ModLoadingContext @@ -50,6 +51,44 @@ public class ModLoadingContext getActiveContainer().registerExtensionPoint(point, extension); } + /** + * Register a {@link IExtensionPoint.DisplayTest} with the mod container. + *

A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)}.

+ * @param displayTest The {@link IExtensionPoint.DisplayTest} to register + */ + public void registerDisplayTest(IExtensionPoint.DisplayTest displayTest) { + getActiveContainer().registerDisplayTest(() -> displayTest); + } + + /** + * Register a {@link IExtensionPoint.DisplayTest} with the mod container. + *

A shorthand for registering a DisplayTest supplier with {@link #registerExtensionPoint(Class, Supplier)}.

+ * @param displayTest The {@link Supplier} to register + */ + public void registerDisplayTest(Supplier displayTest) { + getActiveContainer().registerDisplayTest(displayTest); + } + + /** + * Register a {@link IExtensionPoint.DisplayTest} with the mod container. + *

A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)} that also + * creates the DisplayTest instance for you using the provided parameters.

+ * @see IExtensionPoint.DisplayTest#DisplayTest(String, BiPredicate) + */ + public void registerDisplayTest(String version, BiPredicate remoteVersionTest) { + getActiveContainer().registerDisplayTest(new IExtensionPoint.DisplayTest(version, remoteVersionTest)); + } + + /** + * Register a {@link IExtensionPoint.DisplayTest} with the mod container. + *

A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)} that also + * creates the DisplayTest instance for you using the provided parameters.

+ * @see IExtensionPoint.DisplayTest#DisplayTest(Supplier, BiPredicate) + */ + public void registerDisplayTest(Supplier suppliedVersion, BiPredicate remoteVersionTest) { + getActiveContainer().registerDisplayTest(new IExtensionPoint.DisplayTest(suppliedVersion, remoteVersionTest)); + } + public void registerConfig(ModConfig.Type type, IConfigSpec spec) { if (spec.isEmpty()) {