Advertisement
Guest User

Untitled

a guest
Nov 11th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. package thecraft.mod.common;
  2.  
  3. import ibxm.Player;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.inventory.IInventory;
  6. import net.minecraft.item.ItemStack;
  7. import net.minecraft.nbt.NBTTagCompound;
  8. import net.minecraft.nbt.NBTTagList;
  9. import net.minecraft.tileentity.TileEntity;
  10. import net.minecraft.world.World;
  11. import net.minecraftforge.common.util.Constants;
  12.  
  13. public class TileEntityEwiliteChest extends TileEntity implements IInventory
  14. {
  15. private ItemStack[] contents = new ItemStack[27];
  16. private String customName;
  17. @Override
  18. public void readFromNBT(NBTTagCompound compound)
  19. {
  20. super.readFromNBT(compound);
  21. if(compound.hasKey("CustomName", Constants.NBT.TAG_STRING))
  22. {
  23. this.customName = compound.getString("CustomName"); // on le lit
  24. }
  25.  
  26. NBTTagList nbttaglist = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
  27. this.contents = new ItemStack[this.getSizeInventory()];
  28. for(int i = 0; i < nbttaglist.tagCount(); ++i)
  29. {
  30. NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
  31. int j = nbttagcompound1.getByte("Slot") & 255;
  32.  
  33. if(j >= 0 && j < this.contents.length)
  34. {
  35. this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
  36. }
  37. }
  38. }
  39.  
  40. @Override
  41. public void writeToNBT(NBTTagCompound compound)
  42. {
  43. super.writeToNBT(compound);
  44. if(this.hasCustomInventoryName())
  45. {
  46. compound.setString("CustomName", this.customName);
  47. }
  48.  
  49. NBTTagList nbttaglist = new NBTTagList();
  50. for(int i = 0; i < this.contents.length; ++i)
  51. {
  52. if(this.contents[ i] != null)
  53. {
  54. NBTTagCompound nbttagcompound1 = new NBTTagCompound();
  55. nbttagcompound1.setByte("Slot", (byte)i);
  56. this.contents[ i].writeToNBT(nbttagcompound1);
  57. nbttaglist.appendTag(nbttagcompound1);
  58. }
  59. }
  60. compound.setTag("Items", nbttaglist); //
  61. }
  62. @Override
  63. public int getSizeInventory()
  64. {
  65. return this.contents.length;
  66. }
  67.  
  68. @Override
  69. public ItemStack getStackInSlot(int slotIndex)
  70. {
  71. return this.contents[slotIndex];
  72. }
  73.  
  74. @Override
  75. public ItemStack decrStackSize(int slotIndex, int amount)
  76. {
  77. if(this.contents[slotIndex] != null)
  78. {
  79. ItemStack itemstack;
  80.  
  81. if(this.contents[slotIndex].stackSize <= amount)
  82. {
  83. itemstack = this.contents[slotIndex];
  84. this.contents[slotIndex] = null;
  85. this.markDirty();
  86. return itemstack;
  87. }
  88. else
  89. {
  90. itemstack = this.contents[slotIndex].splitStack(amount);
  91.  
  92. if(this.contents[slotIndex].stackSize == 0)
  93. {
  94. this.contents[slotIndex] = null;
  95. }
  96. this.markDirty();
  97. return itemstack;
  98. }
  99. }
  100. else
  101. {
  102. return null;
  103. }
  104. }
  105.  
  106. @Override
  107. public ItemStack getStackInSlotOnClosing(int slotIndex)
  108. {
  109. if(this.contents[slotIndex] != null)
  110. {
  111. ItemStack itemstack = this.contents[slotIndex];
  112. this.contents[slotIndex] = null;
  113. return itemstack;
  114. }
  115. else
  116. {
  117. return null;
  118. }
  119. }
  120.  
  121. @Override
  122. public void setInventorySlotContents(int slotIndex, ItemStack stack)
  123. {
  124. this.contents[slotIndex] = stack;
  125.  
  126. if(stack != null && stack.stackSize > this.getInventoryStackLimit())
  127. {
  128. stack.stackSize = this.getInventoryStackLimit();
  129. }
  130.  
  131. this.markDirty();
  132. }
  133.  
  134. @Override
  135. public String getInventoryName()
  136. {
  137. return this.hasCustomInventoryName() ? this.customName : "tile.cupboard";
  138. }
  139.  
  140. @Override
  141. public boolean hasCustomInventoryName()
  142. {
  143. return false;
  144. }
  145.  
  146. @Override
  147. public int getInventoryStackLimit()
  148. {
  149. return 64;
  150. }
  151.  
  152. @Override
  153. public boolean isUseableByPlayer(EntityPlayer player)
  154. {
  155. return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
  156. }
  157.  
  158. @Override
  159. public void openInventory()
  160. {
  161.  
  162. }
  163.  
  164. @Override
  165. public void closeInventory()
  166. {
  167.  
  168. }
  169.  
  170. @Override
  171. public boolean isItemValidForSlot(int slotIndex, ItemStack stack)
  172. {
  173. return true;
  174. }
  175.  
  176. public void setCustomName(String displayName, String customName)
  177. {
  178. this.customName = customName;
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement