Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. package com.mod.drakania.inventory;
  2.  
  3. import com.mod.drakania.References;
  4. import com.mod.drakania.items.ItemBackPack;
  5.  
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.inventory.IInventory;
  8. import net.minecraft.item.ItemStack;
  9. import net.minecraft.nbt.NBTTagCompound;
  10. import net.minecraft.nbt.NBTTagList;
  11. import net.minecraftforge.common.util.Constants;
  12.  
  13. /**
  14. * @author SCAREX
  15. *
  16. */
  17. public class InventoryBackPack implements IInventory
  18. {
  19. public ItemStack[] content;
  20. public int size;
  21.  
  22. public InventoryBackPack(ItemStack container, int size) {
  23. this.size = size;
  24. this.content = new ItemStack[size];
  25. if (!container.hasTagCompound()) container.setTagCompound(new NBTTagCompound());
  26. this.readFromNBT(container.getTagCompound());
  27. }
  28.  
  29. /**
  30. * This methods reads the content of the NBTTagCompound inside the container
  31. *
  32. * @param comp
  33. * the container NBTTagCompound
  34. */
  35. public void readFromNBT(NBTTagCompound comp) {
  36. NBTTagList nbtlist = comp.getTagList("Inventory", Constants.NBT.TAG_COMPOUND);
  37. for (int i = 0; i < nbtlist.tagCount(); i++) {
  38. NBTTagCompound comp1 = nbtlist.getCompoundTagAt(i);
  39. int slot = comp1.getInteger("Slot");
  40. this.content[slot] = ItemStack.loadItemStackFromNBT(comp1);
  41. }
  42. }
  43.  
  44. /**
  45. * This methods saves the content inside the container
  46. *
  47. * @param comp
  48. * the NBTTagCompound to write in
  49. */
  50. public void writeToNBT(NBTTagCompound comp) {
  51. NBTTagList nbtlist = new NBTTagList();
  52.  
  53. for (int i = 0; i < this.size; i++) {
  54. if (this.content[i] != null) {
  55. NBTTagCompound comp1 = new NBTTagCompound();
  56. comp1.setInteger("Slot", i);
  57. this.content[i].writeToNBT(comp1);
  58. nbtlist.appendTag(comp1);
  59. }
  60. }
  61. comp.setTag("Inventory", nbtlist);
  62. }
  63.  
  64. @Override
  65. public int getSizeInventory() {
  66. return this.size;
  67. }
  68.  
  69. @Override
  70. public ItemStack getStackInSlot(int index) {
  71. return this.content[index];
  72. }
  73.  
  74. @Override
  75. public ItemStack decrStackSize(int index, int amount) {
  76. ItemStack stack = getStackInSlot(index);
  77. if (stack != null) {
  78. if (stack.stackSize > amount) {
  79. stack = stack.splitStack(amount);
  80. if (stack.stackSize == 0) this.content[index] = null;
  81. } else {
  82. this.content[index] = null;
  83. }
  84. }
  85. return stack;
  86. }
  87.  
  88. @Override
  89. public ItemStack getStackInSlotOnClosing(int index) {
  90. ItemStack stack = getStackInSlot(index);
  91. if (stack != null) this.content[index] = null;
  92. return stack;
  93. }
  94.  
  95. @Override
  96. public void setInventorySlotContents(int index, ItemStack stack) {
  97. this.content[index] = stack;
  98. }
  99.  
  100. @Override
  101. public String getInventoryName() {
  102. return References.MOD_NAME;
  103. }
  104.  
  105. @Override
  106. public boolean hasCustomInventoryName() {
  107. return false;
  108. }
  109.  
  110. @Override
  111. public int getInventoryStackLimit() {
  112. return 64;
  113. }
  114.  
  115. @Override
  116. public void markDirty() {}
  117.  
  118. @Override
  119. public boolean isUseableByPlayer(EntityPlayer player) {
  120. return true;
  121. }
  122.  
  123. @Override
  124. public void openInventory() {}
  125.  
  126. @Override
  127. public void closeInventory() {}
  128.  
  129. /**
  130. * Prevents backpack-ception
  131. */
  132. @Override
  133. public boolean isItemValidForSlot(int index, ItemStack stack) {
  134. return !(stack.getItem() instanceof ItemBackPack);
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement