consolidated config handling
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
package mods.betterfoliage.common.config;
|
||||
|
||||
public class BetterFoliageConfig extends ConfigBase {
|
||||
|
||||
@CfgElement(category="leaves", key="enabled")
|
||||
public boolean leavesEnabled = true;
|
||||
|
||||
@CfgElement(category="leaves", key="skewMode")
|
||||
public boolean leavesSkew = false;
|
||||
|
||||
@CfgElement(category="grass", key="enabled")
|
||||
public boolean grassEnabled = true;
|
||||
|
||||
@CfgElement(category="cactus", key="enabled")
|
||||
public boolean cactusEnabled = true;
|
||||
|
||||
@CfgElement(category="lilypad", key="enabled")
|
||||
public boolean lilypadEnabled = true;
|
||||
|
||||
@CfgElement(category="reed", key="enabled")
|
||||
public boolean reedEnabled = true;
|
||||
|
||||
@CfgElement(category="leaves", key="horizontalOffset")
|
||||
public OptionDouble leavesHOffset = new OptionDouble(0.0, 0.4, 0.025, 0.2);
|
||||
|
||||
@CfgElement(category="leaves", key="verticalOffset")
|
||||
public OptionDouble leavesVOffset = new OptionDouble(0.0, 0.4, 0.025, 0.1);
|
||||
|
||||
@CfgElement(category="leaves", key="size")
|
||||
public OptionDouble leavesSize = new OptionDouble(0.75, 1.8, 0.05, 1.4);
|
||||
|
||||
@CfgElement(category="grass", key="horizontalOffset")
|
||||
public OptionDouble grassHOffset = new OptionDouble(0.0, 0.4, 0.025, 0.2);
|
||||
|
||||
@CfgElement(category="grass", key="heightMin")
|
||||
@Limit(max="grassHeightMax")
|
||||
public OptionDouble grassHeightMin = new OptionDouble(0.1, 1.5, 0.05, 0.5);
|
||||
|
||||
@CfgElement(category="grass", key="heightMax")
|
||||
public OptionDouble grassHeightMax = new OptionDouble(0.1, 1.5, 0.05, 1.0);
|
||||
|
||||
@CfgElement(category="grass", key="size")
|
||||
public OptionDouble grassSize = new OptionDouble(0.5, 1.5, 0.05, 1.0);
|
||||
|
||||
@CfgElement(category="lilypad", key="horizontalOffset")
|
||||
public OptionDouble lilypadHOffset = new OptionDouble(0.0, 0.25, 0.025, 0.1);
|
||||
|
||||
@CfgElement(category="lilypad", key="chance")
|
||||
public OptionInteger lilypadChance = new OptionInteger(0, 64, 1, 16);
|
||||
|
||||
@CfgElement(category="reed", key="horizontalOffset")
|
||||
public OptionDouble reedHOffset = new OptionDouble(0.0, 0.25, 0.025, 0.1);
|
||||
|
||||
@CfgElement(category="reed", key="heightMin")
|
||||
@Limit(max="reedHeightMax")
|
||||
public OptionDouble reedHeightMin = new OptionDouble(1.5, 3.5, 0.1, 2.0);
|
||||
|
||||
@CfgElement(category="reed", key="heightMax")
|
||||
public OptionDouble reedHeightMax = new OptionDouble(1.5, 3.5, 0.1, 2.5);
|
||||
|
||||
@CfgElement(category="reed", key="chance")
|
||||
public OptionInteger reedChance = new OptionInteger(0, 64, 1, 32);
|
||||
|
||||
}
|
||||
@@ -1,125 +0,0 @@
|
||||
package mods.betterfoliage.common.config;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import mods.betterfoliage.BetterFoliage;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
|
||||
public class Config {
|
||||
|
||||
public static boolean leavesEnabled = true;
|
||||
public static boolean leavesSkew = false;
|
||||
public static boolean grassEnabled = true;
|
||||
public static boolean cactusEnabled = true;
|
||||
public static boolean lilypadEnabled = true;
|
||||
public static boolean reedEnabled = true;
|
||||
|
||||
public static OptionDouble leavesHOffset = new OptionDouble(0.0, 0.4, 0.025, 0.2);
|
||||
public static OptionDouble leavesVOffset = new OptionDouble(0.0, 0.4, 0.025, 0.1);
|
||||
public static OptionDouble leavesSize = new OptionDouble(0.75, 1.8, 0.05, 1.4);
|
||||
|
||||
public static OptionDouble grassHOffset = new OptionDouble(0.0, 0.4, 0.025, 0.2);
|
||||
public static OptionDouble grassHeightMin = new OptionDouble(0.1, 1.5, 0.05, 0.5);
|
||||
public static OptionDouble grassHeightMax = new OptionDouble(0.1, 1.5, 0.05, 1.0);
|
||||
public static OptionDouble grassSize = new OptionDouble(0.5, 1.5, 0.05, 1.0);
|
||||
|
||||
public static OptionDouble lilypadHOffset = new OptionDouble(0.0, 0.25, 0.025, 0.1);
|
||||
public static OptionInteger lilypadChance = new OptionInteger(0, 64, 1, 16);
|
||||
|
||||
public static OptionDouble reedHOffset = new OptionDouble(0.0, 0.25, 0.025, 0.1);
|
||||
public static OptionDouble reedHeightMin = new OptionDouble(1.5, 3.5, 0.1, 2.0);
|
||||
public static OptionDouble reedHeightMax = new OptionDouble(1.5, 3.5, 0.1, 2.5);
|
||||
public static OptionInteger reedChance = new OptionInteger(0, 64, 1, 32);
|
||||
|
||||
private Config() {}
|
||||
|
||||
public static void load() {
|
||||
Configuration config = new Configuration(new File(BetterFoliage.configDir, "betterfoliage.cfg"));
|
||||
config.load();
|
||||
|
||||
leavesEnabled = config.get("render", "leavesEnabled", true).getBoolean(true);
|
||||
leavesSkew = config.get("render", "leavesOffsetSkew", false).getBoolean(false);
|
||||
loadValue(config, "render", "leavesHorizontalOffset", leavesHOffset);
|
||||
loadValue(config, "render", "leavesVerticalOffset", leavesVOffset);
|
||||
loadValue(config, "render", "leavesSize", leavesSize);
|
||||
|
||||
grassEnabled = config.get("render", "grassEnabled", true).getBoolean(true);
|
||||
loadValue(config, "render", "grassHorizontalOffset", grassHOffset);
|
||||
loadValue(config, "render", "grassHeightMin", grassHeightMin);
|
||||
loadValue(config, "render", "grassHeightMax", grassHeightMax);
|
||||
if (grassHeightMin.value > grassHeightMax.value) grassHeightMin.value = grassHeightMax.value;
|
||||
|
||||
grassEnabled = config.get("render", "cactusEnabled", true).getBoolean(true);
|
||||
grassEnabled = config.get("render", "lilypadEnabled", true).getBoolean(true);
|
||||
loadValue(config, "render", "lilypadHorizontalOffset", lilypadHOffset);
|
||||
loadValue(config, "render", "lilypadChance", lilypadChance);
|
||||
|
||||
reedEnabled = config.get("render", "reedEnabled", true).getBoolean(true);
|
||||
loadValue(config, "render", "reedHeightMin", reedHeightMin);
|
||||
loadValue(config, "render", "reedHeightMax", reedHeightMax);
|
||||
loadValue(config, "render", "reedChance", reedChance);
|
||||
if (reedHeightMin.value > reedHeightMax.value) reedHeightMin.value = reedHeightMax.value;
|
||||
|
||||
if (config.hasChanged()) config.save();
|
||||
}
|
||||
|
||||
public static void save() {
|
||||
Configuration config = new Configuration(new File(BetterFoliage.configDir, "betterfoliage.cfg"));
|
||||
config.load();
|
||||
|
||||
config.get("render", "leavesEnabled", true).set(leavesEnabled);
|
||||
config.get("render", "leavesOffsetSkew", false).set(leavesSkew);
|
||||
saveValue(config, "render", "leavesHorizontalOffset", leavesHOffset);
|
||||
saveValue(config, "render", "leavesVerticalOffset", leavesVOffset);
|
||||
saveValue(config, "render", "leavesSize", leavesSize);
|
||||
|
||||
config.get("render", "grassEnabled", true).set(grassEnabled);
|
||||
saveValue(config, "render", "grassHorizontalOffset", grassHOffset);
|
||||
saveValue(config, "render", "grassHeightMin", grassHeightMin);
|
||||
saveValue(config, "render", "grassHeightMax", grassHeightMax);
|
||||
|
||||
config.get("render", "cactusEnabled", true).set(cactusEnabled);
|
||||
config.get("render", "lilypadEnabled", true).set(lilypadEnabled);
|
||||
saveValue(config, "render", "lilypadHorizontalOffset", lilypadHOffset);
|
||||
saveValue(config, "render", "lilypadChance", lilypadChance);
|
||||
|
||||
config.get("render", "reedEnabled", true).set(reedEnabled);
|
||||
saveValue(config, "render", "reedHeightMin", reedHeightMin);
|
||||
saveValue(config, "render", "reedHeightMax", reedHeightMax);
|
||||
saveValue(config, "render", "reedChance", reedChance);
|
||||
|
||||
if (config.hasChanged()) config.save();
|
||||
}
|
||||
|
||||
protected static void saveValue(Configuration config, String category, String key, OptionDouble option) {
|
||||
config.get(category, key, option.value).set(option.value);
|
||||
}
|
||||
|
||||
protected static void loadValue(Configuration config, String category, String key, OptionDouble option) {
|
||||
option.value = config.get(category, key, option.value).getDouble(option.value);
|
||||
if (option.value > option.max) {
|
||||
option.value = option.max;
|
||||
saveValue(config, category, key, option);
|
||||
}
|
||||
if (option.value < option.min) {
|
||||
option.value = option.min;
|
||||
saveValue(config, category, key, option);
|
||||
}
|
||||
}
|
||||
|
||||
protected static void saveValue(Configuration config, String category, String key, OptionInteger option) {
|
||||
config.get(category, key, option.value).set(option.value);
|
||||
}
|
||||
|
||||
protected static void loadValue(Configuration config, String category, String key, OptionInteger option) {
|
||||
option.value = config.get(category, key, option.value).getInt(option.value);
|
||||
if (option.value > option.max) {
|
||||
option.value = option.max;
|
||||
saveValue(config, category, key, option);
|
||||
}
|
||||
if (option.value < option.min) {
|
||||
option.value = option.min;
|
||||
saveValue(config, category, key, option);
|
||||
}
|
||||
}
|
||||
}
|
||||
128
src/main/java/mods/betterfoliage/common/config/ConfigBase.java
Normal file
128
src/main/java/mods/betterfoliage/common/config/ConfigBase.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package mods.betterfoliage.common.config;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
|
||||
public class ConfigBase {
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public static @interface CfgElement {
|
||||
String category();
|
||||
String key();
|
||||
String comment() default "";
|
||||
}
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public static @interface Limit {
|
||||
String min() default "";
|
||||
String max() default "";
|
||||
}
|
||||
|
||||
protected Configuration config;
|
||||
|
||||
public void load(File configFile) {
|
||||
config = new Configuration(configFile);
|
||||
config.load();
|
||||
|
||||
for (Field field : getClass().getDeclaredFields()) {
|
||||
CfgElement annot = field.getAnnotation(CfgElement.class);
|
||||
if (annot == null) continue;
|
||||
|
||||
field.setAccessible(true);
|
||||
if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
|
||||
try {
|
||||
Property prop = config.get(annot.category(), annot.key(), field.getBoolean(this));
|
||||
field.setBoolean(this, prop.getBoolean());
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} else if (field.getType().equals(OptionInteger.class)) {
|
||||
try {
|
||||
OptionInteger option = (OptionInteger) field.get(this);
|
||||
Property prop = config.get(annot.category(), annot.key(), option.value);
|
||||
option.value = prop.getInt();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} else if (field.getType().equals(OptionDouble.class)) {
|
||||
try {
|
||||
OptionDouble option = (OptionDouble) field.get(this);
|
||||
Property prop = config.get(annot.category(), annot.key(), option.value);
|
||||
option.value = prop.getDouble();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
validateLimits();
|
||||
if (config.hasChanged()) config.save();
|
||||
}
|
||||
|
||||
protected void validateLimits() {
|
||||
for (Field fieldThis : getClass().getDeclaredFields()) {
|
||||
Limit annot = fieldThis.getAnnotation(Limit.class);
|
||||
if (annot == null) continue;
|
||||
|
||||
try {
|
||||
Field fieldMin = annot.min().isEmpty() ? null : getClass().getDeclaredField(annot.min());
|
||||
Field fieldMax = annot.max().isEmpty() ? null : getClass().getDeclaredField(annot.max());
|
||||
fieldThis.setAccessible(true);
|
||||
fieldMin.setAccessible(true);
|
||||
fieldMax.setAccessible(true);
|
||||
|
||||
if (fieldThis.getType().equals(OptionInteger.class)) {
|
||||
OptionInteger optionThis = (OptionInteger) fieldThis.get(this);
|
||||
OptionInteger optionMin = fieldMin == null ? null : (OptionInteger) fieldMin.get(this);
|
||||
OptionInteger optionMax = fieldMax == null ? null : (OptionInteger) fieldMax.get(this);
|
||||
if (optionMin != null) optionThis.value = Math.max(optionThis.value, optionMin.value);
|
||||
if (optionMax != null) optionThis.value = Math.min(optionThis.value, optionMax.value);
|
||||
} else if (fieldThis.getType().equals(OptionDouble.class)) {
|
||||
OptionDouble optionThis = (OptionDouble) fieldThis.get(this);
|
||||
OptionDouble optionMin = fieldMin == null ? null : (OptionDouble) fieldMin.get(this);
|
||||
OptionDouble optionMax = fieldMax == null ? null : (OptionDouble) fieldMax.get(this);
|
||||
if (optionMin != null) optionThis.value = Math.max(optionThis.value, optionMin.value);
|
||||
if (optionMax != null) optionThis.value = Math.min(optionThis.value, optionMax.value);
|
||||
}
|
||||
} catch (Exception e) {}
|
||||
}
|
||||
}
|
||||
|
||||
public void save() {
|
||||
for (Field field : getClass().getDeclaredFields()) {
|
||||
CfgElement annot = field.getAnnotation(CfgElement.class);
|
||||
if (annot == null) continue;
|
||||
|
||||
field.setAccessible(true);
|
||||
if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
|
||||
try {
|
||||
Property prop = config.get(annot.category(), annot.key(), field.getBoolean(this));
|
||||
prop.set(field.getBoolean(this));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} else if (field.getType().equals(OptionInteger.class)) {
|
||||
try {
|
||||
OptionInteger option = (OptionInteger) field.get(this);
|
||||
Property prop = config.get(annot.category(), annot.key(), option.value);
|
||||
prop.set(option.value);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} else if (field.getType().equals(OptionDouble.class)) {
|
||||
try {
|
||||
OptionDouble option = (OptionDouble) field.get(this);
|
||||
Property prop = config.get(annot.category(), annot.key(), option.value);
|
||||
prop.set(option.value);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
config.save();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user