diff --git a/src/main/java/com/minelittlepony/MineLittlePony.java b/src/main/java/com/minelittlepony/MineLittlePony.java index 381bcfe4..16303ade 100644 --- a/src/main/java/com/minelittlepony/MineLittlePony.java +++ b/src/main/java/com/minelittlepony/MineLittlePony.java @@ -1,9 +1,9 @@ package com.minelittlepony; -import com.minelittlepony.gui.GuiPonySettings; import com.minelittlepony.hdskins.gui.GuiSkinsMineLP; import com.minelittlepony.pony.data.IPonyData; import com.minelittlepony.pony.data.PonyDataSerialzier; +import com.minelittlepony.render.ponies.MobRenderers; import com.minelittlepony.settings.PonyConfig; import com.voxelmodpack.hdskins.HDSkinManager; import com.voxelmodpack.hdskins.gui.GuiSkins; @@ -11,13 +11,16 @@ import com.voxelmodpack.hdskins.skins.SkinServer; import net.minecraft.client.Minecraft; import net.minecraft.client.resources.IReloadableResourceManager; import net.minecraft.client.resources.data.MetadataSerializer; -import net.minecraft.client.settings.KeyBinding; +import net.minecraftforge.client.event.GuiScreenEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.common.config.Config; +import net.minecraftforge.common.config.ConfigManager; +import net.minecraftforge.fml.client.event.ConfigChangedEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.event.FMLLoadCompleteEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import org.apache.logging.log4j.Logger; -import org.lwjgl.input.Keyboard; - @Mod(modid = "minelittlepony", name = MineLittlePony.MOD_NAME, version = MineLittlePony.MOD_VERSION, clientSideOnly = true) public class MineLittlePony { @@ -29,12 +32,8 @@ public class MineLittlePony { private static final String MINELP_LEGACY_SERVER = "legacy:http://minelpskins.voxelmodpack.com;http://minelpskinmanager.voxelmodpack.com"; - // TODO Replace this with a config screen - private static final KeyBinding SETTINGS_GUI = new KeyBinding("Settings", Keyboard.KEY_F9, "Mine Little Pony"); - private static MineLittlePony instance; - private PonyConfig.Loader configLoader; private PonyManager ponyManager; private PonyRenderManager renderManager; @@ -44,9 +43,11 @@ public class MineLittlePony { instance = this; logger = event.getModLog(); - configLoader = new PonyConfig.Loader(event.getModConfigurationDirectory().toPath().resolve("minelittlepony.json")); - ponyManager = new PonyManager(configLoader.getConfig()); + MinecraftForge.EVENT_BUS.register(this); + ConfigManager.sync("minelittlepony", Config.Type.INSTANCE); + + ponyManager = new PonyManager(); IReloadableResourceManager irrm = (IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager(); irrm.registerReloadListener(ponyManager); @@ -71,25 +72,32 @@ public class MineLittlePony { manager.addClearListener(ponyManager); renderManager.initialisePlayerRenderers(); - renderManager.initializeMobRenderers(configLoader.getConfig()); + renderManager.initializeMobRenderers(); } /** * Called on every update tick */ - void onTick(Minecraft minecraft, boolean inGame) { + @SubscribeEvent + public void onTick(GuiScreenEvent.InitGuiEvent.Pre event) { - if (inGame && minecraft.currentScreen == null && SETTINGS_GUI.isPressed()) { - minecraft.displayGuiScreen(new GuiPonySettings()); + if (event.getGui() instanceof GuiSkins && !(event.getGui() instanceof GuiSkinsMineLP)) { + event.setCanceled(true); + Minecraft.getMinecraft().displayGuiScreen(new GuiSkinsMineLP(ponyManager)); } + HDSkinManager.INSTANCE.setEnabled(PonyConfig.hd); + } - boolean skins = minecraft.currentScreen instanceof GuiSkins - && !(minecraft.currentScreen instanceof GuiSkinsMineLP); - if (skins) { - minecraft.displayGuiScreen(new GuiSkinsMineLP(ponyManager)); + @SubscribeEvent + public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) { +// System.out.println("Config " + event.getModID() + "." + event.getConfigID() + " Loaded"); + if (event.getModID().equals("minelittlepony")) { + ConfigManager.sync("minelittlepony", Config.Type.INSTANCE); + for (MobRenderers mobRenderers : MobRenderers.values()) { + mobRenderers.set(mobRenderers.get()); + } + renderManager.initializeMobRenderers(); } - HDSkinManager.INSTANCE.setEnabled(configLoader.getConfig().hd); - } /** @@ -113,11 +121,4 @@ public class MineLittlePony { return renderManager; } - /** - * Gets the global MineLP client configuration. - */ - public static PonyConfig.Loader getConfigLoader() { - return getInstance().configLoader; - } - } diff --git a/src/main/java/com/minelittlepony/PonyManager.java b/src/main/java/com/minelittlepony/PonyManager.java index 5098dc60..386e80e0 100644 --- a/src/main/java/com/minelittlepony/PonyManager.java +++ b/src/main/java/com/minelittlepony/PonyManager.java @@ -45,12 +45,9 @@ public class PonyManager implements IResourceManagerReloadListener, ISkinCacheCl */ private List backgroundPonyList = Lists.newArrayList(); - private PonyConfig config; - private Map poniesCache = Maps.newHashMap(); - public PonyManager(PonyConfig config) { - this.config = config; + public PonyManager() { initmodels(); } @@ -109,7 +106,7 @@ public class PonyManager implements IResourceManagerReloadListener, ISkinCacheCl public Pony getPony(ResourceLocation resource, UUID uuid) { Pony pony = getPony(resource, isSlimSkin(uuid)); - if (config.getPonyLevel() == PonyLevel.PONIES && pony.getMetadata().getRace().isHuman()) { + if (PonyConfig.getPonyLevel() == PonyLevel.PONIES && pony.getMetadata().getRace().isHuman()) { return getBackgroundPony(uuid); } @@ -122,7 +119,7 @@ public class PonyManager implements IResourceManagerReloadListener, ISkinCacheCl * @param uuid id of a player or entity */ public Pony getDefaultPony(UUID uuid) { - if (config.getPonyLevel() != PonyLevel.PONIES) { + if (PonyConfig.getPonyLevel() != PonyLevel.PONIES) { return getPony(DefaultPlayerSkin.getDefaultSkin(uuid), isSlimSkin(uuid)); } diff --git a/src/main/java/com/minelittlepony/PonyRenderManager.java b/src/main/java/com/minelittlepony/PonyRenderManager.java index 830638d2..5503b48b 100644 --- a/src/main/java/com/minelittlepony/PonyRenderManager.java +++ b/src/main/java/com/minelittlepony/PonyRenderManager.java @@ -7,7 +7,6 @@ import com.minelittlepony.model.player.PlayerModels; import com.minelittlepony.render.LevitatingItemRenderer; import com.minelittlepony.render.player.RenderPonyPlayer; import com.minelittlepony.render.ponies.MobRenderers; -import com.minelittlepony.settings.PonyConfig; import net.minecraft.client.renderer.entity.Render; import net.minecraft.client.renderer.entity.RenderManager; import net.minecraft.client.renderer.entity.RenderPlayer; @@ -66,9 +65,9 @@ public class PonyRenderManager { /** * Registers all entity model replacements. (except for players). */ - public void initializeMobRenderers(PonyConfig config) { + public void initializeMobRenderers() { for (MobRenderers i : MobRenderers.values()) { - boolean state = i.get(config); + boolean state = i.get(); i.register(state, this); if (state) { MineLittlePony.logger.info(i.name() + " are now ponies."); @@ -88,10 +87,11 @@ public class PonyRenderManager { */ @SuppressWarnings("unchecked") public void switchRenderer(boolean state, Class type, IRenderFactory renderer) { + // always put the original, regardless of state + if (!renderMap.containsKey(type)) { + renderMap.put(type, manager.getEntityClassRenderObject(type)); + } if (state) { - if (!renderMap.containsKey(type)) { - renderMap.put(type, manager.getEntityClassRenderObject(type)); - } RenderingRegistry.registerEntityRenderingHandler(type, renderer); } else if (renderMap.containsKey(type)) { RenderingRegistry.registerEntityRenderingHandler(type, mngr -> (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 ffb86635..09a5e74f 100644 --- a/src/main/java/com/minelittlepony/gui/GuiPonySettings.java +++ b/src/main/java/com/minelittlepony/gui/GuiPonySettings.java @@ -1,6 +1,5 @@ package com.minelittlepony.gui; -import com.minelittlepony.MineLittlePony; import com.minelittlepony.pony.data.PonyLevel; import com.minelittlepony.render.ponies.MobRenderers; import com.minelittlepony.settings.PonyConfig; @@ -21,13 +20,8 @@ public class GuiPonySettings extends GuiConfig { private static final String MOB_PREFIX = "minelp.mobs."; - private PonyConfig.Loader configLoader; - private PonyConfig config; - public GuiPonySettings() { super(null, "minelittlepony", "Mine Little Pony"); - configLoader = MineLittlePony.getConfigLoader(); - config = configLoader.getConfig(); } @Override @@ -42,18 +36,18 @@ public class GuiPonySettings extends GuiConfig { } addButton(new Label(LEFT, row += 15, PONY_LEVEL, -1)); - addButton(new Slider(LEFT, row += 15, 0, 2, config.getPonyLevel().ordinal(), (int id, String name, float value) -> { + addButton(new Slider(LEFT, row += 15, 0, 2, PonyConfig.getPonyLevel().ordinal(), (int id, String name, float value) -> { return I18n.format(PONY_LEVEL + "." + PonyLevel.valueFor((int) value).name().toLowerCase()); }, v -> { PonyLevel level = PonyLevel.valueFor((int) v); - config.setPonyLevel(level); + PonyConfig.setPonyLevel(level); return (float)level.ordinal(); })); row += 15; addButton(new Label(LEFT, row += 15, OPTIONS_PREFIX + "options", -1)); - for (Setting i : PonyConfig.PonySettings.values()) { - addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + i.name().toLowerCase(), i.get(config), b -> i.set(config, b))); + for (Setting i : PonyConfig.PonySettings.values()) { + addButton(new Checkbox(LEFT, row += 15, OPTIONS_PREFIX + i.name().toLowerCase(), i.get(), i::set)); } if (mustScroll()) { @@ -64,7 +58,7 @@ public class GuiPonySettings extends GuiConfig { 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.get(config), v -> i.set(config, v))); + addButton(new Checkbox(RIGHT, row += 15, MOB_PREFIX + i.name().toLowerCase(), i.get(), i::set)); } } @@ -81,10 +75,6 @@ public class GuiPonySettings extends GuiConfig { super.drawScreen(mouseX, mouseY, partialTicks); } - @Override - public void onGuiClosed() { - configLoader.save(); - } protected String getTitle() { return OPTIONS_PREFIX + "title"; diff --git a/src/main/java/com/minelittlepony/model/components/PonySnout.java b/src/main/java/com/minelittlepony/model/components/PonySnout.java index ba5b06cf..60c7c0b1 100644 --- a/src/main/java/com/minelittlepony/model/components/PonySnout.java +++ b/src/main/java/com/minelittlepony/model/components/PonySnout.java @@ -2,10 +2,10 @@ package com.minelittlepony.model.components; import static com.minelittlepony.model.PonyModelConstants.*; -import com.minelittlepony.MineLittlePony; import com.minelittlepony.model.AbstractPonyModel; import com.minelittlepony.pony.data.PonyGender; import com.minelittlepony.render.plane.PlaneRenderer; +import com.minelittlepony.settings.PonyConfig; public class PonySnout { @@ -50,7 +50,7 @@ public class PonySnout { } public void setGender(PonyGender gender) { - boolean show = !isHidden && MineLittlePony.getConfigLoader().getConfig().snuzzles; + boolean show = !isHidden && PonyConfig.snuzzles; mare.isHidden = !show || gender == PonyGender.STALLION; stallion.isHidden = !show || gender == PonyGender.MARE; diff --git a/src/main/java/com/minelittlepony/pony/data/Pony.java b/src/main/java/com/minelittlepony/pony/data/Pony.java index 759b392c..d250802e 100644 --- a/src/main/java/com/minelittlepony/pony/data/Pony.java +++ b/src/main/java/com/minelittlepony/pony/data/Pony.java @@ -4,6 +4,7 @@ import com.google.common.base.MoreObjects; import com.minelittlepony.MineLittlePony; import com.minelittlepony.mixin.MixinThreadDownloadImageData; import com.minelittlepony.model.ModelWrapper; +import com.minelittlepony.settings.PonyConfig; import com.voxelmodpack.hdskins.DynamicTextureImage; import com.voxelmodpack.hdskins.ThreadDownloadImageETag; @@ -117,7 +118,7 @@ public class Pony { } public PonyRace getRace(boolean ignorePony) { - return metadata.getRace().getEffectiveRace(MineLittlePony.getConfigLoader().getConfig().getEffectivePonyLevel(ignorePony)); + return metadata.getRace().getEffectiveRace(PonyConfig.getEffectivePonyLevel(ignorePony)); } public ResourceLocation getTexture() { diff --git a/src/main/java/com/minelittlepony/pony/data/PonyData.java b/src/main/java/com/minelittlepony/pony/data/PonyData.java index e4803ecc..7450da83 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.settings.PonyConfig; import java.awt.image.BufferedImage; import javax.annotation.concurrent.Immutable; @@ -58,7 +58,7 @@ public class PonyData implements IPonyData { @Override public PonySize getSize() { - return MineLittlePony.getConfigLoader().getConfig().sizes ? size : PonySize.NORMAL; + return PonyConfig.sizes ? 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 5ba235c2..64021e76 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.settings.PonyConfig; public enum PonySize implements ITriggerPixelMapped { NORMAL(0, 0.4f, 1f), @@ -20,14 +20,14 @@ public enum PonySize implements ITriggerPixelMapped { } public float getShadowSize() { - if (MineLittlePony.getConfigLoader().getConfig().showscale) { + if (PonyConfig.showscale) { return shadowSize * 0.9F; } return shadowSize; } public float getScaleFactor() { - if (MineLittlePony.getConfigLoader().getConfig().showscale) { + if (PonyConfig.showscale) { return scale * 0.9F; } return scale; diff --git a/src/main/java/com/minelittlepony/render/RenderPonyMob.java b/src/main/java/com/minelittlepony/render/RenderPonyMob.java index c2027178..2a087a16 100644 --- a/src/main/java/com/minelittlepony/render/RenderPonyMob.java +++ b/src/main/java/com/minelittlepony/render/RenderPonyMob.java @@ -10,6 +10,7 @@ import com.minelittlepony.render.layer.LayerHeldPonyItemMagical; import com.minelittlepony.render.layer.LayerPonyArmor; import com.minelittlepony.render.layer.LayerPonyCustomHead; import com.minelittlepony.render.layer.LayerPonyElytra; +import com.minelittlepony.settings.PonyConfig; import com.voxelmodpack.hdskins.HDSkinManager; import net.minecraft.client.renderer.GlStateManager; @@ -83,7 +84,7 @@ public abstract class RenderPonyMob extends RenderLiving public float getShadowScale() { if (mainModel.isChild) { return 0.25F; - } else if (MineLittlePony.getConfigLoader().getConfig().showscale) { + } else if (PonyConfig.showscale) { return 0.4F; } return 0.5F; @@ -91,7 +92,7 @@ public abstract class RenderPonyMob extends RenderLiving @Override public float getScaleFactor() { - if (MineLittlePony.getConfigLoader().getConfig().showscale) return 0.9F; + if (PonyConfig.showscale) return 0.9F; return 1; } diff --git a/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java b/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java index 3c42303b..41480ec0 100644 --- a/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java +++ b/src/main/java/com/minelittlepony/render/ponies/MobRenderers.java @@ -2,7 +2,7 @@ package com.minelittlepony.render.ponies; import com.minelittlepony.MineLittlePony; import com.minelittlepony.PonyRenderManager; -import com.minelittlepony.settings.PonyConfig; +import com.minelittlepony.settings.MobConfig; import com.minelittlepony.settings.Setting; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.entity.RenderManager; @@ -29,7 +29,7 @@ import net.minecraftforge.fml.client.registry.RenderingRegistry; * * Due to the limitations in Mumfrey's framework, needs to be paired with a field in PonyConfig. */ -public enum MobRenderers implements Setting { +public enum MobRenderers implements Setting { VILLAGERS { @Override public void register(boolean state, PonyRenderManager pony) { @@ -78,8 +78,13 @@ public enum MobRenderers implements Setting { }; @Override - public void set(PonyConfig config, boolean value) { - Setting.super.set(config, value); + public Class getEnclosingClass() { + return MobConfig.class; + } + + @Override + public void set(boolean value) { + Setting.super.set(value); apply(value, MineLittlePony.getInstance().getRenderManager(), Minecraft.getMinecraft().getRenderManager()); } diff --git a/src/main/java/com/minelittlepony/settings/MobConfig.java b/src/main/java/com/minelittlepony/settings/MobConfig.java new file mode 100644 index 00000000..27d6457d --- /dev/null +++ b/src/main/java/com/minelittlepony/settings/MobConfig.java @@ -0,0 +1,14 @@ +package com.minelittlepony.settings; + +import net.minecraftforge.common.config.Config; + +@Config(modid = "minelittlepony", name = "mobs", category = "mobs") +public class MobConfig { + + public static boolean villagers = true; + public static boolean zombies = true; + public static boolean pigzombies = true; + public static boolean skeletons = true; + public static boolean illagers = true; + public static boolean guardians = true; +} diff --git a/src/main/java/com/minelittlepony/settings/PonyConfig.java b/src/main/java/com/minelittlepony/settings/PonyConfig.java index aa5f24db..c172b6da 100644 --- a/src/main/java/com/minelittlepony/settings/PonyConfig.java +++ b/src/main/java/com/minelittlepony/settings/PonyConfig.java @@ -1,37 +1,26 @@ package com.minelittlepony.settings; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.minelittlepony.MineLittlePony; import com.minelittlepony.pony.data.PonyLevel; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.NoSuchFileException; -import java.nio.file.Path; +import net.minecraftforge.common.config.Config; /** * Storage container for MineLP client settings. */ +@Config(modid = "minelittlepony", name = "options") +@Config.LangKey("minelp.options.options") public class PonyConfig { - private PonyLevel ponylevel = PonyLevel.PONIES; + @Config.LangKey("minelp.options.ponylevel") + public static PonyLevel ponylevel = PonyLevel.PONIES; - public boolean sizes = true; - public boolean snuzzles = true; - public boolean hd = true; - public boolean showscale = true; + @Config.LangKey("minelp.options.sizes") + public static boolean sizes = true; + public static boolean snuzzles = true; + public static boolean hd = true; + public static boolean showscale = true; - public boolean villagers = true; - public boolean zombies = true; - public boolean pigzombies = true; - public boolean skeletons = true; - public boolean illagers = true; - public boolean guardians = true; - public enum PonySettings implements Setting { + public enum PonySettings implements Setting { SIZES, SNUZZLES, HD, @@ -44,14 +33,14 @@ public class PonyConfig { * * @param ignorePony true to ignore whatever value the setting has. */ - public PonyLevel getEffectivePonyLevel(boolean ignorePony) { + public static PonyLevel getEffectivePonyLevel(boolean ignorePony) { return ignorePony ? PonyLevel.BOTH : getPonyLevel(); } /** * Actually gets the pony level value. No option to ignore reality here. */ - public PonyLevel getPonyLevel() { + public static PonyLevel getPonyLevel() { if (ponylevel == null) { ponylevel = PonyLevel.PONIES; } @@ -63,51 +52,8 @@ public class PonyConfig { * * @param ponylevel */ - public void setPonyLevel(PonyLevel ponylevel) { - this.ponylevel = ponylevel; + public static void setPonyLevel(PonyLevel ponylevel) { + PonyConfig.ponylevel = ponylevel; } - public void save() { - - } - - public static class Loader { - - private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); - - private final Path path; - - private PonyConfig config; - - public Loader(Path path) { - this.path = path; - } - - public PonyConfig getConfig() { - if (config == null) { - reload(); - } - return config; - } - - public void reload() { - try (BufferedReader reader = Files.newBufferedReader(path)) { - config = gson.fromJson(reader, PonyConfig.class); - } catch (NoSuchFileException e) { - config = new PonyConfig(); - } catch (IOException e) { - MineLittlePony.logger.warn("Error while loading config. Using defaults.", e); - config = new PonyConfig(); - } - save(); - } - - public void save() { - try (BufferedWriter writer = Files.newBufferedWriter(path)) { - gson.toJson(getConfig(), writer); - } catch (IOException e) { - MineLittlePony.logger.warn("Unable to save config.", e); - } - } - } } diff --git a/src/main/java/com/minelittlepony/settings/Setting.java b/src/main/java/com/minelittlepony/settings/Setting.java index 45d0cb5d..45be8e05 100644 --- a/src/main/java/com/minelittlepony/settings/Setting.java +++ b/src/main/java/com/minelittlepony/settings/Setting.java @@ -3,33 +3,37 @@ package com.minelittlepony.settings; import java.lang.reflect.Field; -public interface Setting { +public interface Setting { String name(); - default boolean get(Config config) { + default boolean get() { try { - Field field = getField(config.getClass()); - return field.getBoolean(config); + Field field = getField(); + return field.getBoolean(null); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } - default void set(Config config, boolean value) { + default void set(boolean value) { try { - Field field = getField(config.getClass()); - field.setBoolean(config, value); + Field field = getField(); + field.setBoolean(null, value); } catch (IllegalAccessException e) { throw new RuntimeException(e); } } - default Field getField(Class owner) { + default Class getEnclosingClass() { + return getClass().getEnclosingClass(); + } + + default Field getField() { try { - return owner.getField(name().toLowerCase()); + return getEnclosingClass().getField(name().toLowerCase()); } catch (NoSuchFieldException e) { - throw new IllegalArgumentException(e); + throw new RuntimeException(e); } } } diff --git a/src/main/resources/mcmod.info b/src/main/resources/mcmod.info new file mode 100644 index 00000000..4c01e02a --- /dev/null +++ b/src/main/resources/mcmod.info @@ -0,0 +1,9 @@ +[{ + "modid": "minelittlepony", + "name": "Mine Little Pony", + "description": "Mine Little Pony turns players and mobs into ponies", + "version": "1.0.0.0", + "url": "minecraftforge.net/", + "authorList": ["killjoy"], + "credits": "I'd like to thank my mother and father." +}]