TheOnlyTails

TileRubyBarrel

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