Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RubyBarrelTileEntity extends LockableTileEntity {
- private final IItemHandlerModifiable items = createHandler();
- protected int numPlayersUsing;
- private NonNullList<ItemStack> chestContents = NonNullList.withSize(36, ItemStack.EMPTY);
- private LazyOptional<IItemHandlerModifiable> itemHandler = LazyOptional.of(() -> items);
- public RubyBarrelTileEntity(TileEntityType<?> typeIn) {
- super(typeIn);
- }
- public RubyBarrelTileEntity() {
- this(TileEntitiesRegistry.RUBY_BARREL.get());
- }
- public static int getPlayersUsing(IBlockReader reader, BlockPos pos) {
- BlockState blockstate = reader.getBlockState(pos);
- if (blockstate.hasTileEntity()) {
- TileEntity tileentity = reader.getTileEntity(pos);
- if (tileentity instanceof RubyBarrelTileEntity) {
- return ((RubyBarrelTileEntity) tileentity).numPlayersUsing;
- }
- }
- return 0;
- }
- public static void swapContents(RubyBarrelTileEntity te, RubyBarrelTileEntity otherTe) {
- NonNullList<ItemStack> list = te.getItems();
- te.setItems(otherTe.getItems());
- otherTe.setItems(list);
- }
- @Override
- public int getSizeInventory() {
- return 36;
- }
- @Override
- public boolean isEmpty() {
- return this.getItems().stream().allMatch(ItemStack::isEmpty);
- }
- @Override
- public @NotNull ItemStack getStackInSlot(int index) {
- return this.getItems().get(index);
- }
- @Override
- public @NotNull ItemStack decrStackSize(int index, int count) {
- ItemStack itemstack = ItemStackHelper.getAndSplit(this.getItems(), index, count);
- if (!itemstack.isEmpty()) {
- this.markDirty();
- }
- return itemstack;
- }
- @Override
- public @NotNull ItemStack removeStackFromSlot(int index) {
- return ItemStackHelper.getAndRemove(this.getItems(), index);
- }
- @Override
- public void setInventorySlotContents(int index, @NotNull ItemStack stack) {
- this.getItems().set(index, stack);
- if (stack.getCount() > this.getInventoryStackLimit()) {
- stack.setCount(this.getInventoryStackLimit());
- }
- this.markDirty();
- }
- @Override
- public boolean isUsableByPlayer(@NotNull PlayerEntity player) {
- assert this.world != null;
- if (this.world.getTileEntity(this.pos) != this) {
- return false;
- } else {
- return !(player.getDistanceSq(
- (double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) > 64.0D);
- }
- }
- public @NotNull NonNullList<ItemStack> getItems() {
- return this.chestContents;
- }
- public void setItems(@NotNull NonNullList<ItemStack> itemsIn) {
- this.chestContents = itemsIn;
- }
- @Override
- protected @NotNull ITextComponent getDefaultName() {
- return new TranslationTextComponent("container.ruby_barrel");
- }
- @Override
- protected @NotNull Container createMenu(int id, @NotNull PlayerInventory player) {
- return ChestContainer.createGeneric9X5(id, player);
- }
- @Override
- public @NotNull CompoundNBT write(@NotNull CompoundNBT compound) {
- super.write(compound);
- ItemStackHelper.saveAllItems(compound, this.chestContents);
- return compound;
- }
- @Override
- public void read(@NotNull BlockState blockState, @NotNull CompoundNBT compound) {
- super.read(blockState, compound);
- this.chestContents = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);
- ItemStackHelper.loadAllItems(compound, this.chestContents);
- }
- private void playSound(SoundEvent sound) {
- double dx = (double) this.pos.getX() + 0.5D;
- double dy = (double) this.pos.getY() + 0.5D;
- double dz = (double) this.pos.getZ() + 0.5D;
- assert this.world != null;
- this.world.playSound(null, dx, dy, dz, sound, SoundCategory.BLOCKS, 0.5f,
- this.world.rand.nextFloat() * 0.1f + 0.9f);
- }
- @Override
- public boolean receiveClientEvent(int id, int type) {
- if (id == 1) {
- this.numPlayersUsing = type;
- return true;
- } else {
- return super.receiveClientEvent(id, type);
- }
- }
- @Override
- public void openInventory(PlayerEntity player) {
- if (!player.isSpectator()) {
- if (this.numPlayersUsing < 0) {
- this.numPlayersUsing = 0;
- }
- ++this.numPlayersUsing;
- this.playSound(SoundEvents.BLOCK_BARREL_OPEN);
- this.onOpenOrClose();
- }
- }
- @Override
- public void closeInventory(PlayerEntity player) {
- if (!player.isSpectator()) {
- --this.numPlayersUsing;
- this.playSound(SoundEvents.BLOCK_BARREL_CLOSE);
- this.onOpenOrClose();
- }
- }
- protected void onOpenOrClose() {
- Block block = this.getBlockState().getBlock();
- if (block instanceof RubyBarrelBlock) {
- assert this.world != null;
- this.world.addBlockEvent(this.pos, block, 1, this.numPlayersUsing);
- this.world.notifyNeighborsOfStateChange(this.pos, block);
- }
- }
- @Override
- public void updateContainingBlockInfo() {
- super.updateContainingBlockInfo();
- if (this.itemHandler != null) {
- this.itemHandler.invalidate();
- this.itemHandler = null;
- }
- }
- @Override
- public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
- if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
- return itemHandler.cast();
- }
- return super.getCapability(cap, side);
- }
- private IItemHandlerModifiable createHandler() {
- return new InvWrapper(this);
- }
- @Override
- public void remove() {
- super.remove();
- if (itemHandler != null) {
- itemHandler.invalidate();
- }
- }
- @Override
- public void clear() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment