001 package net.minecraft.src;
002
003 import cpw.mods.fml.common.Side;
004 import cpw.mods.fml.common.asm.SideOnly;
005 import java.util.Iterator;
006 import java.util.List;
007 import java.util.Random;
008
009 public class ContainerEnchantment extends Container
010 {
011 /** SlotEnchantmentTable object with ItemStack to be enchanted */
012 public IInventory tableInventory = new SlotEnchantmentTable(this, "Enchant", 1);
013
014 /** current world (for bookshelf counting) */
015 private World worldPointer;
016 private int posX;
017 private int posY;
018 private int posZ;
019 private Random rand = new Random();
020
021 /** used as seed for EnchantmentNameParts (see GuiEnchantment) */
022 public long nameSeed;
023
024 /** 3-member array storing the enchantment levels of each slot */
025 public int[] enchantLevels = new int[3];
026
027 public ContainerEnchantment(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5)
028 {
029 this.worldPointer = par2World;
030 this.posX = par3;
031 this.posY = par4;
032 this.posZ = par5;
033 this.addSlotToContainer(new SlotEnchantment(this, this.tableInventory, 0, 25, 47));
034 int var6;
035
036 for (var6 = 0; var6 < 3; ++var6)
037 {
038 for (int var7 = 0; var7 < 9; ++var7)
039 {
040 this.addSlotToContainer(new Slot(par1InventoryPlayer, var7 + var6 * 9 + 9, 8 + var7 * 18, 84 + var6 * 18));
041 }
042 }
043
044 for (var6 = 0; var6 < 9; ++var6)
045 {
046 this.addSlotToContainer(new Slot(par1InventoryPlayer, var6, 8 + var6 * 18, 142));
047 }
048 }
049
050 public void addCraftingToCrafters(ICrafting par1ICrafting)
051 {
052 super.addCraftingToCrafters(par1ICrafting);
053 par1ICrafting.updateCraftingInventoryInfo(this, 0, this.enchantLevels[0]);
054 par1ICrafting.updateCraftingInventoryInfo(this, 1, this.enchantLevels[1]);
055 par1ICrafting.updateCraftingInventoryInfo(this, 2, this.enchantLevels[2]);
056 }
057
058 /**
059 * Updates crafting matrix; called from onCraftMatrixChanged. Args: none
060 */
061 public void updateCraftingResults()
062 {
063 super.updateCraftingResults();
064 Iterator var1 = this.crafters.iterator();
065
066 while (var1.hasNext())
067 {
068 ICrafting var2 = (ICrafting)var1.next();
069 var2.updateCraftingInventoryInfo(this, 0, this.enchantLevels[0]);
070 var2.updateCraftingInventoryInfo(this, 1, this.enchantLevels[1]);
071 var2.updateCraftingInventoryInfo(this, 2, this.enchantLevels[2]);
072 }
073 }
074
075 @SideOnly(Side.CLIENT)
076 public void updateProgressBar(int par1, int par2)
077 {
078 if (par1 >= 0 && par1 <= 2)
079 {
080 this.enchantLevels[par1] = par2;
081 }
082 else
083 {
084 super.updateProgressBar(par1, par2);
085 }
086 }
087
088 /**
089 * Callback for when the crafting matrix is changed.
090 */
091 public void onCraftMatrixChanged(IInventory par1IInventory)
092 {
093 if (par1IInventory == this.tableInventory)
094 {
095 ItemStack var2 = par1IInventory.getStackInSlot(0);
096 int var3;
097
098 if (var2 != null && var2.isItemEnchantable())
099 {
100 this.nameSeed = this.rand.nextLong();
101
102 if (!this.worldPointer.isRemote)
103 {
104 var3 = 0;
105 int var4;
106
107 for (var4 = -1; var4 <= 1; ++var4)
108 {
109 for (int var5 = -1; var5 <= 1; ++var5)
110 {
111 if ((var4 != 0 || var5 != 0) && this.worldPointer.isAirBlock(this.posX + var5, this.posY, this.posZ + var4) && this.worldPointer.isAirBlock(this.posX + var5, this.posY + 1, this.posZ + var4))
112 {
113 if (this.worldPointer.getBlockId(this.posX + var5 * 2, this.posY, this.posZ + var4 * 2) == Block.bookShelf.blockID)
114 {
115 ++var3;
116 }
117
118 if (this.worldPointer.getBlockId(this.posX + var5 * 2, this.posY + 1, this.posZ + var4 * 2) == Block.bookShelf.blockID)
119 {
120 ++var3;
121 }
122
123 if (var5 != 0 && var4 != 0)
124 {
125 if (this.worldPointer.getBlockId(this.posX + var5 * 2, this.posY, this.posZ + var4) == Block.bookShelf.blockID)
126 {
127 ++var3;
128 }
129
130 if (this.worldPointer.getBlockId(this.posX + var5 * 2, this.posY + 1, this.posZ + var4) == Block.bookShelf.blockID)
131 {
132 ++var3;
133 }
134
135 if (this.worldPointer.getBlockId(this.posX + var5, this.posY, this.posZ + var4 * 2) == Block.bookShelf.blockID)
136 {
137 ++var3;
138 }
139
140 if (this.worldPointer.getBlockId(this.posX + var5, this.posY + 1, this.posZ + var4 * 2) == Block.bookShelf.blockID)
141 {
142 ++var3;
143 }
144 }
145 }
146 }
147 }
148
149 for (var4 = 0; var4 < 3; ++var4)
150 {
151 this.enchantLevels[var4] = EnchantmentHelper.calcItemStackEnchantability(this.rand, var4, var3, var2);
152 }
153
154 this.updateCraftingResults();
155 }
156 }
157 else
158 {
159 for (var3 = 0; var3 < 3; ++var3)
160 {
161 this.enchantLevels[var3] = 0;
162 }
163 }
164 }
165 }
166
167 /**
168 * enchants the item on the table using the specified slot; also deducts XP from player
169 */
170 public boolean enchantItem(EntityPlayer par1EntityPlayer, int par2)
171 {
172 ItemStack var3 = this.tableInventory.getStackInSlot(0);
173
174 if (this.enchantLevels[par2] > 0 && var3 != null && (par1EntityPlayer.experienceLevel >= this.enchantLevels[par2] || par1EntityPlayer.capabilities.isCreativeMode))
175 {
176 if (!this.worldPointer.isRemote)
177 {
178 List var4 = EnchantmentHelper.buildEnchantmentList(this.rand, var3, this.enchantLevels[par2]);
179
180 if (var4 != null)
181 {
182 par1EntityPlayer.addExperienceLevel(-this.enchantLevels[par2]);
183 Iterator var5 = var4.iterator();
184
185 while (var5.hasNext())
186 {
187 EnchantmentData var6 = (EnchantmentData)var5.next();
188 var3.addEnchantment(var6.enchantmentobj, var6.enchantmentLevel);
189 }
190
191 this.onCraftMatrixChanged(this.tableInventory);
192 }
193 }
194
195 return true;
196 }
197 else
198 {
199 return false;
200 }
201 }
202
203 /**
204 * Callback for when the crafting gui is closed.
205 */
206 public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
207 {
208 super.onCraftGuiClosed(par1EntityPlayer);
209
210 if (!this.worldPointer.isRemote)
211 {
212 ItemStack var2 = this.tableInventory.getStackInSlotOnClosing(0);
213
214 if (var2 != null)
215 {
216 par1EntityPlayer.dropPlayerItem(var2);
217 }
218 }
219 }
220
221 public boolean canInteractWith(EntityPlayer par1EntityPlayer)
222 {
223 return this.worldPointer.getBlockId(this.posX, this.posY, this.posZ) != Block.enchantmentTable.blockID ? false : par1EntityPlayer.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D;
224 }
225
226 /**
227 * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
228 */
229 public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
230 {
231 ItemStack var3 = null;
232 Slot var4 = (Slot)this.inventorySlots.get(par2);
233
234 if (var4 != null && var4.getHasStack())
235 {
236 ItemStack var5 = var4.getStack();
237 var3 = var5.copy();
238
239 if (par2 == 0)
240 {
241 if (!this.mergeItemStack(var5, 1, 37, true))
242 {
243 return null;
244 }
245 }
246 else
247 {
248 if (((Slot)this.inventorySlots.get(0)).getHasStack() || !((Slot)this.inventorySlots.get(0)).isItemValid(var5))
249 {
250 return null;
251 }
252
253 if (var5.hasTagCompound() && var5.stackSize == 1)
254 {
255 ((Slot)this.inventorySlots.get(0)).putStack(var5.copy());
256 var5.stackSize = 0;
257 }
258 else if (var5.stackSize >= 1)
259 {
260 ((Slot)this.inventorySlots.get(0)).putStack(new ItemStack(var5.itemID, 1, var5.getItemDamage()));
261 --var5.stackSize;
262 }
263 }
264
265 if (var5.stackSize == 0)
266 {
267 var4.putStack((ItemStack)null);
268 }
269 else
270 {
271 var4.onSlotChanged();
272 }
273
274 if (var5.stackSize == var3.stackSize)
275 {
276 return null;
277 }
278
279 var4.onPickupFromSlot(par1EntityPlayer, var5);
280 }
281
282 return var3;
283 }
284 }