Additional Typesafety
This commit is contained in:
parent
edb6631768
commit
38c01b8c76
2 changed files with 75 additions and 33 deletions
|
@ -32,15 +32,15 @@ public class AutoSync {
|
||||||
|
|
||||||
@Environment(EnvType.CLIENT)
|
@Environment(EnvType.CLIENT)
|
||||||
public static class ClientConfig extends NamedPathConfig{
|
public static class ClientConfig extends NamedPathConfig{
|
||||||
public static final ConfigToken<Boolean> ENABLED = new ConfigToken<Boolean>(true, "enabled", SYNC_CATEGORY);
|
public static final ConfigToken<Boolean> ENABLED = ConfigToken.Boolean(true, "enabled", SYNC_CATEGORY);
|
||||||
@ConfigUI(leftPadding =8)
|
@ConfigUI(leftPadding =8)
|
||||||
public static final ConfigToken<Boolean> ACCEPT_CONFIGS = new DependendConfigToken<Boolean>(true,"acceptConfigs", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
public static final DependendConfigToken<Boolean> ACCEPT_CONFIGS = DependendConfigToken.Boolean(true,"acceptConfigs", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
||||||
@ConfigUI(leftPadding =8)
|
@ConfigUI(leftPadding =8)
|
||||||
public static final ConfigToken<Boolean> ACCEPT_FILES = new DependendConfigToken<Boolean>(true,"acceptFiles", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
public static final DependendConfigToken<Boolean> ACCEPT_FILES = DependendConfigToken.Boolean(true,"acceptFiles", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
||||||
@ConfigUI(leftPadding =8)
|
@ConfigUI(leftPadding =8)
|
||||||
public static final ConfigToken<Boolean> ACCEPT_MODS = new DependendConfigToken<Boolean>(true,"acceptMods", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
public static final DependendConfigToken<Boolean> ACCEPT_MODS = DependendConfigToken.Boolean(true,"acceptMods", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
||||||
@ConfigUI(topPadding = 12)
|
@ConfigUI(topPadding = 12)
|
||||||
public static final ConfigToken<Boolean> DEBUG_HASHES = new ConfigToken<Boolean>(true, "debugHashes", SYNC_CATEGORY);
|
public static final ConfigToken<Boolean> DEBUG_HASHES = ConfigToken.Boolean(true, "debugHashes", SYNC_CATEGORY);
|
||||||
|
|
||||||
|
|
||||||
public ClientConfig(){
|
public ClientConfig(){
|
||||||
|
@ -56,23 +56,25 @@ public class AutoSync {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAcceptingMods() {
|
public boolean isAcceptingMods() {
|
||||||
return get(ACCEPT_MODS) && isAllowingAutoSync();
|
return get(ACCEPT_MODS) /*&& isAllowingAutoSync()*/;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAcceptingConfigs() {
|
public boolean isAcceptingConfigs() {
|
||||||
return get(ACCEPT_CONFIGS) && isAllowingAutoSync();
|
return get(ACCEPT_CONFIGS) /*&& isAllowingAutoSync()*/;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isAcceptingFiles() {
|
public boolean isAcceptingFiles() {
|
||||||
return get(ACCEPT_FILES) && isAllowingAutoSync();
|
return get(ACCEPT_FILES) /*&& isAllowingAutoSync()*/;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ServerConfig extends NamedPathConfig {
|
public static class ServerConfig extends NamedPathConfig {
|
||||||
public static final ConfigToken<Boolean> ENABLED = new ConfigToken<Boolean>(true, "enabled", SYNC_CATEGORY);
|
public static final ConfigToken<Boolean> ENABLED = ConfigToken.Boolean(true, "enabled", SYNC_CATEGORY);
|
||||||
public static final ConfigToken<Boolean> OFFER_CONFIGS = new ConfigToken<Boolean>(true,"offerConfigs", SYNC_CATEGORY);
|
public static final DependendConfigToken<Boolean> OFFER_CONFIGS = DependendConfigToken.Boolean(true,"offerConfigs", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
||||||
public static final ConfigToken<Boolean> OFFER_FILES = new ConfigToken<Boolean>(true,"offerFiles", SYNC_CATEGORY);
|
public static final DependendConfigToken<Boolean> OFFER_FILES = DependendConfigToken.Boolean(true,"offerFiles", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
||||||
public static final ConfigToken<Boolean> OFFER_MODS = new ConfigToken<Boolean>(true,"offerMods", SYNC_CATEGORY);
|
public static final DependendConfigToken<Boolean> OFFER_MODS = DependendConfigToken.Boolean(true,"offerMods", SYNC_CATEGORY, (config)->config.get(ENABLED));
|
||||||
|
|
||||||
|
public static final ConfigToken<List<String>> ADDITIONAL_MODS = ConfigToken.StringArray(new ArrayList<>(0),"additionalMods", SYNC_CATEGORY);
|
||||||
|
|
||||||
|
|
||||||
public ServerConfig(){
|
public ServerConfig(){
|
||||||
|
@ -84,15 +86,19 @@ public class AutoSync {
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOfferingConfigs() {
|
public boolean isOfferingConfigs() {
|
||||||
return get(OFFER_CONFIGS) && isAllowingAutoSync();
|
return get(OFFER_CONFIGS) /*&& isAllowingAutoSync()*/;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOfferingFiles() {
|
public boolean isOfferingFiles() {
|
||||||
return get(OFFER_FILES) && isAllowingAutoSync();
|
return get(OFFER_FILES) /*&& isAllowingAutoSync()*/;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOfferingMods() {
|
public boolean isOfferingMods() {
|
||||||
return get(OFFER_MODS) && isAllowingAutoSync();
|
return get(OFFER_MODS) /*&& isAllowingAutoSync()*/;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] additionalModsForSync() {
|
||||||
|
return new String[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,11 @@ package ru.bclib.config;
|
||||||
|
|
||||||
import net.minecraft.resources.ResourceLocation;
|
import net.minecraft.resources.ResourceLocation;
|
||||||
import ru.bclib.BCLib;
|
import ru.bclib.BCLib;
|
||||||
|
import ru.bclib.config.ConfigKeeper.BooleanEntry;
|
||||||
|
import ru.bclib.config.ConfigKeeper.FloatEntry;
|
||||||
|
import ru.bclib.config.ConfigKeeper.IntegerEntry;
|
||||||
|
import ru.bclib.config.ConfigKeeper.StringArrayEntry;
|
||||||
|
import ru.bclib.config.ConfigKeeper.StringEntry;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.Modifier;
|
import java.lang.reflect.Modifier;
|
||||||
|
@ -49,47 +54,68 @@ public class NamedPathConfig extends PathConfig{
|
||||||
public static class DependendConfigToken<T> extends ConfigToken<T>{
|
public static class DependendConfigToken<T> extends ConfigToken<T>{
|
||||||
protected final Predicate<NamedPathConfig> dependenciesTrue;
|
protected final Predicate<NamedPathConfig> dependenciesTrue;
|
||||||
|
|
||||||
public DependendConfigToken(T defaultValue, String entry, ResourceLocation path, Predicate<NamedPathConfig> dependenciesTrue) {
|
protected DependendConfigToken(Class<?> type, T defaultValue, String entry, ResourceLocation path, Predicate<NamedPathConfig> dependenciesTrue) {
|
||||||
this(defaultValue, entry, new String[]{path.getNamespace(), path.getPath()}, dependenciesTrue);
|
this(type, defaultValue, entry, new String[]{path.getNamespace(), path.getPath()}, dependenciesTrue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DependendConfigToken(T defaultValue, String entry, String path, Predicate<NamedPathConfig> dependenciesTrue) {
|
protected DependendConfigToken(Class<?> type, T defaultValue, String entry, String path, Predicate<NamedPathConfig> dependenciesTrue) {
|
||||||
super(defaultValue, entry, path);
|
super(type, defaultValue, entry, path);
|
||||||
this.dependenciesTrue = dependenciesTrue;
|
this.dependenciesTrue = dependenciesTrue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DependendConfigToken(T defaultValue, String entry, String[] path, Predicate<NamedPathConfig> dependenciesTrue) {
|
protected DependendConfigToken(Class<?> type, T defaultValue, String entry, String[] path, Predicate<NamedPathConfig> dependenciesTrue) {
|
||||||
super(defaultValue, entry, path);
|
super(type, defaultValue, entry, path);
|
||||||
this.dependenciesTrue = dependenciesTrue;
|
this.dependenciesTrue = dependenciesTrue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dependenciesTrue(NamedPathConfig config){
|
public boolean dependenciesTrue(NamedPathConfig config){
|
||||||
return dependenciesTrue.test(config);
|
return dependenciesTrue.test(config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static DependendConfigToken<Boolean> Boolean(boolean defaultValue, String entry, String path, Predicate<NamedPathConfig> dependenciesTrue) {
|
||||||
|
return new DependendConfigToken<Boolean>(BooleanEntry.class, defaultValue, entry, path, dependenciesTrue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ConfigToken <T> extends ConfigKey{
|
public static class ConfigToken <T> extends ConfigKey{
|
||||||
public final T defaultValue;
|
public final T defaultValue;
|
||||||
public final Class<T> type;
|
public final Class<?> type;
|
||||||
|
|
||||||
public ConfigToken(T defaultValue, String entry, ResourceLocation path) {
|
protected ConfigToken(Class<?> type, T defaultValue, String entry, ResourceLocation path) {
|
||||||
this(defaultValue, entry, path.getNamespace(), path.getPath());
|
this(type, defaultValue, entry, path.getNamespace(), path.getPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public ConfigToken(T defaultValue, String entry, String... path) {
|
protected ConfigToken(Class<?> type, T defaultValue, String entry, String... path) {
|
||||||
super(entry, path);
|
super(entry, path);
|
||||||
this.defaultValue = defaultValue;
|
this.defaultValue = defaultValue;
|
||||||
|
|
||||||
if (defaultValue == null){
|
this.type = type;
|
||||||
BCLib.LOGGER.error("[Internal Error] defaultValue should not be null (" +this.getEntry() +")");
|
|
||||||
}
|
|
||||||
this.type = defaultValue!=null?(Class<T>)defaultValue.getClass():(Class<T>)Object.class;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean dependenciesTrue(NamedPathConfig config){
|
public boolean dependenciesTrue(NamedPathConfig config){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ConfigToken<Boolean> Boolean(boolean defaultValue, String entry, String path) {
|
||||||
|
return new ConfigToken<Boolean>(BooleanEntry.class, defaultValue, entry, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConfigToken<Integer> Int(int defaultValue, String entry, String path) {
|
||||||
|
return new ConfigToken<Integer>(IntegerEntry.class, defaultValue, entry, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConfigToken<Float> Float(float defaultValue, String entry, String path) {
|
||||||
|
return new ConfigToken<Float>(FloatEntry.class, defaultValue, entry, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConfigToken<String> String(String defaultValue, String entry, String path) {
|
||||||
|
return new ConfigToken<String>(StringEntry.class, defaultValue, entry, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ConfigToken<List<String>> StringArray(List<String> defaultValue, String entry, String path) {
|
||||||
|
return new ConfigToken<List<String>>(StringArrayEntry.class, defaultValue, entry, path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public NamedPathConfig(String modID, String group, boolean autoSync, boolean diffContent) {
|
public NamedPathConfig(String modID, String group, boolean autoSync, boolean diffContent) {
|
||||||
|
@ -157,18 +183,21 @@ public class NamedPathConfig extends PathConfig{
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private <T> T _get(ConfigToken<T> what, boolean raw){
|
private <T> T _get(ConfigToken<T> what, boolean raw){
|
||||||
//TODO: Check if we can make config fully Generic to avoid runtime type checks...
|
//TODO: Check if we can make config fully Generic to avoid runtime type checks...
|
||||||
if (Boolean.class.isAssignableFrom(what.type)){
|
if (BooleanEntry.class.isAssignableFrom(what.type)){
|
||||||
return (T)_getBoolean((ConfigToken<Boolean>)what, raw);
|
return (T)_getBoolean((ConfigToken<Boolean>)what, raw);
|
||||||
}
|
}
|
||||||
if (Integer.class.isAssignableFrom(what.type)){
|
if (IntegerEntry.class.isAssignableFrom(what.type)){
|
||||||
return (T)_getInt((ConfigToken<Integer>)what);
|
return (T)_getInt((ConfigToken<Integer>)what);
|
||||||
}
|
}
|
||||||
if (Float.class.isAssignableFrom(what.type)){
|
if (FloatEntry.class.isAssignableFrom(what.type)){
|
||||||
return (T)_getFloat((ConfigToken<Float>)what);
|
return (T)_getFloat((ConfigToken<Float>)what);
|
||||||
}
|
}
|
||||||
if (String.class.isAssignableFrom(what.type)){
|
if (StringEntry.class.isAssignableFrom(what.type)){
|
||||||
return (T)_getString((ConfigToken<String>)what);
|
return (T)_getString((ConfigToken<String>)what);
|
||||||
}
|
}
|
||||||
|
if (StringArrayEntry.class.isAssignableFrom(what.type)){
|
||||||
|
return (T)_getStringArray((ConfigToken<List<String>>)what);
|
||||||
|
}
|
||||||
return this._get(what);
|
return this._get(what);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,5 +238,12 @@ public class NamedPathConfig extends PathConfig{
|
||||||
return this.getString(what, what.defaultValue);
|
return this.getString(what, what.defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void set(ConfigToken<List<String>> what, List<String> value) {
|
||||||
|
this.setStringArray(what, value);
|
||||||
|
}
|
||||||
|
private List<String> _getStringArray(ConfigToken<List<String>> what){
|
||||||
|
return this.getStringArray(what, what.defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue