diff --git a/src/main/java/net/minecraftforge/common/config/ConfigManager.java b/src/main/java/net/minecraftforge/common/config/ConfigManager.java index c1d6a8f9a3..30a8c56f13 100644 --- a/src/main/java/net/minecraftforge/common/config/ConfigManager.java +++ b/src/main/java/net/minecraftforge/common/config/ConfigManager.java @@ -220,8 +220,12 @@ public class ConfigManager { if (!Modifier.isPublic(f.getModifiers())) continue; + + //Only the root class may have static fields. Otherwise category tree nodes of the same type would share the + //contained value messing up the sync if (Modifier.isStatic(f.getModifiers()) != (instance == null)) continue; + if (f.isAnnotationPresent(Config.Ignore.class)) continue; @@ -230,7 +234,7 @@ public class ConfigManager if (ca != null) comment = NEW_LINE.join(ca.value()); - String langKey = modid + "." + (category.isEmpty() ? "" : category + ".") + f.getName().toLowerCase(Locale.ENGLISH); + String langKey = modid + "." + (category.isEmpty() ? "" : category + Configuration.CATEGORY_SPLITTER) + f.getName().toLowerCase(Locale.ENGLISH); LangKey la = f.getAnnotation(LangKey.class); if (la != null) langKey = la.value(); @@ -238,7 +242,7 @@ public class ConfigManager boolean requiresMcRestart = f.isAnnotationPresent(Config.RequiresMcRestart.class); boolean requiresWorldRestart = f.isAnnotationPresent(Config.RequiresWorldRestart.class); - if (FieldWrapper.hasWrapperFor(f)) //Access the field + if (FieldWrapper.hasWrapperFor(f)) //Wrappers exist for primitives, enums, maps and arrays { if (Strings.isNullOrEmpty(category)) throw new RuntimeException("An empty category may not contain anything but objects representing categories!"); @@ -248,9 +252,9 @@ public class ConfigManager ITypeAdapter adapt = wrapper.getTypeAdapter(); Property.Type propType = adapt.getType(); - for (String key : wrapper.getKeys()) + for (String key : wrapper.getKeys()) //Iterate the fully qualified property names the field provides { - String suffix = StringUtils.replaceOnce(key, wrapper.getCategory() + ".", ""); + String suffix = StringUtils.replaceOnce(key, wrapper.getCategory() + Configuration.CATEGORY_SPLITTER, ""); boolean existed = exists(cfg, wrapper.getCategory(), suffix); if (!existed || loading) //Creates keys in category specified by the wrapper if new ones are programaticaly added @@ -263,7 +267,7 @@ public class ConfigManager else wrapper.setValue(key, adapt.getValue(property)); } - else //If the key is not new, sync according to shoudlReadFromVar() + else //If the key is not new, sync according to shouldReadFromVar() { Property property = property(cfg, wrapper.getCategory(), suffix, propType, adapt.isArrayAdapter()); Object propVal = adapt.getValue(property); @@ -278,31 +282,32 @@ public class ConfigManager ConfigCategory confCat = cfg.getCategory(wrapper.getCategory()); - for (Property property : confCat.getOrderedValues())//Are new keys in the Configuration object? + for (Property property : confCat.getOrderedValues()) //Iterate the properties to check for new data from the config side { - if (!wrapper.handlesKey(property.getName())) + String key = confCat.getQualifiedName() + Configuration.CATEGORY_SPLITTER + property.getName(); + if (!wrapper.handlesKey(key)) continue; - if (loading || !wrapper.hasKey(property.getName())) + if (loading || !wrapper.hasKey(key)) { Object value = wrapper.getTypeAdapter().getValue(property); - wrapper.setValue(confCat.getName() + "." + property.getName(), value); + wrapper.setValue(key, value); } } - if (loading) //Doing this after the loops. The wrapper should set cosmetic stuff. + if (loading) wrapper.setupConfiguration(cfg, comment, langKey, requiresMcRestart, requiresWorldRestart); } - catch (Exception e) //If anything goes wrong, add the errored field and class. + catch (Exception e) { String format = "Error syncing field '%s' of class '%s'!"; String error = String.format(format, f.getName(), cls.getName()); throw new RuntimeException(error, e); } } - else if (f.getType().getSuperclass() != null && f.getType().getSuperclass().equals(Object.class)) //Descend the object tree - { + else if (f.getType().getSuperclass() != null && f.getType().getSuperclass().equals(Object.class)) + { //If the field extends Object directly, descend the object tree and access the objects members Object newInstance = null; try { @@ -310,11 +315,11 @@ public class ConfigManager } catch (IllegalAccessException e) { - //This should never happen. Previous checks should eliminate this. throw new RuntimeException(e); } - String sub = (category.isEmpty() ? "" : category + ".") + getName(f).toLowerCase(Locale.ENGLISH); + //Setup the sub category with its respective name, comment, language key, etc. + String sub = (category.isEmpty() ? "" : category + Configuration.CATEGORY_SPLITTER) + getName(f).toLowerCase(Locale.ENGLISH); ConfigCategory confCat = cfg.getCategory(sub); confCat.setComment(comment); confCat.setLanguageKey(langKey); diff --git a/src/main/java/net/minecraftforge/common/config/FieldWrapper.java b/src/main/java/net/minecraftforge/common/config/FieldWrapper.java index 3ce39d2a11..25c4515897 100644 --- a/src/main/java/net/minecraftforge/common/config/FieldWrapper.java +++ b/src/main/java/net/minecraftforge/common/config/FieldWrapper.java @@ -4,12 +4,9 @@ import java.lang.reflect.Field; import java.lang.reflect.GenericArrayType; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.Iterator; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; import java.util.regex.Pattern; -import java.util.Set; import com.google.common.collect.Lists; @@ -68,6 +65,7 @@ public abstract class FieldWrapper implements IFieldWrapper { private Map theMap = null; private Type mType; + private final String baseName; ITypeAdapter adapter; @SuppressWarnings("unchecked") @@ -75,6 +73,8 @@ public abstract class FieldWrapper implements IFieldWrapper { super(category, field, instance); + this.baseName = (this.category == null) ? "" : (this.category + ".") + this.name.toLowerCase(Locale.ENGLISH) + "."; + try { theMap = (Map) field.get(instance); @@ -123,7 +123,7 @@ public abstract class FieldWrapper implements IFieldWrapper Iterator it = keys.iterator(); for (int i = 0; i < keyArray.length; i++) { - keyArray[i] = category + "." + name + "." + it.next(); + keyArray[i] = this.baseName + it.next(); } return keyArray; @@ -132,28 +132,27 @@ public abstract class FieldWrapper implements IFieldWrapper @Override public Object getValue(String key) { - return theMap.get(StringUtils.replaceOnce(key, category + "." + name + ".", "")); + return theMap.get(getSuffix(key)); } @Override public void setValue(String key, Object value) { - String suffix = StringUtils.replaceOnce(key, category + "." + name + ".", ""); - theMap.put(suffix, value); + theMap.put(getSuffix(key), value); } @Override - public boolean hasKey(String name) + public boolean hasKey(String key) { - return theMap.containsKey(name); + return theMap.containsKey(getSuffix(key)); } @Override - public boolean handlesKey(String name) + public boolean handlesKey(String key) { - if (name == null) + if (key == null) return false; - return name.startsWith(category + "." + name + "."); + return key.startsWith(this.baseName); } @Override @@ -169,7 +168,17 @@ public abstract class FieldWrapper implements IFieldWrapper @Override public String getCategory() { - return category + "." + name; + return (this.category == null) ? "" : (this.category + ".") + this.name.toLowerCase(Locale.ENGLISH); + } + + /** + * Removes the {@code this.baseName} prefix from the key + * @param key the key to be edited + * @return the keys suffix + */ + private String getSuffix(String key) + { + return StringUtils.replaceOnce(key, this.baseName, ""); } } @@ -336,15 +345,15 @@ public abstract class FieldWrapper implements IFieldWrapper } @Override - public boolean hasKey(String name) + public boolean hasKey(String key) { - return (this.category + "." + this.name).equals(name); + return (this.category + "." + this.name).equals(key); } @Override - public boolean handlesKey(String name) + public boolean handlesKey(String key) { - return hasKey(name); + return hasKey(key); } @Override @@ -361,7 +370,7 @@ public abstract class FieldWrapper implements IFieldWrapper @Override public String getCategory() { - return category; + return this.category; } } diff --git a/src/main/java/net/minecraftforge/common/config/IFieldWrapper.java b/src/main/java/net/minecraftforge/common/config/IFieldWrapper.java index 47065751d2..b74e393e69 100644 --- a/src/main/java/net/minecraftforge/common/config/IFieldWrapper.java +++ b/src/main/java/net/minecraftforge/common/config/IFieldWrapper.java @@ -2,6 +2,8 @@ package net.minecraftforge.common.config; /** * The objects are expected to get their wrapped field, the owning class, instance and category string on initialization. + * In general: The key is the fully qualified property name, where each subcategory is appended with a dot. + * i.e: general.map.isPresent */ public interface IFieldWrapper { @@ -12,24 +14,46 @@ public interface IFieldWrapper ITypeAdapter getTypeAdapter(); /** - * @param field the field about which to retrieve information - * @param instance The instance whose field shall be queried. - * @return a list of keys handled by this field + * @return a list of fully qualified property keys handled by this field */ String[] getKeys(); - + + /** + * @param key the fully qualified property key + * @return the value the wrapped field associates with the given key + */ Object getValue(String key); - + + /** + * @param key the fully qualified property key + * @param value the target value of the property associated with the key + */ void setValue(String key, Object value); - - boolean hasKey(String name); - - boolean handlesKey(String name); - + + /** + * @param key a fully qualified property key + * @return true if the wrapped field contains a property associated with the given key + */ + boolean hasKey(String key); + + /** + * @param key a fully qualified property key + * @return true if the wrapped field can save information associated with the given key, false otherwise + */ + boolean handlesKey(String key); + + /** + * @param cfg The configuration object holding the properties + * @param desc The properties description + * @param langKey The languageKey of the property, used in GUI + * @param reqMCRestart True, if a change in this property requires a restart of Minecraft + * @param reqWorldRestart True, if the world needs to be reloaded after changes to this property + */ void setupConfiguration(Configuration cfg, String desc, String langKey, boolean reqMCRestart, boolean reqWorldRestart); /** - * @return the category name in which the entries should be saved. + * i.e. general.map in the example above + * @return the category name in which the entries should be saved. This includes the parent categories */ String getCategory(); } diff --git a/src/main/java/net/minecraftforge/common/config/ITypeAdapter.java b/src/main/java/net/minecraftforge/common/config/ITypeAdapter.java index ed157a169d..c5644fcc7c 100644 --- a/src/main/java/net/minecraftforge/common/config/ITypeAdapter.java +++ b/src/main/java/net/minecraftforge/common/config/ITypeAdapter.java @@ -20,15 +20,33 @@ package net.minecraftforge.common.config; import net.minecraftforge.common.config.Property.Type; +/** + * Abstracts the types of properties away. Higher level logic must prevent invalid data types. + */ interface ITypeAdapter { + /** + * Assigns the default value to the property + * @param property the property whose default value will be assigned + * @param value the default value + */ void setDefaultValue(Property property, Object value); - + + /** + * Sets the properties value. + * @param property the property whose value will be set + * @param value the set value + */ void setValue(Property property, Object value); + /** + * Retrieves the properties value + * @param prop the property whose value will be retrieved + * @return the properties value + */ Object getValue(Property prop); Type getType(); - + boolean isArrayAdapter(); } diff --git a/src/test/java/net/minecraftforge/debug/mod/ConfigAnnotationTest.java b/src/test/java/net/minecraftforge/debug/mod/ConfigAnnotationTest.java index d62f579387..56cb7c6754 100644 --- a/src/test/java/net/minecraftforge/debug/mod/ConfigAnnotationTest.java +++ b/src/test/java/net/minecraftforge/debug/mod/ConfigAnnotationTest.java @@ -37,6 +37,7 @@ import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import org.apache.logging.log4j.Logger; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -56,11 +57,22 @@ public class ConfigAnnotationTest @Mod.EventHandler public void init(FMLInitializationEvent event) { + logger.info("Starting config modification test."); + logger.debug("Starting simple variable change test."); logger.debug("Old: {}", CONFIG_TYPES.bool); CONFIG_TYPES.bool = !CONFIG_TYPES.bool; logger.debug("New: {}", CONFIG_TYPES.bool); ConfigManager.sync(MODID, Type.INSTANCE); logger.debug("After sync: {}", CONFIG_TYPES.bool); + + logger.debug("Starting map key addition test."); + logger.debug("Old map keys: {}", Arrays.deepToString(ROOT_CONFIG_TEST.mapTest.keySet().toArray())); + String key = "" + System.currentTimeMillis(); + logger.debug("Adding key: {}", key); + ROOT_CONFIG_TEST.mapTest.put(key, Math.PI); + ConfigManager.sync(MODID, Type.INSTANCE); + logger.debug("Keys after sync: {}", Arrays.deepToString(ROOT_CONFIG_TEST.mapTest.keySet().toArray())); + logger.debug("Synced. Check config gui to verify the key was added properly"); } @SubscribeEvent @@ -72,8 +84,19 @@ public class ConfigAnnotationTest } } + @Config(modid = MODID, type = Type.INSTANCE) + public static class ROOT_CONFIG_TEST { + public static HashMap mapTest = new HashMap<>(); + + static { + mapTest.put("foobar", 2.0); + mapTest.put("foobaz", 100.5); + mapTest.put("barbaz", Double.MAX_VALUE); + } + } + @LangKey("config_test.config.types") - @Config(modid = MODID, type = Type.INSTANCE, name = MODID + "_types") + @Config(modid = MODID, type = Type.INSTANCE, name = MODID + "_types", category = "types") public static class CONFIG_TYPES { public static boolean bool = false; @@ -117,7 +140,7 @@ public class ConfigAnnotationTest } @LangKey("config_test.config.annotations") - @Config(modid = MODID) + @Config(modid = MODID, category = "annotations") public static class CONFIG_ANNOTATIONS { @RangeDouble(min = -10.5, max = 100.5) @@ -140,7 +163,7 @@ public class ConfigAnnotationTest } @LangKey("config_test.config.subcats") - @Config(modid = MODID, name = MODID + "_subcats", category = "") + @Config(modid = MODID, name = MODID + "_subcats", category = "subcats") public static class CONFIG_SUBCATS { //public static String THIS_WILL_ERROR = "DUH"; @@ -176,7 +199,7 @@ public class ConfigAnnotationTest } @LangKey("config_test.config.maps") - @Config(modid = MODID, name = MODID + "_map") + @Config(modid = MODID, name = MODID + "_map", category = "maps") public static class CONFIG_MAP { @Name("map")