From 0adc5024f1c4d181c4398aeb2518ae37d5182953 Mon Sep 17 00:00:00 2001 From: Frank Date: Sat, 21 Aug 2021 09:49:15 +0200 Subject: [PATCH] Adding `enabled` predicate --- .../java/ru/bclib/config/NamedPathConfig.java | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/main/java/ru/bclib/config/NamedPathConfig.java b/src/main/java/ru/bclib/config/NamedPathConfig.java index ff92c67d..7de65025 100644 --- a/src/main/java/ru/bclib/config/NamedPathConfig.java +++ b/src/main/java/ru/bclib/config/NamedPathConfig.java @@ -11,34 +11,48 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.LinkedList; import java.util.List; +import java.util.function.Predicate; public class NamedPathConfig extends PathConfig{ - public abstract static class ConfigToken extends ConfigKey{ - public static class Int extends ConfigToken{ - public final int defaultValue; - public Int(int def, String entry, String... path) { super(entry, path); this.defaultValue=def;} - public Int(int def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;} + public abstract static class ConfigToken extends ConfigKey{ + public static class Int extends ConfigToken{ + public Int(int def, String entry, ResourceLocation path) { this(def, entry, path.getNamespace(), path.getPath());} + public Int(int def, String entry, String... path) { super(def, entry, path);} + } - public static class Float extends ConfigToken{ - public final float defaultValue; - public Float(float def, String entry, String... path) { super(entry, path); this.defaultValue=def;} - public Float(float def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;} + public static class Float extends ConfigToken{ + public Float(float def, String entry, ResourceLocation path) { this(def, entry, path.getNamespace(), path.getPath());} + public Float(float def, String entry, String... path) { super(def, entry, path); } } - public static class Bool extends ConfigToken{ - public final boolean defaultValue; - public Bool(boolean def, String entry, String... path) { super(entry, path); this.defaultValue=def;} - public Bool(boolean def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;} + public static class Bool extends ConfigToken{ + public Bool(boolean def, String entry, ResourceLocation path) { this(def, entry, path.getNamespace(), path.getPath());} + public Bool(boolean def, String entry, String... path) { super(def, entry, path); } } - public static class Str extends ConfigToken{ - public final String defaultValue; - public Str(String def, String entry, String... path) { super(entry, path); this.defaultValue=def;} - public Str(String def, String entry, ResourceLocation path) { super(entry, path); this.defaultValue=def;} + public static class Str extends ConfigToken{ + public Str(String def, String entry, ResourceLocation path) { this(def, entry, path.getNamespace(), path.getPath());} + public Str(String def, String entry, String... path) { super(def, entry, path); } } - ConfigToken(String entry, String... path) { super(entry, path); } - ConfigToken(String entry, ResourceLocation path) { super(entry, path); } + + public static final Predicate ALWAYS_ENABLED = (config) -> true; + + public final T defaultValue; + protected final Predicate enabled; + + ConfigToken(T defaultValue, String entry, ResourceLocation path) { this(defaultValue, entry, path.getNamespace(), path.getPath()); } + ConfigToken(T defaultValue, String entry, String... path) { this(defaultValue, ALWAYS_ENABLED, entry, path); } + + ConfigToken(T defaultValue, String entry, ResourceLocation path, Predicate enabled) { this(defaultValue, enabled, entry, path.getNamespace(), path.getPath()); } + ConfigToken(T defaultValue, String entry, String path, Predicate enabled) { this(defaultValue, enabled, entry, path); } + ConfigToken(T defaultValue, String entry, String[] path, Predicate enabled) { this(defaultValue, enabled, entry, path); } + private ConfigToken(T defaultValue, Predicate enabled, String entry, String... path) { + super(entry, path); + this.enabled = enabled; + this.defaultValue = defaultValue + } + } public NamedPathConfig(String modID, String group, boolean autoSync, boolean diffContent) {