mirror of
https://github.com/FalsePattern/FalsePatternLib.git
synced 2026-11-11 03:17:27 +00:00
added config initialization logic when the config wasn't present
This commit is contained in:
2 files changed
+16
-3
No files matched your search
@@ -175,10 +175,11 @@ public class ParsedConfiguration {
|
||||
maxFieldNameLength = Math.max(maxFieldNameLength, field.getName().length());
|
||||
val fieldClass = field.getType();
|
||||
val name = field.getName();
|
||||
AConfigField<?> configField;
|
||||
if (constructors.containsKey(fieldClass)) {
|
||||
fields.put(name, constructors.get(fieldClass).construct(field, rawConfig, category));
|
||||
fields.put(name, configField = constructors.get(fieldClass).construct(field, rawConfig, category));
|
||||
} else if (fieldClass.isEnum()) {
|
||||
fields.put(name, new EnumConfigField<>(field, rawConfig, category));
|
||||
fields.put(name, configField = new EnumConfigField<>(field, rawConfig, category));
|
||||
} else {
|
||||
throw new ConfigException("Illegal config field: " + field.getName() + " in " + configClass.getName() +
|
||||
": Unsupported type " + fieldClass.getName() +
|
||||
@@ -190,7 +191,7 @@ public class ParsedConfiguration {
|
||||
if (field.isAnnotationPresent(Config.RequiresWorldRestart.class)) {
|
||||
cat.setRequiresWorldRestart(true);
|
||||
}
|
||||
val configField = fields.get(name);
|
||||
configField.init();
|
||||
elements.computeIfAbsent(name, (name2) -> new ConfigElementProxy<>(configField.getProperty(), () -> {
|
||||
configField.load();
|
||||
save();
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
package com.falsepattern.lib.internal.impl.config.fields;
|
||||
|
||||
import com.falsepattern.lib.config.Config;
|
||||
import lombok.val;
|
||||
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
@@ -41,6 +42,7 @@ public abstract class AConfigField<T> {
|
||||
protected final Property.Type type;
|
||||
protected final Property property;
|
||||
protected final String comment;
|
||||
private boolean uninitialized;
|
||||
|
||||
protected AConfigField(Field field, Configuration configuration, String category, Property.Type type) {
|
||||
this(field, configuration, category, type, false);
|
||||
@@ -60,6 +62,8 @@ public abstract class AConfigField<T> {
|
||||
langKey =
|
||||
Optional.ofNullable(field.getAnnotation(Config.LangKey.class)).map(Config.LangKey::value).orElse(name);
|
||||
this.type = type;
|
||||
val cat = configuration.getCategory(category);
|
||||
uninitialized = !cat.containsKey(name);
|
||||
if (isList) {
|
||||
property = configuration.get(category, name, new String[0], comment, type);
|
||||
} else {
|
||||
@@ -99,6 +103,14 @@ public abstract class AConfigField<T> {
|
||||
putConfig(getDefault());
|
||||
}
|
||||
|
||||
public void init() {
|
||||
if (uninitialized) {
|
||||
uninitialized = false;
|
||||
putField(getDefault());
|
||||
putConfig(getDefault());
|
||||
}
|
||||
}
|
||||
|
||||
public Property getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user