diff --git a/src/main/java/com/minelittlepony/settings/SensibleConfig.java b/src/main/java/com/minelittlepony/settings/SensibleConfig.java new file mode 100644 index 00000000..97327160 --- /dev/null +++ b/src/main/java/com/minelittlepony/settings/SensibleConfig.java @@ -0,0 +1,61 @@ +package com.minelittlepony.settings; + +import com.minelittlepony.common.client.gui.IField.IChangeCallback; + +/** + * A sensible config container that actually lets us programmatically index values by a key. + * + * Reflection because Mumfrey pls. + * + */ +// Mumfrey pls. +public abstract class SensibleConfig { + + private static SensibleConfig instance; + + public SensibleConfig() { + instance = this; + } + + public abstract void save(); + + public interface Setting extends IChangeCallback { + String name(); + + /** + * Gets the config value associated with this entry. + */ + default boolean get() { + return instance.getValue(this); + } + + /** + * Sets the config value associated with this entry. + */ + default void set(boolean value) { + instance.setValue(this, value); + } + + @Override + default Boolean perform(Boolean v) { + set(v); + return v; + } + } + + public boolean getValue(Setting key) { + try { + return getClass().getField(key.name().toLowerCase()).getBoolean(this); + } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException ignored) { + return true; + } + } + + public boolean setValue(Setting key, boolean value) { + try { + getClass().getField(key.name().toLowerCase()).setBoolean(this, value); + } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException ignored) { + } + return value; + } +}