Advertisement
ButterAleks

tile entity chest

Jan 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. package com.ButterAleks.RandomIdeas.blocks.tileentity;
  2.  
  3. import com.ButterAleks.RandomIdeas.blocks.container.ContainerRainbowChestBlock;
  4. import com.ButterAleks.RandomIdeas.util.Reference;
  5.  
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.entity.player.InventoryPlayer;
  8. import net.minecraft.init.SoundEvents;
  9. import net.minecraft.inventory.Container;
  10. import net.minecraft.inventory.ItemStackHelper;
  11. import net.minecraft.item.ItemStack;
  12. import net.minecraft.nbt.NBTTagCompound;
  13. import net.minecraft.tileentity.TileEntityLockableLoot;
  14. import net.minecraft.util.ITickable;
  15. import net.minecraft.util.NonNullList;
  16. import net.minecraft.util.SoundCategory;
  17. import net.minecraft.util.math.AxisAlignedBB;
  18.  
  19. public class TileEntityRainbowChestBlock extends TileEntityLockableLoot implements ITickable
  20. {
  21. private NonNullList<ItemStack> chestContants = NonNullList.<ItemStack>withSize(72, ItemStack.EMPTY);
  22. public int numPlayerUsing, ticksSinceSync;
  23. public float lidAngle, prevLidAngle;
  24. @Override
  25. public int getSizeInventory()
  26. {
  27. return 72;
  28. }
  29. @Override
  30. public int getInventoryStackLimit()
  31. {
  32. return 64;
  33. }
  34. @Override
  35. public boolean isEmpty()
  36. {
  37. for(ItemStack stack : this.chestContants)
  38. {
  39. if(!stack.isEmpty()) return false;
  40. }
  41. return true;
  42. }
  43.  
  44. @Override
  45. public String getName()
  46. {
  47. return this.hasCustomName() ? this.customName : "container.rainbow_chest_block";
  48. }
  49.  
  50. @Override
  51. public void readFromNBT(NBTTagCompound compound)
  52. {
  53. super.readFromNBT(compound);
  54. this.chestContants = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);
  55. if(this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, chestContants);
  56. if(compound.hasKey("CustomName", 8)) this.customName = compound.getString("CustomeName");
  57. }
  58. @Override
  59. public NBTTagCompound writeToNBT(NBTTagCompound compound)
  60. {
  61. super.writeToNBT(compound);
  62. if(this.checkLootAndWrite(compound)) ItemStackHelper.saveAllItems(compound, chestContants);
  63. if(compound.hasKey("CustomName", 8)) compound.setString("CustomName", this.customName);
  64. return compound;
  65.  
  66. }
  67. @Override
  68. public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn)
  69. {
  70. return new ContainerRainbowChestBlock(playerInventory, this, playerIn);
  71. }
  72. @Override
  73. public String getGuiID()
  74. {
  75. return Reference.MOD_ID + ":rainbow_chest_block";
  76. }
  77. @Override
  78. protected NonNullList<ItemStack> getItems()
  79. {
  80. return this.chestContants;
  81. }
  82. @Override
  83. public void update()
  84. {
  85. if (!this.world.isRemote && this.numPlayerUsing != 0 && (this.ticksSinceSync + pos.getX() + pos.getY() + pos.getZ()) % 200 == 0)
  86. {
  87. this.numPlayerUsing = 0;
  88. float f = 5.0F;
  89.  
  90. for (EntityPlayer entityplayer : this.world.getEntitiesWithinAABB(EntityPlayer.class, new AxisAlignedBB((double)((float)pos.getX() - 5.0F), (double)((float)pos.getY() - 5.0F), (double)((float)pos.getZ() - 5.0F), (double)((float)(pos.getX() + 1) + 5.0F), (double)((float)(pos.getY() + 1) + 5.0F), (double)((float)(pos.getZ() + 1) + 5.0F))))
  91. {
  92. if (entityplayer.openContainer instanceof ContainerRainbowChestBlock)
  93. {
  94. if (((ContainerRainbowChestBlock)entityplayer.openContainer).getChestInventory() == this)
  95. {
  96. ++this.numPlayerUsing;
  97. }
  98. }
  99. }
  100. }
  101.  
  102. this.prevLidAngle = this.lidAngle;
  103. float f1 = 0.1F;
  104.  
  105. if (this.numPlayerUsing > 0 && this.lidAngle == 0.0F)
  106. {
  107. double d1 = (double)pos.getX() + 0.5D;
  108. double d2 = (double)pos.getZ() + 0.5D;
  109. this.world.playSound((EntityPlayer)null, d1, (double)pos.getY() + 0.5D, d2, SoundEvents.BLOCK_IRON_TRAPDOOR_OPEN, SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
  110. }
  111.  
  112. if (this.numPlayerUsing == 0 && this.lidAngle > 0.0F || this.numPlayerUsing > 0 && this.lidAngle < 1.0F)
  113. {
  114. float f2 = this.lidAngle;
  115.  
  116. if (this.numPlayerUsing > 0)
  117. {
  118. this.lidAngle += 0.1F;
  119. }
  120. else
  121. {
  122. this.lidAngle -= 0.1F;
  123. }
  124.  
  125. if (this.lidAngle > 1.0F)
  126. {
  127. this.lidAngle = 1.0F;
  128. }
  129.  
  130. float f3 = 0.5F;
  131.  
  132. if (this.lidAngle < 0.5F && f2 >= 0.5F)
  133. {
  134. double d3 = (double)pos.getX() + 0.5D;
  135. double d0 = (double)pos.getZ() + 0.5D;
  136. this.world.playSound((EntityPlayer)null, d3, (double)pos.getY() + 0.5D, d0, SoundEvents.BLOCK_IRON_TRAPDOOR_CLOSE, SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
  137. }
  138.  
  139. if (this.lidAngle < 0.0F)
  140. {
  141. this.lidAngle = 0.0F;
  142. }
  143. }
  144. }
  145. @Override
  146. public void openInventory(EntityPlayer player)
  147. {
  148. ++this.numPlayerUsing;
  149. this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayerUsing);
  150. this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
  151. }
  152. @Override
  153. public void closeInventory(EntityPlayer player)
  154. {
  155. --this.numPlayerUsing;
  156. this.world.addBlockEvent(pos, this.getBlockType(), 1, this.numPlayerUsing);
  157. this.world.notifyNeighborsOfStateChange(pos, this.getBlockType(), false);
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement