TheOnlyTails

RubyBarrelTileEntity

Sep 27th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. package com.theonlytails.ruby.tileentity;
  2.  
  3. import com.theonlytails.ruby.blocks.RubyBarrelBlock;
  4. import com.theonlytails.ruby.container.RubyBarrelContainer;
  5. import com.theonlytails.ruby.init.TileEntitiesRegistry;
  6. import net.minecraft.block.BarrelBlock;
  7. import net.minecraft.block.Block;
  8. import net.minecraft.block.BlockState;
  9. import net.minecraft.entity.player.PlayerEntity;
  10. import net.minecraft.entity.player.PlayerInventory;
  11. import net.minecraft.inventory.ItemStackHelper;
  12. import net.minecraft.inventory.container.Container;
  13. import net.minecraft.item.ItemStack;
  14. import net.minecraft.nbt.CompoundNBT;
  15. import net.minecraft.tileentity.LockableLootTileEntity;
  16. import net.minecraft.tileentity.TileEntity;
  17. import net.minecraft.tileentity.TileEntityType;
  18. import net.minecraft.util.*;
  19. import net.minecraft.util.math.BlockPos;
  20. import net.minecraft.util.text.ITextComponent;
  21. import net.minecraft.util.text.TranslationTextComponent;
  22. import net.minecraft.world.IBlockReader;
  23. import net.minecraftforge.common.capabilities.Capability;
  24. import net.minecraftforge.common.util.LazyOptional;
  25. import net.minecraftforge.items.CapabilityItemHandler;
  26. import net.minecraftforge.items.IItemHandlerModifiable;
  27. import net.minecraftforge.items.wrapper.InvWrapper;
  28. import org.jetbrains.annotations.NotNull;
  29. import org.jetbrains.annotations.Nullable;
  30.  
  31. public class RubyBarrelTileEntity extends LockableLootTileEntity {
  32. private final IItemHandlerModifiable items = createHandler();
  33. protected int numPlayersUsing;
  34. private NonNullList<ItemStack> chestContents = NonNullList.withSize(45, ItemStack.EMPTY);
  35. private LazyOptional<IItemHandlerModifiable> itemHandler = LazyOptional.of(() -> items);
  36.  
  37. public RubyBarrelTileEntity(TileEntityType<?> typeIn) {
  38. super(typeIn);
  39. }
  40.  
  41. public RubyBarrelTileEntity() {
  42. this(TileEntitiesRegistry.RUBY_BARREL.get());
  43. }
  44.  
  45. public static int getPlayersUsing(IBlockReader reader, BlockPos pos) {
  46. BlockState blockState = reader.getBlockState(pos);
  47.  
  48. if (blockState.hasTileEntity()) {
  49. TileEntity tileEntity = reader.getTileEntity(pos);
  50.  
  51. if (tileEntity instanceof RubyBarrelTileEntity) {
  52. return ((RubyBarrelTileEntity) tileEntity).numPlayersUsing;
  53. }
  54. }
  55.  
  56. return 0;
  57. }
  58.  
  59. public static void swapContents(RubyBarrelTileEntity te, RubyBarrelTileEntity otherTe) {
  60. NonNullList<ItemStack> list = te.getItems();
  61. te.setItems(otherTe.getItems());
  62. otherTe.setItems(list);
  63. }
  64.  
  65. @Override
  66. @NotNull
  67. public NonNullList<ItemStack> getItems() {
  68. return this.chestContents;
  69. }
  70.  
  71. @Override
  72. protected void setItems(@NotNull NonNullList<ItemStack> itemsIn) {
  73. chestContents = itemsIn;
  74. }
  75.  
  76. @Override
  77. protected @NotNull ITextComponent getDefaultName() {
  78. return new TranslationTextComponent("container.ruby_barrel");
  79. }
  80.  
  81. @Override
  82. protected @NotNull Container createMenu(int id, @NotNull PlayerInventory player) {
  83. return new RubyBarrelContainer(id, player, this);
  84. }
  85.  
  86. @Override
  87. public int getSizeInventory() {
  88. return 45;
  89. }
  90.  
  91. @Override
  92. public void read(@NotNull BlockState state, @NotNull CompoundNBT nbt) {
  93. super.read(state, nbt);
  94. this.chestContents = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
  95. if (!this.checkLootAndRead(nbt)) {
  96. ItemStackHelper.loadAllItems(nbt, this.chestContents);
  97. }
  98. }
  99.  
  100. private void playSound(SoundEvent sound) {
  101. double dx = (double) this.pos.getX() + 0.5D;
  102. double dy = (double) this.pos.getY() + 0.5D;
  103. double dz = (double) this.pos.getZ() + 0.5D;
  104. if (this.world != null) {
  105. this.world.playSound(null,
  106. dx, dy, dz, sound,
  107. SoundCategory.BLOCKS, 0.5f,
  108. this.world.rand.nextFloat() * 0.1f + 0.9f);
  109. }
  110. }
  111.  
  112. @Override
  113. public boolean receiveClientEvent(int id, int type) {
  114. if (id == 1) {
  115. this.numPlayersUsing = type;
  116. return true;
  117. } else {
  118. return super.receiveClientEvent(id, type);
  119. }
  120. }
  121.  
  122. @Override
  123. public void openInventory(@NotNull PlayerEntity player) {
  124. if (!player.isSpectator()) {
  125. if (this.numPlayersUsing < 0) {
  126. this.numPlayersUsing = 0;
  127. }
  128.  
  129. ++this.numPlayersUsing;
  130.  
  131. BlockState blockState = this.getBlockState();
  132. boolean flag = blockState.get(RubyBarrelBlock.OPEN_PROPERTY);
  133.  
  134. if (!flag) {
  135. this.playSound(SoundEvents.BLOCK_BARREL_OPEN);
  136. this.setOpenProperty(blockState, true);
  137. }
  138.  
  139. this.onOpenOrClose();
  140. }
  141. }
  142.  
  143. protected void onOpenOrClose() {
  144. Block block = this.getBlockState().getBlock();
  145.  
  146. if (world != null) {
  147. if (block instanceof RubyBarrelBlock) {
  148. this.world.addBlockEvent(this.pos, block, 1, this.numPlayersUsing);
  149. this.world.notifyNeighborsOfStateChange(this.pos, block);
  150. }
  151. }
  152. }
  153.  
  154. @Override
  155. public void updateContainingBlockInfo() {
  156. super.updateContainingBlockInfo();
  157. if (this.itemHandler != null) {
  158. this.itemHandler.invalidate();
  159. this.itemHandler = null;
  160. }
  161. }
  162.  
  163. @Nullable
  164. @Override
  165. public <T> LazyOptional<T> getCapability(@NotNull Capability<T> cap, @Nullable Direction side) {
  166. if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  167. return itemHandler.cast();
  168. }
  169.  
  170. return super.getCapability(cap, side);
  171. }
  172.  
  173. private IItemHandlerModifiable createHandler() {
  174. return new InvWrapper(this);
  175. }
  176.  
  177. @Override
  178. public void remove() {
  179. super.remove();
  180.  
  181. if (itemHandler != null) {
  182. this.itemHandler.invalidate();
  183. }
  184. }
  185.  
  186. private void setOpenProperty(BlockState state, boolean open) {
  187. assert this.world != null;
  188. this.world.setBlockState(this.getPos(), state.with(BarrelBlock.PROPERTY_OPEN, open), 3);
  189. }
  190. }
  191.  
Advertisement
Add Comment
Please, Sign In to add comment