|
| 1 | +/* |
| 2 | + * To change this license header, choose License Headers in Project Properties. |
| 3 | + * To change this template file, choose Tools | Templates |
| 4 | + * and open the template in the editor. |
| 5 | + */ |
| 6 | +package de.guntram.mcmod.easiervillagertrading; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.List; |
| 10 | +import net.minecraft.client.gui.GuiButton; |
| 11 | +import net.minecraft.client.gui.GuiMerchant; |
| 12 | +import net.minecraft.enchantment.Enchantment; |
| 13 | +import net.minecraft.entity.player.InventoryPlayer; |
| 14 | +import net.minecraft.inventory.ClickType; |
| 15 | +import net.minecraft.inventory.Slot; |
| 16 | +import net.minecraft.item.Item; |
| 17 | +import net.minecraft.item.ItemEnchantedBook; |
| 18 | +import net.minecraft.item.ItemStack; |
| 19 | +import net.minecraft.nbt.NBTTagList; |
| 20 | +import net.minecraft.village.MerchantRecipe; |
| 21 | +import net.minecraft.village.MerchantRecipeList; |
| 22 | +import net.minecraft.world.World; |
| 23 | + |
| 24 | +/** |
| 25 | + * |
| 26 | + * @author gbl |
| 27 | + */ |
| 28 | +public class BetterGuiMerchant extends GuiMerchant { |
| 29 | + |
| 30 | + private final int addXSize=0; |
| 31 | + private final ItemStack tradeOK, tradeNOK; |
| 32 | + |
| 33 | + BetterGuiMerchant (InventoryPlayer inv, GuiMerchant template, World world) { |
| 34 | + super(inv, template.getMerchant(), world); |
| 35 | + this.xSize+=addXSize; |
| 36 | + tradeOK=new ItemStack(Item.getItemById(351), 1, 2); |
| 37 | + tradeNOK=new ItemStack(Item.getItemById(351), 1, 1); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) { |
| 42 | + super.drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public void drawScreen(int mouseX, int mouseY, float partialTicks) { |
| 47 | + super.drawScreen(mouseX, mouseY, partialTicks); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) |
| 53 | + { |
| 54 | + super.drawGuiContainerForegroundLayer(mouseX, mouseY); |
| 55 | + MerchantRecipeList trades=getMerchant().getRecipes(null); |
| 56 | + if (trades==null) |
| 57 | + return; |
| 58 | + String s = trades.size()+" trades"; |
| 59 | + this.fontRenderer.drawString(s, this.xSize-addXSize+5, 0, 0xff00ff); |
| 60 | + // First draw all items, then all tooltips. This is extra effort, |
| 61 | + // but we don't want any items in front of any tooltips. |
| 62 | + for (int i=0; i<trades.size(); i++) { |
| 63 | + MerchantRecipe trade=trades.get(i); |
| 64 | + ItemStack i1=trade.getItemToBuy(); |
| 65 | + ItemStack i2=trade.hasSecondItemToBuy() ? trade.getSecondItemToBuy() : null; |
| 66 | + ItemStack o1=trade.getItemToSell(); |
| 67 | + drawItem(i1, this.xSize-addXSize+5, i*18+20); |
| 68 | + drawItem(i2, this.xSize-addXSize+5+18, i*18+20); |
| 69 | + drawItem(o1, this.xSize-addXSize+5+60, i*18+20); |
| 70 | + |
| 71 | + NBTTagList enchantments; |
| 72 | + |
| 73 | + if (o1.getItem() instanceof ItemEnchantedBook) { |
| 74 | + enchantments=((ItemEnchantedBook)(o1.getItem())).getEnchantments(o1); |
| 75 | + } else { |
| 76 | + enchantments=o1.getEnchantmentTagList(); |
| 77 | + } |
| 78 | + if (enchantments != null) |
| 79 | + { |
| 80 | + StringBuilder enchants=new StringBuilder(); |
| 81 | + for (int t = 0; t < enchantments.tagCount(); ++t) |
| 82 | + { |
| 83 | + int j = enchantments.getCompoundTagAt(t).getShort("id"); |
| 84 | + int k = enchantments.getCompoundTagAt(t).getShort("lvl"); |
| 85 | + |
| 86 | + if (Enchantment.getEnchantmentByID(j) != null) |
| 87 | + { |
| 88 | + if (t>0) |
| 89 | + enchants.append(", "); |
| 90 | + enchants.append(Enchantment.getEnchantmentByID(j).getTranslatedName(k)); |
| 91 | + } |
| 92 | + } |
| 93 | + fontRenderer.drawString(enchants.toString(), this.xSize-addXSize+85, i*18+24, 0xffff00); |
| 94 | + } |
| 95 | + drawItem(trade.isRecipeDisabled() ? tradeNOK : tradeOK, xSize-addXSize+5+40, i*18+20); |
| 96 | + } |
| 97 | + for (int i=0; i<trades.size(); i++) { |
| 98 | + MerchantRecipe trade=trades.get(i); |
| 99 | + ItemStack i1=trade.getItemToBuy(); |
| 100 | + ItemStack i2=trade.hasSecondItemToBuy() ? trade.getSecondItemToBuy() : null; |
| 101 | + ItemStack o1=trade.getItemToSell(); |
| 102 | + drawTooltip(i1, this.xSize-addXSize+5, i*18+20, mouseX, mouseY); |
| 103 | + drawTooltip(i2, this.xSize-addXSize+5+18, i*18+20, mouseX, mouseY); |
| 104 | + drawTooltip(o1, this.xSize-addXSize+5+54, i*18+20, mouseX, mouseY); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void drawItem(ItemStack stack, int x, int y) { |
| 109 | + if (stack==null) |
| 110 | + return; |
| 111 | + itemRender.renderItemAndEffectIntoGUI(stack, x, y); |
| 112 | + itemRender.renderItemOverlays(fontRenderer, stack, x, y); |
| 113 | + } |
| 114 | + |
| 115 | + private void drawTooltip(ItemStack stack, int x, int y, int mousex, int mousey) { |
| 116 | + if (stack==null) |
| 117 | + return; |
| 118 | + mousex-=guiLeft; |
| 119 | + mousey-=guiTop; |
| 120 | + if (mousex>=x && mousex<=x+16 && mousey>=y && mousey<=y+16) |
| 121 | + renderToolTip(stack, mousex, mousey); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + protected void mouseClicked(final int mouseX, final int mouseY, final int mouseButton) throws IOException { |
| 126 | + System.out.println("click at "+mouseX+"/"+mouseY); |
| 127 | + if ((mouseX - this.guiLeft) > this.xSize-addXSize && (mouseX - this.guiLeft) < this.xSize-addXSize+80) { |
| 128 | + int tradeIndex=(mouseY-this.guiTop-20)/18; |
| 129 | + MerchantRecipeList trades=getMerchant().getRecipes(null); |
| 130 | + int numTrades=trades.size(); |
| 131 | + if (tradeIndex>=0 && tradeIndex<numTrades) { |
| 132 | + System.out.println("tradeIndex="+tradeIndex+", numTrades="+numTrades); |
| 133 | + GuiButton myNextButton = this.buttonList.get(0); |
| 134 | + GuiButton myPrevButton = this.buttonList.get(1); |
| 135 | + for (int i=0; i<numTrades; i++) |
| 136 | + this.actionPerformed(myPrevButton); |
| 137 | + for (int i=0; i<tradeIndex; i++) |
| 138 | + this.actionPerformed(myNextButton); |
| 139 | + MerchantRecipe recipe=trades.get(tradeIndex); |
| 140 | + if (!recipe.isRecipeDisabled() |
| 141 | + && inputSlotsAreEmpty() |
| 142 | + && hasEnoughItemsInInventory(recipe) |
| 143 | + && canReceiveOutput(recipe.getItemToSell())) { |
| 144 | + transact(recipe); |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + } else { |
| 149 | + super.mouseClicked(mouseX, mouseY, mouseButton); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private boolean inputSlotsAreEmpty() { |
| 154 | + return |
| 155 | + inventorySlots.getSlot(0).getHasStack() == false |
| 156 | + && inventorySlots.getSlot(1).getHasStack() == false |
| 157 | + && inventorySlots.getSlot(2).getHasStack() == false; |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + private boolean hasEnoughItemsInInventory(MerchantRecipe recipe) { |
| 162 | + if (!hasEnoughItemsInInventory(recipe.getItemToBuy())) |
| 163 | + return false; |
| 164 | + if (recipe.hasSecondItemToBuy() && !hasEnoughItemsInInventory(recipe.getSecondItemToBuy())) |
| 165 | + return false; |
| 166 | + return true; |
| 167 | + } |
| 168 | + |
| 169 | + private boolean hasEnoughItemsInInventory(ItemStack stack) { |
| 170 | + int remaining=stack.getCount(); |
| 171 | + for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) { |
| 172 | + ItemStack invstack=inventorySlots.getSlot(i).getStack(); |
| 173 | + if (invstack==null) |
| 174 | + continue; |
| 175 | + if (stack.getItem().equals(invstack.getItem())) { |
| 176 | + System.out.println("taking "+invstack.getCount()+" items from slot # "+i); |
| 177 | + remaining-=invstack.getCount(); |
| 178 | + } |
| 179 | + if (remaining<=0) |
| 180 | + return true; |
| 181 | + } |
| 182 | + return false; |
| 183 | + } |
| 184 | + |
| 185 | + private boolean canReceiveOutput(ItemStack stack) { |
| 186 | + int remaining=stack.getCount(); |
| 187 | + for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) { |
| 188 | + ItemStack invstack=inventorySlots.getSlot(i).getStack(); |
| 189 | + if (invstack==null || invstack.isEmpty()) { |
| 190 | + System.out.println("can put result into empty slot "+i); |
| 191 | + return true; |
| 192 | + } |
| 193 | + if (stack.getItem().equals(invstack.getItem())) { |
| 194 | + System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i); |
| 195 | + remaining-=(invstack.getMaxStackSize()-invstack.getCount()); |
| 196 | + } |
| 197 | + if (remaining<=0) |
| 198 | + return true; |
| 199 | + } |
| 200 | + return false; |
| 201 | + } |
| 202 | + |
| 203 | + private void transact(MerchantRecipe recipe) { |
| 204 | + System.out.println("fill input slots called"); |
| 205 | + int putback0=-1, putback1=-1; |
| 206 | + putback0=fillSlot(0, recipe.getItemToBuy()); |
| 207 | + if (recipe.hasSecondItemToBuy()) { |
| 208 | + putback1=fillSlot(1, recipe.getSecondItemToBuy()); |
| 209 | + } |
| 210 | + getslot(2, recipe.getItemToSell(), putback0, putback1); |
| 211 | + System.out.println("putting back to slot "+putback0+" from 0, and to "+putback1+"from 1"); |
| 212 | + if (putback0!=-1) { |
| 213 | + mc.playerController.windowClick(mc.player.openContainer.windowId, 0, 0, ClickType.PICKUP, mc.player); |
| 214 | + mc.playerController.windowClick(mc.player.openContainer.windowId, putback0, 0, ClickType.PICKUP, mc.player); |
| 215 | + } |
| 216 | + if (putback1!=-1) { |
| 217 | + mc.playerController.windowClick(mc.player.openContainer.windowId, 1, 0, ClickType.PICKUP, mc.player); |
| 218 | + mc.playerController.windowClick(mc.player.openContainer.windowId, putback1, 0, ClickType.PICKUP, mc.player); |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * |
| 224 | + * @param slot - the number of the (trading) slot that should receive items |
| 225 | + * @param stack - what the trading slot should receive |
| 226 | + * @return the number of the inventory slot into which these items should be put back |
| 227 | + * after the transaction. May be -1 if nothing needs to be put back. |
| 228 | + */ |
| 229 | + private int fillSlot(int slot, ItemStack stack) { |
| 230 | + int remaining=stack.getCount(); |
| 231 | + for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) { |
| 232 | + ItemStack invstack=inventorySlots.getSlot(i).getStack(); |
| 233 | + if (invstack==null) |
| 234 | + continue; |
| 235 | + boolean needPutBack=false; |
| 236 | + if (stack.getItem().equals(invstack.getItem())) { |
| 237 | + if (stack.getCount()+invstack.getCount() > stack.getMaxStackSize()) |
| 238 | + needPutBack=true; |
| 239 | + remaining-=invstack.getCount(); |
| 240 | + System.out.println("taking "+invstack.getCount()+" items from slot # "+i+", remaining is now "+remaining); |
| 241 | + mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player); |
| 242 | + mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player); |
| 243 | + } |
| 244 | + if (needPutBack) { |
| 245 | + mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player); |
| 246 | + } |
| 247 | + if (remaining<=0) |
| 248 | + return remaining<0 ? i : -1; |
| 249 | + } |
| 250 | + // We should not be able to arrive here, since hasEnoughItemsInInventory should have been |
| 251 | + // called before fillSlot. But if we do, something went wrong; in this case better do a bit less. |
| 252 | + return -1; |
| 253 | + } |
| 254 | + |
| 255 | + private void getslot(int slot, ItemStack stack, int... forbidden) { |
| 256 | + int remaining=stack.getCount(); |
| 257 | + mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player); |
| 258 | + for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) { |
| 259 | + ItemStack invstack=inventorySlots.getSlot(i).getStack(); |
| 260 | + if (invstack==null || invstack.isEmpty()) { |
| 261 | + continue; |
| 262 | + } |
| 263 | + if (stack.getItem().equals(invstack.getItem())) { |
| 264 | + System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i); |
| 265 | + remaining-=(invstack.getMaxStackSize()-invstack.getCount()); |
| 266 | + mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player); |
| 267 | + } |
| 268 | + if (remaining<=0) |
| 269 | + return; |
| 270 | + } |
| 271 | + |
| 272 | + // When looking for an empty slot, don't take one that we want to put some input back to. |
| 273 | + for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) { |
| 274 | + boolean isForbidden=false; |
| 275 | + for (int f:forbidden) { |
| 276 | + if (i==f) |
| 277 | + isForbidden=true; |
| 278 | + } |
| 279 | + if (isForbidden) |
| 280 | + continue; |
| 281 | + ItemStack invstack=inventorySlots.getSlot(i).getStack(); |
| 282 | + if (invstack==null || invstack.isEmpty()) { |
| 283 | + mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player); |
| 284 | + System.out.println("putting result into empty slot "+i); |
| 285 | + return; |
| 286 | + } |
| 287 | + } |
| 288 | + } |
| 289 | +} |
0 commit comments