malte0811 4 жил өмнө
parent
commit
4b970b7229

+ 74 - 30
src/main/java/malte0811/ferritecore/mixin/config/FerriteConfig.java

@@ -1,35 +1,45 @@
 package malte0811.ferritecore.mixin.config;
 
-import com.electronwill.nightconfig.core.Config;
 import com.electronwill.nightconfig.core.ConfigSpec;
 import com.electronwill.nightconfig.core.file.CommentedFileConfig;
 import com.electronwill.nightconfig.core.io.ParsingException;
 
+import javax.annotation.Nullable;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
 
 public class FerriteConfig {
-    private static final String NEIGHBOR_LOOKUP = "replaceNeighborLookup";
-    private static final String PROPERTY_MAP = "replacePropertyMap";
-    private static final String PREDICATES = "cacheMultipartPredicates";
-    private static final String MRL_CACHE = "modelResourceLocations";
-    private static final Config CONFIG;
+    public static final Option NEIGHBOR_LOOKUP;
+    public static final Option PROPERTY_MAP;
+    public static final Option PREDICATES;
+    public static final Option MRL_CACHE;
 
     static {
-        ConfigSpec spec = new ConfigSpec();
-        spec.define(NEIGHBOR_LOOKUP, true);
-        spec.define(PROPERTY_MAP, true);
-        spec.define(PREDICATES, true);
-        spec.define(MRL_CACHE, true);
-        CommentedFileConfig configData = read(Paths.get("config", "ferritecore-mixin.toml"));
-        configData.setComment(NEIGHBOR_LOOKUP, "Replace the blockstate neighbor table");
-        configData.setComment(PROPERTY_MAP, "Do not store the properties of a state explicitly and read them" +
-                "from the replace neighbor table instead. Requires " + NEIGHBOR_LOOKUP + " to be enabled");
-        configData.setComment(PREDICATES, "Cache the predicate instances used in multipart models");
-        configData.setComment(MRL_CACHE, "Avoid creation of new strings when creating ModelResourceLocations");
-        spec.correct(configData);
-        configData.save();
-        CONFIG = configData;
+        ConfigBuilder builder = new ConfigBuilder();
+        NEIGHBOR_LOOKUP = builder.createOption("replaceNeighborLookup", "Replace the blockstate neighbor table");
+        PROPERTY_MAP = builder.createOption(
+                "replacePropertyMap",
+                "Do not store the properties of a state explicitly and read them" +
+                        "from the replace neighbor table instead. Requires " + NEIGHBOR_LOOKUP.getName() + " to be enabled"
+        );
+        PREDICATES = builder.createOption(
+                "cacheMultipartPredicates",
+                "Cache the predicate instances used in multipart models"
+        );
+        MRL_CACHE = builder.createOption(
+                "modelResourceLocations",
+                "Avoid creation of new strings when creating ModelResourceLocations"
+        );
+        builder.finish();
+        if (PROPERTY_MAP.isEnabled() && !NEIGHBOR_LOOKUP.isEnabled()) {
+            throw new IllegalStateException(
+                    PROPERTY_MAP.getName() + " is enabled in the FerriteCore config, but " +
+                            NEIGHBOR_LOOKUP.getName() + " is not. This is not supported!"
+            );
+        }
     }
 
     private static CommentedFileConfig read(Path configPath) {
@@ -45,19 +55,53 @@ public class FerriteConfig {
         return configData;
     }
 
-    public static boolean replaceNeighborTable() {
-        return CONFIG.get(NEIGHBOR_LOOKUP);
-    }
+    public static class ConfigBuilder {
+        private final List<Option> options = new ArrayList<>();
 
-    public static boolean cachePredicates() {
-        return CONFIG.get(PREDICATES);
-    }
+        public Option createOption(String name, String comment) {
+            Option result = new Option(name, comment);
+            options.add(result);
+            return result;
+        }
 
-    public static boolean noPropertyState() {
-        return CONFIG.<Boolean>get(PROPERTY_MAP) && CONFIG.<Boolean>get(NEIGHBOR_LOOKUP);
+        public void finish() {
+            ConfigSpec spec = new ConfigSpec();
+            for (Option o : options) {
+                spec.define(o.getName(), true);
+            }
+            CommentedFileConfig configData = read(Paths.get("config", "ferritecore-mixin.toml"));
+            for (Option o : options) {
+                configData.setComment(o.getName(), o.getComment());
+            }
+            spec.correct(configData);
+            configData.save();
+            for (Option o : options) {
+                o.value = configData.get(o.getName());
+            }
+        }
     }
 
-    public static boolean optimizeMRL() {
-        return CONFIG.get(MRL_CACHE);
+    public static class Option {
+        private final String name;
+        private final String comment;
+        @Nullable
+        private Boolean value;
+
+        public Option(String name, String comment) {
+            this.name = name;
+            this.comment = comment;
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public String getComment() {
+            return comment;
+        }
+
+        public boolean isEnabled() {
+            return Objects.requireNonNull(value);
+        }
     }
 }

+ 1 - 1
src/main/java/malte0811/ferritecore/mixin/fastmap/Config.java

@@ -14,6 +14,6 @@ public class Config extends FerriteMixinConfig {
 
     @Override
     protected boolean isEnabled(String mixin) {
-        return FerriteConfig.replaceNeighborTable();
+        return FerriteConfig.NEIGHBOR_LOOKUP.isEnabled();
     }
 }

+ 1 - 1
src/main/java/malte0811/ferritecore/mixin/mrl/Config.java

@@ -14,6 +14,6 @@ public class Config extends FerriteMixinConfig {
 
     @Override
     protected boolean isEnabled(String mixin) {
-        return FerriteConfig.optimizeMRL();
+        return FerriteConfig.MRL_CACHE.isEnabled();
     }
 }

+ 1 - 1
src/main/java/malte0811/ferritecore/mixin/nopropertymap/Config.java

@@ -18,7 +18,7 @@ public class Config extends FerriteMixinConfig {
 
     @Override
     protected boolean isEnabled(String mixin) {
-        return FerriteConfig.noPropertyState() != DUMMY_MIXIN.equals(mixin);
+        return FerriteConfig.PROPERTY_MAP.isEnabled() != DUMMY_MIXIN.equals(mixin);
     }
 
     @Override

+ 1 - 1
src/main/java/malte0811/ferritecore/mixin/predicates/Config.java

@@ -14,6 +14,6 @@ public class Config extends FerriteMixinConfig {
 
     @Override
     protected boolean isEnabled(String mixin) {
-        return FerriteConfig.cachePredicates();
+        return FerriteConfig.PREDICATES.isEnabled();
     }
 }