TheOnlyTails

TileRubyBarrel

Oct 1st, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. package com.theonlytails.ruby.tileentity;
  2.  
  3. import com.theonlytails.ruby.blocks.RubyBarrelBlock;
  4. import com.theonlytails.ruby.init.TileEntityTypesRegistry;
  5. import net.minecraft.block.BlockState;
  6. import net.minecraft.entity.player.PlayerEntity;
  7. import net.minecraft.entity.player.PlayerInventory;
  8. import net.minecraft.inventory.container.ChestContainer;
  9. import net.minecraft.inventory.container.Container;
  10. import net.minecraft.inventory.container.INamedContainerProvider;
  11. import net.minecraft.nbt.CompoundNBT;
  12. import net.minecraft.network.NetworkManager;
  13. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  14. import net.minecraft.tileentity.TileEntity;
  15. import net.minecraft.util.SoundCategory;
  16. import net.minecraft.util.SoundEvent;
  17. import net.minecraft.util.text.ITextComponent;
  18. import net.minecraft.util.text.TranslationTextComponent;
  19. import net.minecraftforge.common.extensions.IForgeTileEntity;
  20. import net.minecraftforge.common.util.LazyOptional;
  21. import net.minecraftforge.items.IItemHandler;
  22. import net.minecraftforge.items.IItemHandlerModifiable;
  23. import net.minecraftforge.items.ItemStackHandler;
  24.  
  25. import javax.annotation.Nullable;
  26.  
  27. public class TileRubyBarrel extends TileEntity implements IForgeTileEntity, INamedContainerProvider {
  28.     public int size = 45;
  29.     public final IItemHandlerModifiable itemHandler = new ItemStackHandler(size) {
  30.         @Override
  31.         protected void onContentsChanged(int slot) {
  32.             super.onContentsChanged(slot);
  33.             markDirty();
  34.         }
  35.     };
  36.     public final LazyOptional<IItemHandler> optional = LazyOptional.of(() -> itemHandler);
  37.     public int players = 0;
  38.  
  39.     public TileRubyBarrel() {
  40.         super(TileEntityTypesRegistry.RUBY_BARREL.get());
  41.     }
  42.  
  43.     @Override
  44.     public ITextComponent getDisplayName() {
  45.         return new TranslationTextComponent(this.getBlockState().getBlock().getTranslationKey());
  46.     }
  47.  
  48.     @Nullable
  49.     @Override
  50.     public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity player) {
  51.         return ChestContainer.createGeneric9X5(id, playerInventory);
  52.     }
  53.  
  54.     public void changeState(BlockState blockState, boolean value) {
  55.         if (blockState.getBlock() instanceof RubyBarrelBlock) {
  56.             if (world != null) {
  57.                 this.world.setBlockState(this.pos, blockState.with(RubyBarrelBlock.PROPERTY_OPEN, value));
  58.             }
  59.         }
  60.     }
  61.  
  62.     public void playSound(SoundEvent soundEvent) {
  63.         if (this.getBlockState().getBlock() instanceof RubyBarrelBlock) {
  64.             double x = this.pos.getX() + 0.5D;
  65.             double y = this.pos.getY() + 0.5D;
  66.             double z = this.pos.getZ() + 0.5D;
  67.             if (this.world != null) {
  68.                 this.world.playSound(null, x, y, z, soundEvent,
  69.                         SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
  70.             }
  71.         }
  72.     }
  73.  
  74.  
  75.  
  76.     @Override
  77.     public CompoundNBT write(CompoundNBT compound) {
  78.         return super.write(compound);
  79.     }
  80.  
  81.     @Override
  82.     public void read(BlockState state, CompoundNBT nbt) {
  83.         super.read(state, nbt);
  84.     }
  85.  
  86.     @Override
  87.     public CompoundNBT getUpdateTag() {
  88.         return super.write(new CompoundNBT());
  89.     }
  90.  
  91.     @Nullable
  92.     @Override
  93.     public SUpdateTileEntityPacket getUpdatePacket() {
  94.         CompoundNBT nbt = new CompoundNBT();
  95.         this.write(nbt);
  96.  
  97.         return new SUpdateTileEntityPacket(this.pos, 0, nbt);
  98.     }
  99.  
  100.     @Override
  101.     public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket pkt) {
  102.         this.read(this.getBlockState(), pkt.getNbtCompound());
  103.     }
  104.  
  105.     @Override
  106.     public void remove() {
  107.         super.remove();
  108.         this.optional.invalidate();
  109.     }
  110. }
  111.  
Add Comment
Please, Sign In to add comment