Advertisement
Guest User

Extended_Jamiga

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