From cae1f490fb8a6830a6a38e38f094804ee5718c08 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 28 May 2019 09:12:08 +0200 Subject: [PATCH] Move sensibleConfig back into MineLP. This was a mistake. The static makes it less useful to other projects --- .../settings/SensibleConfig.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/main/java/com/minelittlepony/settings/SensibleConfig.java 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; + } +}