Jump to content

[1.14.4][SOLVED] problem adding slots to blocks inventory


andGarrett

Recommended Posts

I'm trying to create a block with a 16 slot inventory. I have no trouble adding one slot using mcjty's tutorial, but after that I get all kinds of problems.

 

on line 39 I try to add another slot. if I set the index to 0, it simply mirrors the first slot. it has the same item in it. if I use any number other than 0 for the index, for example 4, I get: java.lang.RuntimeException: Slot 4 not in valid range - [0,1)

 

package com.Garrett.backtobasics.blocks;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import static com.Garrett.backtobasics.blocks.ModBlocks.STORE_CONTAINER;

import javax.annotation.Nonnull;

public class StoreContainer extends Container {

    private TileEntity tileEntity;
    private PlayerEntity playerEntity;
    private IItemHandler playerInventory;
    private static final Logger LOGGER = LogManager.getLogger();

    public StoreContainer(int windowId, World world, BlockPos pos, PlayerInventory PlayerInventory, PlayerEntity player) {

        super(STORE_CONTAINER, windowId);
        tileEntity = world.getTileEntity(pos);
        this.playerEntity = player;
        this.playerInventory = new InvWrapper(PlayerInventory);

        tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(h -> {
            addSlot(new SlotItemHandler(h, 0, 8, 23));
            addSlot(new SlotItemHandler(h, 1, 26, 23));
        });
        layoutPlayerInventorySlots(20, 138);
    }

    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return isWithinUsableDistance(IWorldPosCallable.of(tileEntity.getWorld(), tileEntity.getPos()), playerEntity, ModBlocks.STORE);
    }

    private int addSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx) {
        for (int i = 0 ; i < amount ; i++) {
            addSlot(new SlotItemHandler(handler, index, x, y));
            x += dx;
            index++;
        }
        return index;
    }

    private int addSlotBox(IItemHandler handler, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
        for (int j = 0 ; j < verAmount ; j++) {
            index = addSlotRange(handler, index, x, y, horAmount, dx);
            y += dy;
        }
        return index;
    }


    private void layoutPlayerInventorySlots(int leftCol, int topRow) {
        // Player inventory
        addSlotBox(playerInventory, 25, leftCol, topRow, 9, 18, 3, 18);

        // Hotbar
        topRow += 58;
        addSlotRange(playerInventory, 16, leftCol, topRow, 9, 18);
    }

    /*
     * Handle when the stack in slot is shift-clicked. Normally this moves the stack between the player
     * inventory and the other inventory(s).
     * copied from ChestContainer#transferStackInSlot
     */
    @Nonnull
    @Override
    public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = this.inventorySlots.get(index);
        if (slot != null && slot.getHasStack()) {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();
            if (index == 0) {
                if (!this.mergeItemStack(itemstack1, 1, this.inventorySlots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.mergeItemStack(itemstack1, 0, 1, false)) {
                return ItemStack.EMPTY;
            }

            if (itemstack1.isEmpty()) {
                slot.putStack(ItemStack.EMPTY);
            } else {
                slot.onSlotChanged();
            }
        }
        return itemstack;
    }
/*
    private void createSlotGrid(IItemHandler h, int columns, int rows, int startIndex, int startX, int startY) {

        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {

            }
        }
    }
    
 */
}

 

[SOLUTION]

the problem was that I was working with a block that was placed before I changed the IItemHandler size from 1 to 16

Edited by andGarrett
Link to comment
Share on other sites

5 minutes ago, andGarrett said:

Slot 4 not in valid range - [0,1)

Does the IItemHandler in question have a size of 16? Also post the crash.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

14 hours ago, Animefan8888 said:

Does the IItemHandler in question have a size of 16? Also post the crash.

it does. turns out the problem was that I was working with a block that was placed before I changed the iitemhandler size from 1 to 16. once I placed a new block, it worked perfectly. Thanks for the input.

  • Like 1
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.



×
×
  • Create New...

Important Information

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