Prepare Configs that enable us to derive a ConfigScreen

This commit is contained in:
Frank 2021-08-20 14:15:21 +02:00
parent 8d6dc18ce2
commit fe7e1aa28d
9 changed files with 238 additions and 81 deletions

View file

@ -3,6 +3,8 @@ package ru.bclib.config;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import ru.bclib.BCLib;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.ClientConfig;
import ru.bclib.api.dataexchange.handler.autosync.AutoSync.ServerConfig;
public class Configs {
public static final PathConfig GENERATOR_CONFIG = new PathConfig(BCLib.MOD_ID, "generator");
@ -12,8 +14,8 @@ public class Configs {
public static final PathConfig RECIPE_CONFIG = new PathConfig(BCLib.MOD_ID, "recipes");
@Environment(EnvType.CLIENT)
public static final PathConfig CLIENT_CONFIG = new PathConfig(BCLib.MOD_ID, "client", false);
public static final PathConfig SERVER_CONFIG = new PathConfig(BCLib.MOD_ID, "server", false);
public static final ClientConfig CLIENT_CONFIG = new ClientConfig();
public static final ServerConfig SERVER_CONFIG = new ServerConfig();
public static void save() {
MAIN_CONFIG.saveChanges();

View file

@ -1,2 +1,118 @@
package ru.bclib.config;public class NamedPathConfig {
package ru.bclib.config;
import net.minecraft.resources.ResourceLocation;
import ru.bclib.BCLib;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
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 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 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 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;}
}
ConfigToken(String entry, String... path) { super(entry, path); }
ConfigToken(String entry, ResourceLocation path) { super(entry, path); }
}
public NamedPathConfig(String modID, String group, boolean autoSync, boolean diffContent) {
super(modID, group, autoSync, diffContent);
onInit();
}
public NamedPathConfig(String modID, String group, boolean autoSync) {
super(modID, group, autoSync);
onInit();
}
public NamedPathConfig(String modID, String group) {
super(modID, group);
onInit();
}
List<ConfigToken> getAllOptions(){
List<ConfigToken> res = new LinkedList<>();
for (Field fl : this.getClass().getDeclaredFields()){
int modifiers = fl.getModifiers();
if (Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && ConfigToken.class.isAssignableFrom(fl.getType())) {
try {
res.add((ConfigToken) fl.get(null));
}
catch (IllegalAccessException e) {
BCLib.LOGGER.error("Could not access " + fl);
}
}
}
return res;
}
protected void onInit(){
getAllOptions().forEach(e -> get(e));
this.saveChanges();
}
private void set(ConfigToken what, Object value) {
BCLib.LOGGER.error("Accessing " + what + " as general type is not supported.");
}
private Object get(ConfigToken what){
BCLib.LOGGER.error("Accessing " + what + " as general type is not supported.");
return null;
}
public void set(ConfigToken.Int what, int value) {
this.setInt(what, value);
}
public int get(ConfigToken.Int what){
return this.getInt(what, what.defaultValue);
}
public void set(ConfigToken.Bool what, boolean value) {
this.setBoolean(what, value);
}
public boolean get(ConfigToken.Bool what){
return this.getBoolean(what, what.defaultValue);
}
public void set(ConfigToken.Str what, String value) {
this.setString(what, value);
}
public String get(ConfigToken.Str what){
return this.getString(what, what.defaultValue);
}
public void set(ConfigToken.Float what, float value) {
this.setFloat(what, value);
}
public float get(ConfigToken.Float what){
return this.getFloat(what, what.defaultValue);
}
}

View file

@ -22,11 +22,11 @@ public class PathConfig extends Config {
@Override
protected void registerEntries() {}
protected ConfigKey createKey(String category, String key) {
protected static ConfigKey createKey(String category, String key) {
return new ConfigKey(key, category.split("\\."));
}
protected ConfigKey createKey(String key) {
protected static ConfigKey createKey(String key) {
return createKey("", key);
}