From bc65369964768436ac616a019fc897e1d0bd7648 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 15 May 2018 16:26:52 +0200 Subject: [PATCH] Reorganise how settings work, and renderers change immediately when you toggle an option --- .../java/com/minelittlepony/PonyConfig.java | 14 ++- .../com/minelittlepony/PonyRenderManager.java | 118 +++--------------- .../minelittlepony/gui/GuiPonySettings.java | 19 ++- .../render/ponies/MobRenderers.java | 82 ++++++++++++ .../settings/SensibleConfig.java | 60 +++++++++ 5 files changed, 179 insertions(+), 114 deletions(-) create mode 100644 src/main/java/com/minelittlepony/render/ponies/MobRenderers.java create mode 100644 src/main/java/com/minelittlepony/settings/SensibleConfig.java diff --git a/src/main/java/com/minelittlepony/PonyConfig.java b/src/main/java/com/minelittlepony/PonyConfig.java index 1f72a40b..b4f969d4 100644 --- a/src/main/java/com/minelittlepony/PonyConfig.java +++ b/src/main/java/com/minelittlepony/PonyConfig.java @@ -2,22 +2,32 @@ package com.minelittlepony; import com.google.gson.annotations.Expose; import com.minelittlepony.pony.data.PonyLevel; +import com.minelittlepony.settings.SensibleConfig; import com.mumfrey.liteloader.modconfig.ConfigStrategy; import com.mumfrey.liteloader.modconfig.Exposable; import com.mumfrey.liteloader.modconfig.ExposableOptions; /** - * Storage contained for MineLP client settings. + * Storage container for MineLP client settings. * */ @ExposableOptions(filename = "minelittlepony", strategy = ConfigStrategy.Unversioned) -public class PonyConfig implements Exposable { +public class PonyConfig extends SensibleConfig implements Exposable { @Expose private PonyLevel ponylevel = PonyLevel.PONIES; + @Expose public boolean sizes = true; @Expose public boolean snuzzles = true; @Expose public boolean hd = true; @Expose public boolean showscale = true; + + public enum PonySettings implements Setting { + SIZES, + SNUZZLES, + HD, + SHOWSCALE; + } + @Expose public boolean villagers = true; @Expose public boolean zombies = true; @Expose public boolean pigzombies = true; diff --git a/src/main/java/com/minelittlepony/PonyRenderManager.java b/src/main/java/com/minelittlepony/PonyRenderManager.java index 76c143ae..d7c9fba0 100644 --- a/src/main/java/com/minelittlepony/PonyRenderManager.java +++ b/src/main/java/com/minelittlepony/PonyRenderManager.java @@ -9,36 +9,12 @@ import com.minelittlepony.hdskins.gui.RenderPonyModel; import com.minelittlepony.model.player.PlayerModels; import com.minelittlepony.render.LevitatingItemRenderer; import com.minelittlepony.render.player.RenderPonyPlayer; -import com.minelittlepony.render.ponies.RenderPonyGuardian; -import com.minelittlepony.render.ponies.RenderPonyIllager; -import com.minelittlepony.render.ponies.RenderPonyPigman; -import com.minelittlepony.render.ponies.RenderPonySkeleton; -import com.minelittlepony.render.ponies.RenderPonyVex; -import com.minelittlepony.render.ponies.RenderPonyVillager; -import com.minelittlepony.render.ponies.RenderPonyWitch; -import com.minelittlepony.render.ponies.RenderPonyZombie; -import com.minelittlepony.render.ponies.RenderPonyZombieVillager; +import com.minelittlepony.render.ponies.MobRenderers; import com.mumfrey.liteloader.util.ModUtilities; import net.minecraft.client.renderer.entity.Render; import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.entity.Entity; -import net.minecraft.entity.monster.EntityElderGuardian; -import net.minecraft.entity.monster.EntityEvoker; -import net.minecraft.entity.monster.EntityGiantZombie; -import net.minecraft.entity.monster.EntityGuardian; -import net.minecraft.entity.monster.EntityHusk; -import net.minecraft.entity.monster.EntityIllusionIllager; -import net.minecraft.entity.monster.EntityPigZombie; -import net.minecraft.entity.monster.EntitySkeleton; -import net.minecraft.entity.monster.EntityStray; -import net.minecraft.entity.monster.EntityVex; -import net.minecraft.entity.monster.EntityVindicator; -import net.minecraft.entity.monster.EntityWitch; -import net.minecraft.entity.monster.EntityWitherSkeleton; -import net.minecraft.entity.monster.EntityZombie; -import net.minecraft.entity.monster.EntityZombieVillager; -import net.minecraft.entity.passive.EntityVillager; /** * Render manager responsible for replacing and restoring entity renderers when the client settings change. @@ -80,92 +56,32 @@ public class PonyRenderManager { * Registers all entity model replacements. (except for players). */ public void initializeMobRenderers(RenderManager manager, PonyConfig config) { - - if (config.villagers) { - pushNewRenderer(manager, EntityVillager.class, new RenderPonyVillager(manager)); - pushNewRenderer(manager, EntityWitch.class, new RenderPonyWitch(manager)); - pushNewRenderer(manager, EntityZombieVillager.class, new RenderPonyZombieVillager(manager)); - MineLittlePony.logger.info("Villagers are now ponies."); - } else { - restoreRenderer(EntityVillager.class); - restoreRenderer(EntityWitch.class); - restoreRenderer(EntityZombieVillager.class); - } - - if (config.zombies) { - pushNewRenderer(manager, EntityZombie.class, new RenderPonyZombie<>(manager)); - pushNewRenderer(manager, EntityHusk.class, new RenderPonyZombie.Husk(manager)); - pushNewRenderer(manager, EntityGiantZombie.class, new RenderPonyZombie.Giant(manager)); - MineLittlePony.logger.info("Zombies are now ponies."); - } else { - restoreRenderer(EntityZombie.class); - restoreRenderer(EntityHusk.class); - restoreRenderer(EntityGiantZombie.class); - } - - if (config.pigzombies) { - pushNewRenderer(manager, EntityPigZombie.class, new RenderPonyPigman(manager)); - MineLittlePony.logger.info("Zombie pigmen are now ponies."); - } else { - restoreRenderer(EntityPigZombie.class); - } - - if (config.skeletons) { - pushNewRenderer(manager, EntitySkeleton.class, new RenderPonySkeleton<>(manager)); - pushNewRenderer(manager, EntityStray.class, new RenderPonySkeleton.Stray(manager)); - pushNewRenderer(manager, EntityWitherSkeleton.class, new RenderPonySkeleton.Wither(manager)); - MineLittlePony.logger.info("Skeletons are now ponies."); - } else { - restoreRenderer(EntitySkeleton.class); - restoreRenderer(EntityStray.class); - restoreRenderer(EntityWitherSkeleton.class); - } - - if (config.illagers) { - pushNewRenderer(manager, EntityVex.class, new RenderPonyVex(manager)); - pushNewRenderer(manager, EntityEvoker.class, new RenderPonyIllager.Evoker(manager)); - pushNewRenderer(manager, EntityVindicator.class, new RenderPonyIllager.Vindicator(manager)); - pushNewRenderer(manager, EntityIllusionIllager.class, new RenderPonyIllager.Illusionist(manager)); - MineLittlePony.logger.info("Illagers are now ponies."); - } else { - restoreRenderer(EntityVex.class); - restoreRenderer(EntityEvoker.class); - restoreRenderer(EntityVindicator.class); - restoreRenderer(EntityIllusionIllager.class); - } - - if (config.guardians) { - pushNewRenderer(manager, EntityGuardian.class, new RenderPonyGuardian(manager)); - pushNewRenderer(manager, EntityElderGuardian.class, new RenderPonyGuardian.Elder(manager)); - MineLittlePony.logger.info("Guardians are now ponies."); - } else { - restoreRenderer(EntityGuardian.class); - restoreRenderer(EntityElderGuardian.class); + for (MobRenderers i : MobRenderers.values()) { + i.apply(this, manager); } } /** - * Pushes a new renderer replacement storing the original internally. This change can be undone with {@link #restoreRenderer(Class)} + * + * Replaces an entity renderer depending on whether we want ponies or not. + * + * @param state True if we want ponies (the original will be stored) * @param manager The render manager * @param type The type to replace * @param renderer The replacement value * @param The entity type */ @SuppressWarnings("unchecked") - public void pushNewRenderer(RenderManager manager, Class type, Render renderer) { - if (!renderMap.containsKey(type)) { - renderMap.put(type, manager.getEntityClassRenderObject(type)); - } - ModUtilities.addRenderer((Class)type, renderer); - } - - /** - * Restores a renderer to its previous value. - */ - @SuppressWarnings("unchecked") - public void restoreRenderer(Class type) { - if (renderMap.containsKey(type)) { - ModUtilities.addRenderer(type, (Render)renderMap.get(type)); + public void switchRenderer(boolean state, RenderManager manager, Class type, Render renderer) { + if (state) { + if (!renderMap.containsKey(type)) { + renderMap.put(type, manager.getEntityClassRenderObject(type)); + } + ModUtilities.addRenderer((Class)type, renderer); + } else { + if (renderMap.containsKey(type)) { + ModUtilities.addRenderer(type, (Render)renderMap.get(type)); + } } } diff --git a/src/main/java/com/minelittlepony/gui/GuiPonySettings.java b/src/main/java/com/minelittlepony/gui/GuiPonySettings.java index ada4bf93..f11f25f2 100644 --- a/src/main/java/com/minelittlepony/gui/GuiPonySettings.java +++ b/src/main/java/com/minelittlepony/gui/GuiPonySettings.java @@ -4,7 +4,9 @@ import java.io.IOException; import com.minelittlepony.MineLittlePony; 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; @@ -51,10 +53,9 @@ public class GuiPonySettings extends GuiScreen { row += 15; addButton(new Label(LEFT, row += 15, OPTIONS_PREFIX + "options", -1)); - addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "hd", config.hd, v -> config.hd = v)); - addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "snuzzles", config.snuzzles, v -> config.snuzzles = v)); - addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "sizes", config.sizes, v -> config.sizes = v)); - addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "showscale", config.showscale, v -> config.showscale = v)); + for (PonySettings i : PonySettings.values()) { + addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + i.name().toLowerCase(), i.get(), i)); + } if (mustScroll()) { row += 15; @@ -63,12 +64,9 @@ public class GuiPonySettings extends GuiScreen { } addButton(new Label(RIGHT, row += 15, MOB_PREFIX + "title", -1)); - addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "villagers", config.villagers, v -> config.villagers = v)); - addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "zombies", config.zombies, v -> config.zombies = v)); - addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "zombiepigmen", config.pigzombies, v -> config.pigzombies = v)); - addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "skeletons", config.skeletons, v -> config.skeletons = v)); - addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "illagers", config.illagers, v -> config.illagers = v)); - addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "guardians", config.guardians, v -> config.guardians = v)); + for (MobRenderers i : MobRenderers.values()) { + addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + i.name().toLowerCase(), i.get(), i)); + } } @Override @@ -87,7 +85,6 @@ public class GuiPonySettings extends GuiScreen { @Override public void onGuiClosed() { LiteLoader.getInstance().writeConfig(config); - MineLittlePony.getInstance().getRenderManager().initializeMobRenderers(mc.getRenderManager(), config); } protected String getTitle() { diff --git a/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java b/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java new file mode 100644 index 00000000..e7f17034 --- /dev/null +++ b/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java @@ -0,0 +1,82 @@ +package com.minelittlepony.render.ponies; + +import com.minelittlepony.MineLittlePony; +import com.minelittlepony.PonyRenderManager; +import com.minelittlepony.settings.SensibleConfig.Setting; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.entity.RenderManager; +import net.minecraft.entity.monster.*; +import net.minecraft.entity.passive.*; + +/** + * 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 Setting { + VILLAGERS { + @Override + public void register(boolean state, PonyRenderManager pony, RenderManager manager) { + pony.switchRenderer(state, manager, EntityVillager.class, new RenderPonyVillager(manager)); + pony.switchRenderer(state, manager, EntityWitch.class, new RenderPonyWitch(manager)); + pony.switchRenderer(state, manager, EntityZombieVillager.class, new RenderPonyZombieVillager(manager)); + } + }, + ZOMBIES { + @Override + public void register(boolean state, PonyRenderManager pony, RenderManager manager) { + pony.switchRenderer(state, manager, EntityZombie.class, new RenderPonyZombie<>(manager)); + pony.switchRenderer(state, manager, EntityHusk.class, new RenderPonyZombie.Husk(manager)); + pony.switchRenderer(state, manager, EntityGiantZombie.class, new RenderPonyZombie.Giant(manager)); + } + }, + PIGZOMBIES { + @Override + public void register(boolean state, PonyRenderManager pony, RenderManager manager) { + pony.switchRenderer(state, manager, EntityPigZombie.class, new RenderPonyPigman(manager)); + } + }, + SKELETONS { + @Override + public void register(boolean state, PonyRenderManager pony, RenderManager manager) { + pony.switchRenderer(state, manager, EntitySkeleton.class, new RenderPonySkeleton<>(manager)); + pony.switchRenderer(state, manager, EntityStray.class, new RenderPonySkeleton.Stray(manager)); + pony.switchRenderer(state, manager, EntityWitherSkeleton.class, new RenderPonySkeleton.Wither(manager)); + } + }, + ILLAGERS { + @Override + public void register(boolean state, PonyRenderManager pony, RenderManager manager) { + pony.switchRenderer(state, manager, EntityVex.class, new RenderPonyVex(manager)); + pony.switchRenderer(state, manager, EntityEvoker.class, new RenderPonyIllager.Evoker(manager)); + pony.switchRenderer(state, manager, EntityVindicator.class, new RenderPonyIllager.Vindicator(manager)); + pony.switchRenderer(state, manager, EntityIllusionIllager.class, new RenderPonyIllager.Illusionist(manager)); + } + }, + GUARDIANS { + @Override + public void register(boolean state, PonyRenderManager pony, RenderManager manager) { + pony.switchRenderer(state, manager, EntityGuardian.class, new RenderPonyGuardian(manager)); + pony.switchRenderer(state, manager, EntityElderGuardian.class, new RenderPonyGuardian.Elder(manager)); + } + }; + + @Override + public void set(boolean value) { + Setting.super.set(value); + apply(MineLittlePony.getInstance().getRenderManager(), Minecraft.getMinecraft().getRenderManager()); + } + + public void apply(PonyRenderManager pony, RenderManager manager) { + boolean state = get(); + register(state, pony, manager); + if (state) { + MineLittlePony.logger.info(name() + " are now ponies."); + } else { + MineLittlePony.logger.info(name() + " are no longer ponies."); + } + } + + public abstract void register(boolean state, PonyRenderManager pony, RenderManager manager); +} \ No newline at end of file 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..a12f1f3f --- /dev/null +++ b/src/main/java/com/minelittlepony/settings/SensibleConfig.java @@ -0,0 +1,60 @@ +package com.minelittlepony.settings; + +import com.minelittlepony.gui.IGUIAction; + +/** + * A sensible config container that actually lets us programatically index values by a key. + * + * Reflection because Mumfrey pls. + * + */ +// Mumfrey pls. +public abstract class SensibleConfig { + + private static SensibleConfig instance; + + public SensibleConfig() { + instance = this; + } + + public interface Setting extends IGUIAction { + 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 this.getClass().getField(key.name().toLowerCase()).getBoolean(this); + } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException ignored) { + return true; + } + } + + public boolean setValue(Setting key, boolean value) { + try { + this.getClass().getField(key.name().toLowerCase()).setBoolean(this, value); + } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException ignored) { + } + return value; + } +}