TheOnlyTails

TileRubyBarrel

Oct 2nd, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 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.ItemStackHandler;
  21.  
  22. import javax.annotation.Nullable;
  23.  
  24. public class TileRubyBarrel extends TileEntity implements INamedContainerProvider {
  25.     public int size = 45;
  26.     public final ItemStackHandler itemHandler = new ItemStackHandler(size) {
  27.         @Override
  28.         protected void onContentsChanged(int slot) {
  29.             super.onContentsChanged(slot);
  30.             markDirty();
  31.         }
  32.     };
  33.     private final LazyOptional<ItemStackHandler> optional = LazyOptional.of(() -> itemHandler);
  34.     public int players = 0;
  35.  
  36.     public TileRubyBarrel() {
  37.         super(TileEntityTypesRegistry.RUBY_BARREL.get());
  38.     }
  39.  
  40.     @Override
  41.     public ITextComponent getDisplayName() {
  42.         return new TranslationTextComponent(this.getBlockState().getBlock().getTranslationKey());
  43.     }
  44.  
  45.     @Nullable
  46.     @Override
  47.     public Container createMenu(int id, PlayerInventory playerInventory, PlayerEntity player) {
  48.         return new RubyBarrelContainer(id, playerInventory, this);
  49.     }
  50.  
  51.     public void changeState(BlockState blockState, boolean value) {
  52.         if (blockState.getBlock() instanceof RubyBarrelBlock) {
  53.             if (world != null) {
  54.                 this.world.setBlockState(this.pos, blockState.with(RubyBarrelBlock.PROPERTY_OPEN, value));
  55.             }
  56.         }
  57.     }
  58.  
  59.     public void playSound(SoundEvent soundEvent) {
  60.         if (this.getBlockState().getBlock() instanceof RubyBarrelBlock) {
  61.             double x = this.pos.getX() + 0.5D;
  62.             double y = this.pos.getY() + 0.5D;
  63.             double z = this.pos.getZ() + 0.5D;
  64.             if (this.world != null) {
  65.                 this.world.playSound(null, x, y, z, soundEvent,
  66.                         SoundCategory.BLOCKS, 0.5F, this.world.rand.nextFloat() * 0.1F + 0.9F);
  67.             }
  68.         }
  69.     }
  70.  
  71.  
  72.     @Override
  73.     public CompoundNBT write(CompoundNBT nbt) {
  74.         optional.ifPresent(h -> nbt.put("inv", h.serializeNBT()));
  75.         itemHandler.serializeNBT();
  76.         return super.write(nbt);
  77.     }
  78.  
  79.     @Override
  80.     public void read(BlockState state, CompoundNBT nbt) {
  81.         optional.ifPresent(h -> h.deserializeNBT(nbt.getCompound("inv")));
  82.         itemHandler.deserializeNBT(nbt.getCompound("inv"));
  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.  
Advertisement
Add Comment
Please, Sign In to add comment