Guest User

Code

a guest
May 8th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. public class MinefusInventoryPlayer implements IInventory
  2.  
  3. {
  4. /** The name your custom inventory will display in the GUI, possibly just "Inventory" */
  5. private String name;
  6.  
  7. /** The key used to store and retrieve the inventory from NBT */
  8. private final String tagName = "CustomInvTag";
  9.  
  10. /** Define the inventory size here for easy reference */
  11. // This is also the place to define which slot is which if you have different types,
  12. // for example SLOT_SHIELD = 0, SLOT_AMULET = 1;
  13. public static final int INV_SIZE = 64;
  14.  
  15. /** Inventory's size must be same as number of slots you add to the Container class */
  16. private ItemStack[] inventory = new ItemStack[INV_SIZE];
  17.  
  18. public MinefusInventoryPlayer()
  19. {
  20. }
  21.  
  22. @Override
  23. public int getSizeInventory()
  24. {
  25. return inventory.length;
  26. }
  27.  
  28. @Override
  29. public ItemStack getStackInSlot(int slot)
  30. {
  31. return inventory[slot];
  32. }
  33.  
  34. @Override
  35. public ItemStack decrStackSize(int slot, int amount)
  36. {
  37. ItemStack stack = getStackInSlot(slot);
  38. if (stack != null)
  39. {
  40. if (stack.stackSize > amount)
  41. {
  42. stack = stack.splitStack(amount);
  43. if (stack.stackSize == 0)
  44. {
  45. setInventorySlotContents(slot, null);
  46. }
  47. }
  48. else
  49. {
  50. setInventorySlotContents(slot, null);
  51. }
  52.  
  53. this.markDirty();;
  54. }
  55. return stack;
  56. }
  57.  
  58. @Override
  59. public ItemStack getStackInSlotOnClosing(int slot)
  60. {
  61. ItemStack stack = getStackInSlot(slot);
  62. if (stack != null)
  63. {
  64. setInventorySlotContents(slot, null);
  65. }
  66. return stack;
  67. }
  68.  
  69. @Override
  70. public void setInventorySlotContents(int slot, ItemStack itemstack)
  71. {
  72. this.inventory[slot] = itemstack;
  73.  
  74. if (itemstack != null && itemstack.stackSize > this.getInventoryStackLimit())
  75. {
  76. itemstack.stackSize = this.getInventoryStackLimit();
  77. }
  78.  
  79. this.markDirty();
  80. }
  81.  
  82. @Override
  83. public String getInventoryName()
  84. {
  85. return this.hasCustomInventoryName() ? this.name : "key.minefusinventory.desc";
  86. }
  87.  
  88. public MinefusInventoryPlayer getMinefusInventory(){
  89.  
  90. return this.getMinefusInventory();
  91. }
  92.  
  93. @Override
  94. public boolean hasCustomInventoryName()
  95. {
  96. return this.name != null && this.name.length() > 0;
  97. }
  98.  
  99. /**
  100. * Our custom slots are similar to armor - only one item per slot
  101. */
  102. @Override
  103. public int getInventoryStackLimit()
  104. {
  105. return 64;
  106. }
  107.  
  108. @Override
  109. public void markDirty()
  110. {
  111. for (int i = 0; i < this.getSizeInventory(); ++i)
  112. {
  113. if (this.getStackInSlot(i) != null && this.getStackInSlot(i).stackSize == 0)
  114. this.setInventorySlotContents(i, null);
  115. }
  116. }
  117.  
  118. @Override
  119. public boolean isUseableByPlayer(EntityPlayer entityplayer)
  120. {
  121. return true;
  122. }
  123.  
  124. /**
  125. * This method doesn't seem to do what it claims to do, as
  126. * items can still be left-clicked and placed in the inventory
  127. * even when this returns false
  128. */
  129. @Override
  130. public boolean isItemValidForSlot(int slot, ItemStack itemstack)
  131. {
  132. return true;
  133. }
  134.  
  135. public void writeToNBT(NBTTagCompound compound)
  136. {
  137. NBTTagList items = new NBTTagList();
  138.  
  139. for (int i = 0; i < getSizeInventory(); ++i)
  140. {
  141. if (getStackInSlot(i) != null)
  142. {
  143. NBTTagCompound item = new NBTTagCompound();
  144. item.setByte("Slot", (byte) i);
  145. getStackInSlot(i).writeToNBT(item);
  146. items.appendTag(item);
  147. }
  148. }
  149.  
  150. // We're storing our items in a custom tag list using our 'tagName' from above
  151. // to prevent potential conflicts
  152. compound.setTag(tagName, items);
  153. }
  154.  
  155. public void readFromNBT(NBTTagCompound compound) {
  156.  
  157. NBTTagList items = compound.getTagList(tagName, compound.getId());
  158. for (int i = 0; i < items.tagCount(); ++i) {
  159.  
  160. NBTTagCompound item = items.getCompoundTagAt(i);
  161. byte slot = item.getByte("Slot");
  162.  
  163. if (slot >= 0 && slot < getSizeInventory()) {
  164. inventory[slot] = ItemStack.loadItemStackFromNBT(item);
  165. }
  166. }
  167. }
  168.  
  169. @Override
  170. public void openInventory() {
  171.  
  172. }
  173.  
  174. @Override
  175. public void closeInventory() {
  176.  
  177. }
  178.  
  179. public void setCustomGuiName(String name)
  180. {
  181. this.name = name;
  182. }
  183.  
  184. public int getFirstEmptyStack(){
  185.  
  186. for (int i = 0; i < this.inventory.length; ++i)
  187. {
  188. if (this.inventory[i] == null)
  189. {
  190. return i;
  191. }
  192. }
  193.  
  194. return -1;
  195. }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment