Skip to content

Commit

Permalink
Allow config GUI cycling button elements generated from enums to disp…
Browse files Browse the repository at this point in the history
…lay toString return values, rather than actual values. (#5125)
  • Loading branch information
Phylogeny authored and tterrag1098 committed Jan 4, 2019
1 parent 3dd8c8a commit d3f27cf
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ public String[] getValidValues()
return isProperty ? prop.getValidValues() : null;
}

@Override
public String[] getValidValuesDisplay()
{
return isProperty ? prop.getValidValuesDisplay() : null;
}

@Override
public String getLanguageKey()
{
Expand Down
103 changes: 101 additions & 2 deletions src/main/java/net/minecraftforge/common/config/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,25 @@ public Property get(String category, String key, String defaultValue, String com
return prop;
}

/**
* Gets a string Property with a comment using the defined validValues array and otherwise default settings.
*
* @param category the config category
* @param key the Property key value
* @param defaultValue the default value
* @param comment a String comment
* @param validValues an array of valid values that this Property can be set to. If an array is provided the Config GUI control will be
* a value cycle button.
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
* @return a string Property with the defined validValues array, validationPattern = null
*/
public Property get(String category, String key, String defaultValue, String comment, String[] validValues, String[] validValuesDisplay)
{
Property prop = get(category, key, defaultValue, comment, validValues);
prop.setValidValuesDisplay(validValuesDisplay);
return prop;
}

/**
* Gets a string array Property without a comment using the default settings.
*
Expand Down Expand Up @@ -1541,6 +1560,22 @@ public String getString(String name, String category, String defaultValue, Strin
return getString(name, category, defaultValue, comment, validValues, name);
}

/**
* Creates a string property.
*
* @param name Name of the property.
* @param category Category of the property.
* @param defaultValue Default value of the property.
* @param comment A brief description what the property does.
* @param validValues A list of valid values that this property can be set to.
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
* @return The value of the new string property.
*/
public String getString(String name, String category, String defaultValue, String comment, String[] validValues, String[] validValuesDisplay)
{
return setPropertyAndGetString(name, category, defaultValue, comment, validValues, validValuesDisplay, name);
}

/**
* Creates a string property.
*
Expand All @@ -1553,9 +1588,32 @@ public String getString(String name, String category, String defaultValue, Strin
* @return The value of the new string property.
*/
public String getString(String name, String category, String defaultValue, String comment, String[] validValues, String langKey)
{
return setPropertyAndGetString(name, category, defaultValue, comment, validValues, null, langKey);
}

/**
* Creates a string property.
*
* @param name Name of the property.
* @param category Category of the property.
* @param defaultValue Default value of the property.
* @param comment A brief description what the property does.
* @param validValues A list of valid values that this property can be set to.
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
* @param langKey A language key used for localization of GUIs
* @return The value of the new string property.
*/
public String getString(String name, String category, String defaultValue, String comment, String[] validValues, String[] validValuesDisplay, String langKey)
{
return setPropertyAndGetString(name, category, defaultValue, comment, validValues, validValuesDisplay, langKey);
}

private String setPropertyAndGetString(String name, String category, String defaultValue, String comment, String[] validValues, String[] validValuesDisplay, String langKey)
{
Property prop = this.get(category, name, defaultValue);
prop.setValidValues(validValues);
prop.setValidValuesDisplay(validValuesDisplay);
prop.setLanguageKey(langKey);
prop.setComment(comment + " [default: " + defaultValue + "]");
return prop.getString();
Expand Down Expand Up @@ -1596,14 +1654,55 @@ public String[] getStringList(String name, String category, String[] defaultValu
* @param category Category of the property.
* @param defaultValue Default value of the property.
* @param comment A brief description what the property does.
* @param validValues A list of valid values that this property can be set to.
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
* @return The value of the new string property.
*/
public String[] getStringList(String name, String category, String[] defaultValue, String comment, String[] validValues, String[] validValuesDisplay)
{
return setPropertyAndGetStringList(name, category, defaultValue, comment, validValues, validValuesDisplay, name);
}

/**
* Creates a string list property.
*
* @param name Name of the property.
* @param category Category of the property.
* @param defaultValue Default value of the property.
* @param comment A brief description what the property does.
* @param validValues A list of valid values that this property can be set to.
* @param langKey A language key used for localization of GUIs
* @return The value of the new string property.
*/
public String[] getStringList(String name, String category, String[] defaultValue, String comment, String[] validValues, String langKey)
{
return setPropertyAndGetStringList(name, category, defaultValue, comment, validValues, null, langKey);
}

/**
* Creates a string list property.
*
* @param name Name of the property.
* @param category Category of the property.
* @param defaultValue Default value of the property.
* @param comment A brief description what the property does.
* @param validValues A list of valid values that this property can be set to.
* @param validValuesDisplay an array of the config GUI display versions of the valid values that this Property can be set to.
* @param langKey A language key used for localization of GUIs
* @return The value of the new string property.
*/
public String[] getStringList(String name, String category, String[] defaultValue, String comment, String[] validValues, String[] validValuesDisplay, String langKey)
{
return setPropertyAndGetStringList(name, category, defaultValue, comment, validValues, validValuesDisplay, langKey);
}

private String[] setPropertyAndGetStringList(String name, String category, String[] defaultValue, String comment, String[] validValues, String[] validValuesDisplay, String langKey)
{
Property prop = this.get(category, name, defaultValue);
prop.setLanguageKey(langKey);
prop.setValidValues(validValues);
prop.setComment(comment + " [default: " + prop.getDefault() + "]");
prop.setValidValuesDisplay(validValuesDisplay);
prop.setLanguageKey(langKey);
prop.setComment(comment + " [default: " + defaultValue + "]");
return prop.getStringList();
}

Expand Down
15 changes: 10 additions & 5 deletions src/main/java/net/minecraftforge/common/config/FieldWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,19 @@ public void setupConfiguration(Configuration cfg, String desc, String langKey, b

Property prop = cfg.getCategory(this.category).get(this.name); // Will be setup in general by ConfigManager

List<String> lst = Lists.newArrayList();
List<String> listValidValues = Lists.newArrayList();
List<String> listValidValuesDisplay = Lists.newArrayList();
for (Enum e : ((Class<? extends Enum>) field.getType()).getEnumConstants())
lst.add(e.name());
{
listValidValues.add(e.name());
listValidValuesDisplay.add(e.toString());
}

prop.setValidationPattern(Pattern.compile(PIPE.join(lst)));
prop.setValidValues(lst.toArray(new String[0]));
prop.setValidationPattern(Pattern.compile(PIPE.join(listValidValues)));
prop.setValidValues(listValidValues.toArray(new String[0]));
prop.setValidValuesDisplay(listValidValuesDisplay.toArray(new String[0]));

String validValues = NEW_LINE.join(lst);
String validValues = NEW_LINE.join(listValidValues);

if (desc != null)
prop.setComment(NEW_LINE.join(new String[] { desc, "Valid values:" }) + "\n" + validValues);
Expand Down
56 changes: 48 additions & 8 deletions src/main/java/net/minecraftforge/common/config/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public char getID()
private String[] values;
private String[] defaultValues;
private String[] validValues;
private String[] validValuesDisplay;
private String langKey;
private String minValue;
private String maxValue;
Expand All @@ -98,35 +99,45 @@ public char getID()

public Property(String name, String value, Type type)
{
this(name, value, type, false, new String[0], name);
this(name, value, type, false, new String[0], new String[0], name);
}

public Property(String name, String value, Type type, boolean read)
{
this(name, value, type, read, new String[0], name);
this(name, value, type, read, new String[0], new String[0], name);
}

public Property(String name, String value, Type type, String[] validValues)
{
this(name, value, type, false, validValues, name);
this(name, value, type, false, validValues, new String[0], name);
}

public Property(String name, String value, Type type, String[] validValues, String[] validValuesDisplay)
{
this(name, value, type, false, validValues, validValuesDisplay, name);
}

public Property(String name, String value, Type type, String langKey)
{
this(name, value, type, false, new String[0], langKey);
this(name, value, type, false, new String[0], 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);
this(name, value, type, read, new String[0], new String[0], langKey);
}

public Property(String name, String value, Type type, String[] validValues, String langKey)
{
this(name, value, type, false, validValues, langKey);
this(name, value, type, false, validValues, new String[0], langKey);
}

public Property(String name, String value, Type type, String[] validValues, String[] validValuesDisplay, String langKey)
{
this(name, value, type, false, validValues, validValuesDisplay, langKey);
}

Property(String name, String value, Type type, boolean read, String[] validValues, String langKey)
Property(String name, String value, Type type, boolean read, String[] validValues, String[] validValuesDisplay, String langKey)
{
setName(name);
this.value = value;
Expand All @@ -137,6 +148,7 @@ public Property(String name, String value, Type type, String[] validValues, Stri
this.defaultValue = value;
this.defaultValues = new String[0];
this.validValues = validValues;
this.validValuesDisplay = validValues;
this.isListLengthFixed = false;
this.maxListLength = -1;
this.minValue = String.valueOf(Integer.MIN_VALUE);
Expand Down Expand Up @@ -166,6 +178,11 @@ public Property(String name, String[] values, Type type, String langKey)
}

Property(String name, String[] values, Type type, boolean read, String[] validValues, String langKey)
{
this(name, values, type, read, validValues, new String[0], langKey);
}

Property(String name, String[] values, Type type, boolean read, String[] validValues, String[] validValuesDisplay, String langKey)
{
setName(name);
this.type = type;
Expand All @@ -179,6 +196,7 @@ public Property(String name, String[] values, Type type, String langKey)
this.defaultValue = this.defaultValue.replaceFirst(", ", "");
this.defaultValues = Arrays.copyOf(values, values.length);
this.validValues = validValues;
this.validValuesDisplay = validValues;
this.isListLengthFixed = false;
this.maxListLength = -1;
this.minValue = String.valueOf(Integer.MIN_VALUE);
Expand Down Expand Up @@ -692,7 +710,7 @@ public Property setValidValues(String[] validValues)
}

/**
* Gets the array of valid values that this String Property can be set to, or null if not defined.
* Gets the array of valid values that this String Property can be set to, or null or empty if not defined.
*
* @return a String array of valid values
*/
Expand All @@ -701,6 +719,28 @@ public String[] getValidValues()
return this.validValues;
}

/**
* Sets the array of the config GUI display versions of the 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 validValueAliases a String array of the aliases of valid values
*/
public Property setValidValuesDisplay(String[] validValuesDisplay)
{
this.validValuesDisplay = validValuesDisplay;
return this;
}

/**
* Gets the array of the config GUI display versions of the valid values that this String Property can be set to, or null or empty if not defined.
*
* @return a String array of the aliases of the valid values
*/
public String[] getValidValuesDisplay()
{
return this.validValuesDisplay;
}

/**
* Returns the value in this property as an integer,
* if the value is not a valid integer, it will return the initially provided default.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class DummyConfigElement implements IConfigElement
protected Object[] values;
protected Object[] defaultValues;
protected String[] validValues;
protected String[] validValuesDisplay;
protected Pattern validStringPattern;
protected Object minValue;
protected Object maxValue;
Expand Down Expand Up @@ -165,15 +166,16 @@ public Object getDefault()
return Arrays.toString(this.defaultValues);
}
}
public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, String[] validValues, Pattern validStringPattern, Object minValue, Object maxValue)

public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, String[] validValues, String[] validValuesDisplay, Pattern validStringPattern, Object minValue, Object maxValue)
{
this.name = name;
this.defaultValue = defaultValue;
this.value = defaultValue;
this.type = type;
this.langKey = langKey;
this.validValues = validValues;
this.validValuesDisplay = validValuesDisplay;
this.validStringPattern = validStringPattern;
if (minValue == null)
{
Expand All @@ -194,7 +196,12 @@ else if (type == ConfigGuiType.DOUBLE)
else
this.maxValue = maxValue;
}


public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, String[] validValues, Pattern validStringPattern, Object minValue, Object maxValue)
{
this(name, defaultValue, type, langKey, validValues, null, validStringPattern, minValue, maxValue);
}

public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, Pattern validStringPattern)
{
this(name, defaultValue, type, langKey, null, validStringPattern, null, null);
Expand All @@ -204,7 +211,12 @@ public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type,
{
this(name, defaultValue, type, langKey, validValues, null, null, null);
}


public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey, String[] validValues, String[] validValuesDisplay)
{
this(name, defaultValue, type, langKey, validValues, validValuesDisplay, null, null, null);
}

public DummyConfigElement(String name, Object defaultValue, ConfigGuiType type, String langKey)
{
this(name, defaultValue, type, langKey, null, null, null, null);
Expand Down Expand Up @@ -382,6 +394,12 @@ public String[] getValidValues()
{
return validValues;
}

@Override
public String[] getValidValuesDisplay()
{
return validValuesDisplay;
}

@Override
public Pattern getValidationPattern()
Expand Down

0 comments on commit d3f27cf

Please sign in to comment.