mirror of
https://github.com/GTNewHorizons/GTNHLib.git
synced 2026-11-26 14:53:14 +00:00
Add @Config.{RequiresMod, RequiresModOr and RequiresModAnd} to the config system (#334)
This commit is contained in:
1 parent
09c0d95ed2
commit
71cd464286
4 files changed
+138
-24
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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.
|
||||
* <p>
|
||||
* A single mod: {@code @Config.RequiresMod("modid")}<br>
|
||||
* Any of several mods (OR): {@code @Config.RequiresMod({"modA", "modB"})}<br>
|
||||
* 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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, Integer> fieldOrder = new HashMap<>();
|
||||
final Map<String, ConfigNode> children = new HashMap<>();
|
||||
public final int order;
|
||||
public final String[] requiredModsOr;
|
||||
public final String[] requiredModsAnd;
|
||||
public final Map<String, ConfigNode> 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()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> implements IConfigElement<T> {
|
||||
|
||||
private final IConfigElement<T> 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<T> proxied, Runnable onUpdate, ConfigurationManager.ConfigNode node,
|
||||
int order) {
|
||||
public IConfigElementProxy(IConfigElement<T> 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<T> implements IConfigElement<T> {
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return order;
|
||||
return node.order;
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@@ -70,10 +70,9 @@ public class IConfigElementProxy<T> implements IConfigElement<T> {
|
||||
List<IConfigElement> 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<T> implements IConfigElement<T> {
|
||||
|
||||
@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
|
||||
|
||||
Reference in new issue
Block a user