Reorganise how settings work, and renderers change immediately when you toggle an option

This commit is contained in:
Sollace 2018-05-15 16:26:52 +02:00
parent e56611c3fc
commit bc65369964
5 changed files with 179 additions and 114 deletions

View file

@ -2,22 +2,32 @@ package com.minelittlepony;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import com.minelittlepony.pony.data.PonyLevel; import com.minelittlepony.pony.data.PonyLevel;
import com.minelittlepony.settings.SensibleConfig;
import com.mumfrey.liteloader.modconfig.ConfigStrategy; import com.mumfrey.liteloader.modconfig.ConfigStrategy;
import com.mumfrey.liteloader.modconfig.Exposable; import com.mumfrey.liteloader.modconfig.Exposable;
import com.mumfrey.liteloader.modconfig.ExposableOptions; import com.mumfrey.liteloader.modconfig.ExposableOptions;
/** /**
* Storage contained for MineLP client settings. * Storage container for MineLP client settings.
* *
*/ */
@ExposableOptions(filename = "minelittlepony", strategy = ConfigStrategy.Unversioned) @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 private PonyLevel ponylevel = PonyLevel.PONIES;
@Expose public boolean sizes = true; @Expose public boolean sizes = true;
@Expose public boolean snuzzles = true; @Expose public boolean snuzzles = true;
@Expose public boolean hd = true; @Expose public boolean hd = true;
@Expose public boolean showscale = true; @Expose public boolean showscale = true;
public enum PonySettings implements Setting {
SIZES,
SNUZZLES,
HD,
SHOWSCALE;
}
@Expose public boolean villagers = true; @Expose public boolean villagers = true;
@Expose public boolean zombies = true; @Expose public boolean zombies = true;
@Expose public boolean pigzombies = true; @Expose public boolean pigzombies = true;

View file

@ -9,36 +9,12 @@ import com.minelittlepony.hdskins.gui.RenderPonyModel;
import com.minelittlepony.model.player.PlayerModels; import com.minelittlepony.model.player.PlayerModels;
import com.minelittlepony.render.LevitatingItemRenderer; import com.minelittlepony.render.LevitatingItemRenderer;
import com.minelittlepony.render.player.RenderPonyPlayer; import com.minelittlepony.render.player.RenderPonyPlayer;
import com.minelittlepony.render.ponies.RenderPonyGuardian; import com.minelittlepony.render.ponies.MobRenderers;
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.mumfrey.liteloader.util.ModUtilities; import com.mumfrey.liteloader.util.ModUtilities;
import net.minecraft.client.renderer.entity.Render; import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.entity.Entity; 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. * 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). * Registers all entity model replacements. (except for players).
*/ */
public void initializeMobRenderers(RenderManager manager, PonyConfig config) { public void initializeMobRenderers(RenderManager manager, PonyConfig config) {
for (MobRenderers i : MobRenderers.values()) {
if (config.villagers) { i.apply(this, manager);
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);
} }
} }
/** /**
* 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 manager The render manager
* @param type The type to replace * @param type The type to replace
* @param renderer The replacement value * @param renderer The replacement value
* @param <T> The entity type * @param <T> The entity type
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T extends Entity, V extends T> void pushNewRenderer(RenderManager manager, Class<V> type, Render<T> renderer) { public <T extends Entity, V extends T> void switchRenderer(boolean state, RenderManager manager, Class<V> type, Render<T> renderer) {
if (state) {
if (!renderMap.containsKey(type)) { if (!renderMap.containsKey(type)) {
renderMap.put(type, manager.getEntityClassRenderObject(type)); renderMap.put(type, manager.getEntityClassRenderObject(type));
} }
ModUtilities.addRenderer((Class<T>)type, renderer); ModUtilities.addRenderer((Class<T>)type, renderer);
} } else {
/**
* Restores a renderer to its previous value.
*/
@SuppressWarnings("unchecked")
public <T extends Entity> void restoreRenderer(Class<T> type) {
if (renderMap.containsKey(type)) { if (renderMap.containsKey(type)) {
ModUtilities.addRenderer(type, (Render<T>)renderMap.get(type)); ModUtilities.addRenderer(type, (Render<V>)renderMap.get(type));
}
} }
} }

View file

@ -4,7 +4,9 @@ import java.io.IOException;
import com.minelittlepony.MineLittlePony; import com.minelittlepony.MineLittlePony;
import com.minelittlepony.PonyConfig; import com.minelittlepony.PonyConfig;
import com.minelittlepony.PonyConfig.PonySettings;
import com.minelittlepony.pony.data.PonyLevel; import com.minelittlepony.pony.data.PonyLevel;
import com.minelittlepony.render.ponies.MobRenderers;
import com.mumfrey.liteloader.core.LiteLoader; import com.mumfrey.liteloader.core.LiteLoader;
import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiButton;
@ -51,10 +53,9 @@ public class GuiPonySettings extends GuiScreen {
row += 15; row += 15;
addButton(new Label(LEFT, row += 15, OPTIONS_PREFIX + "options", -1)); 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)); for (PonySettings i : PonySettings.values()) {
addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + "snuzzles", config.snuzzles, v -> config.snuzzles = v)); addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + i.name().toLowerCase(), i.get(), i));
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));
if (mustScroll()) { if (mustScroll()) {
row += 15; row += 15;
@ -63,12 +64,9 @@ public class GuiPonySettings extends GuiScreen {
} }
addButton(new Label(RIGHT, row += 15, MOB_PREFIX + "title", -1)); 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)); for (MobRenderers i : MobRenderers.values()) {
addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + "zombies", config.zombies, v -> config.zombies = v)); addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + i.name().toLowerCase(), i.get(), i));
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));
} }
@Override @Override
@ -87,7 +85,6 @@ public class GuiPonySettings extends GuiScreen {
@Override @Override
public void onGuiClosed() { public void onGuiClosed() {
LiteLoader.getInstance().writeConfig(config); LiteLoader.getInstance().writeConfig(config);
MineLittlePony.getInstance().getRenderManager().initializeMobRenderers(mc.getRenderManager(), config);
} }
protected String getTitle() { protected String getTitle() {

View file

@ -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);
}

View file

@ -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<Boolean> {
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;
}
}