From 3bdde094e0dae6910afdbc028305b5631026ef92 Mon Sep 17 00:00:00 2001 From: Guntram Blohm Date: Sun, 5 Mar 2017 08:02:37 +0100 Subject: [PATCH] Various bugfixes and improvements: - long trades lists center-align with the original gui now instead of top; this helps sheperds especially with their long list of different colored wool - fixed display of items like pumpkins and melons that were shown too dark - several additional null pointer checks to prevent crashes - code cleanup - removed most debugging output - better check if two itemstacks can be merged, this prevents trade results being put into the wrong locations resulting in items in the hand after trading - added link to mod URL --- .../BetterGuiMerchant.java | 122 ++++++++++++------ .../EasierVillagerTrading.java | 2 +- src/main/resources/mcmod.info | 2 +- 3 files changed, 84 insertions(+), 42 deletions(-) diff --git a/src/main/java/de/guntram/mcmod/easiervillagertrading/BetterGuiMerchant.java b/src/main/java/de/guntram/mcmod/easiervillagertrading/BetterGuiMerchant.java index 577841b..7a8565e 100644 --- a/src/main/java/de/guntram/mcmod/easiervillagertrading/BetterGuiMerchant.java +++ b/src/main/java/de/guntram/mcmod/easiervillagertrading/BetterGuiMerchant.java @@ -6,13 +6,12 @@ package de.guntram.mcmod.easiervillagertrading; import java.io.IOException; -import java.util.List; import net.minecraft.client.gui.GuiButton; import net.minecraft.client.gui.GuiMerchant; +import net.minecraft.client.renderer.RenderHelper; import net.minecraft.enchantment.Enchantment; import net.minecraft.entity.player.InventoryPlayer; import net.minecraft.inventory.ClickType; -import net.minecraft.inventory.Slot; import net.minecraft.item.Item; import net.minecraft.item.ItemEnchantedBook; import net.minecraft.item.ItemStack; @@ -29,6 +28,13 @@ public class BetterGuiMerchant extends GuiMerchant { private final int addXSize=0; private final ItemStack tradeOK, tradeNOK; + private final int lineHeight=18; + private final int titleDistance=20; + private final int firstBuyItemXpos=0; + private final int secondBuyItemXpos=18; + private final int okNokXpos=40; + private final int sellItemXpos=60; + private final int textXpos=85; BetterGuiMerchant (InventoryPlayer inv, GuiMerchant template, World world) { super(inv, template.getMerchant(), world); @@ -55,18 +61,21 @@ protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) MerchantRecipeList trades=getMerchant().getRecipes(null); if (trades==null) return; + int topAdjust=getTopAdjust(trades.size()); String s = trades.size()+" trades"; - this.fontRenderer.drawString(s, this.xSize-addXSize+5, 0, 0xff00ff); + this.fontRenderer.drawString(s, this.xSize-addXSize+5, -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(); + RenderHelper.enableGUIStandardItemLighting(); for (int i=0; i0) enchants.append(", "); - enchants.append(Enchantment.getEnchantmentByID(j).getTranslatedName(k)); + enchants.append(enchant.getTranslatedName(k)); } } - fontRenderer.drawString(enchants.toString(), this.xSize-addXSize+85, i*18+24, 0xffff00); + fontRenderer.drawString(enchants.toString(), this.xSize-addXSize+textXpos, i*lineHeight-topAdjust+24, 0xffff00); } - drawItem(trade.isRecipeDisabled() ? tradeNOK : tradeOK, xSize-addXSize+5+40, i*18+20); + drawItem(trade.isRecipeDisabled() ? tradeNOK : tradeOK, xSize-addXSize+5+okNokXpos, i*lineHeight-topAdjust+titleDistance); } + RenderHelper.disableStandardItemLighting(); for (int i=0; i this.xSize-addXSize && (mouseX - this.guiLeft) < this.xSize-addXSize+80) { - int tradeIndex=(mouseY-this.guiTop-20)/18; + // System.out.println("click at "+mouseX+"/"+mouseY); + if (mouseButton==0 + && (mouseX - this.guiLeft) > this.xSize-addXSize + && (mouseX - this.guiLeft) < this.xSize-addXSize+textXpos + ) { MerchantRecipeList trades=getMerchant().getRecipes(null); + if (trades==null) + return; int numTrades=trades.size(); + int topAdjust=getTopAdjust(numTrades); + int tradeIndex=(mouseY+topAdjust-this.guiTop-titleDistance)/lineHeight; if (tradeIndex>=0 && tradeIndex= stack.getCount() + invstack.getCount()) { + //System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i); remaining-=(invstack.getMaxStackSize()-invstack.getCount()); } if (remaining<=0) @@ -201,21 +226,21 @@ private boolean canReceiveOutput(ItemStack stack) { } private void transact(MerchantRecipe recipe) { - System.out.println("fill input slots called"); - int putback0=-1, putback1=-1; + //System.out.println("fill input slots called"); + int putback0, putback1=-1; putback0=fillSlot(0, recipe.getItemToBuy()); if (recipe.hasSecondItemToBuy()) { putback1=fillSlot(1, recipe.getSecondItemToBuy()); } getslot(2, recipe.getItemToSell(), putback0, putback1); - System.out.println("putting back to slot "+putback0+" from 0, and to "+putback1+"from 1"); + //System.out.println("putting back to slot "+putback0+" from 0, and to "+putback1+"from 1"); if (putback0!=-1) { - mc.playerController.windowClick(mc.player.openContainer.windowId, 0, 0, ClickType.PICKUP, mc.player); - mc.playerController.windowClick(mc.player.openContainer.windowId, putback0, 0, ClickType.PICKUP, mc.player); + slotClick(0); + slotClick(putback0); } if (putback1!=-1) { - mc.playerController.windowClick(mc.player.openContainer.windowId, 1, 0, ClickType.PICKUP, mc.player); - mc.playerController.windowClick(mc.player.openContainer.windowId, putback1, 0, ClickType.PICKUP, mc.player); + slotClick(1); + slotClick(putback1); } } @@ -233,16 +258,16 @@ private int fillSlot(int slot, ItemStack stack) { if (invstack==null) continue; boolean needPutBack=false; - if (stack.getItem().equals(invstack.getItem())) { + if (areItemStacksMergable(stack, invstack)) { if (stack.getCount()+invstack.getCount() > stack.getMaxStackSize()) needPutBack=true; remaining-=invstack.getCount(); - System.out.println("taking "+invstack.getCount()+" items from slot # "+i+", remaining is now "+remaining); - mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player); - mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player); + // System.out.println("taking "+invstack.getCount()+" items from slot # "+i+", remaining is now "+remaining); + slotClick(i); + slotClick(slot); } if (needPutBack) { - mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player); + slotClick(i); } if (remaining<=0) return remaining<0 ? i : -1; @@ -252,18 +277,30 @@ private int fillSlot(int slot, ItemStack stack) { return -1; } + private boolean areItemStacksMergable(ItemStack a, ItemStack b) { + if (a==null || b==null) + return false; + if (a.getItem() == b.getItem() + && (!a.getHasSubtypes() || a.getItemDamage()==b.getItemDamage()) + && ItemStack.areItemStackTagsEqual(a, b)) + return true; + return false; + } + private void getslot(int slot, ItemStack stack, int... forbidden) { int remaining=stack.getCount(); - mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player); + slotClick(slot); for (int i=inventorySlots.inventorySlots.size()-36; i