Jump to content

[1.12] ItemStackHandler size issue(?)


B0undarybreaker

Recommended Posts

I'm trying to make an inventory with five slots, but every time I try to put more than one slot on the Container file for the inventory, the game crashes as soon as I open the inventory. It says that it only has slots [0,1) available in the crash log even though I have the IStackHandler's size parameter set to 5. Am I doing something wrong?

 

(side note: I kinda have the feeling that my container file could be a little more efficient with all the markDirty(); so if that can be improved please let me know)
 

relevant files:

Spoiler

TileEntityStoneworks


package space.bbkr.stoneworks.tileentity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.ItemStackHandler;

import javax.annotation.Nullable;

public class TileEntityStoneworks extends TileEntity {

    private ItemStackHandler inventory = new ItemStackHandler(5);

    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        compound.setTag("inventory", inventory.serializeNBT());
        return super.writeToNBT(compound);
    }

    @Override
    public void readFromNBT(NBTTagCompound compound) {
        inventory.deserializeNBT(compound.getCompoundTag("inventory"));
        super.readFromNBT(compound);
    }

    @Override
    public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
        return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY || super.hasCapability(capability, facing);
    }

    @Nullable
    @Override
    public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
        return capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY ? (T)inventory : super.getCapability(capability, facing);
    }
}

ContainerStoneworks


package space.bbkr.stoneworks.container;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import space.bbkr.stoneworks.tileentity.TileEntityStoneworks;

public class ContainerStoneworks extends Container {

    public ContainerStoneworks(InventoryPlayer playerInv, final TileEntityStoneworks stoneworks) {
        IItemHandler inventory = stoneworks.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, EnumFacing.NORTH);
        addSlotToContainer(new SlotItemHandler(inventory, 0, 45, 13) {
            @Override
            public void onSlotChanged() {
                stoneworks.markDirty();
            }
        });

        addSlotToContainer(new SlotItemHandler(inventory, 1, 44, 61) {
            @Override
            public void onSlotChanged() {
                stoneworks.markDirty();
            }
        });

        addSlotToContainer(new SlotItemHandler(inventory, 2, 80, 61) {
            @Override
            public void onSlotChanged() {
                stoneworks.markDirty();
            }
        });

        addSlotToContainer(new SlotItemHandler(inventory, 3, 134, 61) {
            @Override
            public void onSlotChanged() {
                stoneworks.markDirty();
            }
        });

        addSlotToContainer(new SlotItemHandler(inventory, 4, 134, 12) {
            @Override
            public void onSlotChanged() {
                stoneworks.markDirty();
            }
        });

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 9; j++) {
                addSlotToContainer(new Slot(playerInv, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }

        for (int k = 0; k < 9; k++) {
            addSlotToContainer(new Slot(playerInv, k, 8 + k * 18, 142));
        }
    }

    @Override
    public boolean canInteractWith(EntityPlayer player) {
        return true;
    }

    @Override
    public ItemStack transferStackInSlot(EntityPlayer player, int index) {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = inventorySlots.get(index);

        if (slot != null && slot.getHasStack()) {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();

            int containerSlots = inventorySlots.size() - player.inventory.mainInventory.size();

            if (index < containerSlots) {
                if (!this.mergeItemStack(itemstack1, containerSlots, inventorySlots.size(), true)) {
                    return ItemStack.EMPTY;
                }
            } else if (!this.mergeItemStack(itemstack1, 0, containerSlots, false)) {
                return ItemStack.EMPTY;
            }

            if (itemstack1.getCount() == 0) {
                slot.putStack(ItemStack.EMPTY);
            } else {
                slot.onSlotChanged();
            }

            if (itemstack1.getCount() == itemstack.getCount()) {
                return ItemStack.EMPTY;
            }

            slot.onTake(player, itemstack1);
        }

        return itemstack;
    }
}

Crash Log


---- Minecraft Crash Report ----
// Ooh. Shiny.

Time: 9/23/17 5:55 PM
Description: Ticking player

java.lang.RuntimeException: Slot 1 not in valid range - [0,1)
	at net.minecraftforge.items.ItemStackHandler.validateSlotIndex(ItemStackHandler.java:210)
	at net.minecraftforge.items.ItemStackHandler.getStackInSlot(ItemStackHandler.java:75)
	at net.minecraftforge.items.SlotItemHandler.getStack(SlotItemHandler.java:79)
	at net.minecraft.inventory.Container.detectAndSendChanges(Container.java:97)
	at net.minecraft.entity.player.EntityPlayerMP.onUpdate(EntityPlayerMP.java:365)
	at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2154)
	at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:872)
	at net.minecraft.world.World.updateEntity(World.java:2114)
	at net.minecraft.world.WorldServer.tickPlayers(WorldServer.java:673)
	at net.minecraft.world.World.updateEntities(World.java:1901)
	at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:644)
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:836)
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:740)
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:589)
	at java.lang.Thread.run(Thread.java:748)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Server thread
Stacktrace:
	at net.minecraftforge.items.ItemStackHandler.validateSlotIndex(ItemStackHandler.java:210)
	at net.minecraftforge.items.ItemStackHandler.getStackInSlot(ItemStackHandler.java:75)
	at net.minecraftforge.items.SlotItemHandler.getStack(SlotItemHandler.java:79)
	at net.minecraft.inventory.Container.detectAndSendChanges(Container.java:97)
	at net.minecraft.entity.player.EntityPlayerMP.onUpdate(EntityPlayerMP.java:365)
	at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2154)
	at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:872)
	at net.minecraft.world.World.updateEntity(World.java:2114)

-- Player being ticked --
Details:
	Entity Type: null (net.minecraft.entity.player.EntityPlayerMP)
	Entity ID: 175
	Entity Name: Player29
	Entity's Exact location: 258.07, 64.27, 241.76
	Entity's Block location: World: (258,64,241), Chunk: (at 2,4,1 in 16,15; contains blocks 256,0,240 to 271,255,255), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)
	Entity's Momentum: 0.00, 0.00, 0.00
	Entity's Passengers: []
	Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
	at net.minecraft.world.WorldServer.tickPlayers(WorldServer.java:673)
	at net.minecraft.world.World.updateEntities(World.java:1901)
	at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:644)

-- Affected level --
Details:
	Level name: test
	All players: 1 total; [EntityPlayerMP['Player29'/175, l='test', x=258.07, y=64.27, z=241.76]]
	Chunk stats: ServerChunkCache: 625 Drop: 0
	Level seed: -2303893384725939711
	Level generator: ID 00 - default, ver 1. Features enabled: true
	Level generator options: 
	Level spawn location: World: (253,64,229), Chunk: (at 13,4,5 in 15,14; contains blocks 240,0,224 to 255,255,239), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)
	Level time: 9905 game time, 9905 day time
	Level dimension: 0
	Level storage version: 0x04ABD - Anvil
	Level weather: Rain time: 164388 (now: false), thunder time: 32665 (now: false)
	Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: true
Stacktrace:
	at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:836)
	at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:740)
	at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:589)
	at java.lang.Thread.run(Thread.java:748)

-- System Details --
Details:
	Minecraft Version: 1.12
	Operating System: Mac OS X (x86_64) version 10.13
	Java Version: 1.8.0_144, Oracle Corporation
	Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
	Memory: 402071184 bytes (383 MB) / 940572672 bytes (897 MB) up to 1908932608 bytes (1820 MB)
	JVM Flags: 0 total; 
	IntCache: cache: 0, tcache: 0, allocated: 13, tallocated: 95
	FML: MCP 9.40 Powered by Forge 14.21.1.2443 5 mods loaded, 5 mods active
	States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored
	UCHIJAAAA	minecraft{1.12} [Minecraft] (minecraft.jar) 
	UCHIJAAAA	mcp{9.19} [Minecraft Coder Pack] (minecraft.jar) 
	UCHIJAAAA	FML{8.0.99.99} [Forge Mod Loader] (forgeSrc-1.12-14.21.1.2443.jar) 
	UCHIJAAAA	forge{14.21.1.2443} [Minecraft Forge] (forgeSrc-1.12-14.21.1.2443.jar) 
	UCHIJAAAA	stoneworks{1.0} [Stoneworks] (stoneworks_main) 
	Loaded coremods (and transformers): 
	Profiler Position: N/A (disabled)
	Player Count: 1 / 8; [EntityPlayerMP['Player29'/175, l='test', x=258.07, y=64.27, z=241.76]]
	Type: Integrated Server (map_client.txt)
	Is Modded: Definitely; Client brand changed to 'fml,forge'

 

 

Link to comment
Share on other sites

(You can pass null as a facing, by the way. That's what @Nullable means. null is typically used to represent an "internal" or "self" access.)

 

Did you ever have the inventory size as being only 1? Because if you did, and you saved that world, and you're reloading that world, the deserialization process is reading an inventory of size 1 and overriding your new with size 5.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.