TheOnlyTails

TileEntity

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