TheOnlyTails

Tile entity

Sep 29th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.18 KB | None | 0 0
  1. public class RubyBarrelTileEntity extends LockableTileEntity {
  2.     private final IItemHandlerModifiable items = createHandler();
  3.     protected int numPlayersUsing;
  4.     private NonNullList<ItemStack> chestContents = NonNullList.withSize(36, ItemStack.EMPTY);
  5.     private LazyOptional<IItemHandlerModifiable> itemHandler = LazyOptional.of(() -> items);
  6.  
  7.     public RubyBarrelTileEntity(TileEntityType<?> typeIn) {
  8.         super(typeIn);
  9.     }
  10.  
  11.     public RubyBarrelTileEntity() {
  12.         this(TileEntitiesRegistry.RUBY_BARREL.get());
  13.     }
  14.  
  15.     public static int getPlayersUsing(IBlockReader reader, BlockPos pos) {
  16.         BlockState blockstate = reader.getBlockState(pos);
  17.         if (blockstate.hasTileEntity()) {
  18.             TileEntity tileentity = reader.getTileEntity(pos);
  19.             if (tileentity instanceof RubyBarrelTileEntity) {
  20.                 return ((RubyBarrelTileEntity) tileentity).numPlayersUsing;
  21.             }
  22.         }
  23.         return 0;
  24.     }
  25.  
  26.     public static void swapContents(RubyBarrelTileEntity te, RubyBarrelTileEntity otherTe) {
  27.         NonNullList<ItemStack> list = te.getItems();
  28.         te.setItems(otherTe.getItems());
  29.         otherTe.setItems(list);
  30.     }
  31.  
  32.     @Override
  33.     public int getSizeInventory() {
  34.         return 36;
  35.     }
  36.  
  37.     @Override
  38.     public boolean isEmpty() {
  39.         return this.getItems().stream().allMatch(ItemStack::isEmpty);
  40.     }
  41.  
  42.     @Override
  43.     public @NotNull ItemStack getStackInSlot(int index) {
  44.         return this.getItems().get(index);
  45.     }
  46.  
  47.     @Override
  48.     public @NotNull ItemStack decrStackSize(int index, int count) {
  49.         ItemStack itemstack = ItemStackHelper.getAndSplit(this.getItems(), index, count);
  50.         if (!itemstack.isEmpty()) {
  51.             this.markDirty();
  52.         }
  53.  
  54.         return itemstack;
  55.     }
  56.  
  57.     @Override
  58.     public @NotNull ItemStack removeStackFromSlot(int index) {
  59.         return ItemStackHelper.getAndRemove(this.getItems(), index);
  60.     }
  61.  
  62.     @Override
  63.     public void setInventorySlotContents(int index, @NotNull ItemStack stack) {
  64.         this.getItems().set(index, stack);
  65.         if (stack.getCount() > this.getInventoryStackLimit()) {
  66.             stack.setCount(this.getInventoryStackLimit());
  67.         }
  68.  
  69.         this.markDirty();
  70.     }
  71.  
  72.     @Override
  73.     public boolean isUsableByPlayer(@NotNull PlayerEntity player) {
  74.         assert this.world != null;
  75.         if (this.world.getTileEntity(this.pos) != this) {
  76.             return false;
  77.         } else {
  78.             return !(player.getDistanceSq(
  79.                     (double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) > 64.0D);
  80.         }
  81.     }
  82.  
  83.     public @NotNull NonNullList<ItemStack> getItems() {
  84.         return this.chestContents;
  85.     }
  86.  
  87.     public void setItems(@NotNull NonNullList<ItemStack> itemsIn) {
  88.         this.chestContents = itemsIn;
  89.     }
  90.  
  91.     @Override
  92.     protected @NotNull ITextComponent getDefaultName() {
  93.         return new TranslationTextComponent("container.ruby_barrel");
  94.     }
  95.  
  96.     @Override
  97.     protected @NotNull Container createMenu(int id, @NotNull PlayerInventory player) {
  98.         return ChestContainer.createGeneric9X5(id, player);
  99.     }
  100.  
  101.     @Override
  102.     public @NotNull CompoundNBT write(@NotNull CompoundNBT compound) {
  103.         super.write(compound);
  104.  
  105.         ItemStackHelper.saveAllItems(compound, this.chestContents);
  106.  
  107.         return compound;
  108.     }
  109.  
  110.     @Override
  111.     public void read(@NotNull BlockState blockState, @NotNull CompoundNBT compound) {
  112.         super.read(blockState, compound);
  113.         this.chestContents = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
  114.  
  115.         ItemStackHelper.loadAllItems(compound, this.chestContents);
  116.     }
  117.  
  118.     private void playSound(SoundEvent sound) {
  119.         double dx = (double) this.pos.getX() + 0.5D;
  120.         double dy = (double) this.pos.getY() + 0.5D;
  121.         double dz = (double) this.pos.getZ() + 0.5D;
  122.  
  123.         assert this.world != null;
  124.         this.world.playSound(null, dx, dy, dz, sound, SoundCategory.BLOCKS, 0.5f,
  125.                 this.world.rand.nextFloat() * 0.1f + 0.9f);
  126.     }
  127.  
  128.     @Override
  129.     public boolean receiveClientEvent(int id, int type) {
  130.         if (id == 1) {
  131.             this.numPlayersUsing = type;
  132.             return true;
  133.         } else {
  134.             return super.receiveClientEvent(id, type);
  135.         }
  136.     }
  137.  
  138.     @Override
  139.     public void openInventory(PlayerEntity player) {
  140.         if (!player.isSpectator()) {
  141.             if (this.numPlayersUsing < 0) {
  142.                 this.numPlayersUsing = 0;
  143.             }
  144.  
  145.             ++this.numPlayersUsing;
  146.             this.playSound(SoundEvents.BLOCK_BARREL_OPEN);
  147.             this.onOpenOrClose();
  148.         }
  149.     }
  150.  
  151.     @Override
  152.     public void closeInventory(PlayerEntity player) {
  153.         if (!player.isSpectator()) {
  154.             --this.numPlayersUsing;
  155.             this.playSound(SoundEvents.BLOCK_BARREL_CLOSE);
  156.             this.onOpenOrClose();
  157.         }
  158.     }
  159.  
  160.     protected void onOpenOrClose() {
  161.         Block block = this.getBlockState().getBlock();
  162.         if (block instanceof RubyBarrelBlock) {
  163.             assert this.world != null;
  164.             this.world.addBlockEvent(this.pos, block, 1, this.numPlayersUsing);
  165.             this.world.notifyNeighborsOfStateChange(this.pos, block);
  166.         }
  167.     }
  168.  
  169.     @Override
  170.     public void updateContainingBlockInfo() {
  171.         super.updateContainingBlockInfo();
  172.         if (this.itemHandler != null) {
  173.             this.itemHandler.invalidate();
  174.             this.itemHandler = null;
  175.         }
  176.     }
  177.  
  178.     @Override
  179.     public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
  180.         if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
  181.             return itemHandler.cast();
  182.         }
  183.         return super.getCapability(cap, side);
  184.     }
  185.  
  186.     private IItemHandlerModifiable createHandler() {
  187.         return new InvWrapper(this);
  188.     }
  189.  
  190.     @Override
  191.     public void remove() {
  192.         super.remove();
  193.         if (itemHandler != null) {
  194.             itemHandler.invalidate();
  195.         }
  196.     }
  197.  
  198.     @Override
  199.     public void clear() {
  200.  
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment