diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/ExampleConfig.java b/src/main/java/com/gtnewhorizon/gtnhlib/ExampleConfig.java index a83706ec..1f80c268 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/ExampleConfig.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/ExampleConfig.java @@ -35,9 +35,9 @@ public class ExampleConfig { @Config.Entry(RedCategory.class) @Config.Order(0) // the ordering happens right after SortedCategory so it will be on top - public static testCategory redCategory = new testCategory(); + public static TestCategory redCategory = new TestCategory(); - public static class testCategory { + public static class TestCategory { @Config.Comment("This category button should have RED text.") @Config.Entry(RedCategory.class) @@ -68,6 +68,29 @@ public class ExampleConfig { public boolean yellowBoolean = true; } + @Config.Comment("Only visible in GUI if 'Hodgepodge' mod is loaded.") + @Config.RequiresMod("hodgepodge") + public static boolean hodgepodgeIntegration = true; + + @Config.Comment("Only visible in GUI if 'nei' or 'jei' is loaded.") + @Config.RequiresMod({ "nei", "jei" }) + public static boolean itemSearchIntegration = false; + + @Config.Comment("This entire category is hidden unless 'thaumcraft' is loaded.") + @Config.RequiresMod(value = { "thaumcraft", "thaumicenergistics" }, mode = Config.RequiresMod.Mode.AND) + @Config.Order(1) + public static ThaumcraftCategory thaumcraft = new ThaumcraftCategory(); + + public static class ThaumcraftCategory { + + @Config.Comment("Enable Thaumcraft research tweaks.") + public boolean researchTweaks = true; + + @Config.Comment("Only visible if 'thaumicbases' is also present.") + @Config.RequiresMod("thaumicbases") + public boolean thaumicBasesCompat = false; + } + public static class SortedCategory extends GuiConfigEntries.CategoryEntry { public SortedCategory(GuiConfig owningScreen, GuiConfigEntries owningEntryList, IConfigElement configElement) { diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java b/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java index 1904994e..0d5f97f8 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/config/Config.java @@ -276,4 +276,29 @@ public @interface Config { int value() default Integer.MAX_VALUE; } + /** + * Hides this field or category from the config GUI if the required mods are not loaded. The field is still loaded + * from and saved to the config file regardless. + *

+ * A single mod: {@code @Config.RequiresMod("modid")}
+ * Any of several mods (OR): {@code @Config.RequiresMod({"modA", "modB"})}
+ * All of several mods (AND): {@code @Config.RequiresMod(value = {"modA", "modB"}, mode = + * Config.RequiresMod.Mode.AND)} + */ + @Retention(RetentionPolicy.RUNTIME) + @Target({ ElementType.FIELD, ElementType.TYPE }) + @interface RequiresMod { + + String[] value(); + + Mode mode() default Mode.OR; + + enum Mode { + /** Hidden unless at least one of the specified mods is loaded. */ + OR, + /** Hidden unless all of the specified mods are loaded. */ + AND + } + } + } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java b/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java index f5c0ff54..4aa83b87 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/config/ConfigurationManager.java @@ -392,9 +392,6 @@ public class ConfigurationManager { ConfigNode rootNode = configNode.get(configClass); ConfigNode node = rootNode.children.getOrDefault(element.getName().toLowerCase(), rootNode); - Config.Order orderAnn = configClass.getAnnotation(Config.Order.class); - int order = orderAnn != null ? orderAnn.value() : Integer.MAX_VALUE; - return new IConfigElementProxy(element, () -> { try { processConfigInternal(configClass, category, rawConfig, null); @@ -403,7 +400,7 @@ public class ConfigurationManager { | ConfigException e) { e.printStackTrace(); } - }, node, order); + }, node); } private static String getLangKey(Class configClass, @Nullable Config.LangKey langKey, @Nullable String fieldName, @@ -609,18 +606,63 @@ public class ConfigurationManager { public static class ConfigNode { - final Map fieldOrder = new HashMap<>(); - final Map children = new HashMap<>(); + public final int order; + public final String[] requiredModsOr; + public final String[] requiredModsAnd; + public final Map children = new HashMap<>(); public ConfigNode(Class configClass) { + Config.Order orderAnn = configClass.getAnnotation(Config.Order.class); + Config.RequiresMod reqAnn = configClass.getAnnotation(Config.RequiresMod.class); + + this.order = orderAnn != null ? orderAnn.value() : Integer.MAX_VALUE; + + if (reqAnn != null && reqAnn.mode() == Config.RequiresMod.Mode.OR) { + this.requiredModsOr = reqAnn.value(); + this.requiredModsAnd = null; + } else if (reqAnn != null && reqAnn.mode() == Config.RequiresMod.Mode.AND) { + this.requiredModsOr = null; + this.requiredModsAnd = reqAnn.value(); + } else { + this.requiredModsOr = null; + this.requiredModsAnd = null; + } + + buildChildren(configClass); + } + + public ConfigNode(int order, @Nullable String[] requiredModsOr, @Nullable String[] requiredModsAnd, + @Nullable Class categoryClass) { + this.order = order; + this.requiredModsOr = requiredModsOr; + this.requiredModsAnd = requiredModsAnd; + if (categoryClass != null) buildChildren(categoryClass); + } + + private void buildChildren(Class configClass) { for (Field field : configClass.getDeclaredFields()) { - if (shouldSkipField(field)) { - continue; - } + if (shouldSkipField(field)) continue; String fieldName = ConfigFieldParser.getFieldName(field).toLowerCase(); - Config.Order ann = field.getAnnotation(Config.Order.class); - if (ann != null) fieldOrder.put(fieldName, ann.value()); - if (ConfigurationManager.isFieldSubCategory(field)) { + Config.Order orderAnn = field.getAnnotation(Config.Order.class); + int fieldOrder = orderAnn != null ? orderAnn.value() : Integer.MAX_VALUE; + + Config.RequiresMod reqAnn = field.getAnnotation(Config.RequiresMod.class); + String[] fieldModsOr = null; + String[] fieldModsAnd = null; + + if (reqAnn != null) { + if (reqAnn.mode() == Config.RequiresMod.Mode.OR) { + fieldModsOr = reqAnn.value(); + } else if (reqAnn.mode() == Config.RequiresMod.Mode.AND) { + fieldModsAnd = reqAnn.value(); + } + } + + if (!ConfigurationManager.isFieldSubCategory(field)) { + children.put(fieldName, new ConfigNode(fieldOrder, fieldModsOr, fieldModsAnd, null)); + } else if (orderAnn != null || reqAnn != null) { + children.put(fieldName, new ConfigNode(fieldOrder, fieldModsOr, fieldModsAnd, field.getType())); + } else { children.put(fieldName, new ConfigNode(field.getType())); } } diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/config/IConfigElementProxy.java b/src/main/java/com/gtnewhorizon/gtnhlib/config/IConfigElementProxy.java index 1c6c95ff..3fa5aeb0 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/config/IConfigElementProxy.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/config/IConfigElementProxy.java @@ -8,20 +8,20 @@ import cpw.mods.fml.client.config.ConfigGuiType; import cpw.mods.fml.client.config.GuiConfigEntries; import cpw.mods.fml.client.config.GuiEditArrayEntries; import cpw.mods.fml.client.config.IConfigElement; +import cpw.mods.fml.common.Loader; public class IConfigElementProxy implements IConfigElement { private final IConfigElement proxied; private final Runnable onUpdate; private final ConfigurationManager.ConfigNode node; - private final int order; + private Boolean requiredModsOrVisible = null; + private Boolean requiredModsAndVisible = null; - public IConfigElementProxy(IConfigElement proxied, Runnable onUpdate, ConfigurationManager.ConfigNode node, - int order) { + public IConfigElementProxy(IConfigElement proxied, Runnable onUpdate, ConfigurationManager.ConfigNode node) { this.proxied = proxied; this.onUpdate = onUpdate; this.node = node; - this.order = order; } @Override @@ -61,7 +61,7 @@ public class IConfigElementProxy implements IConfigElement { } public int getOrder() { - return order; + return node.order; } @SuppressWarnings("rawtypes") @@ -70,10 +70,9 @@ public class IConfigElementProxy implements IConfigElement { List elements = new ArrayList<>(); for (IConfigElement element : proxied.getChildElements()) { String name = element.getName().toLowerCase(); - if (node != null) { - ConfigurationManager.ConfigNode childNode = node.children.get(name); - int childOrder = node.fieldOrder.getOrDefault(name, Integer.MAX_VALUE); - elements.add(new IConfigElementProxy<>(element, onUpdate, childNode, childOrder)); + ConfigurationManager.ConfigNode childNode = node.children.get(name); + if (childNode != null) { + elements.add(new IConfigElementProxy<>(element, onUpdate, childNode)); } } return elements; @@ -126,7 +125,32 @@ public class IConfigElementProxy implements IConfigElement { @Override public boolean showInGui() { - return proxied.showInGui(); + if (!proxied.showInGui()) return false; + + if (node.requiredModsOr != null && node.requiredModsOr.length > 0) { + if (requiredModsOrVisible == null) { + requiredModsOrVisible = isVisible(node.requiredModsOr, false); + } + if (!requiredModsOrVisible) return false; + } + + if (node.requiredModsAnd != null && node.requiredModsAnd.length > 0) { + if (requiredModsAndVisible == null) { + requiredModsAndVisible = isVisible(node.requiredModsAnd, true); + } + if (!requiredModsAndVisible) return false; + } + + return true; + } + + private boolean isVisible(String[] mods, boolean requireAll) { + + for (String mod : mods) { + if (Loader.isModLoaded(mod) != requireAll) return !requireAll; + } + + return requireAll; } @Override