From 191fde27533ce790128bfce5f3cd7c556f10393d Mon Sep 17 00:00:00 2001 From: Guntram Blohm Date: Tue, 11 Apr 2017 11:04:25 +0200 Subject: [PATCH] Version 1.1.0: Just Enough Items compatibility: show trades list to the left if JEI is loaded Left/Right and left pixel offset can be set in config --- build.gradle | 2 +- .../BetterGuiMerchant.java | 35 +++++++---- .../ConfigurationHandler.java | 62 +++++++++++++++++++ .../EasierVillagerTrading.java | 20 +++++- .../easiervillagertrading/GuiConfig.java | 16 +++++ .../easiervillagertrading/GuiFactory.java | 27 ++++++++ 6 files changed, 146 insertions(+), 16 deletions(-) create mode 100644 src/main/java/de/guntram/mcmod/easiervillagertrading/ConfigurationHandler.java create mode 100644 src/main/java/de/guntram/mcmod/easiervillagertrading/GuiConfig.java create mode 100644 src/main/java/de/guntram/mcmod/easiervillagertrading/GuiFactory.java diff --git a/build.gradle b/build.gradle index d1833f6..0e669d8 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ apply plugin: 'net.minecraftforge.gradle.forge' //Only edit below this line, the above code adds and enables the necessary things for Forge to be setup. -version = "1.01" +version = "1.1.0" group = "de.guntram.mcmod.easiervillagertrading" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "easiervillagertrading" diff --git a/src/main/java/de/guntram/mcmod/easiervillagertrading/BetterGuiMerchant.java b/src/main/java/de/guntram/mcmod/easiervillagertrading/BetterGuiMerchant.java index 7a8565e..56d8190 100644 --- a/src/main/java/de/guntram/mcmod/easiervillagertrading/BetterGuiMerchant.java +++ b/src/main/java/de/guntram/mcmod/easiervillagertrading/BetterGuiMerchant.java @@ -26,7 +26,7 @@ */ public class BetterGuiMerchant extends GuiMerchant { - private final int addXSize=0; + private int xBase=0; private final ItemStack tradeOK, tradeNOK; private final int lineHeight=18; private final int titleDistance=20; @@ -38,7 +38,13 @@ public class BetterGuiMerchant extends GuiMerchant { BetterGuiMerchant (InventoryPlayer inv, GuiMerchant template, World world) { super(inv, template.getMerchant(), world); - this.xSize+=addXSize; + if (ConfigurationHandler.showLeft()) { + xBase=-ConfigurationHandler.leftPixelOffset(); + if (xBase==0) + xBase=-this.getXSize(); + } + else + xBase=this.getXSize()+5; tradeOK=new ItemStack(Item.getItemById(351), 1, 2); tradeNOK=new ItemStack(Item.getItemById(351), 1, 1); } @@ -63,7 +69,7 @@ protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) return; int topAdjust=getTopAdjust(trades.size()); String s = trades.size()+" trades"; - this.fontRenderer.drawString(s, this.xSize-addXSize+5, -topAdjust, 0xff00ff); + this.fontRenderer.drawString(s, xBase, -topAdjust, 0xff00ff); // First draw all items, then all tooltips. This is extra effort, // but we don't want any items in front of any tooltips. RenderHelper.enableStandardItemLighting(); @@ -73,9 +79,9 @@ protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) ItemStack i1=trade.getItemToBuy(); ItemStack i2=trade.hasSecondItemToBuy() ? trade.getSecondItemToBuy() : null; ItemStack o1=trade.getItemToSell(); - drawItem(i1, this.xSize-addXSize+5+firstBuyItemXpos, i*lineHeight-topAdjust+titleDistance); - drawItem(i2, this.xSize-addXSize+5+secondBuyItemXpos, i*lineHeight-topAdjust+titleDistance); - drawItem(o1, this.xSize-addXSize+5+sellItemXpos, i*lineHeight-topAdjust+titleDistance); + drawItem(i1, xBase+firstBuyItemXpos, i*lineHeight-topAdjust+titleDistance); + drawItem(i2, xBase+secondBuyItemXpos, i*lineHeight-topAdjust+titleDistance); + drawItem(o1, xBase+sellItemXpos, i*lineHeight-topAdjust+titleDistance); NBTTagList enchantments; @@ -100,9 +106,12 @@ protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) enchants.append(enchant.getTranslatedName(k)); } } - fontRenderer.drawString(enchants.toString(), this.xSize-addXSize+textXpos, i*lineHeight-topAdjust+24, 0xffff00); + String shownEnchants=enchants.toString(); + if (xBase<0) + shownEnchants=fontRenderer.trimStringToWidth(shownEnchants, -xBase-textXpos-5); + fontRenderer.drawString(shownEnchants, xBase+textXpos, i*lineHeight-topAdjust+24, 0xffff00); } - drawItem(trade.isRecipeDisabled() ? tradeNOK : tradeOK, xSize-addXSize+5+okNokXpos, i*lineHeight-topAdjust+titleDistance); + drawItem(trade.isRecipeDisabled() ? tradeNOK : tradeOK, xBase+okNokXpos, i*lineHeight-topAdjust+titleDistance); } RenderHelper.disableStandardItemLighting(); for (int i=0; i this.xSize-addXSize - && (mouseX - this.guiLeft) < this.xSize-addXSize+textXpos + && (mouseX - this.guiLeft) >= xBase + && (mouseX - this.guiLeft) <= xBase+textXpos ) { MerchantRecipeList trades=getMerchant().getRecipes(null); if (trades==null) diff --git a/src/main/java/de/guntram/mcmod/easiervillagertrading/ConfigurationHandler.java b/src/main/java/de/guntram/mcmod/easiervillagertrading/ConfigurationHandler.java new file mode 100644 index 0000000..5ec5562 --- /dev/null +++ b/src/main/java/de/guntram/mcmod/easiervillagertrading/ConfigurationHandler.java @@ -0,0 +1,62 @@ +package de.guntram.mcmod.easiervillagertrading; + +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.client.event.ConfigChangedEvent; +import java.io.File; +import net.minecraftforge.common.config.Configuration; +import net.minecraftforge.fml.common.Loader; + +public class ConfigurationHandler { + + private static ConfigurationHandler instance; + + private Configuration config; + private String configFileName; + private boolean showLeft; + private int leftPixelOffset; + + public static ConfigurationHandler getInstance() { + if (instance==null) + instance=new ConfigurationHandler(); + return instance; + } + + public void load(final File configFile) { + if (config == null) { + config = new Configuration(configFile); + configFileName=configFile.getPath(); + loadConfig(); + } + } + + @SubscribeEvent + public void onConfigChanged(ConfigChangedEvent.OnConfigChangedEvent event) { + // System.out.println("OnConfigChanged for "+event.getModID()); + if (event.getModID().equalsIgnoreCase(EasierVillagerTrading.MODID)) { + loadConfig(); + } + } + + private void loadConfig() { + showLeft=config.getBoolean("Trades list left", Configuration.CATEGORY_CLIENT, + Loader.isModLoaded("jei"), + "Show trades list to the left, for Just Enough Items compatibility"); + leftPixelOffset=config.getInt("Trades left pixel offset", Configuration.CATEGORY_CLIENT, + 0, 0, Integer.MAX_VALUE, + "How many pixels left of the GUI the trades list will be shown. Use 0 for auto detect. "+ + "Only used if Trades list left is true."); + if (config.hasChanged()) + config.save(); + } + + public static Configuration getConfig() { + return getInstance().config; + } + + public static String getConfigFileName() { + return getInstance().configFileName; + } + + public static boolean showLeft() { return getInstance().showLeft; } + public static int leftPixelOffset() { return getInstance().leftPixelOffset; } +} diff --git a/src/main/java/de/guntram/mcmod/easiervillagertrading/EasierVillagerTrading.java b/src/main/java/de/guntram/mcmod/easiervillagertrading/EasierVillagerTrading.java index 6375049..69d485f 100644 --- a/src/main/java/de/guntram/mcmod/easiervillagertrading/EasierVillagerTrading.java +++ b/src/main/java/de/guntram/mcmod/easiervillagertrading/EasierVillagerTrading.java @@ -4,16 +4,32 @@ import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod.EventHandler; import net.minecraftforge.fml.common.event.FMLInitializationEvent; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; + +@Mod( + modid = EasierVillagerTrading.MODID, + version = EasierVillagerTrading.VERSION, + clientSideOnly = true, + guiFactory = "de.guntram.mcmod.easiervillagertrading.GuiFactory", + acceptedMinecraftVersions = "[1.11.2]", + dependencies = "after:jei" +) -@Mod(modid = EasierVillagerTrading.MODID, version = EasierVillagerTrading.VERSION) public class EasierVillagerTrading { public static final String MODID = "easiervillagertrading"; - public static final String VERSION = "1.01"; + public static final String VERSION = "1.1.0"; @EventHandler public void init(FMLInitializationEvent event) { MinecraftForge.EVENT_BUS.register(OpenTradeEventHandler.getInstance()); } + + @EventHandler + public void preInit(final FMLPreInitializationEvent event) { + ConfigurationHandler confHandler = ConfigurationHandler.getInstance(); + confHandler.load(event.getSuggestedConfigurationFile()); + MinecraftForge.EVENT_BUS.register(confHandler); + } } diff --git a/src/main/java/de/guntram/mcmod/easiervillagertrading/GuiConfig.java b/src/main/java/de/guntram/mcmod/easiervillagertrading/GuiConfig.java new file mode 100644 index 0000000..e58b0af --- /dev/null +++ b/src/main/java/de/guntram/mcmod/easiervillagertrading/GuiConfig.java @@ -0,0 +1,16 @@ +package de.guntram.mcmod.easiervillagertrading; + +import net.minecraft.client.gui.GuiScreen; +import net.minecraftforge.common.config.ConfigElement; +import static net.minecraftforge.common.config.Configuration.CATEGORY_CLIENT; + +public class GuiConfig extends net.minecraftforge.fml.client.config.GuiConfig { + public GuiConfig(GuiScreen parent) { + super(parent, + new ConfigElement(ConfigurationHandler.getConfig().getCategory(CATEGORY_CLIENT)).getChildElements(), + EasierVillagerTrading.MODID, + false, + false, + "Easier Villager Trading configuration"); + } +} diff --git a/src/main/java/de/guntram/mcmod/easiervillagertrading/GuiFactory.java b/src/main/java/de/guntram/mcmod/easiervillagertrading/GuiFactory.java new file mode 100644 index 0000000..7d41361 --- /dev/null +++ b/src/main/java/de/guntram/mcmod/easiervillagertrading/GuiFactory.java @@ -0,0 +1,27 @@ +package de.guntram.mcmod.easiervillagertrading; + +import java.util.Set; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraftforge.fml.client.IModGuiFactory; + +public class GuiFactory implements IModGuiFactory { + @Override + public void initialize(final Minecraft minecraftInstance) { + } + + @Override + public Class mainConfigGuiClass() { + return de.guntram.mcmod.easiervillagertrading.GuiConfig.class; + } + + @Override + public Set runtimeGuiCategories() { + return null; + } + + @Override + public IModGuiFactory.RuntimeOptionGuiHandler getHandlerFor(final IModGuiFactory.RuntimeOptionCategoryElement element) { + return null; + } +}