| 1 | package net.minecraft.inventory;
|
| 2 |
|
| 3 | import net.minecraft.entity.player.EntityPlayer;
|
| 4 | import net.minecraft.entity.player.InventoryPlayer;
|
| 5 | import net.minecraft.init.Blocks;
|
| 6 | import net.minecraft.init.Items;
|
| 7 | import net.minecraft.item.Item;
|
| 8 | import net.minecraft.item.ItemArmor;
|
| 9 | import net.minecraft.item.ItemStack;
|
| 10 | import net.minecraft.item.crafting.CraftingManager;
|
| 11 |
|
| 12 | public class ContainerPlayer extends Container
|
| 13 | {
|
| 14 | /** The crafting matrix inventory. */
|
| 15 | public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
|
| 16 | public IInventory craftResult = new InventoryCraftResult();
|
| 17 |
|
| 18 | /** Determines if inventory manipulation should be handled. */
|
| 19 | public boolean isLocalWorld;
|
| 20 | private final EntityPlayer thePlayer;
|
| 21 |
|
| 22 | public ContainerPlayer(final InventoryPlayer playerInventory, boolean localWorld, EntityPlayer player)
|
| 23 | {
|
| 24 | this.isLocalWorld = localWorld;
|
| 25 | this.thePlayer = player;
|
| 26 | this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 144, 36));
|
| 27 |
|
| 28 | for (int i = 0; i < 2; ++i)
|
| 29 | {
|
| 30 | for (int j = 0; j < 2; ++j)
|
| 31 | {
|
| 32 | this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 88 + j * 18, 26 + i * 18));
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | for (int k = 0; k < 4; ++k)
|
| 37 | {
|
| 38 | final int k_f = k;
|
| 39 | this.addSlotToContainer(new Slot(playerInventory, playerInventory.getSizeInventory() - 1 - k, 8, 8 + k * 18)
|
| 40 | {
|
| 41 | public int getSlotStackLimit()
|
| 42 | {
|
| 43 | return 1;
|
| 44 | }
|
| 45 | public boolean isItemValid(ItemStack stack)
|
| 46 | {
|
| 47 | return stack == null ? false : (stack.getItem() instanceof ItemArmor ? ((ItemArmor)stack.getItem()).armorType == k_f : (stack.getItem() != Item.getItemFromBlock(Blocks.pumpkin) && stack.getItem() != Items.skull ? false : k_f == 0));
|
| 48 | }
|
| 49 | public String getSlotTexture()
|
| 50 | {
|
| 51 | return ItemArmor.EMPTY_SLOT_NAMES[k_f];
|
| 52 | }
|
| 53 | });
|
| 54 | }
|
| 55 |
|
| 56 | for (int l = 0; l < 3; ++l)
|
| 57 | {
|
| 58 | for (int j1 = 0; j1 < 9; ++j1)
|
| 59 | {
|
| 60 | this.addSlotToContainer(new Slot(playerInventory, j1 + (l + 1) * 9, 8 + j1 * 18, 84 + l * 18));
|
| 61 | }
|
| 62 | }
|
| 63 |
|
| 64 | for (int i1 = 0; i1 < 9; ++i1)
|
| 65 | {
|
| 66 | this.addSlotToContainer(new Slot(playerInventory, i1, 8 + i1 * 18, 142));
|
| 67 | }
|
| 68 |
|
| 69 | this.onCraftMatrixChanged(this.craftMatrix);
|
| 70 | }
|
| 71 |
|
| 72 | /**
|
| 73 | * Callback for when the crafting matrix is changed.
|
| 74 | */
|
| 75 | public void onCraftMatrixChanged(IInventory inventoryIn)
|
| 76 | {
|
| 77 | this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.thePlayer.worldObj));
|
| 78 | }
|
| 79 |
|
| 80 | /**
|
| 81 | * Called when the container is closed.
|
| 82 | */
|
| 83 | public void onContainerClosed(EntityPlayer playerIn)
|
| 84 | {
|
| 85 | super.onContainerClosed(playerIn);
|
| 86 |
|
| 87 | for (int i = 0; i < 4; ++i)
|
| 88 | {
|
| 89 | ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);
|
| 90 |
|
| 91 | if (itemstack != null)
|
| 92 | {
|
| 93 | playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | this.craftResult.setInventorySlotContents(0, (ItemStack)null);
|
| 98 | }
|
| 99 |
|
| 100 | public boolean canInteractWith(EntityPlayer playerIn)
|
| 101 | {
|
| 102 | return true;
|
| 103 | }
|
| 104 |
|
| 105 | /**
|
| 106 | * Take a stack from the specified inventory slot.
|
| 107 | */
|
| 108 | public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
|
| 109 | {
|
| 110 | ItemStack itemstack = null;
|
| 111 | Slot slot = (Slot)this.inventorySlots.get(index);
|
| 112 |
|
| 113 | if (slot != null && slot.getHasStack())
|
| 114 | {
|
| 115 | ItemStack itemstack1 = slot.getStack();
|
| 116 | itemstack = itemstack1.copy();
|
| 117 |
|
| 118 | if (index == 0)
|
| 119 | {
|
| 120 | if (!this.mergeItemStack(itemstack1, 9, 45, true))
|
| 121 | {
|
| 122 | return null;
|
| 123 | }
|
| 124 |
|
| 125 | slot.onSlotChange(itemstack1, itemstack);
|
| 126 | }
|
| 127 | else if (index >= 1 && index < 5)
|
| 128 | {
|
| 129 | if (!this.mergeItemStack(itemstack1, 9, 45, false))
|
| 130 | {
|
| 131 | return null;
|
| 132 | }
|
| 133 | }
|
| 134 | else if (index >= 5 && index < 9)
|
| 135 | {
|
| 136 | if (!this.mergeItemStack(itemstack1, 9, 45, false))
|
| 137 | {
|
| 138 | return null;
|
| 139 | }
|
| 140 | }
|
| 141 | else if (itemstack.getItem() instanceof ItemArmor && !((Slot)this.inventorySlots.get(5 + ((ItemArmor)itemstack.getItem()).armorType)).getHasStack())
|
| 142 | {
|
| 143 | int i = 5 + ((ItemArmor)itemstack.getItem()).armorType;
|
| 144 |
|
| 145 | if (!this.mergeItemStack(itemstack1, i, i + 1, false))
|
| 146 | {
|
| 147 | return null;
|
| 148 | }
|
| 149 | }
|
| 150 | else if (index >= 9 && index < 36)
|
| 151 | {
|
| 152 | if (!this.mergeItemStack(itemstack1, 36, 45, false))
|
| 153 | {
|
| 154 | return null;
|
| 155 | }
|
| 156 | }
|
| 157 | else if (index >= 36 && index < 45)
|
| 158 | {
|
| 159 | if (!this.mergeItemStack(itemstack1, 9, 36, false))
|
| 160 | {
|
| 161 | return null;
|
| 162 | }
|
| 163 | }
|
| 164 | else if (!this.mergeItemStack(itemstack1, 9, 45, false))
|
| 165 | {
|
| 166 | return null;
|
| 167 | }
|
| 168 |
|
| 169 | if (itemstack1.stackSize == 0)
|
| 170 | {
|
| 171 | slot.putStack((ItemStack)null);
|
| 172 | }
|
| 173 | else
|
| 174 | {
|
| 175 | slot.onSlotChanged();
|
| 176 | }
|
| 177 |
|
| 178 | if (itemstack1.stackSize == itemstack.stackSize)
|
| 179 | {
|
| 180 | return null;
|
| 181 | }
|
| 182 |
|
| 183 | slot.onPickupFromSlot(playerIn, itemstack1);
|
| 184 | }
|
| 185 |
|
| 186 | return itemstack;
|
| 187 | }
|
| 188 |
|
| 189 | /**
|
| 190 | * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
|
| 191 | * is null for the initial slot that was double-clicked.
|
| 192 | */
|
| 193 | public boolean canMergeSlot(ItemStack stack, Slot slotIn)
|
| 194 | {
|
| 195 | return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
|
| 196 | }
|
| 197 | }
|
| 198 |
|