Jump to content

Custom inventory crashes 2.0


alexschopbarteld

Recommended Posts

I tried to create a custom inventory inside an item, it gave this crash:

Spoiler

[19:34:09] [Server thread/FATAL]: Error executing task
java.util.concurrent.ExecutionException: java.lang.NullPointerException: The validated object is null
    at java.util.concurrent.FutureTask.report(Unknown Source) ~[?:1.8.0_131]
    at java.util.concurrent.FutureTask.get(Unknown Source) ~[?:1.8.0_131]
    at net.minecraft.util.Util.runTask(Util.java:30) [Util.class:?]
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:754) [MinecraftServer.class:?]
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:699) [MinecraftServer.class:?]
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) [IntegratedServer.class:?]
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:548) [MinecraftServer.class:?]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
Caused by: java.lang.NullPointerException: The validated object is null
    at org.apache.commons.lang3.Validate.notNull(Validate.java:222) ~[commons-lang3-3.3.2.jar:3.3.2]
    at org.apache.commons.lang3.Validate.notNull(Validate.java:203) ~[commons-lang3-3.3.2.jar:3.3.2]
    at net.minecraft.util.NonNullList.add(NonNullList.java:57) ~[NonNullList.class:?]
    at java.util.AbstractList.add(Unknown Source) ~[?:1.8.0_131]
    at net.minecraft.inventory.Container.getInventory(Container.java:71) ~[Container.class:?]
    at net.minecraft.inventory.Container.addListener(Container.java:57) ~[Container.class:?]
    at net.minecraftforge.fml.common.network.internal.FMLNetworkHandler.openGui(FMLNetworkHandler.java:102) ~[FMLNetworkHandler.class:?]
    at net.minecraft.entity.player.EntityPlayer.openGui(EntityPlayer.java:2741) ~[EntityPlayer.class:?]
    at com.walkingdead.walkingdeadmod.items.ItemSmallBackPack.onItemRightClick(ItemSmallBackPack.java:41) ~[ItemSmallBackPack.class:?]
    at net.minecraft.item.ItemStack.useItemRightClick(ItemStack.java:213) ~[ItemStack.class:?]
    at net.minecraft.server.management.PlayerInteractionManager.processRightClick(PlayerInteractionManager.java:387) ~[PlayerInteractionManager.class:?]
    at net.minecraft.network.NetHandlerPlayServer.processTryUseItem(NetHandlerPlayServer.java:739) ~[NetHandlerPlayServer.class:?]
    at net.minecraft.network.play.client.CPacketPlayerTryUseItem.processPacket(CPacketPlayerTryUseItem.java:43) ~[CPacketPlayerTryUseItem.class:?]
    at net.minecraft.network.play.client.CPacketPlayerTryUseItem.processPacket(CPacketPlayerTryUseItem.java:9) ~[CPacketPlayerTryUseItem.class:?]
    at net.minecraft.network.PacketThreadUtil$1.run(PacketThreadUtil.java:21) ~[PacketThreadUtil$1.class:?]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_131]
    at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_131]
    at net.minecraft.util.Util.runTask(Util.java:29) ~[Util.class:?]
    ... 5 more
[19:34:09] [Server thread/ERROR]: Encountered an unexpected exception
net.minecraft.util.ReportedException: Ticking player
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:801) ~[MinecraftServer.class:?]
    at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:699) ~[MinecraftServer.class:?]
    at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:156) ~[IntegratedServer.class:?]
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:548) [MinecraftServer.class:?]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
Caused by: java.lang.NullPointerException
    at net.minecraft.item.ItemStack.areItemStacksEqual(ItemStack.java:439) ~[ItemStack.class:?]
    at net.minecraft.inventory.Container.detectAndSendChanges(Container.java:96) ~[Container.class:?]
    at net.minecraft.entity.player.EntityPlayerMP.onUpdate(EntityPlayerMP.java:322) ~[EntityPlayerMP.class:?]
    at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2119) ~[World.class:?]
    at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:876) ~[WorldServer.class:?]
    at net.minecraft.world.World.updateEntity(World.java:2085) ~[World.class:?]
    at net.minecraft.world.WorldServer.tickPlayers(WorldServer.java:677) ~[WorldServer.class:?]
    at net.minecraft.world.World.updateEntities(World.java:1872) ~[World.class:?]
    at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:648) ~[WorldServer.class:?]
    at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:795) ~[MinecraftServer.class:?]
    ... 4 more

container:

Spoiler

package com.walkingdead.walkingdeadmod.items.backpack;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerModTileEntity extends Container{

    public static int GUIID = 2;
    public final InventoryItem inventory;
    private static final int INV_START = InventoryItem.INV_SIZE, INV_END = INV_START+26,
            HOTBAR_START = INV_END+1, HOTBAR_END = HOTBAR_START+8;
    
        public ContainerModTileEntity(EntityPlayer par1Player, InventoryPlayer inventoryPlayer, InventoryItem inventoryItem) {
            
        this.inventory = inventoryItem;
        
        int i;
        
        InventoryBasic backpackgui = new InventoryBasic("backpackgui", true, 9);        
        
        
        for (int y = 0; y < 3; ++y) {
            for (int x = 0; x < 3; ++x) {
                this.addSlotToContainer(new SingleItemSlot(this.inventory, x + y * 3, 62 + x * 18, 17 + y * 18));
            }
        }
        
        
        for (i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
            }
        }

        
        for (i = 0; i < 9; ++i)
        {
            this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
        }

    }
    
    

    @Override
    public boolean canInteractWith(EntityPlayer player) {
        return inventory.isUsableByPlayer(player);
    }

    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int index)
    {
        ItemStack itemstack = null;
        Slot slot = (Slot) this.inventorySlots.get(index);

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

            if (index < INV_START)
            {
                if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END+1, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else
            {
                if (index >= INV_START)
                {
                    if (!this.mergeItemStack(itemstack1, 0, INV_START, false))
                    {
                        return null;
                    }
                }
                
                else if (index >= HOTBAR_START && index < HOTBAR_END+1)
                {
                    if (!this.mergeItemStack(itemstack1, INV_START, INV_END+1, false))
                    {
                        return null;
                    }
                }
            }

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

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

        }

        return itemstack;
    }
    @Override
    public ItemStack slotClick(int slot, int button, ClickType clickTypeIn, EntityPlayer player) {
        if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItemMainhand()) {
            return null;
        }
        return super.slotClick(slot, button, clickTypeIn, player);
    }
    


    @Override
    protected boolean mergeItemStack(ItemStack stack, int start, int end, boolean backwards)
    {
        boolean flag1 = false;
        int k = (backwards ? end - 1 : start);
        Slot slot;
        ItemStack itemstack1;

        if (stack.isStackable())
        {
            while (stack.getCount() > 0 && (!backwards && k < end || backwards && k >= start))
            {
                slot = (Slot) inventorySlots.get(k);
                itemstack1 = slot.getStack();

                if (!slot.isItemValid(stack)) {
                    k += (backwards ? -1 : 1);
                    continue;
                }

                if (itemstack1 != null && itemstack1.getItem() == stack.getItem() &&
                        (!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, itemstack1))
                {
                    int l = itemstack1.getCount() + stack.getCount();

                    if (l <= stack.getMaxStackSize() && l <= slot.getSlotStackLimit()) {
                        stack.setCount(0);;
                        itemstack1.setCount(l);
                        inventory.markDirty();
                        flag1 = true;
                    } else if (itemstack1.getCount() < stack.getMaxStackSize() && l < slot.getSlotStackLimit()) {
                        stack.setCount(stack.getCount() - stack.getMaxStackSize() - itemstack1.getCount());
                        itemstack1.setCount(stack.getMaxStackSize());
                        inventory.markDirty();
                        flag1 = true;
                    }
                }

                k += (backwards ? -1 : 1);
            }
        }
        if (stack.getCount() > 0)
        {
            k = (backwards ? end - 1 : start);
            while (!backwards && k < end || backwards && k >= start) {
                slot = (Slot) inventorySlots.get(k);
                itemstack1 = slot.getStack();

                if (!slot.isItemValid(stack)) {
                    k += (backwards ? -1 : 1);
                    continue;
                }

                if (itemstack1 == null) {
                    int l = stack.getCount();
                    if (l <= slot.getSlotStackLimit()) {
                        slot.putStack(stack.copy());
                        stack.setCount(0);
                        inventory.markDirty();
                        flag1 = true;
                        break;
                    } else {
                        putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage()));
                        stack.setCount(stack.getCount() - slot.getSlotStackLimit());
                        inventory.markDirty();
                        flag1 = true;
                    }
                }

                k += (backwards ? -1 : 1);
            }
        }

        return flag1;
    }
}
 

gui:

 

Spoiler

package com.walkingdead.walkingdeadmod.items.backpack;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;


public class GuiWindow extends GuiContainer{
    private final InventoryItem inventory;
    
    
    public GuiWindow(ContainerModTileEntity containerItem) {
        super(containerItem);
        this.inventory = containerItem.inventory;
        this.xSize = 176;
        this.ySize = 166;
    }

    private static final ResourceLocation texture = new ResourceLocation("textures/gui/container/dispenser.png");

    protected void drawGuiContainerBackgroundLayer(float par1, int par2, int par3) {
        int posX = (this.width) / 2;
        int posY = (this.height) / 2;
        
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

        this.mc.renderEngine.bindTexture(texture);
        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(k, l, 0, 0, this.xSize, this.ySize);

        zLevel = 100.0F;

    }


    public void updateScreen() {
        super.updateScreen();
        int posX = (this.width) / 2;
        int posY = (this.height) / 2;

    }

    @Override
    protected void keyTyped(char par1, int par2) throws java.io.IOException {

        super.keyTyped(par1, par2);

    }

    protected void drawGuiContainerForegroundLayer(int par1, int par2) {
        int posX = (this.width) / 2;
        int posY = (this.height) / 2;

    }

    public void onGuiClosed() {
        super.onGuiClosed();
        Keyboard.enableRepeatEvents(false);
    }
    
    

    public void initGui() {
        super.initGui();
        this.guiLeft = (this.width - 176) / 2;
        this.guiTop = (this.height - 166) / 2;
        Keyboard.enableRepeatEvents(true);
        this.buttonList.clear();
        int posX = (this.width) / 2;
        int posY = (this.height) / 2;

    }

    protected void actionPerformed(GuiButton button) {
        MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
        World world = server.worlds[0];

    }

    public boolean doesGuiPauseGame() {
        return false;
    }

}
 

iiventoryclass

Spoiler

package com.walkingdead.walkingdeadmod.items.backpack;

import java.util.Arrays;
import java.util.List;

import com.walkingdead.walkingdeadmod.items.ItemSmallBackPack;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.NonNullList;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.common.util.Constants;

public class InventoryItem implements IInventory{
    

    
    private String name = "SmallBackPack";
    private final ItemStack invItem;
    public static final int INV_SIZE = 8;
    private ItemStack[] inventory = new ItemStack[INV_SIZE];
    
    public InventoryItem(ItemStack stack)
    {
        invItem = stack;
        if (!stack.hasTagCompound()) {
            stack.setTagCompound(new NBTTagCompound());
        }
        readFromNBT(stack.getTagCompound());
    }
    
    
    @Override
    public String getName() {
        return name;
    }

    @Override
    public boolean hasCustomName() {
        return name.length() > 0;
    }

    @Override
    public ITextComponent getDisplayName() {
        String display =
                "SmallBackPack" + '\n';
        ITextComponent itextcomponent = new TextComponentString(display);
        return itextcomponent;
    }

    @Override
    public int getSizeInventory() {
        return inventory.length;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public ItemStack getStackInSlot(int index) {
        return inventory[index];
    }

    @Override
    public ItemStack decrStackSize(int slot, int amount) {
        ItemStack stack = getStackInSlot(slot);
        if(stack != null)
        {
            if(stack.getCount() > amount)
            {
                stack = stack.splitStack(amount);
                markDirty();
            }
            else
            {
                setInventorySlotContents(slot, null);
            }
        }
        return stack;
    }

    @Override
    public ItemStack removeStackFromSlot(int index) {
            return ItemStack.EMPTY;
    }

    @Override
    public void setInventorySlotContents(int slot, ItemStack stack) {
        inventory[slot] = stack;

        if (stack != null && stack.getCount() > getInventoryStackLimit())
        {
            stack.setCount(getInventoryStackLimit());
        }

        markDirty();
        
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public void markDirty() {
        for (int i = 0; i < getSizeInventory(); ++i)
        {
            if (getStackInSlot(i) != null && getStackInSlot(i).getCount() == 0) {
                inventory = null;
            }
        }    
        writeToNBT(invItem.getTagCompound());
    }

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

    @Override
    public void openInventory(EntityPlayer player) {
    }

    @Override
    public void closeInventory(EntityPlayer player) {
    }

    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack) {
        return !(stack.getItem() instanceof ItemSmallBackPack);
    }

    @Override
    public int getField(int id) {
        return id;
    }

    @Override
    public void setField(int id, int value) {
    }

    @Override
    public int getFieldCount() {
        return 0;
    }

    @Override
    public void clear() {
    }
    
    public void readFromNBT(NBTTagCompound compound)
    {
        NBTTagList items = compound.getTagList("ItemInventory", Constants.NBT.TAG_COMPOUND);

        for (int i = 0; i < items.tagCount(); ++i)
        {
            NBTTagCompound item = (NBTTagCompound) items.getCompoundTagAt(i);
            int slot = item.getInteger("Slot");
        }
    }


    public void writeToNBT(NBTTagCompound tagcompound)
    {
        NBTTagList items = new NBTTagList();

        for (int i = 0; i < getSizeInventory(); ++i)
        {
            if (getStackInSlot(i) != null)
            {
                NBTTagCompound item = new NBTTagCompound();
                item.setInteger("Slot", i);

                getStackInSlot(i).writeToNBT(item);


                items.appendTag(items);
            }
        }
        tagcompound.setTag("ItemInventory", items);
    }

}
 

slot:

Spoiler

package com.walkingdead.walkingdeadmod.items.backpack;

import com.walkingdead.walkingdeadmod.items.ItemSmallBackPack;

import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class SingleItemSlot extends Slot {

    public SingleItemSlot(IInventory inventory, int index, int xPosition, int yPosition) {
        super(inventory, index, xPosition, yPosition);
    }

    @Override
    public boolean isItemValid(ItemStack itemstack)
    {
        return !(itemstack.getItem() instanceof ItemSmallBackPack);
    }
}

item:
 

Spoiler


    package com.walkingdead.walkingdeadmod.items;

    import com.walkingdead.walkingdeadmod.WalkingDeadMod;
    import com.walkingdead.walkingdeadmod.armor.ModArmor;
    import com.walkingdead.walkingdeadmod.items.backpack.InventoryItem;

    import net.minecraft.creativetab.CreativeTabs;
    import net.minecraft.entity.EntityLivingBase;
    import net.minecraft.entity.item.EntityItem;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.item.EnumAction;
    import net.minecraft.item.Item;
    import net.minecraft.item.ItemStack;
    import net.minecraft.util.ActionResult;
    import net.minecraft.util.EnumActionResult;
    import net.minecraft.util.EnumHand;
    import net.minecraft.world.World;
    import net.minecraftforge.fml.relauncher.Side;
    import net.minecraftforge.fml.relauncher.SideOnly;

    public class ItemSmallBackPack extends Item{
        public ItemSmallBackPack(String unlocalizedName, String registryName)
        {
            super();
            this.setUnlocalizedName(unlocalizedName);
            this.setRegistryName(registryName);
            this.setMaxStackSize(1);
            setCreativeTab(WalkingDeadMod.WalkingDead);
        }

        
        @Override
        public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn) {
            
            
            if(worldIn.isRemote){
                return new ActionResult(EnumActionResult.PASS, playerIn.getHeldItem(handIn));
            }
            else {
                playerIn.openGui(WalkingDeadMod.instance, WalkingDeadMod.GUI_ITEM_INV, worldIn, (int) playerIn.posX, (int) playerIn.posY, (int) playerIn.posZ);
                return new ActionResult(EnumActionResult.SUCCESS, playerIn.getHeldItem(handIn));
            }
        }
        @Override
        public int getMaxItemUseDuration(ItemStack stack) {
            return 1;
        }

    }
 

proxy:

Spoiler

package com.walkingdead.walkingdeadmod;

import com.walkingdead.walkingdeadmod.eventhandlers.ApplyEffects;
import com.walkingdead.walkingdeadmod.eventhandlers.EffectUtility;
import com.walkingdead.walkingdeadmod.eventhandlers.PotionDuration;
import com.walkingdead.walkingdeadmod.eventhandlers.RenderGuiHandler;
import com.walkingdead.walkingdeadmod.eventhandlers.playerdata.waternew.Handler;
import com.walkingdead.walkingdeadmod.eventhandlers.playerdata.waternew.WaterDataCapability;
import com.walkingdead.walkingdeadmod.items.backpack.ContainerModTileEntity;
import com.walkingdead.walkingdeadmod.items.backpack.GuiWindow;
import com.walkingdead.walkingdeadmod.items.backpack.InventoryItem;

import net.minecraft.client.model.ModelBiped;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.network.IGuiHandler;

public class CommonProxyTestEnvironmentMod implements IGuiHandler{
    

    
    public EntityPlayer getClientPlayer(){return null;}

    
    public ModelBiped getMilitairyArmorModel(int id) {
    return null;    
    }
    public ModelBiped getGhillyArmorModel(int id) {
    return null;    
    }
    public ModelBiped getWinterArmorModel(int id) {
    return null;    
    }
    public ModelBiped getSwatArmorModel(int id) {
    return null;    
    }
    public ModelBiped getDiverArmorModel(int id) {
    return null;    
    }
    
    
    public void registerRenderers(WalkingDeadMod ins) {

    }
    
    public void Eventstartup() {
    new WaterDataCapability().register();
    MinecraftForge.EVENT_BUS.register(new RenderGuiHandler());
    MinecraftForge.EVENT_BUS.register(new Handler());
    MinecraftForge.EVENT_BUS.register(new ApplyEffects());
    MinecraftForge.EVENT_BUS.register(new EffectUtility());
    MinecraftForge.EVENT_BUS.register(new PotionDuration());
    System.out.println("initialized event handler");
    }


    @Override
    public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
        if (id == WalkingDeadMod.GUI_ITEM_INV) {
            return new ContainerModTileEntity(player, player.inventory, new InventoryItem(player.getHeldItemMainhand()));
        }
        return null;
    }

    @Override
    public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z) {
        if (id == WalkingDeadMod.GUI_ITEM_INV){
            return new GuiWindow((ContainerModTileEntity) new ContainerModTileEntity(player, player.inventory, new InventoryItem(player.getHeldItemMainhand())));
        }
        return null;
    }

}
 

 

Link to comment
Share on other sites

ItemStacks can't be null in 1.11 and onwards. Your IInventory contains null ItemStacks right here

4 minutes ago, alexschopbarteld said:

    private ItemStack[] inventory = new ItemStack[INV_SIZE];

hence the crash you are getting.

This is one of the reasons to not use IInventory and use capabilities and things like ItemStackHandler.

Edited by V0idWa1k3r
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.