001package net.minecraft.inventory; 002 003import net.minecraft.block.Block; 004import net.minecraft.entity.player.EntityPlayer; 005import net.minecraft.entity.player.InventoryPlayer; 006import net.minecraft.item.ItemStack; 007import net.minecraft.item.crafting.CraftingManager; 008import net.minecraft.world.World; 009 010public class ContainerWorkbench extends Container 011{ 012 /** The crafting matrix inventory (3x3). */ 013 public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3); 014 public IInventory craftResult = new InventoryCraftResult(); 015 private World worldObj; 016 private int posX; 017 private int posY; 018 private int posZ; 019 020 public ContainerWorkbench(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5) 021 { 022 this.worldObj = par2World; 023 this.posX = par3; 024 this.posY = par4; 025 this.posZ = par5; 026 this.addSlotToContainer(new SlotCrafting(par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 124, 35)); 027 int l; 028 int i1; 029 030 for (l = 0; l < 3; ++l) 031 { 032 for (i1 = 0; i1 < 3; ++i1) 033 { 034 this.addSlotToContainer(new Slot(this.craftMatrix, i1 + l * 3, 30 + i1 * 18, 17 + l * 18)); 035 } 036 } 037 038 for (l = 0; l < 3; ++l) 039 { 040 for (i1 = 0; i1 < 9; ++i1) 041 { 042 this.addSlotToContainer(new Slot(par1InventoryPlayer, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18)); 043 } 044 } 045 046 for (l = 0; l < 9; ++l) 047 { 048 this.addSlotToContainer(new Slot(par1InventoryPlayer, l, 8 + l * 18, 142)); 049 } 050 051 this.onCraftMatrixChanged(this.craftMatrix); 052 } 053 054 /** 055 * Callback for when the crafting matrix is changed. 056 */ 057 public void onCraftMatrixChanged(IInventory par1IInventory) 058 { 059 this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj)); 060 } 061 062 /** 063 * Callback for when the crafting gui is closed. 064 */ 065 public void onCraftGuiClosed(EntityPlayer par1EntityPlayer) 066 { 067 super.onCraftGuiClosed(par1EntityPlayer); 068 069 if (!this.worldObj.isRemote) 070 { 071 for (int i = 0; i < 9; ++i) 072 { 073 ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i); 074 075 if (itemstack != null) 076 { 077 par1EntityPlayer.dropPlayerItem(itemstack); 078 } 079 } 080 } 081 } 082 083 public boolean canInteractWith(EntityPlayer par1EntityPlayer) 084 { 085 return this.worldObj.getBlockId(this.posX, this.posY, this.posZ) != Block.workbench.blockID ? false : par1EntityPlayer.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D; 086 } 087 088 /** 089 * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that. 090 */ 091 public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) 092 { 093 ItemStack itemstack = null; 094 Slot slot = (Slot)this.inventorySlots.get(par2); 095 096 if (slot != null && slot.getHasStack()) 097 { 098 ItemStack itemstack1 = slot.getStack(); 099 itemstack = itemstack1.copy(); 100 101 if (par2 == 0) 102 { 103 if (!this.mergeItemStack(itemstack1, 10, 46, true)) 104 { 105 return null; 106 } 107 108 slot.onSlotChange(itemstack1, itemstack); 109 } 110 else if (par2 >= 10 && par2 < 37) 111 { 112 if (!this.mergeItemStack(itemstack1, 37, 46, false)) 113 { 114 return null; 115 } 116 } 117 else if (par2 >= 37 && par2 < 46) 118 { 119 if (!this.mergeItemStack(itemstack1, 10, 37, false)) 120 { 121 return null; 122 } 123 } 124 else if (!this.mergeItemStack(itemstack1, 10, 46, false)) 125 { 126 return null; 127 } 128 129 if (itemstack1.stackSize == 0) 130 { 131 slot.putStack((ItemStack)null); 132 } 133 else 134 { 135 slot.onSlotChanged(); 136 } 137 138 if (itemstack1.stackSize == itemstack.stackSize) 139 { 140 return null; 141 } 142 143 slot.onPickupFromSlot(par1EntityPlayer, itemstack1); 144 } 145 146 return itemstack; 147 } 148 149 public boolean func_94530_a(ItemStack par1ItemStack, Slot par2Slot) 150 { 151 return par2Slot.inventory != this.craftResult && super.func_94530_a(par1ItemStack, par2Slot); 152 } 153}