From 538c9c1b89f323f1fc4298a69bd5922be100d0dd Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 30 Jul 2018 15:14:41 +0200 Subject: [PATCH] Rewrite pony configs --- .../com/minelittlepony/MineLittlePony.java | 2 +- .../java/com/minelittlepony/PonyConfig.java | 106 +++--------------- .../com/minelittlepony/PonyRenderManager.java | 2 +- .../java/com/minelittlepony/gui/Checkbox.java | 13 +-- .../minelittlepony/gui/GuiPonySettings.java | 12 +- .../com/minelittlepony/gui/IActionable.java | 4 +- .../com/minelittlepony/gui/IGuiAction.java | 15 +++ .../com/minelittlepony/gui/IGuiCallback.java | 15 +++ .../model/components/PonySnout.java | 4 +- .../minelittlepony/pony/data/PonyData.java | 4 +- .../minelittlepony/pony/data/PonySize.java | 6 +- .../render/LevitatingItemRenderer.java | 3 +- .../render/PonySkullRenderer.java | 3 +- .../render/layer/LayerPonyCustomHead.java | 2 +- .../render/ponies/MobRenderers.java | 39 +++---- .../render/ponies/RenderPonySkeleton.java | 4 +- .../render/ponies/RenderPonyZombie.java | 2 +- .../minelittlepony/settings/BaseValue.java | 20 ---- .../com/minelittlepony/settings/Value.java | 12 -- .../minelittlepony/settings/ValueConfig.java | 62 +++++++--- .../settings/ValueSerializer.java | 25 ----- 21 files changed, 137 insertions(+), 218 deletions(-) create mode 100644 src/main/java/com/minelittlepony/gui/IGuiAction.java create mode 100644 src/main/java/com/minelittlepony/gui/IGuiCallback.java delete mode 100644 src/main/java/com/minelittlepony/settings/BaseValue.java delete mode 100644 src/main/java/com/minelittlepony/settings/Value.java delete mode 100644 src/main/java/com/minelittlepony/settings/ValueSerializer.java diff --git a/src/main/java/com/minelittlepony/MineLittlePony.java b/src/main/java/com/minelittlepony/MineLittlePony.java index 5c9e3594..fb343dec 100644 --- a/src/main/java/com/minelittlepony/MineLittlePony.java +++ b/src/main/java/com/minelittlepony/MineLittlePony.java @@ -81,7 +81,7 @@ public class MineLittlePony { RenderManager rm = minecraft.getRenderManager(); renderManager.initialisePlayerRenderers(rm); - renderManager.initializeMobRenderers(rm, config); + renderManager.initializeMobRenderers(rm); } void onTick(Minecraft minecraft, boolean inGame) { diff --git a/src/main/java/com/minelittlepony/PonyConfig.java b/src/main/java/com/minelittlepony/PonyConfig.java index 29e3a16d..942490fa 100644 --- a/src/main/java/com/minelittlepony/PonyConfig.java +++ b/src/main/java/com/minelittlepony/PonyConfig.java @@ -2,69 +2,40 @@ package com.minelittlepony; import com.google.gson.annotations.Expose; import com.minelittlepony.pony.data.PonyLevel; -import com.minelittlepony.settings.Value; import com.minelittlepony.settings.ValueConfig; import com.mumfrey.liteloader.modconfig.ConfigStrategy; import com.mumfrey.liteloader.modconfig.ExposableOptions; -import java.util.function.Function; - /** * Storage container for MineLP client settings. */ @ExposableOptions(filename = "minelittlepony", strategy = ConfigStrategy.Unversioned) public class PonyConfig extends ValueConfig { - @Expose private PonyLevel ponylevel = PonyLevel.PONIES; + @Expose + private PonyLevel ponylevel = PonyLevel.PONIES; - @Expose private final Value sizes = Value.of(true); - @Expose private final Value snuzzles = Value.of(true); - @Expose private final Value hd = Value.of(true); - @Expose private final Value showscale = Value.of(true); - @Expose private final Value fpsmagic = Value.of(true); - @Expose private final Value ponyskulls = Value.of(true); - - @Expose private final Value villagers = Value.of(true); - @Expose private final Value zombies = Value.of(true); - @Expose private final Value pigzombies = Value.of(true); - @Expose private final Value skeletons = Value.of(true); - @Expose private final Value illagers = Value.of(true); - @Expose private final Value guardians = Value.of(true); - - public enum PonySettings implements Value { - - SIZES(PonyConfig::getSizes), - SNUZZLES(PonyConfig::getSnuzzles), - HD(PonyConfig::getHd), - SHOWSCALE(PonyConfig::getShowscale), - FPSMAGIC(PonyConfig::getFpsmagic), - PONYSKULLS(PonyConfig::getPonyskulls); - - private Value config; - - PonySettings(Function> config) { - this.config = config.apply(MineLittlePony.getConfig()); - } + public enum PonySettings implements ValueConfig.Flag { + SIZES, + SNUZZLES, + HD, + SHOWSCALE, + FPSMAGIC, + PONYSKULLS; @Override - public Boolean get() { - return config.get(); - } - - @Override - public void set(Boolean value) { - config.set(value); + public ValueConfig config() { + return MineLittlePony.getConfig(); } } - /** * Gets the current PonyLevel. That is the level of ponies you would like to see. * * @param ignorePony true to ignore whatever value the setting has. */ public PonyLevel getEffectivePonyLevel(boolean ignorePony) { - return ignorePony ? PonyLevel.BOTH : ponylevel; + return ignorePony ? PonyLevel.BOTH : getPonyLevel(); } public PonyLevel getPonyLevel() { @@ -79,55 +50,8 @@ public class PonyConfig extends ValueConfig { * * @param ponylevel */ - public void setPonyLevel(PonyLevel ponylevel) { - this.ponylevel = ponylevel; - } - - public Value getSizes() { - return sizes; - } - - public Value getSnuzzles() { - return snuzzles; - } - - public Value getHd() { - return hd; - } - - public Value getShowscale() { - return showscale; - } - - public Value getFpsmagic() { - return fpsmagic; - } - - public Value getPonyskulls() { - return ponyskulls; - } - - public Value getVillagers() { - return villagers; - } - - public Value getZombies() { - return zombies; - } - - public Value getPigzombies() { - return pigzombies; - } - - public Value getSkeletons() { - return skeletons; - } - - public Value getIllagers() { - return illagers; - } - - public Value getGuardians() { - return guardians; + public void setPonyLevel(PonyLevel newlevel) { + ponylevel = newlevel; + write(); } } diff --git a/src/main/java/com/minelittlepony/PonyRenderManager.java b/src/main/java/com/minelittlepony/PonyRenderManager.java index 6b3fe77c..d1ee1b4f 100644 --- a/src/main/java/com/minelittlepony/PonyRenderManager.java +++ b/src/main/java/com/minelittlepony/PonyRenderManager.java @@ -55,7 +55,7 @@ public class PonyRenderManager { /** * Registers all entity model replacements. (except for players). */ - public void initializeMobRenderers(RenderManager manager, PonyConfig config) { + public void initializeMobRenderers(RenderManager manager) { for (MobRenderers i : MobRenderers.values()) { i.apply(this, manager); } diff --git a/src/main/java/com/minelittlepony/gui/Checkbox.java b/src/main/java/com/minelittlepony/gui/Checkbox.java index c0c4b475..a071c976 100644 --- a/src/main/java/com/minelittlepony/gui/Checkbox.java +++ b/src/main/java/com/minelittlepony/gui/Checkbox.java @@ -1,22 +1,21 @@ package com.minelittlepony.gui; -import com.minelittlepony.settings.Value; import com.mumfrey.liteloader.client.gui.GuiCheckbox; import net.minecraft.client.resources.I18n; public class Checkbox extends GuiCheckbox implements IActionable { - private final Value setting; + private final IGuiCallback action; - public Checkbox(int x, int y, String displayString, Value setting) { + public Checkbox(int x, int y, String displayString, boolean value, IGuiCallback callback) { super(0, x, y, I18n.format(displayString)); - this.setting = setting; - checked = setting.get(); + action = callback; + checked = value; } @Override - public void perform() { - setting.set(checked ^= true); + public void performAction() { + checked = action.perform(checked ^= true); } } diff --git a/src/main/java/com/minelittlepony/gui/GuiPonySettings.java b/src/main/java/com/minelittlepony/gui/GuiPonySettings.java index 21b55bba..e884d109 100644 --- a/src/main/java/com/minelittlepony/gui/GuiPonySettings.java +++ b/src/main/java/com/minelittlepony/gui/GuiPonySettings.java @@ -5,7 +5,6 @@ import com.minelittlepony.PonyConfig; import com.minelittlepony.PonyConfig.PonySettings; import com.minelittlepony.pony.data.PonyLevel; import com.minelittlepony.render.ponies.MobRenderers; -import com.mumfrey.liteloader.core.LiteLoader; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiScreen; import net.minecraft.client.resources.I18n; @@ -53,7 +52,7 @@ public class GuiPonySettings extends GuiScreen { row += 15; addButton(new Label(LEFT, row += 15, OPTIONS_PREFIX + "options", -1)); for (PonySettings i : PonySettings.values()) { - addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + i.name().toLowerCase(), i)); + addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + i.key(), i.get(), i)); } if (mustScroll()) { @@ -64,14 +63,14 @@ public class GuiPonySettings extends GuiScreen { addButton(new Label(RIGHT, row += 15, MOB_PREFIX + "title", -1)); for (MobRenderers i : MobRenderers.values()) { - addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + i.name().toLowerCase(), i)); + addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + i.key(), i.get(), i)); } } @Override protected void actionPerformed(GuiButton button) throws IOException { if (button instanceof IActionable) { - ((IActionable)button).perform(); + ((IActionable)button).performAction(); } } @@ -81,11 +80,6 @@ public class GuiPonySettings extends GuiScreen { super.drawScreen(mouseX, mouseY, partialTicks); } - @Override - public void onGuiClosed() { - LiteLoader.getInstance().writeConfig(config); - } - protected String getTitle() { return OPTIONS_PREFIX + "title"; } diff --git a/src/main/java/com/minelittlepony/gui/IActionable.java b/src/main/java/com/minelittlepony/gui/IActionable.java index 3ad22158..db854074 100644 --- a/src/main/java/com/minelittlepony/gui/IActionable.java +++ b/src/main/java/com/minelittlepony/gui/IActionable.java @@ -1,12 +1,12 @@ package com.minelittlepony.gui; /** - * An element that can perform an action. + * An element that responds to interface events. */ @FunctionalInterface public interface IActionable { /** * Does whatever. */ - void perform(); + void performAction(); } diff --git a/src/main/java/com/minelittlepony/gui/IGuiAction.java b/src/main/java/com/minelittlepony/gui/IGuiAction.java new file mode 100644 index 00000000..9a6be2ca --- /dev/null +++ b/src/main/java/com/minelittlepony/gui/IGuiAction.java @@ -0,0 +1,15 @@ +package com.minelittlepony.gui; + +/** + * Response actions for UI events. + */ +@FunctionalInterface +public interface IGuiAction { + /** + * Performs this action now. + * + * @param sender the element handling this event + */ + void perform(T sender); + +} diff --git a/src/main/java/com/minelittlepony/gui/IGuiCallback.java b/src/main/java/com/minelittlepony/gui/IGuiCallback.java new file mode 100644 index 00000000..7f4d06be --- /dev/null +++ b/src/main/java/com/minelittlepony/gui/IGuiCallback.java @@ -0,0 +1,15 @@ +package com.minelittlepony.gui; + +/** + * Response actions for UI events. + */ +@FunctionalInterface +public interface IGuiCallback { + /** + * Performs this action now. + * + * @param value New Value of the field being changed + * @return Adjusted value the field must take on + */ + public T perform(T in); +} diff --git a/src/main/java/com/minelittlepony/model/components/PonySnout.java b/src/main/java/com/minelittlepony/model/components/PonySnout.java index c83961b7..f1cd3766 100644 --- a/src/main/java/com/minelittlepony/model/components/PonySnout.java +++ b/src/main/java/com/minelittlepony/model/components/PonySnout.java @@ -2,7 +2,7 @@ package com.minelittlepony.model.components; import static com.minelittlepony.model.PonyModelConstants.*; -import com.minelittlepony.MineLittlePony; +import com.minelittlepony.PonyConfig.PonySettings; import com.minelittlepony.model.capabilities.ICapitated; import com.minelittlepony.pony.data.PonyGender; import com.minelittlepony.render.plane.PlaneRenderer; @@ -57,7 +57,7 @@ public class PonySnout { } public void setGender(PonyGender gender) { - boolean show = !head.hasHeadGear() && !isHidden && MineLittlePony.getConfig().getSnuzzles().get(); + boolean show = !head.hasHeadGear() && !isHidden && PonySettings.SNUZZLES.get(); mare.isHidden = !show || gender == PonyGender.STALLION; stallion.isHidden = !show || gender == PonyGender.MARE; diff --git a/src/main/java/com/minelittlepony/pony/data/PonyData.java b/src/main/java/com/minelittlepony/pony/data/PonyData.java index 3b4847bb..bdb2e5e2 100644 --- a/src/main/java/com/minelittlepony/pony/data/PonyData.java +++ b/src/main/java/com/minelittlepony/pony/data/PonyData.java @@ -1,7 +1,7 @@ package com.minelittlepony.pony.data; import com.google.common.base.MoreObjects; -import com.minelittlepony.MineLittlePony; +import com.minelittlepony.PonyConfig.PonySettings; import com.minelittlepony.model.anim.BasicEasingInterpolator; import com.minelittlepony.model.anim.IInterpolator; @@ -62,7 +62,7 @@ public class PonyData implements IPonyData { @Override public PonySize getSize() { - return MineLittlePony.getConfig().getSizes().get() ? size : PonySize.NORMAL; + return PonySettings.SIZES.get() ? size : PonySize.NORMAL; } @Override diff --git a/src/main/java/com/minelittlepony/pony/data/PonySize.java b/src/main/java/com/minelittlepony/pony/data/PonySize.java index 8f6135c9..ac774f4a 100644 --- a/src/main/java/com/minelittlepony/pony/data/PonySize.java +++ b/src/main/java/com/minelittlepony/pony/data/PonySize.java @@ -1,6 +1,6 @@ package com.minelittlepony.pony.data; -import com.minelittlepony.MineLittlePony; +import com.minelittlepony.PonyConfig.PonySettings; import com.minelittlepony.transform.PonyTransformation; public enum PonySize implements ITriggerPixelMapped { @@ -24,14 +24,14 @@ public enum PonySize implements ITriggerPixelMapped { } public float getShadowSize() { - if (MineLittlePony.getConfig().getShowscale().get()) { + if (PonySettings.SHOWSCALE.get()) { return shadowSize * 0.9F; } return shadowSize; } public float getScaleFactor() { - if (MineLittlePony.getConfig().getShowscale().get()) { + if (PonySettings.SHOWSCALE.get()) { return scale * 0.9F; } return scale; diff --git a/src/main/java/com/minelittlepony/render/LevitatingItemRenderer.java b/src/main/java/com/minelittlepony/render/LevitatingItemRenderer.java index c9cdffd1..dd96cc84 100644 --- a/src/main/java/com/minelittlepony/render/LevitatingItemRenderer.java +++ b/src/main/java/com/minelittlepony/render/LevitatingItemRenderer.java @@ -3,6 +3,7 @@ package com.minelittlepony.render; import static net.minecraft.client.renderer.GlStateManager.*; import com.minelittlepony.MineLittlePony; +import com.minelittlepony.PonyConfig.PonySettings; import com.minelittlepony.ducks.IRenderItem; import com.minelittlepony.pony.data.Pony; import com.minelittlepony.util.coordinates.Color; @@ -69,7 +70,7 @@ public class LevitatingItemRenderer { pushMatrix(); - boolean doMagic = MineLittlePony.getConfig().getFpsmagic().get() && pony.getMetadata().hasMagic(); + boolean doMagic = PonySettings.FPSMAGIC.get() && pony.getMetadata().hasMagic(); if (doMagic) { setupPerspective(entity, stack, left); diff --git a/src/main/java/com/minelittlepony/render/PonySkullRenderer.java b/src/main/java/com/minelittlepony/render/PonySkullRenderer.java index eff17fe4..a1f62abb 100644 --- a/src/main/java/com/minelittlepony/render/PonySkullRenderer.java +++ b/src/main/java/com/minelittlepony/render/PonySkullRenderer.java @@ -2,6 +2,7 @@ package com.minelittlepony.render; import com.minelittlepony.MineLittlePony; import com.minelittlepony.PonyConfig; +import com.minelittlepony.PonyConfig.PonySettings; import com.minelittlepony.ducks.IRenderItem; import com.minelittlepony.pony.data.Pony; import com.mojang.authlib.GameProfile; @@ -35,7 +36,7 @@ public class PonySkullRenderer extends TileEntitySkullRenderer implements IRende * Original/Existing renderer is stored to a backup variable as a fallback in case of mods. */ public static TileEntitySkullRenderer resolve() { - if (MineLittlePony.getConfig().getPonyskulls().get()) { + if (PonySettings.PONYSKULLS.get()) { if (!(instance instanceof PonySkullRenderer)) { backup = instance; ModUtilities.addRenderer(TileEntitySkull.class, ponyInstance); diff --git a/src/main/java/com/minelittlepony/render/layer/LayerPonyCustomHead.java b/src/main/java/com/minelittlepony/render/layer/LayerPonyCustomHead.java index f5900193..9b906e47 100644 --- a/src/main/java/com/minelittlepony/render/layer/LayerPonyCustomHead.java +++ b/src/main/java/com/minelittlepony/render/layer/LayerPonyCustomHead.java @@ -101,7 +101,7 @@ public class LayerPonyCustomHead implements LayerRen } private ModelWrapper getModel() { - return ((IRenderPony) renderer).getModelWrapper(); + return ((IRenderPony) renderer).getModelWrapper(); } @Override diff --git a/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java b/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java index 18fe1b80..64a8254f 100644 --- a/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java +++ b/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java @@ -1,9 +1,8 @@ package com.minelittlepony.render.ponies; import com.minelittlepony.MineLittlePony; -import com.minelittlepony.PonyConfig; import com.minelittlepony.PonyRenderManager; -import com.minelittlepony.settings.Value; +import com.minelittlepony.settings.ValueConfig; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.entity.monster.EntityElderGuardian; @@ -23,15 +22,13 @@ import net.minecraft.entity.monster.EntityZombie; import net.minecraft.entity.monster.EntityZombieVillager; import net.minecraft.entity.passive.EntityVillager; -import java.util.function.Function; - /** * Central location where new entity renderers are registered and applied. * * Due to the limitations in Mumfrey's framework, needs to be paired with a field in PonyConfig. */ -public enum MobRenderers implements Value { - VILLAGERS(PonyConfig::getVillagers) { +public enum MobRenderers implements ValueConfig.Flag { + VILLAGERS { @Override public void register(boolean state, PonyRenderManager pony, RenderManager manager) { pony.switchRenderer(state, manager, EntityVillager.class, new RenderPonyVillager(manager)); @@ -39,7 +36,7 @@ public enum MobRenderers implements Value { pony.switchRenderer(state, manager, EntityZombieVillager.class, new RenderPonyZombieVillager(manager)); } }, - ZOMBIES(PonyConfig::getZombies) { + ZOMBIES { @Override public void register(boolean state, PonyRenderManager pony, RenderManager manager) { pony.switchRenderer(state, manager, EntityZombie.class, new RenderPonyZombie<>(manager)); @@ -47,13 +44,13 @@ public enum MobRenderers implements Value { pony.switchRenderer(state, manager, EntityGiantZombie.class, new RenderPonyZombie.Giant(manager)); } }, - PIGZOMBIES(PonyConfig::getPigzombies) { + PIGZOMBIES { @Override public void register(boolean state, PonyRenderManager pony, RenderManager manager) { pony.switchRenderer(state, manager, EntityPigZombie.class, new RenderPonyPigman(manager)); } }, - SKELETONS(PonyConfig::getSkeletons) { + SKELETONS { @Override public void register(boolean state, PonyRenderManager pony, RenderManager manager) { pony.switchRenderer(state, manager, EntitySkeleton.class, new RenderPonySkeleton<>(manager)); @@ -61,7 +58,7 @@ public enum MobRenderers implements Value { pony.switchRenderer(state, manager, EntityWitherSkeleton.class, new RenderPonySkeleton.Wither(manager)); } }, - ILLAGERS(PonyConfig::getIllagers) { + ILLAGERS { @Override public void register(boolean state, PonyRenderManager pony, RenderManager manager) { pony.switchRenderer(state, manager, EntityVex.class, new RenderPonyVex(manager)); @@ -70,7 +67,7 @@ public enum MobRenderers implements Value { pony.switchRenderer(state, manager, EntityIllusionIllager.class, new RenderPonyIllager.Illusionist(manager)); } }, - GUARDIANS(PonyConfig::getGuardians) { + GUARDIANS { @Override public void register(boolean state, PonyRenderManager pony, RenderManager manager) { pony.switchRenderer(state, manager, EntityGuardian.class, new RenderPonyGuardian(manager)); @@ -78,25 +75,14 @@ public enum MobRenderers implements Value { } }; - private final Value setting; - - MobRenderers(Function> setting) { - this.setting = setting.apply(MineLittlePony.getConfig()); - } - - @Override - public Boolean get() { - return setting.get(); - } - @Override public void set(Boolean value) { - setting.set(value); + ValueConfig.Flag.super.set(value); apply(MineLittlePony.getInstance().getRenderManager(), Minecraft.getMinecraft().getRenderManager()); } public void apply(PonyRenderManager pony, RenderManager manager) { - boolean state = setting.get(); + boolean state = get(); register(state, pony, manager); if (state) { MineLittlePony.logger.info(name() + " are now ponies."); @@ -106,4 +92,9 @@ public enum MobRenderers implements Value { } public abstract void register(boolean state, PonyRenderManager pony, RenderManager manager); + + @Override + public ValueConfig config() { + return MineLittlePony.getConfig(); + } } diff --git a/src/main/java/com/minelittlepony/render/ponies/RenderPonySkeleton.java b/src/main/java/com/minelittlepony/render/ponies/RenderPonySkeleton.java index 2651f408..143617c9 100644 --- a/src/main/java/com/minelittlepony/render/ponies/RenderPonySkeleton.java +++ b/src/main/java/com/minelittlepony/render/ponies/RenderPonySkeleton.java @@ -25,7 +25,7 @@ public class RenderPonySkeleton extends Rende public static final ISkull SKULL = new PonySkull() { @Override public boolean canRender(PonyConfig config) { - return config.getSkeletons().get(); + return MobRenderers.SKELETONS.get(); } @Override @@ -73,7 +73,7 @@ public class RenderPonySkeleton extends Rende public static final ISkull SKULL = new PonySkull() { @Override public boolean canRender(PonyConfig config) { - return config.getSkeletons().get(); + return MobRenderers.SKELETONS.get(); } @Override diff --git a/src/main/java/com/minelittlepony/render/ponies/RenderPonyZombie.java b/src/main/java/com/minelittlepony/render/ponies/RenderPonyZombie.java index 445c29eb..c1422456 100644 --- a/src/main/java/com/minelittlepony/render/ponies/RenderPonyZombie.java +++ b/src/main/java/com/minelittlepony/render/ponies/RenderPonyZombie.java @@ -22,7 +22,7 @@ public class RenderPonyZombie extends RenderPonyMob public static final ISkull SKULL = new PonySkull() { @Override public boolean canRender(PonyConfig config) { - return config.getZombies().get(); + return MobRenderers.ZOMBIES.get(); } @Override diff --git a/src/main/java/com/minelittlepony/settings/BaseValue.java b/src/main/java/com/minelittlepony/settings/BaseValue.java deleted file mode 100644 index 0631938a..00000000 --- a/src/main/java/com/minelittlepony/settings/BaseValue.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.minelittlepony.settings; - -public class BaseValue implements Value { - - private T value; - - public BaseValue(T value) { - this.value = value; - } - - @Override - public T get() { - return value; - } - - @Override - public void set(T value) { - this.value = value; - } -} diff --git a/src/main/java/com/minelittlepony/settings/Value.java b/src/main/java/com/minelittlepony/settings/Value.java deleted file mode 100644 index 31a0eb71..00000000 --- a/src/main/java/com/minelittlepony/settings/Value.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.minelittlepony.settings; - -public interface Value { - - T get(); - - void set(T value); - - static Value of(T value) { - return new BaseValue<>(value); - } -} diff --git a/src/main/java/com/minelittlepony/settings/ValueConfig.java b/src/main/java/com/minelittlepony/settings/ValueConfig.java index 6e3f7cc7..940f88a3 100644 --- a/src/main/java/com/minelittlepony/settings/ValueConfig.java +++ b/src/main/java/com/minelittlepony/settings/ValueConfig.java @@ -1,26 +1,62 @@ package com.minelittlepony.settings; -import com.google.gson.GsonBuilder; -import com.mumfrey.liteloader.modconfig.AdvancedExposable; - -import java.io.File; +import java.util.HashMap; +import java.util.Map; +import com.google.gson.annotations.Expose; +import com.minelittlepony.gui.IGuiCallback; +import com.mumfrey.liteloader.core.LiteLoader; +import com.mumfrey.liteloader.modconfig.Exposable; /** * A sensible config container that actually lets us programatically index values by a key. * - * Reflection because Mumfrey pls. - * */ // Mumfrey pls. -public class ValueConfig implements AdvancedExposable { +public abstract class ValueConfig implements Exposable { - @Override - public void setupGsonSerialiser(GsonBuilder gsonBuilder) { - gsonBuilder.registerTypeAdapter(Value.class, new ValueSerializer()); + // TODO: Mumfrey pls. ValueConfig cannot extend from HashMap directly. + @Expose + private Map properties = new HashMap(); + + protected void write() { + LiteLoader.getInstance().writeConfig(this); } - @Override - public File getConfigFile(File configFile, File configFileLocation, String defaultFileName) { - return null; + public interface Flag extends Setting { + default Boolean initial() { + return true; + } + } + + public interface Setting extends IGuiCallback { + + ValueConfig config(); + + String name(); + + T initial(); + + default String key() { + return name().toLowerCase(); + } + + @SuppressWarnings("unchecked") + default T get() { + if (!config().properties.containsKey(key())) { + set(initial()); + } + return (T)config().properties.get(key()); + } + + default void set(T value) { + config().properties.put(key(), value); + config().write(); + } + + @Override + default T perform(T in) { + set(in); + return get(); + } } } diff --git a/src/main/java/com/minelittlepony/settings/ValueSerializer.java b/src/main/java/com/minelittlepony/settings/ValueSerializer.java deleted file mode 100644 index 5f138fcd..00000000 --- a/src/main/java/com/minelittlepony/settings/ValueSerializer.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.minelittlepony.settings; - -import com.google.gson.JsonDeserializationContext; -import com.google.gson.JsonDeserializer; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSerializationContext; -import com.google.gson.JsonSerializer; - -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -public class ValueSerializer implements JsonSerializer>, JsonDeserializer> { - - @Override - public Value deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - ParameterizedType type = (ParameterizedType) typeOfT; - return Value.of(context.deserialize(json, type.getActualTypeArguments()[0])); - } - - @Override - public JsonElement serialize(Value src, Type typeOfSrc, JsonSerializationContext context) { - return context.serialize(src.get()); - } -}