PageRenderTime 61ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Client/src/net/minecraft/src/Container.java

http://phantom-labs-mc.googlecode.com/
Java | 423 lines | 324 code | 64 blank | 35 comment | 111 complexity | 6a4ecbab2b6d28b7f87cdf3a60ca9e3c MD5 | raw file
  1. package net.minecraft.src;
  2. import java.util.*;
  3. public abstract class Container
  4. {
  5. /** the list of all items(stacks) for the corresponding slot */
  6. public List inventoryItemStacks;
  7. /** the list of all slots in the inventory */
  8. public List inventorySlots;
  9. public int windowId;
  10. private short transactionID;
  11. /**
  12. * list of all people that need to be notified when this craftinventory changes
  13. */
  14. protected List crafters;
  15. private Set field_20918_b;
  16. public Container()
  17. {
  18. inventoryItemStacks = new ArrayList();
  19. inventorySlots = new ArrayList();
  20. windowId = 0;
  21. transactionID = 0;
  22. crafters = new ArrayList();
  23. field_20918_b = new HashSet();
  24. }
  25. /**
  26. * adds the slot to the inventory it is in
  27. */
  28. protected void addSlot(Slot par1Slot)
  29. {
  30. par1Slot.slotNumber = inventorySlots.size();
  31. inventorySlots.add(par1Slot);
  32. inventoryItemStacks.add(null);
  33. }
  34. /**
  35. * Updates crafting matrix; called from onCraftMatrixChanged. Args: none
  36. */
  37. public void updateCraftingResults()
  38. {
  39. for (int i = 0; i < inventorySlots.size(); i++)
  40. {
  41. ItemStack itemstack = ((Slot)inventorySlots.get(i)).getStack();
  42. ItemStack itemstack1 = (ItemStack)inventoryItemStacks.get(i);
  43. if (ItemStack.areItemStacksEqual(itemstack1, itemstack))
  44. {
  45. continue;
  46. }
  47. itemstack1 = itemstack != null ? itemstack.copy() : null;
  48. inventoryItemStacks.set(i, itemstack1);
  49. for (int j = 0; j < crafters.size(); j++)
  50. {
  51. ((ICrafting)crafters.get(j)).updateCraftingInventorySlot(this, i, itemstack1);
  52. }
  53. }
  54. }
  55. /**
  56. * enchants the item on the table using the specified slot; also deducts XP from player
  57. */
  58. public boolean enchantItem(EntityPlayer par1EntityPlayer, int par2)
  59. {
  60. return false;
  61. }
  62. public Slot getSlot(int par1)
  63. {
  64. return (Slot)inventorySlots.get(par1);
  65. }
  66. /**
  67. * Called to transfer a stack from one inventory to the other eg. when shift clicking.
  68. */
  69. public ItemStack transferStackInSlot(int par1)
  70. {
  71. Slot slot = (Slot)inventorySlots.get(par1);
  72. if (slot != null)
  73. {
  74. return slot.getStack();
  75. }
  76. else
  77. {
  78. return null;
  79. }
  80. }
  81. public ItemStack slotClick(int par1, int par2, boolean par3, EntityPlayer par4EntityPlayer)
  82. {
  83. ItemStack itemstack = null;
  84. if (par2 > 1)
  85. {
  86. return null;
  87. }
  88. if (par2 == 0 || par2 == 1)
  89. {
  90. InventoryPlayer inventoryplayer = par4EntityPlayer.inventory;
  91. if (par1 == -999)
  92. {
  93. if (inventoryplayer.getItemStack() != null && par1 == -999)
  94. {
  95. if (par2 == 0)
  96. {
  97. par4EntityPlayer.dropPlayerItem(inventoryplayer.getItemStack());
  98. inventoryplayer.setItemStack(null);
  99. }
  100. if (par2 == 1)
  101. {
  102. par4EntityPlayer.dropPlayerItem(inventoryplayer.getItemStack().splitStack(1));
  103. if (inventoryplayer.getItemStack().stackSize == 0)
  104. {
  105. inventoryplayer.setItemStack(null);
  106. }
  107. }
  108. }
  109. }
  110. else if (par3)
  111. {
  112. ItemStack itemstack1 = transferStackInSlot(par1);
  113. if (itemstack1 != null)
  114. {
  115. int i = itemstack1.itemID;
  116. itemstack = itemstack1.copy();
  117. Slot slot1 = (Slot)inventorySlots.get(par1);
  118. if (slot1 != null && slot1.getStack() != null && slot1.getStack().itemID == i)
  119. {
  120. retrySlotClick(par1, par2, par3, par4EntityPlayer);
  121. }
  122. }
  123. }
  124. else
  125. {
  126. if (par1 < 0)
  127. {
  128. return null;
  129. }
  130. Slot slot = (Slot)inventorySlots.get(par1);
  131. if (slot != null)
  132. {
  133. slot.onSlotChanged();
  134. ItemStack itemstack2 = slot.getStack();
  135. ItemStack itemstack4 = inventoryplayer.getItemStack();
  136. if (itemstack2 != null)
  137. {
  138. itemstack = itemstack2.copy();
  139. }
  140. if (itemstack2 == null)
  141. {
  142. if (itemstack4 != null && slot.isItemValid(itemstack4))
  143. {
  144. int j = par2 != 0 ? 1 : itemstack4.stackSize;
  145. if (j > slot.getSlotStackLimit())
  146. {
  147. j = slot.getSlotStackLimit();
  148. }
  149. slot.putStack(itemstack4.splitStack(j));
  150. if (itemstack4.stackSize == 0)
  151. {
  152. inventoryplayer.setItemStack(null);
  153. }
  154. }
  155. }
  156. else if (itemstack4 == null)
  157. {
  158. int k = par2 != 0 ? (itemstack2.stackSize + 1) / 2 : itemstack2.stackSize;
  159. ItemStack itemstack6 = slot.decrStackSize(k);
  160. inventoryplayer.setItemStack(itemstack6);
  161. if (itemstack2.stackSize == 0)
  162. {
  163. slot.putStack(null);
  164. }
  165. slot.onPickupFromSlot(inventoryplayer.getItemStack());
  166. }
  167. else if (slot.isItemValid(itemstack4))
  168. {
  169. if (itemstack2.itemID != itemstack4.itemID || itemstack2.getHasSubtypes() && itemstack2.getItemDamage() != itemstack4.getItemDamage() || !ItemStack.func_46154_a(itemstack2, itemstack4))
  170. {
  171. if (itemstack4.stackSize <= slot.getSlotStackLimit())
  172. {
  173. ItemStack itemstack5 = itemstack2;
  174. slot.putStack(itemstack4);
  175. inventoryplayer.setItemStack(itemstack5);
  176. }
  177. }
  178. else
  179. {
  180. int l = par2 != 0 ? 1 : itemstack4.stackSize;
  181. if (l > slot.getSlotStackLimit() - itemstack2.stackSize)
  182. {
  183. l = slot.getSlotStackLimit() - itemstack2.stackSize;
  184. }
  185. if (l > itemstack4.getMaxStackSize() - itemstack2.stackSize)
  186. {
  187. l = itemstack4.getMaxStackSize() - itemstack2.stackSize;
  188. }
  189. itemstack4.splitStack(l);
  190. if (itemstack4.stackSize == 0)
  191. {
  192. inventoryplayer.setItemStack(null);
  193. }
  194. itemstack2.stackSize += l;
  195. }
  196. }
  197. else if (itemstack2.itemID == itemstack4.itemID && itemstack4.getMaxStackSize() > 1 && (!itemstack2.getHasSubtypes() || itemstack2.getItemDamage() == itemstack4.getItemDamage()) && ItemStack.func_46154_a(itemstack2, itemstack4))
  198. {
  199. int i1 = itemstack2.stackSize;
  200. if (i1 > 0 && i1 + itemstack4.stackSize <= itemstack4.getMaxStackSize())
  201. {
  202. itemstack4.stackSize += i1;
  203. ItemStack itemstack3 = slot.decrStackSize(i1);
  204. if (itemstack3.stackSize == 0)
  205. {
  206. slot.putStack(null);
  207. }
  208. slot.onPickupFromSlot(inventoryplayer.getItemStack());
  209. }
  210. }
  211. }
  212. }
  213. }
  214. return itemstack;
  215. }
  216. protected void retrySlotClick(int par1, int par2, boolean par3, EntityPlayer par4EntityPlayer)
  217. {
  218. slotClick(par1, par2, par3, par4EntityPlayer);
  219. }
  220. /**
  221. * Callback for when the crafting gui is closed.
  222. */
  223. public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
  224. {
  225. InventoryPlayer inventoryplayer = par1EntityPlayer.inventory;
  226. if (inventoryplayer.getItemStack() != null)
  227. {
  228. par1EntityPlayer.dropPlayerItem(inventoryplayer.getItemStack());
  229. inventoryplayer.setItemStack(null);
  230. }
  231. }
  232. /**
  233. * Callback for when the crafting matrix is changed.
  234. */
  235. public void onCraftMatrixChanged(IInventory par1IInventory)
  236. {
  237. updateCraftingResults();
  238. }
  239. /**
  240. * args: slotID, itemStack to put in slot
  241. */
  242. public void putStackInSlot(int par1, ItemStack par2ItemStack)
  243. {
  244. getSlot(par1).putStack(par2ItemStack);
  245. }
  246. /**
  247. * places itemstacks in first x slots, x being aitemstack.lenght
  248. */
  249. public void putStacksInSlots(ItemStack par1ArrayOfItemStack[])
  250. {
  251. for (int i = 0; i < par1ArrayOfItemStack.length; i++)
  252. {
  253. getSlot(i).putStack(par1ArrayOfItemStack[i]);
  254. }
  255. }
  256. public void updateProgressBar(int i, int j)
  257. {
  258. }
  259. /**
  260. * Gets a unique transaction ID. Parameter is unused.
  261. */
  262. public short getNextTransactionID(InventoryPlayer par1InventoryPlayer)
  263. {
  264. transactionID++;
  265. return transactionID;
  266. }
  267. public void func_20113_a(short word0)
  268. {
  269. }
  270. public void func_20110_b(short word0)
  271. {
  272. }
  273. public abstract boolean canInteractWith(EntityPlayer entityplayer);
  274. /**
  275. * merges provided ItemStack with the first avaliable one in the container/player inventory
  276. */
  277. protected boolean mergeItemStack(ItemStack par1ItemStack, int par2, int par3, boolean par4)
  278. {
  279. boolean flag = false;
  280. int i = par2;
  281. if (par4)
  282. {
  283. i = par3 - 1;
  284. }
  285. if (par1ItemStack.isStackable())
  286. {
  287. while (par1ItemStack.stackSize > 0 && (!par4 && i < par3 || par4 && i >= par2))
  288. {
  289. Slot slot = (Slot)inventorySlots.get(i);
  290. ItemStack itemstack = slot.getStack();
  291. if (itemstack != null && itemstack.itemID == par1ItemStack.itemID && (!par1ItemStack.getHasSubtypes() || par1ItemStack.getItemDamage() == itemstack.getItemDamage()) && ItemStack.func_46154_a(par1ItemStack, itemstack))
  292. {
  293. int k = itemstack.stackSize + par1ItemStack.stackSize;
  294. if (k <= par1ItemStack.getMaxStackSize())
  295. {
  296. par1ItemStack.stackSize = 0;
  297. itemstack.stackSize = k;
  298. slot.onSlotChanged();
  299. flag = true;
  300. }
  301. else if (itemstack.stackSize < par1ItemStack.getMaxStackSize())
  302. {
  303. par1ItemStack.stackSize -= par1ItemStack.getMaxStackSize() - itemstack.stackSize;
  304. itemstack.stackSize = par1ItemStack.getMaxStackSize();
  305. slot.onSlotChanged();
  306. flag = true;
  307. }
  308. }
  309. if (par4)
  310. {
  311. i--;
  312. }
  313. else
  314. {
  315. i++;
  316. }
  317. }
  318. }
  319. if (par1ItemStack.stackSize > 0)
  320. {
  321. int j;
  322. if (par4)
  323. {
  324. j = par3 - 1;
  325. }
  326. else
  327. {
  328. j = par2;
  329. }
  330. do
  331. {
  332. if ((par4 || j >= par3) && (!par4 || j < par2))
  333. {
  334. break;
  335. }
  336. Slot slot1 = (Slot)inventorySlots.get(j);
  337. ItemStack itemstack1 = slot1.getStack();
  338. if (itemstack1 == null)
  339. {
  340. slot1.putStack(par1ItemStack.copy());
  341. slot1.onSlotChanged();
  342. par1ItemStack.stackSize = 0;
  343. flag = true;
  344. break;
  345. }
  346. if (par4)
  347. {
  348. j--;
  349. }
  350. else
  351. {
  352. j++;
  353. }
  354. }
  355. while (true);
  356. }
  357. return flag;
  358. }
  359. }