Skip to content

Commit

Permalink
feat: DisplayTest in mods.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
SrRapero720 committed Nov 30, 2023
1 parent 09f1b8d commit 73b887c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
8 changes: 8 additions & 0 deletions mdk/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ logoFile="examplemod.png" #optional
credits="Thanks for this example mod goes to Java" #optional
# A text field displayed in the mod UI
authors="Love, Cheese and small house plants" #optional
# Display Test controls the display for your mod in the server connection screen
# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod.
# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod.
# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component.
# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value.
# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself.
#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional)

# The description text for the mod (multi line!) (#mandatory)
description='''
This is a long form description of the mod. You can write whatever you want here
Expand Down
37 changes: 34 additions & 3 deletions src/main/java/net/minecraftforge/fml/ModContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.IModBusEvent;
import net.minecraftforge.fml.network.FMLNetworkConstants;
import net.minecraftforge.forgespi.language.IConfigurable;
import net.minecraftforge.forgespi.language.IModInfo;
import org.apache.commons.lang3.tuple.Pair;

Expand All @@ -20,6 +22,7 @@
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;

Expand Down Expand Up @@ -56,9 +59,37 @@ public ModContainer(IModInfo info)
this.namespace = this.modId;
this.modInfo = info;
this.modLoadingStage = ModLoadingStage.CONSTRUCT;
// default displaytest extension checks for version string match
registerExtensionPoint(ExtensionPoint.DISPLAYTEST, () -> Pair.of(()->this.modInfo.getVersion().toString(),
(incoming, isNetwork)->Objects.equals(incoming, this.modInfo.getVersion().toString())));

// SPI doesn't give config
IConfigurable configurable = (IConfigurable) info;
final String displayTestOption = configurable.<String>getConfigElement("displayTest").orElse("MATCH_VERSION"); // missing defaults to DEFAULT type
Supplier<Pair<Supplier<String>, BiPredicate<String, Boolean>>> displayTest;
switch (displayTestOption) {
case "MATCH_VERSION": // default displaytest checks for version string match
displayTest = () -> Pair.of(() -> this.modInfo.getVersion().toString(),
(incoming, isNetwork) -> Objects.equals(incoming, this.modInfo.getVersion().toString()));
break;

case "IGNORE_SERVER_VERSION": // Ignores any version information coming from the server - use for server only
displayTest = () -> Pair.of(() -> FMLNetworkConstants.IGNORESERVERONLY, (incoming, isNetwork) -> true);
break;

case "IGNORE_ALL_VERSION": // Ignores all information and provides no information
displayTest = () -> Pair.of(() -> "", (incoming, isNetwork) -> true);
break;

case "NONE":// NO display test at all - use this if you're going to do your own display test
displayTest = null;
break;

default:// any other value throws an exception
throw new IllegalArgumentException("Invalid displayTest value supplied in mods.toml");
}

if (displayTest != null)
registerExtensionPoint(ExtensionPoint.DISPLAYTEST, displayTest);
else
extensionPoints.remove(ExtensionPoint.DISPLAYTEST);
}

/**
Expand Down

0 comments on commit 73b887c

Please sign in to comment.