TheOnlyTails

RubyBarrelTileEntity

Sep 29th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. package com.theonlytails.ruby.tileentity;
  2.  
  3. import com.theonlytails.ruby.blocks.RubyBarrelBlock;
  4. import com.theonlytails.ruby.init.TileEntitiesRegistry;
  5. import net.minecraft.block.BarrelBlock;
  6. import net.minecraft.block.BlockState;
  7. import net.minecraft.entity.player.PlayerEntity;
  8. import net.minecraft.entity.player.PlayerInventory;
  9. import net.minecraft.inventory.ItemStackHelper;
  10. import net.minecraft.inventory.container.ChestContainer;
  11. import net.minecraft.inventory.container.Container;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.nbt.CompoundNBT;
  14. import net.minecraft.tileentity.ChestTileEntity;
  15. import net.minecraft.tileentity.LockableLootTileEntity;
  16. import net.minecraft.tileentity.TileEntityType;
  17. import net.minecraft.util.NonNullList;
  18. import net.minecraft.util.SoundCategory;
  19. import net.minecraft.util.SoundEvent;
  20. import net.minecraft.util.SoundEvents;
  21. import net.minecraft.util.text.ITextComponent;
  22. import net.minecraft.util.text.TranslationTextComponent;
  23. import org.jetbrains.annotations.NotNull;
  24.  
  25. import static com.theonlytails.ruby.blocks.RubyBarrelBlock.*;
  26.  
  27. public class RubyBarrelTileEntity extends LockableLootTileEntity {
  28.     protected int numPlayersUsing;
  29.     private NonNullList<ItemStack> contents = NonNullList.withSize(45, ItemStack.EMPTY);
  30.  
  31.     public RubyBarrelTileEntity(TileEntityType<?> typeIn) {
  32.         super(typeIn);
  33.     }
  34.  
  35.     public RubyBarrelTileEntity() {
  36.         this(TileEntitiesRegistry.RUBY_BARREL.get());
  37.     }
  38.  
  39.     @Override
  40.     public @NotNull CompoundNBT write(@NotNull CompoundNBT nbt) {
  41.         super.write(nbt);
  42.  
  43.         if (!checkLootAndWrite(nbt)) {
  44.             ItemStackHelper.saveAllItems(nbt, getItems());
  45.         }
  46.  
  47.         return nbt;
  48.     }
  49.  
  50.     @Override
  51.     protected @NotNull ITextComponent getDefaultName() {
  52.         return new TranslationTextComponent(this.getBlockState().getBlock().getTranslationKey());
  53.     }
  54.  
  55.     @Override
  56.     protected @NotNull Container createMenu(int id, @NotNull PlayerInventory player) {
  57.         return ChestContainer.createGeneric9X5(id, player);
  58.     }
  59.  
  60.     @Override
  61.     @NotNull
  62.     public NonNullList<ItemStack> getItems() {
  63.         return this.contents;
  64.     }
  65.  
  66.     @Override
  67.     protected void setItems(@NotNull NonNullList<ItemStack> itemsIn) {
  68.         this.contents = itemsIn;
  69.     }
  70.  
  71.     @Override
  72.     public int getSizeInventory() {
  73.         return 45;
  74.     }
  75.  
  76.     @Override
  77.     public void openInventory(@NotNull PlayerEntity player) {
  78.         if (!player.isSpectator()) {
  79.             if (numPlayersUsing < 0) {
  80.                 numPlayersUsing = 0;
  81.             }
  82.         }
  83.  
  84.         numPlayersUsing++;
  85.         BlockState blockState = this.getBlockState();
  86.  
  87.         if (!blockState.get(OPEN)) {
  88.             this.playSound(SoundEvents.BLOCK_BARREL_OPEN);
  89.             this.setOpenProperty(blockState, true);
  90.         }
  91.  
  92.         this.scheduleTick();
  93.     }
  94.  
  95.     private void scheduleTick() {
  96.         assert this.world != null;
  97.         this.world.getPendingBlockTicks().scheduleTick(this.pos, this.getBlockState().getBlock(), 5);
  98.     }
  99.  
  100.     public void barrelTick() {
  101.         int x = pos.getX();
  102.         int y = pos.getY();
  103.         int z = pos.getZ();
  104.  
  105.         assert world != null;
  106.         numPlayersUsing = ChestTileEntity.calculatePlayersUsing(world, this, x, y, z);
  107.  
  108.         if (numPlayersUsing > 0) {
  109.             scheduleTick();
  110.         } else {
  111.             BlockState blockstate = getBlockState();
  112.             if (!(blockstate.getBlock() instanceof RubyBarrelBlock)) {
  113.                 remove();
  114.                 return;
  115.             }
  116.  
  117.             boolean flag = blockstate.get(BarrelBlock.PROPERTY_OPEN);
  118.             if (flag) {
  119.                 this.playSound(SoundEvents.BLOCK_BARREL_CLOSE);
  120.                 this.setOpenProperty(blockstate, false);
  121.             }
  122.         }
  123.     }
  124.  
  125.     @Override
  126.     public void closeInventory(PlayerEntity player) {
  127.         if (!player.isSpectator()) {
  128.             numPlayersUsing--;
  129.         }
  130.     }
  131.  
  132.     private void setOpenProperty(BlockState blockstate, boolean open) {
  133.         assert world != null;
  134.         world.setBlockState(this.pos, blockstate.with(OPEN, open), 3);
  135.     }
  136.  
  137.     private void playSound(SoundEvent soundEvent) {
  138.         double x = (double) this.pos.getX() + 0.5D;
  139.         double y = (double) this.pos.getY() + 0.5D;
  140.         double z = (double) this.pos.getZ() + 0.5D;
  141.         assert world != null;
  142.         world.playSound(null, x, y, z, soundEvent, SoundCategory.BLOCKS, 0.5F,
  143.                 this.world.rand.nextFloat() * 0.1f + 0.9f);
  144.     }
  145.  
  146.     @Override
  147.     public @NotNull CompoundNBT getUpdateTag() {
  148.         return write(new CompoundNBT());
  149.     }
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment