Jump to content

[1.16.5] Empty slots in ItemStackHandler


stopper

Recommended Posts

This is my TileEntity code:

Spoiler

    private ItemStackHandler itemHandler = createHandler();
    private LazyOptional<IItemHandler> handler = LazyOptional.of(() -> itemHandler);

    public AutomaticTile() {
        super(Automation2.AUTO_TILE.get());
    }
    public int actionTick = 0;

    @Override
    public void remove() {
        super.remove();
        handler.invalidate();
    }

    @Override
    public void tick() {
        if (world.isRemote) {
            return;
        }
        actionTick++;
        if (actionTick == 20) {
            actionTick = 0;
            if (!itemHandler.getStackInSlot(3).isEmpty() && !itemHandler.getStackInSlot(4).isEmpty()) {
                if (!itemHandler.getStackInSlot(0).isEmpty() && itemHandler.getStackInSlot(9).getCount() != 64) {
                    itemHandler.extractItem(0, 1, false);
                    itemHandler.extractItem(3, 1, false);
                    itemHandler.extractItem(4, 1, false);

                    itemHandler.insertItem(9, Items.REDSTONE.getDefaultInstance(), false);
                    return;
                }
                if (!itemHandler.getStackInSlot(1).isEmpty() && itemHandler.getStackInSlot(10).getCount() != 64) {
                    itemHandler.extractItem(1, 1, false);
                    itemHandler.extractItem(3, 1, false);
                    itemHandler.extractItem(4, 1, false);

                    itemHandler.insertItem(10, Items.REDSTONE.getDefaultInstance(), false);
                    return;
                }
                if (!itemHandler.getStackInSlot(2).isEmpty() && itemHandler.getStackInSlot(11).getCount() != 64) {
                    itemHandler.extractItem(2, 1, false);
                    itemHandler.extractItem(3, 1, false);
                    itemHandler.extractItem(4, 1, false);

                    itemHandler.insertItem(11, Items.REDSTONE.getDefaultInstance(), false);
                    return;
                }
            }
        }
    }

    @Override
    public void read(CompoundNBT compound) {
        itemHandler.deserializeNBT(compound.getCompound("inv"));
        super.read(compound);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        compound.put("inv", itemHandler.serializeNBT());
        return super.write(compound);
    }

    private ItemStackHandler createHandler() {
        return new ItemStackHandler(12) {
            @Override
            protected void onContentsChanged(int slot) {
                markDirty();
            }

            public boolean isValid(@Nonnull int slot, @Nonnull ItemStack stack) {
                switch (slot) {
                    default:
                        return false;
                    case (0):
                        return stack.getItem() == Items.REDSTONE;
                    case (1):
                        return stack.getItem() == Items.REDSTONE;
                    case (2):
                        return stack.getItem() == Items.REDSTONE;
                    case (3):
                        return stack.getItem() == Items.REDSTONE;
                    case (4):
                        return stack.getItem() == Items.REDSTONE;
                    case (5):
                        return stack.getItem() == Items.REDSTONE;
                    case (6):
                        return stack.getItem() == Items.DIAMOND;
                    case (7):
                        return stack.getItem() == Items.REDSTONE;
                    case (8):
                        return stack.getItem() == Items.GOLD_INGOT;
                    case (9):
                        return stack.getItem() == Items.REDSTONE;
                    case (10):
                        return stack.getItem() == Items.REDSTONE;
                    case (11):
                        return stack.getItem() == Items.REDSTONE;
                }
            }

            @Nonnull
            @Override
            public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
                if (!isValid(slot, stack)) {
                    return stack;
                }
                return super.insertItem(slot, stack, simulate);
            }

            @Override
            public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
                return isValid(slot, stack);
            }
        };
    }

    @Override
    public CompoundNBT getUpdateTag() {
        CompoundNBT compoundNBT = new CompoundNBT();
        compoundNBT.put("inv", itemHandler.serializeNBT());
        return compoundNBT;
    }

    @Nonnull
    @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, @Nullable Direction side) {
        if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
            return handler.cast();
        }
        return super.getCapability(cap, side);
    }

 

If I try to get item from any slot (itemHandler.getStackInSlot(0)), it returns air x1.

It seems I can store items (that I'am placed in isValid()) in block, insert and extract.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.