Added support for new FML config GUI classes

refactored lots of stuff based on comments

added Configuration.load() exception handling and logging
This commit is contained in:
bspkrs committed 2014-06-04 17:39:47 -04:00
1 parent 2e152c52c4
commit 04c2ed554d
8 files changed
+2976 -245

No files matched your search

@@ -1,5 +1,11 @@
/**
* This software is provided under the terms of the Minecraft Forge Public
* License v1.0.
*/
package net.minecraftforge.common.config;
import static net.minecraftforge.common.config.Configuration.COMMENT_SEPARATOR;
import static net.minecraftforge.common.config.Configuration.NEW_LINE;
import static net.minecraftforge.common.config.Configuration.allowedProperties;
@@ -7,22 +13,34 @@ import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.base.Splitter;
import cpw.mods.fml.client.config.GuiConfigEntries.IConfigEntry;
public class ConfigCategory implements Map<String, Property>
{
private String name;
private String comment;
private String languagekey;
private ArrayList<ConfigCategory> children = new ArrayList<ConfigCategory>();
private Map<String, Property> properties = new TreeMap<String, Property>();
private int propNumber = 0;
public final ConfigCategory parent;
private boolean changed = false;
private boolean requiresWorldRestart = false;
private boolean showInGui = true;
private boolean requiresMcRestart = false;
private Class<? extends IConfigEntry> customEntryClass = null;
private List<String> propertyOrder = null;
public ConfigCategory(String name)
{
@@ -51,6 +69,11 @@ public class ConfigCategory implements Map<String, Property>
return false;
}
public String getName()
{
return name;
}
public String getQualifiedName()
{
return getQualifiedName(name, parent);
@@ -76,11 +99,134 @@ public class ConfigCategory implements Map<String, Property>
return ImmutableMap.copyOf(properties);
}
public void setComment(String comment)
public List<Property> getOrderedValues()
{
if (this.propertyOrder != null)
{
ArrayList<Property> set = new ArrayList<Property>();
for (String key : this.propertyOrder)
if (properties.containsKey(key))
set.add(properties.get(key));
return ImmutableList.copyOf(set);
}
else
return ImmutableList.copyOf(properties.values());
}
public ConfigCategory setConfigEntryClass(Class<? extends IConfigEntry> clazz)
{
this.customEntryClass = clazz;
return this;
}
public Class<? extends IConfigEntry> getConfigEntryClass()
{
return this.customEntryClass;
}
public ConfigCategory setLanguageKey(String languagekey)
{
this.languagekey = languagekey;
return this;
}
public String getLanguagekey()
{
if (this.languagekey != null)
return this.languagekey;
else
return getQualifiedName();
}
public ConfigCategory setComment(String comment)
{
this.comment = comment;
return this;
}
public String getComment()
{
return this.comment;
}
/**
* Sets the flag for whether or not this category can be edited while a world is running. Care should be taken to ensure
* that only properties that are truly dynamic can be changed from the in-game options menu. Only set this flag to
* true if all child properties/categories are unable to be modified while a world is running.
*/
public ConfigCategory setRequiresWorldRestart(boolean requiresWorldRestart)
{
this.requiresWorldRestart = requiresWorldRestart;
return this;
}
/**
* Returns whether or not this category is able to be edited while a world is running using the in-game Mod Options screen
* as well as the Mods list screen, or only from the Mods list screen.
*/
public boolean requiresWorldRestart()
{
return this.requiresWorldRestart;
}
/**
* Sets whether or not this ConfigCategory should be allowed to show on config GUIs.
* Defaults to true.
*/
public ConfigCategory setShowInGui(boolean showInGui)
{
this.showInGui = showInGui;
return this;
}
/**
* Gets whether or not this ConfigCategory should be allowed to show on config GUIs.
* Defaults to true unless set to false.
*/
public boolean showInGui()
{
return showInGui;
}
/**
* Sets whether or not this ConfigCategory requires Minecraft to be restarted when changed.
* Defaults to false. Only set this flag to true if ALL child properties/categories require
* Minecraft to be restarted when changed. Setting this flag will also prevent modification
* of the child properties/categories while a world is running.
*/
public ConfigCategory setRequiresMcRestart(boolean requiresMcRestart)
{
this.requiresMcRestart = this.requiresWorldRestart = requiresMcRestart;
return this;
}
/**
* Gets whether or not this ConfigCategory requires Minecraft to be restarted when changed.
* Defaults to false unless set to true.
*/
public boolean requiresMcRestart()
{
return this.requiresMcRestart;
}
public ConfigCategory setPropertyOrder(List<String> propertyOrder)
{
this.propertyOrder = propertyOrder;
for (String s : properties.keySet())
if (!propertyOrder.contains(s))
propertyOrder.add(s);
return this;
}
public List<String> getPropertyOrder()
{
if (this.propertyOrder != null)
return ImmutableList.copyOf(this.propertyOrder);
else
return ImmutableList.copyOf(properties.keySet());
}
public boolean containsKey(String key)
{
return properties.containsKey(key);
@@ -111,22 +257,21 @@ public class ConfigCategory implements Map<String, Property>
String pad1 = getIndent(indent + 1);
String pad2 = getIndent(indent + 2);
write(out, pad0, "####################");
write(out, pad0, "# ", name);
if (comment != null)
if (comment != null && !comment.isEmpty())
{
write(out, pad0, "#===================");
write(out, pad0, COMMENT_SEPARATOR);
write(out, pad0, "# ", name);
write(out, pad0, "#--------------------------------------------------------------------------------------------------------#");
Splitter splitter = Splitter.onPattern("\r?\n");
for (String line : splitter.split(comment))
{
write(out, pad0, "# ", line);
}
write(out, pad0, COMMENT_SEPARATOR, NEW_LINE);
}
write(out, pad0, "####################", NEW_LINE);
if (!allowedProperties.matchesAllOf(name))
{
name = '"' + name + '"';
@@ -134,13 +279,13 @@ public class ConfigCategory implements Map<String, Property>
write(out, pad0, name, " {");
Property[] props = properties.values().toArray(new Property[properties.size()]);
Property[] props = getOrderedValues().toArray(new Property[] {});
for (int x = 0; x < props.length; x++)
{
Property prop = props[x];
if (prop.comment != null)
if (prop.comment != null && !prop.comment.isEmpty())
{
if (x != 0)
{
@@ -185,6 +330,9 @@ public class ConfigCategory implements Map<String, Property>
}
}
if (children.size() > 0)
out.newLine();
for (ConfigCategory child : children)
{
child.write(out, indent + 1);
@@ -232,6 +380,8 @@ public class ConfigCategory implements Map<String, Property>
@Override public Property put(String key, Property value)
{
changed = true;
if (this.propertyOrder != null && !this.propertyOrder.contains(key))
this.propertyOrder.add(key);
return properties.put(key, value);
}
@Override public Property remove(Object key)
@@ -242,6 +392,10 @@ public class ConfigCategory implements Map<String, Property>
@Override public void putAll(Map<? extends String, ? extends Property> m)
{
changed = true;
if (this.propertyOrder != null)
for (String key : m.keySet())
if (!this.propertyOrder.contains(key))
this.propertyOrder.add(key);
properties.putAll(m);
}
@Override public void clear()
@@ -260,12 +414,13 @@ public class ConfigCategory implements Map<String, Property>
public Set<ConfigCategory> getChildren(){ return ImmutableSet.copyOf(children); }
public void removeChild(ConfigCategory child)
public ConfigCategory removeChild(ConfigCategory child)
{
if (children.contains(child))
{
children.remove(child);
changed = true;
}
return this;
}
}
@@ -0,0 +1,359 @@
/**
* This software is provided under the terms of the Minecraft Forge Public
* License v1.0.
*/
package net.minecraftforge.common.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import cpw.mods.fml.client.config.ConfigGuiType;
import cpw.mods.fml.client.config.GuiConfigEntries.IConfigEntry;
import cpw.mods.fml.client.config.GuiEditArrayEntries.IArrayEntry;
import cpw.mods.fml.client.config.IConfigElement;
/**
* This class bridges the gap between the FML config GUI classes and the Forge Configuration classes.
*/
public class ConfigElement<T> implements IConfigElement<T>
{
private Property prop;
private Property.Type type;
private boolean isProperty;
private ConfigCategory ctgy;
private boolean categoriesFirst = true;
public ConfigElement(ConfigCategory ctgy)
{
this.ctgy = ctgy;
isProperty = false;
}
public ConfigElement(Property prop)
{
this.prop = prop;
this.type = prop.getType();
this.isProperty = true;
}
public ConfigElement<T> listCategoriesFirst(boolean categoriesFirst)
{
this.categoriesFirst = categoriesFirst;
return this;
}
@Override
public List<IConfigElement> getChildElements()
{
if (!isProperty)
{
List<IConfigElement> elements = new ArrayList<IConfigElement>();
Iterator<ConfigCategory> ccI = ctgy.getChildren().iterator();
Iterator<Property> pI = ctgy.getOrderedValues().iterator();
int index = 0;
if (categoriesFirst)
while (ccI.hasNext())
{
ConfigElement temp = new ConfigElement(ccI.next());
if (temp.showInGui()) // don't bother adding elements that shouldn't show
elements.add(temp);
}
while (pI.hasNext())
{
ConfigElement<?> temp = getTypedElement(pI.next());
if (temp.showInGui())
elements.add(temp);
}
if (!categoriesFirst)
while (ccI.hasNext())
{
ConfigElement temp = new ConfigElement(ccI.next());
if (temp.showInGui())
elements.add(temp);
}
return elements;
}
return null;
}
public static ConfigElement<?> getTypedElement(Property prop)
{
switch (getType(prop))
{
case BOOLEAN:
return new ConfigElement<Boolean>(prop);
case DOUBLE:
return new ConfigElement<Double>(prop);
case INTEGER:
return new ConfigElement<Integer>(prop);
default:
return new ConfigElement<String>(prop);
}
}
@Override
public String getName()
{
return isProperty ? prop.getName() : ctgy.getName();
}
@Override
public boolean isProperty()
{
return isProperty;
}
@Override
public Class<? extends IConfigEntry> getConfigEntryClass()
{
return isProperty ? prop.getConfigEntryClass() : ctgy.getConfigEntryClass();
}
@Override
public Class<? extends IArrayEntry> getArrayEntryClass()
{
return isProperty ? prop.getArrayEntryClass() : null;
}
@Override
public String getQualifiedName()
{
return isProperty ? prop.getName() : ctgy.getQualifiedName();
}
@Override
public ConfigGuiType getType()
{
return isProperty ? getType(this.prop) : ConfigGuiType.CONFIG_CATEGORY;
}
public static ConfigGuiType getType(Property prop)
{
return prop.getType() == Property.Type.BOOLEAN ? ConfigGuiType.BOOLEAN : prop.getType() == Property.Type.DOUBLE ? ConfigGuiType.DOUBLE :
prop.getType() == Property.Type.INTEGER ? ConfigGuiType.INTEGER : prop.getType() == Property.Type.COLOR ? ConfigGuiType.COLOR :
prop.getType() == Property.Type.MOD_ID ? ConfigGuiType.MOD_ID : ConfigGuiType.STRING;
}
@Override
public boolean isList()
{
return isProperty && prop.isList();
}
@Override
public boolean isListLengthFixed()
{
return isProperty && prop.isListLengthFixed();
}
@Override
public int getMaxListLength()
{
return isProperty ? prop.getMaxListLength() : -1;
}
@Override
public String getComment()
{
return isProperty ? prop.comment : ctgy.getComment();
}
@Override
public boolean isDefault()
{
return !isProperty || prop.isDefault();
}
@Override
public void setToDefault()
{
if (isProperty)
prop.setToDefault();
}
@Override
public boolean requiresWorldRestart()
{
return isProperty ? prop.requiresWorldRestart() : ctgy.requiresWorldRestart();
}
@Override
public boolean showInGui()
{
return isProperty ? prop.showInGui() : ctgy.showInGui();
}
@Override
public boolean requiresMcRestart()
{
return isProperty ? prop.requiresMcRestart() : ctgy.requiresMcRestart();
}
@Override
public String[] getValidValues()
{
return isProperty ? prop.getValidValues() : null;
}
@Override
public String getLanguageKey()
{
return isProperty ? prop.getLanguageKey() : ctgy.getLanguagekey();
}
@Override
public Object getDefault()
{
return isProperty ? (T) prop.getDefault() : null;
}
@Override
public Object[] getDefaults()
{
if (isProperty)
{
String[] aVal = prop.getDefaults();
if (type == Property.Type.BOOLEAN)
{
Boolean[] ba = new Boolean[aVal.length];
for(int i = 0; i < aVal.length; i++)
ba[i] = Boolean.valueOf(aVal[i]);
return ba;
}
else if (type == Property.Type.DOUBLE)
{
Double[] da = new Double[aVal.length];
for(int i = 0; i < aVal.length; i++)
da[i] = Double.valueOf(aVal[i].toString());
return da;
}
else if (type == Property.Type.INTEGER)
{
Integer[] ia = new Integer[aVal.length];
for(int i = 0; i < aVal.length; i++)
ia[i] = Integer.valueOf(aVal[i].toString());
return ia;
}
else
return aVal;
}
return null;
}
@Override
public Pattern getValidationPattern()
{
return isProperty ? prop.getValidationPattern() : null;
}
@Override
public Object get()
{
return isProperty ? (T) prop.getString() : null;
}
@Override
public Object[] getList()
{
if (isProperty)
{
String[] aVal = prop.getStringList();
if (type == Property.Type.BOOLEAN)
{
Boolean[] ba = new Boolean[aVal.length];
for(int i = 0; i < aVal.length; i++)
ba[i] = Boolean.valueOf(aVal[i]);
return ba;
}
else if (type == Property.Type.DOUBLE)
{
Double[] da = new Double[aVal.length];
for(int i = 0; i < aVal.length; i++)
da[i] = Double.valueOf(aVal[i].toString());
return da;
}
else if (type == Property.Type.INTEGER)
{
Integer[] ia = new Integer[aVal.length];
for(int i = 0; i < aVal.length; i++)
ia[i] = Integer.valueOf(aVal[i].toString());
return ia;
}
else
return aVal;
}
return null;
}
@Override
public void set(T value)
{
if (isProperty)
{
if (type == Property.Type.BOOLEAN)
prop.set(Boolean.parseBoolean(value.toString()));
else if (type == Property.Type.DOUBLE)
prop.set(Double.parseDouble(value.toString()));
else if (type == Property.Type.INTEGER)
prop.set(Integer.parseInt(value.toString()));
else
prop.set(value.toString());
}
}
@Override
public void set(T[] aVal)
{
if (isProperty)
{
if (type == Property.Type.BOOLEAN)
{
boolean[] ba = new boolean[aVal.length];
for(int i = 0; i < aVal.length; i++)
ba[i] = Boolean.valueOf(aVal[i].toString());
prop.set(ba);
}
else if (type == Property.Type.DOUBLE)
{
double[] da = new double[aVal.length];
for(int i = 0; i < aVal.length; i++)
da[i] = Double.valueOf(aVal[i].toString());
prop.set(da);
}
else if (type == Property.Type.INTEGER)
{
int[] ia = new int[aVal.length];
for(int i = 0; i < aVal.length; i++)
ia[i] = Integer.valueOf(aVal[i].toString());
prop.set(ia);
}
else
{
String[] is = new String[aVal.length];
for(int i = 0; i < aVal.length; i++)
is[i] = aVal[i].toString();
prop.set(is);
}
}
}
@Override
public T getMinValue()
{
return isProperty ? (T) prop.getMinValue() : null;
}
@Override
public T getMaxValue()
{
return isProperty ? (T) prop.getMaxValue() : null;
}
}
File diff suppressed because it is too large. Load diff
@@ -6,6 +6,11 @@
package net.minecraftforge.common.config;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Pattern;
import cpw.mods.fml.client.config.GuiConfigEntries.IConfigEntry;
import cpw.mods.fml.client.config.GuiEditArrayEntries.IArrayEntry;
public class Property
{
@@ -14,17 +19,17 @@ public class Property
STRING,
INTEGER,
BOOLEAN,
DOUBLE;
private static Type[] values = {STRING, INTEGER, BOOLEAN, DOUBLE};
DOUBLE,
COLOR,
MOD_ID;
public static Type tryParse(char id)
{
for (int x = 0; x < values.length; x++)
for (int x = 0; x < values().length; x++)
{
if (values[x].getID() == id)
if (values()[x].getID() == id)
{
return values[x];
return values()[x];
}
}
@@ -39,33 +44,76 @@ public class Property
private String name;
private String value;
private String defaultValue;
public String comment;
private String[] values;
private String[] defaultValues;
private String[] validValues;
private String langKey;
private String minValue;
private String maxValue;
private Class<? extends IConfigEntry> configEntryClass = null;
private Class<? extends IArrayEntry> arrayEntryClass = null;
private boolean requiresWorldRestart = false;
private boolean showInGui = true;
private boolean requiresMcRestart = false;
private Pattern validationPattern;
private final boolean wasRead;
private final boolean isList;
private boolean isListLengthFixed = false;
private int maxListLength = -1;
private final Type type;
private boolean changed = false;
public Property()
{
wasRead = false;
type = null;
isList = false;
}
public Property(String name, String value, Type type)
{
this(name, value, type, false);
this(name, value, type, false, new String[0], name);
}
Property(String name, String value, Type type, boolean read)
public Property(String name, String value, Type type, boolean read)
{
this(name, value, type, read, new String[0], name);
}
public Property(String name, String value, Type type, String[] validValues)
{
this(name, value, type, false, validValues, name);
}
public Property(String name, String value, Type type, String langKey)
{
this(name, value, type, false, new String[0], langKey);
}
public Property(String name, String value, Type type, boolean read, String langKey)
{
this(name, value, type, read, new String[0], langKey);
}
public Property(String name, String value, Type type, String[] validValues, String langKey)
{
this(name, value, type, false, validValues, langKey);
}
Property(String name, String value, Type type, boolean read, String[] validValues, String langKey)
{
setName(name);
this.value = value;
this.values = new String[0];
this.type = type;
wasRead = read;
isList = false;
this.defaultValue = value;
this.defaultValues = new String[0];
this.validValues = validValues;
this.isListLengthFixed = false;
this.maxListLength = -1;
this.minValue = String.valueOf(Integer.MIN_VALUE);
this.maxValue = String.valueOf(Integer.MAX_VALUE);
this.langKey = langKey;
this.comment = "";
}
public Property(String name, String[] values, Type type)
@@ -74,14 +122,514 @@ public class Property
}
Property(String name, String[] values, Type type, boolean read)
{
this(name, values, type, read, new String[0], name);
}
public Property(String name, String[] values, Type type, String langKey)
{
this(name, values, type, false, langKey);
}
Property(String name, String[] values, Type type, boolean read, String langKey)
{
this(name, values, type, read, new String[0], langKey);
}
Property(String name, String[] values, Type type, boolean read, String[] validValues, String langKey)
{
setName(name);
this.type = type;
this.values = values;
this.values = Arrays.copyOf(values, values.length);
wasRead = read;
isList = true;
this.value = "";
this.defaultValue = "";
for (String s : values)
this.defaultValue += ", [" + s + "]";
this.defaultValue = this.defaultValue.replaceFirst(", ", "");
this.defaultValues = Arrays.copyOf(values, values.length);
this.validValues = validValues;
this.isListLengthFixed = false;
this.maxListLength = -1;
this.minValue = String.valueOf(Integer.MIN_VALUE);
this.maxValue = String.valueOf(Integer.MAX_VALUE);
this.langKey = langKey;
this.comment = "";
}
/**
* Returns whether or not this Property is defaulted.
*
* @return true if the current value(s) is(are) deeply equal to the default value(s)
*/
public boolean isDefault()
{
if (this.isBooleanList())
{
if (values.length == defaultValues.length)
{
for (int i = 0; i < values.length; i++)
if (Boolean.parseBoolean(values[i]) != Boolean.parseBoolean(defaultValues[i]))
return false;
return true;
}
else
return false;
}
if (this.isIntList())
{
if (values.length == defaultValues.length)
{
for (int i = 0; i < values.length; i++)
if (Integer.parseInt(values[i]) != Integer.parseInt(defaultValues[i]))
return false;
return true;
}
else
return false;
}
if (this.isDoubleList())
{
if (values.length == defaultValues.length)
{
for (int i = 0; i < values.length; i++)
if (Double.parseDouble(values[i]) != Double.parseDouble(defaultValues[i]))
return false;
return true;
}
else
return false;
}
if (this.isList())
{
if (values.length == defaultValues.length)
{
for (int i = 0; i < values.length; i++)
if (!values[i].equals(defaultValues[i]))
return false;
return true;
}
else
return false;
}
if (this.type == Type.BOOLEAN && this.isBooleanValue())
return Boolean.parseBoolean(value) == Boolean.parseBoolean(defaultValue);
if (this.type == Type.INTEGER && this.isIntValue())
return Integer.parseInt(value) == Integer.parseInt(defaultValue);
if (this.type == Type.DOUBLE && this.isDoubleValue())
return Double.parseDouble(value) == Double.parseDouble(defaultValue);
return value.equals(defaultValue);
}
/**
* Sets the current value(s) of this Property to the default value(s).
*/
public Property setToDefault()
{
this.value = this.defaultValue;
this.values = Arrays.copyOf(this.defaultValues, this.defaultValues.length);
return this;
}
/**
* Gets the raw String default value of this Property. Check for isList() == false first.
*
* @return the default value String
*/
public String getDefault()
{
return defaultValue;
}
/**
* Gets the raw String[] default values of this Property. Check for isList() == true first.
*
* @return the default values String[]
*/
public String[] getDefaults()
{
return Arrays.copyOf(this.defaultValues, this.defaultValues.length);
}
/**
* Sets the flag for whether or not this Property can be edited while a world is running. Care should be taken to ensure
* that only properties that are truly dynamic can be changed from the in-game options menu. When set to false the Property will be
* editable from both the main menu Mods list config screen and the in-game Mod Options config screen. When set to true the Property
* will only be editable from the main menu Mods list config screen.
*/
public Property setRequiresWorldRestart(boolean requiresWorldRestart)
{
this.requiresWorldRestart = requiresWorldRestart;
return this;
}
/**
* Returns whether or not this Property is able to be edited while a world is running using the in-game Mod Options screen
* as well as the Mods list screen, or only from the Mods list screen. Setting this flag to true will disable editing of
* this property while a world is running.
*/
public boolean requiresWorldRestart()
{
return this.requiresWorldRestart;
}
/**
* Sets whether or not this Property should be allowed to show on config GUIs.
* Defaults to true.
*/
public Property setShowInGui(boolean showInGui)
{
this.showInGui = showInGui;
return this;
}
/**
* Gets whether or not this Property should be allowed to show on config GUIs.
* Defaults to true unless set to false.
*/
public boolean showInGui()
{
return showInGui;
}
/**
* Sets whether or not this Property requires Minecraft to be restarted when changed.
* Defaults to false. Setting this flag to true will also disable editing of
* this property while a world is running.
*/
public Property setRequiresMcRestart(boolean requiresMcRestart)
{
this.requiresMcRestart = this.requiresWorldRestart = requiresMcRestart;
return this;
}
/**
* Gets whether or not this Property requires Minecraft to be restarted when changed.
* Defaults to false unless set to true.
*/
public boolean requiresMcRestart()
{
return this.requiresMcRestart;
}
/**
* Sets the maximum length of this list/array Property. Only important if isList() == true. If the current values array or default
* values array is longer than the new maximum it will be resized. If calling both this method and setIsListLengthFixed(true), this
* method should be called afterwards (but is not required).
*/
public Property setMaxListLength(int max)
{
this.maxListLength = max;
if (this.maxListLength != -1)
{
if (values != null && values.length != maxListLength)
if (this.isListLengthFixed || values.length > maxListLength)
values = Arrays.copyOf(values, maxListLength);
if (defaultValues != null && defaultValues.length != maxListLength)
if (this.isListLengthFixed || defaultValues.length > maxListLength)
defaultValues = Arrays.copyOf(defaultValues, maxListLength);
}
return this;
}
/**
* Gets the maximum length of this list/array Property. Only important if isList() == true.
*/
public int getMaxListLength()
{
return this.maxListLength;
}
/**
* Sets the flag for whether this list/array Property has a fixed length. Only important if isList() == true. If calling both this
* method and setMaxListLength(), this method should be called first (but is not required).
*/
public Property setIsListLengthFixed(boolean isListLengthFixed)
{
this.isListLengthFixed = isListLengthFixed;
return this;
}
/**
* Returns whether or not this list/array has a fixed length. Only important if isList() == true.
*/
public boolean isListLengthFixed()
{
return this.isListLengthFixed;
}
/**
* Sets a custom IConfigEntry class that should be used in place of the standard entry class for this Property type. This class
* MUST provide a constructor with the following parameter types: {@code GuiConfig} (the owning GuiConfig screen will be provided),
* {@code GuiConfigEntries} (the owning GuiConfigEntries will be provided), {@code IConfigElement} (the IConfigElement for this Property
* will be provided).
*/
public Property setConfigEntryClass(Class<? extends IConfigEntry> clazz)
{
this.configEntryClass = clazz;
return this;
}
/**
* Gets the custom IConfigEntry class that should be used in place of the standard entry class for this Property type, or null if
* none has been set.
*
* @return a class that implements IConfigEntry
*/
public Class<? extends IConfigEntry> getConfigEntryClass()
{
return this.configEntryClass;
}
/**
* Sets a custom IGuiEditListEntry class that should be used in place of the standard entry class for this Property type. This class
* MUST provide a constructor with the following parameter types: {@code GuiEditList} (the owning GuiEditList screen will be provided),
* {@code GuiPropertyList} (the parent GuiPropertyList will be provided), {@code IConfigProperty} (the IConfigProperty for this Property
* will be provided).
*
* @param clazz a class that implements IConfigEntry
*/
public Property setArrayEntryClass(Class<? extends IArrayEntry> clazz)
{
this.arrayEntryClass = clazz;
return this;
}
/**
* Gets the custom IArrayEntry class that should be used in place of the standard entry class for this Property type, or null if
* none has been set.
*
* @return a class that implements IArrayEntry
*/
public Class<? extends IArrayEntry> getArrayEntryClass()
{
return this.arrayEntryClass;
}
/**
* Sets a regex Pattern object used to validate user input for formatted String or String[] properties.
*
* @param validationPattern
*/
public Property setValidationPattern(Pattern validationPattern)
{
this.validationPattern = validationPattern;
return this;
}
/**
* Gets the Pattern object used to validate user input for this Property.
*
* @return the user input validation Pattern object, or null if none is set
*/
public Pattern getValidationPattern()
{
return this.validationPattern;
}
/**
* Sets the localization language key for this Property so that the config GUI screens are nice and pretty <3. The string languageKey +
* ".tooltip" is used for tooltips when a user hovers the mouse over a GUI property label.
*
* @param langKey a string language key such as myawesomemod.config.myPropName
*/
public Property setLanguageKey(String langKey)
{
this.langKey = langKey;
return this;
}
/**
* Gets the language key string for this Property.
*
* @return the language key
*/
public String getLanguageKey()
{
return this.langKey;
}
/**
* Sets the default string value of this Property.
*
* @param defaultValue a String value
*/
public Property setDefaultValue(String defaultValue)
{
this.defaultValue = defaultValue;
return this;
}
/**
* Sets the default String[] values of this Property.
*
* @param defaultValues an array of String values
*/
public Property setDefaultValues(String[] defaultValues)
{
this.defaultValue = "";
for (String s : defaultValues)
this.defaultValue += ", [" + s + "]";
this.defaultValue = this.defaultValue.replaceFirst(", ", "");
this.defaultValues = Arrays.copyOf(defaultValues, defaultValues.length);
return this;
}
/**
* Sets the default int value of this Property.
*
* @param defaultValue an int value
*/
public Property setDefaultValue(int defaultValue)
{
setDefaultValue(Integer.toString(defaultValue));
return this;
}
/**
* Sets the default int[] values of this Property.
*
* @param defaultValues an array of int values
*/
public Property setDefaultValues(int[] defaultValues)
{
String[] temp = new String[defaultValues.length];
for (int i = 0; i < defaultValues.length; i++)
temp[i] = Integer.toString(defaultValues[i]);
setDefaultValues(temp);
return this;
}
/**
* Sets the default double value of this Property.
*
* @param defaultValue a double value
*/
public Property setDefaultValue(double defaultValue)
{
setDefaultValue(Double.toString(defaultValue));
return this;
}
/**
* Sets the default double[] values of this Property
*
* @param defaultValues an array of double values
*/
public Property setDefaultValues(double[] defaultValues)
{
String[] temp = new String[defaultValues.length];
for (int i = 0; i < defaultValues.length; i++)
temp[i] = Double.toString(defaultValues[i]);
setDefaultValues(temp);
return this;
}
/**
* Sets the default boolean value of this Property.
*
* @param defaultValue a boolean value
*/
public Property setDefaultValue(boolean defaultValue)
{
setDefaultValue(Boolean.toString(defaultValue));
return this;
}
/**
* Sets the default boolean[] values of this Property.
*
* @param defaultValues an array of boolean values
*/
public Property setDefaultValues(boolean[] defaultValues)
{
String[] temp = new String[defaultValues.length];
for (int i = 0; i < defaultValues.length; i++)
temp[i] = Boolean.toString(defaultValues[i]);
setDefaultValues(temp);
return this;
}
/**
* Sets the minimum int value of this Property.
*
* @param minValue an int value
*/
public Property setMinValue(int minValue)
{
this.minValue = Integer.toString(minValue);
return this;
}
/**
* Sets the maximum int value of this Property.
*
* @param maxValue an int value
*/
public Property setMaxValue(int maxValue)
{
this.maxValue = Integer.toString(maxValue);
return this;
}
/**
* Sets the minimum double value of this Property.
*
* @param minValue a double value
*/
public Property setMinValue(double minValue)
{
this.minValue = Double.toString(minValue);
return this;
}
/**
* Sets the maximum double value of this Property.
*
* @param maxValue a double value
*/
public Property setMaxValue(double maxValue)
{
this.maxValue = Double.toString(maxValue);
return this;
}
/**
* Gets the minimum value.
*
* @return the minimum value bound
*/
public String getMinValue()
{
return minValue;
}
/**
* Gets the maximum value.
*
* @return the maximum value bound
*/
public String getMaxValue()
{
return maxValue;
}
/**
* Returns the value in this property as it's raw string.
*
@@ -92,15 +640,44 @@ public class Property
return value;
}
/**
* Sets the array of valid values that this String Property can be set to. When an array of valid values is defined for a Property the
* GUI control for that property will be a value cycle button.
*
* @param validValues a String array of valid values
*/
public Property setValidValues(String[] validValues)
{
this.validValues = validValues;
return this;
}
/**
* Gets the array of valid values that this String Property can be set to, or null if not defined.
*
* @return a String array of valid values
*/
public String[] getValidValues()
{
return this.validValues;
}
/**
* Returns the value in this property as an integer,
* if the value is not a valid integer, it will return -1.
* if the value is not a valid integer, it will return the initially provided default.
*
* @return The value
*/
public int getInt()
{
return getInt(-1);
try
{
return Integer.parseInt(value);
}
catch (NumberFormatException e)
{
return Integer.parseInt(defaultValue);
}
}
/**
@@ -160,8 +737,26 @@ public class Property
}
}
/**
* Returns the value in this property as a boolean, if the value is not a valid boolean, it will return the provided default.
*
* @return The value as a boolean, or the default
*/
public boolean getBoolean()
{
if (isBooleanValue())
{
return Boolean.parseBoolean(value);
}
else
{
return Boolean.parseBoolean(defaultValue);
}
}
/**
* Checks if the current value held by this property is a valid boolean value.
*
* @return True if it is a boolean value
*/
public boolean isBooleanValue()
@@ -206,6 +801,24 @@ public class Property
}
}
/**
* Returns the value in this property as a double, if the value is not a valid double, it will return the provided default.
*
* @param _default The default to provide if the current value is not a valid double
* @return The value
*/
public double getDouble()
{
try
{
return Double.parseDouble(value);
}
catch (NumberFormatException e)
{
return Double.parseDouble(defaultValue);
}
}
public String[] getStringList()
{
return values;
@@ -246,18 +859,19 @@ public class Property
*/
public boolean isIntList()
{
for (String value : values)
{
try
if (isList && type == Type.INTEGER)
for (String value : values)
{
Integer.parseInt(value);
try
{
Integer.parseInt(value);
}
catch (NumberFormatException e)
{
return false;
}
}
catch (NumberFormatException e)
{
return false;
}
}
return true;
return isList && type == Type.INTEGER;
}
/**
@@ -294,15 +908,16 @@ public class Property
*/
public boolean isBooleanList()
{
for (String value : values)
{
if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value))
if (isList && type == Type.BOOLEAN)
for (String value : values)
{
return false;
if (!"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value))
{
return false;
}
}
}
return true;
return isList && type == Type.BOOLEAN;
}
/**
@@ -339,34 +954,46 @@ public class Property
*/
public boolean isDoubleList()
{
for (String value : values)
{
try
if (isList && type == Type.DOUBLE)
for (String value : values)
{
Double.parseDouble(value);
try
{
Double.parseDouble(value);
}
catch (NumberFormatException e)
{
return false;
}
}
catch (NumberFormatException e)
{
return false;
}
}
return true;
return isList && type == Type.DOUBLE;
}
/**
* Gets the name/key for this Property.
*
* @return the Property name
*/
public String getName()
{
return name;
}
public void setName(String name)
/**
* Sets the name/key for this Property.
*
* @param name a name
*/
public Property setName(String name)
{
this.name = name;
return this;
}
/**
* Determines if this config value was just created, or if it was read from the config file.
* This is useful for mods who auto-assign there blocks to determine if the ID returned is
* This is useful for mods who auto-assign their blocks to determine if the ID returned is
* a configured one, or a automatically generated one.
*
* @return True if this property was loaded from the config file with a value
@@ -376,31 +1003,141 @@ public class Property
return wasRead;
}
/**
* Gets the Property.Type enum value for this Property.
*
* @return the Property's type
*/
public Type getType()
{
return type;
}
/**
* Returns whether or not this Property is a list/array.
*
* @return true if this Property is a list/array, false otherwise
*/
public boolean isList()
{
return isList;
}
/**
* Gets the changed status of this Property.
*
* @return true if this Property has changed, false otherwise
*/
public boolean hasChanged(){ return changed; }
void resetChangedState(){ changed = false; }
public void set(String value)
/**
* Sets the value of this Property to the provided String value.
*/
public Property setValue(String value)
{
this.value = value;
changed = true;
return this;
}
public void set(String value)
{
this.setValue(value);
}
/**
* Sets the values of this Property to the provided String[] values.
*/
public Property setValues(String[] values)
{
this.values = Arrays.copyOf(values, values.length);
changed = true;
return this;
}
public void set(String[] values)
{
this.values = values;
this.setValues(values);
}
/**
* Sets the value of this Property to the provided int value.
*/
public Property setValue(int value)
{
setValue(Integer.toString(value));
return this;
}
/**
* Sets the value of this Property to the provided boolean value.
*/
public Property setValue(boolean value)
{
setValue(Boolean.toString(value));
return this;
}
/**
* Sets the value of this Property to the provided double value.
*/
public Property setValue(double value)
{
setValue(Double.toString(value));
return this;
}
/**
* Sets the values of this Property to the provided boolean[] values.
*/
public Property setValues(boolean[] values)
{
this.values = new String[values.length];
for (int i = 0; i < values.length; i++)
this.values[i] = String.valueOf(values[i]);
changed = true;
return this;
}
public void set(boolean[] values)
{
this.setValues(values);
}
/**
* Sets the values of this Property to the provided int[] values.
*/
public Property setValues(int[] values)
{
this.values = new String[values.length];
for (int i = 0; i < values.length; i++)
this.values[i] = String.valueOf(values[i]);
changed = true;
return this;
}
public void set(int[] values)
{
this.setValues(values);
}
/**
* Sets the values of this Property to the provided double[] values.
*/
public Property setValues(double[] values)
{
this.values = new String[values.length];
for (int i = 0; i < values.length; i++)
this.values[i] = String.valueOf(values[i]);
changed = true;
return this;
}
public void set(double[] values)
{
this.setValues(values);
}
public void set(int value){ set(Integer.toString(value)); }
public void set(boolean value){ set(Boolean.toString(value)); }
public void set(double value){ set(Double.toString(value)); }