Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.