| 1 | package net.minecraft.inventory;
|
| 2 |
|
| 3 | import net.minecraft.entity.IMerchant;
|
| 4 | import net.minecraft.entity.player.EntityPlayer;
|
| 5 | import net.minecraft.entity.player.InventoryPlayer;
|
| 6 | import net.minecraft.item.ItemStack;
|
| 7 | import net.minecraft.world.World;
|
| 8 |
|
| 9 | public class ContainerMerchant extends Container
|
| 10 | {
|
| 11 | /** Instance of Merchant. */
|
| 12 | private IMerchant theMerchant;
|
| 13 | private InventoryMerchant merchantInventory;
|
| 14 |
|
| 15 | /** Instance of World. */
|
| 16 | private final World theWorld;
|
| 17 |
|
| 18 | public ContainerMerchant(InventoryPlayer playerInventory, IMerchant merchant, World worldIn)
|
| 19 | {
|
| 20 | this.theMerchant = merchant;
|
| 21 | this.theWorld = worldIn;
|
| 22 | this.merchantInventory = new InventoryMerchant(playerInventory.player, merchant);
|
| 23 | this.addSlotToContainer(new Slot(this.merchantInventory, 0, 36, 53));
|
| 24 | this.addSlotToContainer(new Slot(this.merchantInventory, 1, 62, 53));
|
| 25 | this.addSlotToContainer(new SlotMerchantResult(playerInventory.player, merchant, this.merchantInventory, 2, 120, 53));
|
| 26 |
|
| 27 | for (int i = 0; i < 3; ++i)
|
| 28 | {
|
| 29 | for (int j = 0; j < 9; ++j)
|
| 30 | {
|
| 31 | this.addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | for (int k = 0; k < 9; ++k)
|
| 36 | {
|
| 37 | this.addSlotToContainer(new Slot(playerInventory, k, 8 + k * 18, 142));
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | public InventoryMerchant getMerchantInventory()
|
| 42 | {
|
| 43 | return this.merchantInventory;
|
| 44 | }
|
| 45 |
|
| 46 | public void onCraftGuiOpened(ICrafting listener)
|
| 47 | {
|
| 48 | super.onCraftGuiOpened(listener);
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Looks for changes made in the container, sends them to every listener.
|
| 53 | */
|
| 54 | public void detectAndSendChanges()
|
| 55 | {
|
| 56 | super.detectAndSendChanges();
|
| 57 | }
|
| 58 |
|
| 59 | /**
|
| 60 | * Callback for when the crafting matrix is changed.
|
| 61 | */
|
| 62 | public void onCraftMatrixChanged(IInventory inventoryIn)
|
| 63 | {
|
| 64 | this.merchantInventory.resetRecipeAndSlots();
|
| 65 | super.onCraftMatrixChanged(inventoryIn);
|
| 66 | }
|
| 67 |
|
| 68 | public void setCurrentRecipeIndex(int currentRecipeIndex)
|
| 69 | {
|
| 70 | this.merchantInventory.setCurrentRecipeIndex(currentRecipeIndex);
|
| 71 | }
|
| 72 |
|
| 73 | public void updateProgressBar(int id, int data)
|
| 74 | {
|
| 75 | }
|
| 76 |
|
| 77 | public boolean canInteractWith(EntityPlayer playerIn)
|
| 78 | {
|
| 79 | return this.theMerchant.getCustomer() == playerIn;
|
| 80 | }
|
| 81 |
|
| 82 | /**
|
| 83 | * Take a stack from the specified inventory slot.
|
| 84 | */
|
| 85 | public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
|
| 86 | {
|
| 87 | ItemStack itemstack = null;
|
| 88 | Slot slot = (Slot)this.inventorySlots.get(index);
|
| 89 |
|
| 90 | if (slot != null && slot.getHasStack())
|
| 91 | {
|
| 92 | ItemStack itemstack1 = slot.getStack();
|
| 93 | itemstack = itemstack1.copy();
|
| 94 |
|
| 95 | if (index == 2)
|
| 96 | {
|
| 97 | if (!this.mergeItemStack(itemstack1, 3, 39, true))
|
| 98 | {
|
| 99 | return null;
|
| 100 | }
|
| 101 |
|
| 102 | slot.onSlotChange(itemstack1, itemstack);
|
| 103 | }
|
| 104 | else if (index != 0 && index != 1)
|
| 105 | {
|
| 106 | if (index >= 3 && index < 30)
|
| 107 | {
|
| 108 | if (!this.mergeItemStack(itemstack1, 30, 39, false))
|
| 109 | {
|
| 110 | return null;
|
| 111 | }
|
| 112 | }
|
| 113 | else if (index >= 30 && index < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))
|
| 114 | {
|
| 115 | return null;
|
| 116 | }
|
| 117 | }
|
| 118 | else if (!this.mergeItemStack(itemstack1, 3, 39, false))
|
| 119 | {
|
| 120 | return null;
|
| 121 | }
|
| 122 |
|
| 123 | if (itemstack1.stackSize == 0)
|
| 124 | {
|
| 125 | slot.putStack((ItemStack)null);
|
| 126 | }
|
| 127 | else
|
| 128 | {
|
| 129 | slot.onSlotChanged();
|
| 130 | }
|
| 131 |
|
| 132 | if (itemstack1.stackSize == itemstack.stackSize)
|
| 133 | {
|
| 134 | return null;
|
| 135 | }
|
| 136 |
|
| 137 | slot.onPickupFromSlot(playerIn, itemstack1);
|
| 138 | }
|
| 139 |
|
| 140 | return itemstack;
|
| 141 | }
|
| 142 |
|
| 143 | /**
|
| 144 | * Called when the container is closed.
|
| 145 | */
|
| 146 | public void onContainerClosed(EntityPlayer playerIn)
|
| 147 | {
|
| 148 | super.onContainerClosed(playerIn);
|
| 149 | this.theMerchant.setCustomer((EntityPlayer)null);
|
| 150 | super.onContainerClosed(playerIn);
|
| 151 |
|
| 152 | if (!this.theWorld.isRemote)
|
| 153 | {
|
| 154 | ItemStack itemstack = this.merchantInventory.removeStackFromSlot(0);
|
| 155 |
|
| 156 | if (itemstack != null)
|
| 157 | {
|
| 158 | playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
|
| 159 | }
|
| 160 |
|
| 161 | itemstack = this.merchantInventory.removeStackFromSlot(1);
|
| 162 |
|
| 163 | if (itemstack != null)
|
| 164 | {
|
| 165 | playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 |
|