| 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.item.ItemStack;
|
| 7 | import net.minecraft.item.crafting.CraftingManager;
|
| 8 | import net.minecraft.util.BlockPos;
|
| 9 | import net.minecraft.world.World;
|
| 10 |
|
| 11 | public class ContainerWorkbench extends Container
|
| 12 | {
|
| 13 | /** The crafting matrix inventory (3x3). */
|
| 14 | public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
|
| 15 | public IInventory craftResult = new InventoryCraftResult();
|
| 16 | private World worldObj;
|
| 17 |
|
| 18 | /** Position of the workbench */
|
| 19 | private BlockPos pos;
|
| 20 |
|
| 21 | public ContainerWorkbench(InventoryPlayer playerInventory, World worldIn, BlockPos posIn)
|
| 22 | {
|
| 23 | this.worldObj = worldIn;
|
| 24 | this.pos = posIn;
|
| 25 | this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));
|
| 26 |
|
| 27 | for (int i = 0; i < 3; ++i)
|
| 28 | {
|
| 29 | for (int j = 0; j < 3; ++j)
|
| 30 | {
|
| 31 | this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 30 + j * 18, 17 + i * 18));
|
| 32 | }
|
| 33 | }
|
| 34 |
|
| 35 | for (int k = 0; k < 3; ++k)
|
| 36 | {
|
| 37 | for (int i1 = 0; i1 < 9; ++i1)
|
| 38 | {
|
| 39 | this.addSlotToContainer(new Slot(playerInventory, i1 + k * 9 + 9, 8 + i1 * 18, 84 + k * 18));
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | for (int l = 0; l < 9; ++l)
|
| 44 | {
|
| 45 | this.addSlotToContainer(new Slot(playerInventory, l, 8 + l * 18, 142));
|
| 46 | }
|
| 47 |
|
| 48 | this.onCraftMatrixChanged(this.craftMatrix);
|
| 49 | }
|
| 50 |
|
| 51 | /**
|
| 52 | * Callback for when the crafting matrix is changed.
|
| 53 | */
|
| 54 | public void onCraftMatrixChanged(IInventory inventoryIn)
|
| 55 | {
|
| 56 | this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
|
| 57 | }
|
| 58 |
|
| 59 | /**
|
| 60 | * Called when the container is closed.
|
| 61 | */
|
| 62 | public void onContainerClosed(EntityPlayer playerIn)
|
| 63 | {
|
| 64 | super.onContainerClosed(playerIn);
|
| 65 |
|
| 66 | if (!this.worldObj.isRemote)
|
| 67 | {
|
| 68 | for (int i = 0; i < 9; ++i)
|
| 69 | {
|
| 70 | ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);
|
| 71 |
|
| 72 | if (itemstack != null)
|
| 73 | {
|
| 74 | playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | public boolean canInteractWith(EntityPlayer playerIn)
|
| 81 | {
|
| 82 | return this.worldObj.getBlockState(this.pos).getBlock() != Blocks.crafting_table ? false : playerIn.getDistanceSq((double)this.pos.getX() + 0.5D, (double)this.pos.getY() + 0.5D, (double)this.pos.getZ() + 0.5D) <= 64.0D;
|
| 83 | }
|
| 84 |
|
| 85 | /**
|
| 86 | * Take a stack from the specified inventory slot.
|
| 87 | */
|
| 88 | public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
|
| 89 | {
|
| 90 | ItemStack itemstack = null;
|
| 91 | Slot slot = (Slot)this.inventorySlots.get(index);
|
| 92 |
|
| 93 | if (slot != null && slot.getHasStack())
|
| 94 | {
|
| 95 | ItemStack itemstack1 = slot.getStack();
|
| 96 | itemstack = itemstack1.copy();
|
| 97 |
|
| 98 | if (index == 0)
|
| 99 | {
|
| 100 | if (!this.mergeItemStack(itemstack1, 10, 46, true))
|
| 101 | {
|
| 102 | return null;
|
| 103 | }
|
| 104 |
|
| 105 | slot.onSlotChange(itemstack1, itemstack);
|
| 106 | }
|
| 107 | else if (index >= 10 && index < 37)
|
| 108 | {
|
| 109 | if (!this.mergeItemStack(itemstack1, 37, 46, false))
|
| 110 | {
|
| 111 | return null;
|
| 112 | }
|
| 113 | }
|
| 114 | else if (index >= 37 && index < 46)
|
| 115 | {
|
| 116 | if (!this.mergeItemStack(itemstack1, 10, 37, false))
|
| 117 | {
|
| 118 | return null;
|
| 119 | }
|
| 120 | }
|
| 121 | else if (!this.mergeItemStack(itemstack1, 10, 46, false))
|
| 122 | {
|
| 123 | return null;
|
| 124 | }
|
| 125 |
|
| 126 | if (itemstack1.stackSize == 0)
|
| 127 | {
|
| 128 | slot.putStack((ItemStack)null);
|
| 129 | }
|
| 130 | else
|
| 131 | {
|
| 132 | slot.onSlotChanged();
|
| 133 | }
|
| 134 |
|
| 135 | if (itemstack1.stackSize == itemstack.stackSize)
|
| 136 | {
|
| 137 | return null;
|
| 138 | }
|
| 139 |
|
| 140 | slot.onPickupFromSlot(playerIn, itemstack1);
|
| 141 | }
|
| 142 |
|
| 143 | return itemstack;
|
| 144 | }
|
| 145 |
|
| 146 | /**
|
| 147 | * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
|
| 148 | * is null for the initial slot that was double-clicked.
|
| 149 | */
|
| 150 | public boolean canMergeSlot(ItemStack stack, Slot slotIn)
|
| 151 | {
|
| 152 | return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
|
| 153 | }
|
| 154 | }
|
| 155 |
|