Rewrite pony configs

This commit is contained in:
Sollace 2018-07-30 15:14:41 +02:00
parent 6337b055c9
commit 538c9c1b89
21 changed files with 137 additions and 218 deletions

View file

@ -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) {

View file

@ -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<Boolean> sizes = Value.of(true);
@Expose private final Value<Boolean> snuzzles = Value.of(true);
@Expose private final Value<Boolean> hd = Value.of(true);
@Expose private final Value<Boolean> showscale = Value.of(true);
@Expose private final Value<Boolean> fpsmagic = Value.of(true);
@Expose private final Value<Boolean> ponyskulls = Value.of(true);
@Expose private final Value<Boolean> villagers = Value.of(true);
@Expose private final Value<Boolean> zombies = Value.of(true);
@Expose private final Value<Boolean> pigzombies = Value.of(true);
@Expose private final Value<Boolean> skeletons = Value.of(true);
@Expose private final Value<Boolean> illagers = Value.of(true);
@Expose private final Value<Boolean> guardians = Value.of(true);
public enum PonySettings implements Value<Boolean> {
SIZES(PonyConfig::getSizes),
SNUZZLES(PonyConfig::getSnuzzles),
HD(PonyConfig::getHd),
SHOWSCALE(PonyConfig::getShowscale),
FPSMAGIC(PonyConfig::getFpsmagic),
PONYSKULLS(PonyConfig::getPonyskulls);
private Value<Boolean> config;
PonySettings(Function<PonyConfig, Value<Boolean>> 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<Boolean> getSizes() {
return sizes;
}
public Value<Boolean> getSnuzzles() {
return snuzzles;
}
public Value<Boolean> getHd() {
return hd;
}
public Value<Boolean> getShowscale() {
return showscale;
}
public Value<Boolean> getFpsmagic() {
return fpsmagic;
}
public Value<Boolean> getPonyskulls() {
return ponyskulls;
}
public Value<Boolean> getVillagers() {
return villagers;
}
public Value<Boolean> getZombies() {
return zombies;
}
public Value<Boolean> getPigzombies() {
return pigzombies;
}
public Value<Boolean> getSkeletons() {
return skeletons;
}
public Value<Boolean> getIllagers() {
return illagers;
}
public Value<Boolean> getGuardians() {
return guardians;
public void setPonyLevel(PonyLevel newlevel) {
ponylevel = newlevel;
write();
}
}

View file

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

View file

@ -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<Boolean> setting;
private final IGuiCallback<Boolean> action;
public Checkbox(int x, int y, String displayString, Value<Boolean> setting) {
public Checkbox(int x, int y, String displayString, boolean value, IGuiCallback<Boolean> 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);
}
}

View file

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

View file

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

View file

@ -0,0 +1,15 @@
package com.minelittlepony.gui;
/**
* Response actions for UI events.
*/
@FunctionalInterface
public interface IGuiAction<T> {
/**
* Performs this action now.
*
* @param sender the element handling this event
*/
void perform(T sender);
}

View file

@ -0,0 +1,15 @@
package com.minelittlepony.gui;
/**
* Response actions for UI events.
*/
@FunctionalInterface
public interface IGuiCallback<T> {
/**
* 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);
}

View file

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

View file

@ -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

View file

@ -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<PonySize> {
@ -24,14 +24,14 @@ public enum PonySize implements ITriggerPixelMapped<PonySize> {
}
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;

View file

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

View file

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

View file

@ -101,7 +101,7 @@ public class LayerPonyCustomHead<T extends EntityLivingBase> implements LayerRen
}
private ModelWrapper getModel() {
return ((IRenderPony) renderer).getModelWrapper();
return ((IRenderPony<?>) renderer).getModelWrapper();
}
@Override

View file

@ -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<Boolean> {
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<Boolean> {
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<Boolean> {
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<Boolean> {
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<Boolean> {
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<Boolean> {
}
};
private final Value<Boolean> setting;
MobRenderers(Function<PonyConfig, Value<Boolean>> 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<Boolean> {
}
public abstract void register(boolean state, PonyRenderManager pony, RenderManager manager);
@Override
public ValueConfig config() {
return MineLittlePony.getConfig();
}
}

View file

@ -25,7 +25,7 @@ public class RenderPonySkeleton<Skeleton extends AbstractSkeleton> 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<Skeleton extends AbstractSkeleton> extends Rende
public static final ISkull SKULL = new PonySkull() {
@Override
public boolean canRender(PonyConfig config) {
return config.getSkeletons().get();
return MobRenderers.SKELETONS.get();
}
@Override

View file

@ -22,7 +22,7 @@ public class RenderPonyZombie<Zombie extends EntityZombie> extends RenderPonyMob
public static final ISkull SKULL = new PonySkull() {
@Override
public boolean canRender(PonyConfig config) {
return config.getZombies().get();
return MobRenderers.ZOMBIES.get();
}
@Override

View file

@ -1,20 +0,0 @@
package com.minelittlepony.settings;
public class BaseValue<T> implements Value<T> {
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;
}
}

View file

@ -1,12 +0,0 @@
package com.minelittlepony.settings;
public interface Value<T> {
T get();
void set(T value);
static <T> Value<T> of(T value) {
return new BaseValue<>(value);
}
}

View file

@ -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<String, Object> properties = new HashMap<String, Object>();
protected void write() {
LiteLoader.getInstance().writeConfig(this);
}
@Override
public File getConfigFile(File configFile, File configFileLocation, String defaultFileName) {
return null;
public interface Flag extends Setting<Boolean> {
default Boolean initial() {
return true;
}
}
public interface Setting<T> extends IGuiCallback<T> {
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();
}
}
}

View file

@ -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<Value<?>>, JsonDeserializer<Value<?>> {
@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());
}
}