001 package net.minecraft.inventory;
002
003 import net.minecraft.block.Block;
004 import net.minecraft.entity.player.EntityPlayer;
005 import net.minecraft.entity.player.InventoryPlayer;
006 import net.minecraft.item.ItemStack;
007 import net.minecraft.item.crafting.CraftingManager;
008 import net.minecraft.world.World;
009
010 public 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 var6;
028 int var7;
029
030 for (var6 = 0; var6 < 3; ++var6)
031 {
032 for (var7 = 0; var7 < 3; ++var7)
033 {
034 this.addSlotToContainer(new Slot(this.craftMatrix, var7 + var6 * 3, 30 + var7 * 18, 17 + var6 * 18));
035 }
036 }
037
038 for (var6 = 0; var6 < 3; ++var6)
039 {
040 for (var7 = 0; var7 < 9; ++var7)
041 {
042 this.addSlotToContainer(new Slot(par1InventoryPlayer, var7 + var6 * 9 + 9, 8 + var7 * 18, 84 + var6 * 18));
043 }
044 }
045
046 for (var6 = 0; var6 < 9; ++var6)
047 {
048 this.addSlotToContainer(new Slot(par1InventoryPlayer, var6, 8 + var6 * 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 var2 = 0; var2 < 9; ++var2)
072 {
073 ItemStack var3 = this.craftMatrix.getStackInSlotOnClosing(var2);
074
075 if (var3 != null)
076 {
077 par1EntityPlayer.dropPlayerItem(var3);
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 var3 = null;
094 Slot var4 = (Slot)this.inventorySlots.get(par2);
095
096 if (var4 != null && var4.getHasStack())
097 {
098 ItemStack var5 = var4.getStack();
099 var3 = var5.copy();
100
101 if (par2 == 0)
102 {
103 if (!this.mergeItemStack(var5, 10, 46, true))
104 {
105 return null;
106 }
107
108 var4.onSlotChange(var5, var3);
109 }
110 else if (par2 >= 10 && par2 < 37)
111 {
112 if (!this.mergeItemStack(var5, 37, 46, false))
113 {
114 return null;
115 }
116 }
117 else if (par2 >= 37 && par2 < 46)
118 {
119 if (!this.mergeItemStack(var5, 10, 37, false))
120 {
121 return null;
122 }
123 }
124 else if (!this.mergeItemStack(var5, 10, 46, false))
125 {
126 return null;
127 }
128
129 if (var5.stackSize == 0)
130 {
131 var4.putStack((ItemStack)null);
132 }
133 else
134 {
135 var4.onSlotChanged();
136 }
137
138 if (var5.stackSize == var3.stackSize)
139 {
140 return null;
141 }
142
143 var4.onPickupFromSlot(par1EntityPlayer, var5);
144 }
145
146 return var3;
147 }
148 }