moved BlockMatcher configuration into main config file
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package mods.betterfoliage.common.config;
|
||||
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import cpw.mods.fml.client.config.GuiConfig;
|
||||
import cpw.mods.fml.client.config.GuiConfigEntries;
|
||||
import cpw.mods.fml.client.config.IConfigElement;
|
||||
|
||||
|
||||
public class AlternateTextBooleanEntry extends GuiConfigEntries.ButtonEntry {
|
||||
|
||||
protected final boolean beforeValue;
|
||||
protected boolean currentValue;
|
||||
|
||||
public AlternateTextBooleanEntry(GuiConfig owningScreen, GuiConfigEntries owningEntryList, IConfigElement<Boolean> configElement)
|
||||
{
|
||||
super(owningScreen, owningEntryList, configElement);
|
||||
this.beforeValue = Boolean.valueOf(configElement.get().toString());
|
||||
this.currentValue = beforeValue;
|
||||
this.btnValue.enabled = enabled();
|
||||
updateValueButtonText();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateValueButtonText()
|
||||
{
|
||||
this.btnValue.displayString = I18n.format(configElement.getLanguageKey() + "." + String.valueOf(currentValue));
|
||||
// btnValue.packedFGColour = currentValue ? GuiUtils.getColorCode('2', true) : GuiUtils.getColorCode('4', true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void valueButtonPressed(int slotIndex)
|
||||
{
|
||||
if (enabled())
|
||||
currentValue = !currentValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDefault()
|
||||
{
|
||||
return currentValue == Boolean.valueOf(configElement.getDefault().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setToDefault()
|
||||
{
|
||||
if (enabled())
|
||||
{
|
||||
currentValue = Boolean.valueOf(configElement.getDefault().toString());
|
||||
updateValueButtonText();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isChanged()
|
||||
{
|
||||
return currentValue != beforeValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undoChanges()
|
||||
{
|
||||
if (enabled())
|
||||
{
|
||||
currentValue = beforeValue;
|
||||
updateValueButtonText();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public boolean saveConfigElement()
|
||||
{
|
||||
if (enabled() && isChanged())
|
||||
{
|
||||
configElement.set(currentValue);
|
||||
return configElement.requiresMcRestart();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean getCurrentValue()
|
||||
{
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean[] getCurrentValues()
|
||||
{
|
||||
return new Boolean[] { getCurrentValue() };
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,10 @@ import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import mods.betterfoliage.BetterFoliage;
|
||||
import mods.betterfoliage.client.BlockMatcher;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.config.ConfigCategory;
|
||||
import net.minecraftforge.common.config.ConfigElement;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
@@ -18,11 +21,19 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
public class Config {
|
||||
|
||||
public enum Category {
|
||||
extraLeaves, shortGrass, cactus, lilypad, reed, algae, coral, fallingLeaves, connectedGrass;
|
||||
blockTypes, extraLeaves, shortGrass, cactus, lilypad, reed, algae, coral, fallingLeaves, connectedGrass;
|
||||
}
|
||||
|
||||
/** {@link Configuration} object bound to the config file */
|
||||
public static Configuration rawConfig;
|
||||
|
||||
// block matchers
|
||||
public static BlockMatcher leaves = new BlockMatcher();
|
||||
public static BlockMatcher crops = new BlockMatcher();
|
||||
public static BlockMatcher dirt = new BlockMatcher();
|
||||
public static BlockMatcher grass = new BlockMatcher();
|
||||
|
||||
// extracted config values
|
||||
public static boolean leavesEnabled;
|
||||
public static boolean leavesSkew;
|
||||
public static double leavesHOffset;
|
||||
@@ -46,7 +57,7 @@ public class Config {
|
||||
public static double reedHOffset;
|
||||
public static double reedHeightMin;
|
||||
public static double reedHeightMax;
|
||||
public static int reedChance;
|
||||
public static int reedPopulation;
|
||||
|
||||
public static boolean algaeEnabled;
|
||||
public static double algaeHOffset;
|
||||
@@ -75,25 +86,30 @@ public class Config {
|
||||
public static boolean ctxGrassClassicEnabled;
|
||||
public static boolean ctxGrassAggressiveEnabled;
|
||||
|
||||
/** Read the config file
|
||||
* @param configFile
|
||||
*/
|
||||
public static void readConfig(File configFile) {
|
||||
rawConfig = new Configuration(configFile, true);
|
||||
updateValues();
|
||||
if (rawConfig.hasChanged()) rawConfig.save();
|
||||
}
|
||||
|
||||
/** Extract the config properties to static value fields for quick access */
|
||||
public static void updateValues() {
|
||||
leavesEnabled = getBoolean(Category.extraLeaves, "enabled", true, "betterfoliage.enabled");
|
||||
leavesSkew = getBoolean(Category.extraLeaves, "skewMode", true, "betterfoliage.leavesMode");
|
||||
leavesSkew = getBoolean(Category.extraLeaves, "skewMode", false, "betterfoliage.leavesMode");
|
||||
leavesHOffset = getDouble(Category.extraLeaves, "hOffset", 0.2, 0.0, 0.4, "betterfoliage.hOffset");
|
||||
leavesVOffset = getDouble(Category.extraLeaves, "vOffset", 0.1, 0.0, 0.4, "betterfoliage.vOffset");
|
||||
leavesSize = getDouble(Category.extraLeaves, "size", 1.4, 0.75, 1.8, "betterfoliage.size");
|
||||
|
||||
grassEnabled = getBoolean(Category.shortGrass, "enabled", true, "betterfoliage.enabled");
|
||||
grassHOffset = getDouble(Category.shortGrass, "hOffset", 0.2, 0.0, 0.4, "betterfoliage.hOffset");
|
||||
grassHeightMin = getDouble(Category.shortGrass, "heightMin", 0.8, 0.1, 1.5, "betterfoliage.minHeight");
|
||||
grassHeightMin = getDouble(Category.shortGrass, "heightMin", 0.6, 0.1, 1.5, "betterfoliage.minHeight");
|
||||
grassHeightMax = getDouble(Category.shortGrass, "heightMax", 0.8, 0.1, 1.5, "betterfoliage.maxHeight");
|
||||
grassSize = getDouble(Category.shortGrass, "size", 1.0, 0.5, 1.5, "betterfoliage.size");
|
||||
grassUseGenerated = getBoolean(Category.shortGrass, "useGenerated", false, "betterfoliage.shortGrass.useGenerated");
|
||||
grassHeightMin = clampDoubleToMax(Category.shortGrass, "heightMin", "heightMax");
|
||||
|
||||
cactusEnabled = getBoolean(Category.cactus, "enabled", true, "betterfoliage.enabled");
|
||||
|
||||
@@ -105,7 +121,8 @@ public class Config {
|
||||
reedHOffset = getDouble(Category.reed, "hOffset", 0.2, 0.0, 0.4, "betterfoliage.hOffset");
|
||||
reedHeightMin = getDouble(Category.reed, "heightMin", 2.0, 1.5, 3.5, "betterfoliage.minHeight");
|
||||
reedHeightMax = getDouble(Category.reed, "heightMax", 2.5, 1.5, 3.5, "betterfoliage.maxHeight");
|
||||
reedChance = getInt(Category.reed, "chance", 32, 0, 64, "betterfoliage.chance");
|
||||
reedPopulation = getInt(Category.reed, "population", 32, 0, 64, "betterfoliage.population");
|
||||
reedHeightMin = clampDoubleToMax(Category.reed, "heightMin", "heightMax");
|
||||
|
||||
algaeEnabled = getBoolean(Category.algae, "enabled", true, "betterfoliage.enabled");
|
||||
algaeHOffset = getDouble(Category.algae, "hOffset", 0.1, 0.0, 0.25, "betterfoliage.hOffset");
|
||||
@@ -113,13 +130,14 @@ public class Config {
|
||||
algaeHeightMin = getDouble(Category.algae, "heightMin", 0.5, 0.1, 1.5, "betterfoliage.minHeight");
|
||||
algaeHeightMax = getDouble(Category.algae, "heightMax", 1.0, 0.1, 1.5, "betterfoliage.maxHeight");
|
||||
algaePopulation = getInt(Category.algae, "population", 48, 0, 64, "betterfoliage.population");
|
||||
|
||||
algaeHeightMin = clampDoubleToMax(Category.algae, "heightMin", "heightMax");
|
||||
|
||||
coralEnabled = getBoolean(Category.coral, "enabled", true, "betterfoliage.enabled");
|
||||
coralHOffset = getDouble(Category.coral, "hOffset", 0.2, 0.0, 0.4, "betterfoliage.hOffset");
|
||||
coralVOffset = getDouble(Category.coral, "vOffset", 0.1, 0.0, 0.4, "betterfoliage.vOffset");
|
||||
coralSize = getDouble(Category.coral, "size", 1.0, 0.5, 1.5, "betterfoliage.coral.size");
|
||||
coralCrustSize = getDouble(Category.coral, "crustSize", 1.0, 0.5, 1.5, "betterfoliage.coral.crustSize");
|
||||
coralChance = getInt(Category.coral, "chance", 48, 0, 64, "betterfoliage.coral.chance");
|
||||
coralSize = getDouble(Category.coral, "size", 0.7, 0.5, 1.5, "betterfoliage.coral.size");
|
||||
coralCrustSize = getDouble(Category.coral, "crustSize", 1.4, 0.5, 1.5, "betterfoliage.coral.crustSize");
|
||||
coralChance = getInt(Category.coral, "chance", 32, 0, 64, "betterfoliage.coral.chance");
|
||||
coralPopulation = getInt(Category.coral, "population", 48, 0, 64, "betterfoliage.population");
|
||||
|
||||
leafFXEnabled = getBoolean(Category.fallingLeaves, "enabled", true, "betterfoliage.enabled");
|
||||
@@ -129,18 +147,31 @@ public class Config {
|
||||
leafFXSize = getDouble(Category.fallingLeaves, "size", 0.75, 0.25, 1.5, "betterfoliage.fallingLeaves.size");
|
||||
leafFXChance = getDouble(Category.fallingLeaves, "chance", 0.05, 0.001, 1.0, "betterfoliage.fallingLeaves.chance");
|
||||
leafFXPerturb = getDouble(Category.fallingLeaves, "perturb", 0.25, 0.01, 1.0, "betterfoliage.fallingLeaves.perturb");
|
||||
leafFXLifetime = getDouble(Category.fallingLeaves, "lifetime", 5.0, 1.0, 10.0, "betterfoliage.fallingLeaves.lifetime");
|
||||
leafFXLifetime = getDouble(Category.fallingLeaves, "lifetime", 5.0, 1.0, 15.0, "betterfoliage.fallingLeaves.lifetime");
|
||||
|
||||
ctxGrassClassicEnabled = getBoolean(Category.connectedGrass, "classic", true, "betterfoliage.connectedGrass.classic");
|
||||
ctxGrassAggressiveEnabled= getBoolean(Category.connectedGrass, "aggressive", true, "betterfoliage.connectedGrass.aggressive");
|
||||
|
||||
updateBlockMatcher(dirt, Category.blockTypes, "dirtWhitelist", "betterfoliage.blockTypes.dirtWhitelist", "dirtBlacklist", "betterfoliage.blockTypes.dirtBlacklist", new ResourceLocation("betterfoliage:classesDirtDefault.cfg"));
|
||||
updateBlockMatcher(grass, Category.blockTypes, "grassWhitelist", "betterfoliage.blockTypes.grassWhitelist", "grassBlacklist", "betterfoliage.blockTypes.grassBlacklist", new ResourceLocation("betterfoliage:classesGrassDefault.cfg"));
|
||||
updateBlockMatcher(leaves, Category.blockTypes, "leavesWhitelist", "betterfoliage.blockTypes.leavesWhitelist", "leavesBlacklist", "betterfoliage.blockTypes.leavesBlacklist", new ResourceLocation("betterfoliage:classesLeavesDefault.cfg"));
|
||||
updateBlockMatcher(crops, Category.blockTypes, "cropWhitelist", "betterfoliage.blockTypes.cropWhitelist", "cropBlacklist", "betterfoliage.blockTypes.cropBlacklist", new ResourceLocation("betterfoliage:classesCropDefault.cfg"));
|
||||
|
||||
rawConfig.getCategory(Category.extraLeaves.toString()).get("skewMode").setConfigEntryClass(AlternateTextBooleanEntry.class);
|
||||
for (Category category : Category.values()) rawConfig.setCategoryLanguageKey(category.toString(), String.format("betterfoliage.%s", category.toString()));
|
||||
|
||||
setOrder(Category.extraLeaves, "enabled", "skewMode", "hOffset", "vOffset", "size");
|
||||
setOrder(Category.shortGrass, "enabled", "useGenerated", "hOffset", "heightMin", "heightMax", "size");
|
||||
setOrder(Category.lilypad, "enabled", "hOffset", "flowerChance");
|
||||
setOrder(Category.reed, "enabled", "hOffset", "heightMin", "heightMax", "population");
|
||||
setOrder(Category.algae, "enabled", "hOffset", "heightMin", "heightMax", "population");
|
||||
setOrder(Category.coral, "enabled", "hOffset", "vOffset", "size", "crustSize", "population", "chance");
|
||||
setOrder(Category.fallingLeaves, "enabled", "size", "chance", "lifetime", "speed", "windStrength", "stormStrength", "perturb");
|
||||
setOrder(Category.connectedGrass, "classic", "aggressive");
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
public static List<IConfigElement> getConfigRootCategories() {
|
||||
public static List<IConfigElement> getConfigRootElements() {
|
||||
List<IConfigElement> result = Lists.newLinkedList();
|
||||
for (Category category : Category.values()) {
|
||||
ConfigElement<?> element = new ConfigElement(rawConfig.getCategory(category.toString()));
|
||||
@@ -157,6 +188,14 @@ public class Config {
|
||||
return prop.getDouble();
|
||||
}
|
||||
|
||||
protected static double clampDoubleToMax(Category category, String keySmaller, String keyLarger) {
|
||||
ConfigCategory cfgCat = rawConfig.getCategory(category.toString());
|
||||
Property smaller = cfgCat.get(keySmaller);
|
||||
Property larger = cfgCat.get(keyLarger);
|
||||
if (smaller.getDouble() > larger.getDouble()) smaller.set(larger.getDouble());
|
||||
return smaller.getDouble();
|
||||
}
|
||||
|
||||
protected static int getInt(Category category, String key, int defaultValue, int min, int max, String langKey) {
|
||||
Property prop = rawConfig.get(category.toString(), key, defaultValue);
|
||||
prop.setMinValue(min);
|
||||
@@ -165,21 +204,37 @@ public class Config {
|
||||
return prop.getInt();
|
||||
}
|
||||
|
||||
protected static boolean getBoolean(Category category, String key, boolean defaultValue, String langKey) {
|
||||
Property prop = rawConfig.get(category.toString(), key, defaultValue);
|
||||
prop.setLanguageKey(langKey);
|
||||
return prop.getBoolean();
|
||||
protected static boolean getBoolean(Category category, String key, boolean defaultValue, String langKey) {
|
||||
Property prop = rawConfig.get(category.toString(), key, defaultValue);
|
||||
prop.setLanguageKey(langKey);
|
||||
return prop.getBoolean();
|
||||
}
|
||||
|
||||
protected static void updateBlockMatcher(BlockMatcher bm, Category category, String whitelistKey, String whitelistLangKey, String blacklistKey, String blacklistLangKey, ResourceLocation defaults) {
|
||||
List<String> defaultWhitelist = Lists.newLinkedList();
|
||||
List<String> defaultBlacklist = Lists.newLinkedList();
|
||||
BlockMatcher.loadDefaultLists(defaults, defaultBlacklist, defaultWhitelist);
|
||||
|
||||
Property whitelist = rawConfig.get(category.toString(), whitelistKey, defaultWhitelist.toArray(new String[]{}));
|
||||
Property blacklist = rawConfig.get(category.toString(), blacklistKey, defaultBlacklist.toArray(new String[]{}));
|
||||
|
||||
whitelist.setLanguageKey(whitelistLangKey);
|
||||
blacklist.setLanguageKey(blacklistLangKey);
|
||||
whitelist.setConfigEntryClass(NonVerboseArrayEntry.class);
|
||||
blacklist.setConfigEntryClass(NonVerboseArrayEntry.class);
|
||||
|
||||
bm.updateClassLists(whitelist.getStringList(), blacklist.getStringList());
|
||||
}
|
||||
|
||||
protected static void setOrder(Category category, String... properties) {
|
||||
rawConfig.setCategoryPropertyOrder(category.toString(), Lists.newArrayList(properties));
|
||||
}
|
||||
|
||||
|
||||
@SubscribeEvent
|
||||
public void handleConfigChange(ConfigChangedEvent.OnConfigChangedEvent event) {
|
||||
if (event.modID.equals(BetterFoliage.MOD_ID)) {
|
||||
rawConfig.save();
|
||||
updateValues();
|
||||
if (rawConfig.hasChanged()) rawConfig.save();
|
||||
Minecraft.getMinecraft().renderGlobal.loadRenderers();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package mods.betterfoliage.common.config;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import net.minecraft.client.resources.I18n;
|
||||
import net.minecraft.util.EnumChatFormatting;
|
||||
import cpw.mods.fml.client.config.GuiConfig;
|
||||
import cpw.mods.fml.client.config.GuiConfigEntries;
|
||||
import cpw.mods.fml.client.config.IConfigElement;
|
||||
|
||||
|
||||
public class NonVerboseArrayEntry extends GuiConfigEntries.ArrayEntry {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public NonVerboseArrayEntry(GuiConfig owningScreen, GuiConfigEntries owningEntryList, IConfigElement<?> configElement) {
|
||||
super(owningScreen, owningEntryList, configElement);
|
||||
|
||||
// strip defaults from tooltip as they can get quite long
|
||||
boolean defaultRows = false;
|
||||
Iterator<String> iter = toolTip.iterator();
|
||||
while(iter.hasNext()) {
|
||||
if (iter.next().startsWith(EnumChatFormatting.AQUA.toString())) defaultRows = true;
|
||||
if (defaultRows) iter.remove();
|
||||
}
|
||||
|
||||
String shortDefaults = I18n.format("betterfoliage.arrayEntryDisplay", configElement.getDefaults().length);
|
||||
toolTip.addAll(this.mc.fontRenderer.listFormattedStringToWidth(EnumChatFormatting.AQUA + I18n.format("fml.configgui.tooltip.default", shortDefaults),300));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateValueButtonText() {
|
||||
this.btnValue.displayString = I18n.format("betterfoliage.arrayEntryDisplay", currentValues.length);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user