Allow Entries to be conditionally shown

- Define a required mod using `requiredMod = "modid"` in entries & comments
This commit is contained in:
Martin Prokoph
2024-10-11 00:08:45 +02:00
parent 765ad60d0b
commit 07e6049fa0
4 changed files with 28 additions and 14 deletions

View File

@@ -92,7 +92,6 @@ public abstract class MidnightConfig {
EntryInfo info = new EntryInfo();
if ((field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class)) && !field.isAnnotationPresent(Server.class) && !field.isAnnotationPresent(Hidden.class) && PlatformFunctions.isClientEnv())
initClient(modid, field, info);
if (field.isAnnotationPresent(Comment.class)) info.centered = field.getAnnotation(Comment.class).centered();
if (field.isAnnotationPresent(Entry.class))
try { info.defaultValue = field.get(null);
} catch (IllegalAccessException ignored) {}
@@ -111,10 +110,14 @@ public abstract class MidnightConfig {
private static void initClient(String modid, Field field, EntryInfo info) {
info.dataType = getUnderlyingType(field);
Entry e = field.getAnnotation(Entry.class);
Comment c = field.getAnnotation(Comment.class);
info.width = e != null ? e.width() : 0;
info.field = field; info.modid = modid;
boolean requiredModLoaded = true;
if (e != null) {
if (!e.requiredMod().isEmpty()) requiredModLoaded = PlatformFunctions.isModLoaded(e.requiredMod());
if (!requiredModLoaded) return;
if (!e.name().isEmpty()) info.name = Text.translatable(e.name());
if (info.dataType == int.class) textField(info, Integer::parseInt, INTEGER_ONLY, (int) e.min(), (int) e.max(), true);
else if (info.dataType == float.class) textField(info, Float::parseFloat, DECIMAL_ONLY, (float) e.min(), (float) e.max(), false);
@@ -132,8 +135,11 @@ public abstract class MidnightConfig {
int index = values.indexOf(info.value) + 1;
info.value = values.get(index >= values.size() ? 0 : index); button.setMessage(func.apply(info.value));
}, func);
}}
entries.add(info);
}} else if (c != null) {
if (!c.requiredMod().isEmpty()) requiredModLoaded = PlatformFunctions.isModLoaded(c.requiredMod());
info.centered = c.centered();
}
if (requiredModLoaded) entries.add(info);
}
public static Class<?> getUnderlyingType(Field field) {
if (field.getType() == List.class) {
@@ -496,6 +502,7 @@ public abstract class MidnightConfig {
boolean isSlider() default false;
int precision() default 100;
String category() default "default";
String requiredMod() default "";
}
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Client {}
@@ -504,6 +511,7 @@ public abstract class MidnightConfig {
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface Comment {
boolean centered() default false;
String category() default "default";
String requiredMod() default "";
}
public static class HiddenAnnotationExclusionStrategy implements ExclusionStrategy {

View File

@@ -21,6 +21,7 @@ configurations {
runtimeClasspath.extendsFrom common
developmentFabric.extendsFrom common
archivesBaseName = rootProject.archives_base_name + "-fabric"
version = rootProject.mod_version + "+" + rootProject.minecraft_version
}
dependencies {
@@ -33,10 +34,10 @@ dependencies {
}
processResources {
inputs.property "version", project.version
inputs.property "version", rootProject.version
filesMatching("fabric.mod.json") {
expand "version": project.version
expand "version": rootProject.version
}
}
@@ -66,7 +67,7 @@ components.java {
unifiedPublishing {
project {
displayName = "MidnightLib $project.version - Fabric $project.minecraft_version"
displayName = "MidnightLib $rootProject.version - Fabric $project.minecraft_version"
releaseType = "$project.release_type"
changelog = releaseChangelog()
gameVersions = []
@@ -93,7 +94,7 @@ unifiedPublishing {
modrinth {
token = MODRINTH_TOKEN
id = rootProject.modrinth_id
version = "$project.version-$project.name"
version = "$rootProject.version-$project.name"
gameVersions.addAll project.minecraft_version, project.supported_versions
}
}

View File

@@ -35,6 +35,8 @@ configurations {
canBeResolved = true
canBeConsumed = false
}
archivesBaseName = rootProject.archives_base_name + "-neoforge"
version = rootProject.mod_version + "+" + rootProject.minecraft_version
}
dependencies {
@@ -45,10 +47,10 @@ dependencies {
}
processResources {
inputs.property 'version', project.version
inputs.property 'version', rootProject.version
filesMatching('META-INF/neoforge.mods.toml') {
expand version: project.version
expand version: rootProject.version
}
}
@@ -75,7 +77,7 @@ components.java {
unifiedPublishing {
project {
displayName = "MidnightLib $project.version - NeoForge $project.minecraft_version"
displayName = "MidnightLib $rootProject.version - NeoForge $project.minecraft_version"
releaseType = "$project.release_type"
changelog = releaseChangelog()
gameVersions = []
@@ -96,7 +98,7 @@ unifiedPublishing {
modrinth {
token = MODRINTH_TOKEN
id = rootProject.modrinth_id
version = "$project.version-$project.name"
version = "$rootProject.version-$project.name"
gameVersions.addAll project.minecraft_version, project.supported_versions
}
}

View File

@@ -2,7 +2,6 @@ package eu.midnightdust.neoforge;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import eu.midnightdust.core.MidnightLib;
import eu.midnightdust.lib.config.AutoCommand;
import eu.midnightdust.lib.config.MidnightConfig;
import net.minecraft.server.command.ServerCommandSource;
import net.neoforged.api.distmarker.Dist;
@@ -16,6 +15,7 @@ import net.neoforged.neoforge.client.gui.IConfigScreenFactory;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.List;
@Mod("midnightlib")
@@ -32,7 +32,7 @@ public class MidnightLibNeoForge {
@SubscribeEvent
public static void onPostInit(FMLClientSetupEvent event) {
ModList.get().forEachModContainer((modid, modContainer) -> {
if (MidnightConfig.configClass.containsKey(modid)) {
if (MidnightConfig.configClass.containsKey(modid) && !MidnightLib.hiddenMods.contains(modid)) {
modContainer.registerExtensionPoint(IConfigScreenFactory.class, (minecraftClient, screen) -> MidnightConfig.getScreen(screen, modid));
}
});
@@ -43,7 +43,10 @@ public class MidnightLibNeoForge {
public static class MidnightLibEvents {
@SubscribeEvent
public static void registerCommands(RegisterCommandsEvent event) {
try {
commands.forEach(command -> event.getDispatcher().register(command));
}
catch (ConcurrentModificationException ignored) {}
}
}
}