Add a helper to ensure that the String value we get from the configuration file is a valid one

This commit is contained in:
Pahimar 2014-07-18 18:21:56 -04:00
parent 4ea086f1b8
commit 6fd0b0de79
2 changed files with 37 additions and 1 deletions

View file

@ -3,6 +3,8 @@ package com.pahimar.ee3.handler;
import com.pahimar.ee3.reference.Messages;
import com.pahimar.ee3.reference.Reference;
import com.pahimar.ee3.reference.Settings;
import com.pahimar.ee3.util.ConfigurationHelper;
import com.pahimar.ee3.util.LogHelper;
import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.util.StatCollector;
@ -25,8 +27,9 @@ public class ConfigurationHandler
private static void loadConfiguration()
{
Settings.TRANSMUTATION_KNOWLEDGE_MODE = configuration.getString(Messages.Configuration.TRANSMUTATION_KNOWLEDGE_MODE, Messages.Configuration.CATEGORY_TRANSMUTATION, "All", StatCollector.translateToLocal(Messages.Configuration.TRANSMUTATION_KNOWLEDGE_MODE_COMMENT), new String[]{"All", "Select", "None"}, Messages.Configuration.TRANSMUTATION_KNOWLEDGE_MODE_LABEL);
Settings.TRANSMUTATION_KNOWLEDGE_MODE = ConfigurationHelper.getString(configuration, Messages.Configuration.TRANSMUTATION_KNOWLEDGE_MODE, Messages.Configuration.CATEGORY_TRANSMUTATION, "All", StatCollector.translateToLocal(Messages.Configuration.TRANSMUTATION_KNOWLEDGE_MODE_COMMENT), new String[]{"All", "Select", "None"}, Messages.Configuration.TRANSMUTATION_KNOWLEDGE_MODE_LABEL);
LogHelper.info(Settings.TRANSMUTATION_KNOWLEDGE_MODE);
if (configuration.hasChanged())
{
configuration.save();

View file

@ -0,0 +1,33 @@
package com.pahimar.ee3.util;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;
public class ConfigurationHelper
{
public static String getString(Configuration configuration, String name, String category, String defaultValue, String comment, String[] validValues, String langKey)
{
Property property = configuration.get(category, name, defaultValue);
property.setValidValues(validValues);
property.setLanguageKey(langKey);
property.comment = comment + " [default: " + defaultValue + "]";
String value = property.getString();
boolean isValueValid = false;
for (int i = 0; i < validValues.length; i++)
{
if (value.equalsIgnoreCase(validValues[i]))
{
isValueValid = true;
value = validValues[i];
}
}
if (isValueValid)
{
return value;
}
return defaultValue;
}
}