Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. /**
  2. * Container that includes the players inventory.
  3. *
  4. * @author Nividica
  5. *
  6. */
  7. public abstract class ContainerWithPlayerInventory
  8. extends TheContainerBase
  9. {
  10. /**
  11. * The number of rows in the player inventory
  12. */
  13. private static int ROWS = 3;
  14.  
  15. /**
  16. * The number of columns in the player inventory
  17. */
  18. private static int COLUMNS = 9;
  19.  
  20. /**
  21. * The width and height of the slots
  22. */
  23. protected static final int SLOT_SIZE = 18;
  24.  
  25. /**
  26. * X position offset for inventory slots
  27. */
  28. private static final int INVENTORY_X_OFFSET = 8;
  29.  
  30. /**
  31. * Array of player hotbar slots
  32. */
  33. private final Slot[] hotbarSlots = new Slot[ContainerWithPlayerInventory.COLUMNS];
  34.  
  35. /**
  36. * Array of player inventory slots.
  37. */
  38. private final Slot[] playerSlots = new Slot[ContainerWithPlayerInventory.COLUMNS * ContainerWithPlayerInventory.ROWS];
  39.  
  40. public ContainerWithPlayerInventory( final EntityPlayer player )
  41. {
  42. super( player );
  43. }
  44.  
  45. /**
  46. * Attempt to merge the specified slot stack with the hotbar inventory
  47. *
  48. * @param slotStack
  49. * @return
  50. */
  51. protected final boolean mergeSlotWithHotbarInventory( final ItemStack slotStack )
  52. {
  53. return this.mergeItemStack( slotStack, this.hotbarSlots[0].slotNumber,
  54. this.hotbarSlots[ContainerWithPlayerInventory.COLUMNS - 1].slotNumber + 1, false );
  55. }
  56.  
  57. /**
  58. * Attempt to merge the specified slot stack with the player inventory
  59. *
  60. * @param slotStack
  61. * @return
  62. */
  63. protected final boolean mergeSlotWithPlayerInventory( final ItemStack slotStack )
  64. {
  65. return this.mergeItemStack( slotStack, this.playerSlots[0].slotNumber,
  66. this.playerSlots[( ContainerWithPlayerInventory.COLUMNS * ContainerWithPlayerInventory.ROWS ) - 1].slotNumber + 1, false );
  67. }
  68.  
  69. /**
  70. * Checks if the slot clicked was in the hotbar inventory
  71. *
  72. * @param slotNumber
  73. * @return True if it was in the hotbar inventory, false otherwise.
  74. */
  75. protected final boolean slotClickedWasInHotbarInventory( final int slotNumber )
  76. {
  77. return ( slotNumber >= this.hotbarSlots[0].slotNumber ) &&
  78. ( slotNumber <= this.hotbarSlots[ContainerWithPlayerInventory.COLUMNS - 1].slotNumber );
  79. }
  80.  
  81. /**
  82. * Checks if the slot clicked was in the player inventory
  83. *
  84. * @param slotNumber
  85. * @return True if it was in the player inventory, false otherwise.
  86. */
  87. protected final boolean slotClickedWasInPlayerInventory( final int slotNumber )
  88. {
  89. return ( slotNumber >= this.playerSlots[0].slotNumber ) &&
  90. ( slotNumber <= this.playerSlots[( ContainerWithPlayerInventory.COLUMNS * ContainerWithPlayerInventory.ROWS ) -
  91. 1].slotNumber );
  92. }
  93.  
  94. /**
  95. * Attempt to move the item from hotbar <-> player inventory
  96. *
  97. * @param slotNumber
  98. * @return
  99. */
  100. protected final boolean swapSlotInventoryHotbar( final int slotNumber, final ItemStack slotStack )
  101. {
  102. if( this.slotClickedWasInHotbarInventory( slotNumber ) )
  103. {
  104. return this.mergeSlotWithPlayerInventory( slotStack );
  105. }
  106. else if( this.slotClickedWasInPlayerInventory( slotNumber ) )
  107. {
  108. return this.mergeSlotWithHotbarInventory( slotStack );
  109. }
  110.  
  111. return false;
  112. }
  113.  
  114. /**
  115. * Searches the players inventory & hotbar for the specified stack.
  116. *
  117. * @param searchStack
  118. * @param amount
  119. * @return
  120. */
  121. @Nullable
  122. protected final ItemStack takeItemFromPlayer( final ItemStack searchStack, final int amount )
  123. {
  124. // Sanity check
  125. if( ( searchStack == null ) || ( amount <= 0 ) )
  126. {
  127. return null;
  128. }
  129.  
  130. Slot matchingSlot = null;
  131.  
  132. // Search inventory
  133. for( Slot slot : this.playerSlots )
  134. {
  135. // Empty?
  136. if( !slot.getHasStack() )
  137. {
  138. // Skip
  139. continue;
  140. }
  141.  
  142. // Check for match
  143. if( ThEUtils.areStacksEqualIgnoreAmount( searchStack, slot.getStack() ) )
  144. {
  145. // Found match
  146. matchingSlot = slot;
  147. break;
  148. }
  149. }
  150.  
  151. if( matchingSlot == null )
  152. {
  153. // Search hotbar
  154. for( Slot slot : this.hotbarSlots )
  155. {
  156. // Empty?
  157. if( !slot.getHasStack() )
  158. {
  159. // Skip
  160. continue;
  161. }
  162.  
  163. // Check for match
  164. if( ThEUtils.areStacksEqualIgnoreAmount( searchStack, slot.getStack() ) )
  165. {
  166. // Found match
  167. matchingSlot = slot;
  168. break;
  169. }
  170. }
  171. }
  172.  
  173. if( matchingSlot == null )
  174. {
  175. // No matches
  176. return null;
  177. }
  178.  
  179. // Check amount
  180. ItemStack matchStack = matchingSlot.getStack();
  181. if( matchStack.stackSize < amount )
  182. {
  183. // Not enough to take
  184. return null;
  185. }
  186.  
  187. // Split
  188. ItemStack result = matchStack.splitStack( amount );
  189.  
  190. // Anything left in the stack?
  191. if( matchStack.stackSize == 0 )
  192. {
  193. // Null the slot
  194. matchingSlot.putStack( null );
  195. }
  196. else
  197. {
  198. // Update the slot
  199. matchingSlot.putStack( matchStack );
  200. }
  201.  
  202. return result;
  203. }
  204.  
  205. /**
  206. * Binds the player inventory to this container.
  207. *
  208. * @param playerInventory
  209. * Inventory to bind
  210. * @param indexOffset
  211. * The Y position offset for the slots
  212. * @param hotbarPositionY
  213. * The Y position offset for hotbar slots
  214. */
  215. public final void bindPlayerInventory( final IInventory playerInventory, final int inventoryOffsetY, final int hotbarPositionY )
  216. {
  217. // Hot-bar ID's 0-8
  218. for( int column = 0; column < ContainerWithPlayerInventory.COLUMNS; column++ )
  219. {
  220. // Create the slot
  221. this.hotbarSlots[column] = new Slot( playerInventory, column,
  222. ContainerWithPlayerInventory.INVENTORY_X_OFFSET + ( column * ContainerWithPlayerInventory.SLOT_SIZE ), hotbarPositionY );
  223.  
  224. // Add the slot
  225. this.addSlotToContainer( this.hotbarSlots[column] );
  226. }
  227.  
  228. // Main inventory ID's 9-36
  229. for( int row = 0; row < ContainerWithPlayerInventory.ROWS; row++ )
  230. {
  231. for( int column = 0; column < ContainerWithPlayerInventory.COLUMNS; column++ )
  232. {
  233. // Calculate index
  234. int index = column + ( row * ContainerWithPlayerInventory.COLUMNS );
  235.  
  236. // Create the slot
  237. this.playerSlots[index] = new Slot( playerInventory, ContainerWithPlayerInventory.COLUMNS + index,
  238. ContainerWithPlayerInventory.INVENTORY_X_OFFSET + ( column * ContainerWithPlayerInventory.SLOT_SIZE ),
  239. ( row * ContainerWithPlayerInventory.SLOT_SIZE ) + inventoryOffsetY );
  240.  
  241. // Add the slot
  242. this.addSlotToContainer( this.playerSlots[index] );
  243. }
  244. }
  245. }
  246.  
  247. /**
  248. * Gets all non-empty slot from the hotbar inventory.
  249. *
  250. * @return
  251. */
  252. public final ArrayList<Slot> getNonEmptySlotsFromHotbar()
  253. {
  254. ArrayList<Slot> list = new ArrayList<Slot>();
  255.  
  256. for( Slot slot : this.hotbarSlots )
  257. {
  258. if( slot.getHasStack() )
  259. {
  260. list.add( slot );
  261. }
  262. }
  263.  
  264. return list;
  265. }
  266.  
  267. /**
  268. * Gets all non-empty slot from the player inventory.
  269. *
  270. * @return
  271. */
  272. public final ArrayList<Slot> getNonEmptySlotsFromPlayerInventory()
  273. {
  274. ArrayList<Slot> list = new ArrayList<Slot>();
  275.  
  276. for( Slot slot : this.playerSlots )
  277. {
  278. if( slot.getHasStack() )
  279. {
  280. list.add( slot );
  281. }
  282. }
  283.  
  284. return list;
  285. }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement