Jump to content

Recommended Posts

Posted

Ok so I'm writing a mod that adds a entity that has its own inventory like the player.  its inventory holds 27 Itemstacks like a chest. now the only way to open this GUI is to be ridding the entity(only why both the client and server can get the inventory instance right now).  now i use

	 
player.openGui(ShurtugalMod.instance, 1, this.worldObj,(int) posX,(int) posY,(int) posZ);

to open the GUI, i don't do anything with x y z because its not a TileEntity.  now that being said when i open the GUI with a keybinding i wrote that tell the server when its clicked from my entity file it opens but when i try to access any of the container slots over id of 45 it crashes giving this error saying that the size of this array-list is only 45

2013-01-10 15:15:17 [iNFO] [sTDERR] Caused by: java.lang.IndexOutOfBoundsException: Index: 56, Size: 45
2013-01-10 15:15:17 [iNFO] [sTDERR] 	at java.util.ArrayList.rangeCheck(ArrayList.java:571)
2013-01-10 15:15:17 [iNFO] [sTDERR] 	at java.util.ArrayList.get(ArrayList.java:349)
2013-01-10 15:15:17 [iNFO] [sTDERR] 	at net.minecraft.inventory.Container.slotClick(Container.java:198)

this is the line it crashes on

 var7 = (Slot)this.inventorySlots.get(par1);

so i decided to see what the size of this array-list was so the line before the crash i added a console print out of "this.inventorySlots.size()" and that returned 63(the number of slots in this gui) like it should be.  so to see if it was because i open it from an entity not a block and that was problem so i quickly mod the GUI open statement for a block i made and so it brings up the same GUI as the dragon like this

Player.openGui(ShurtugalMod.instance, 1, par1World,par2, par3, par4);

and it comes up and works without a problem. im not sure what i did wrong? if anyone could help that would be awesome.

heres all the relevant code, ask if you need anymore

 

IInventory class

 

package Shurtugal.common.entity.Dragon.Inventory;

import Shurtugal.common.entity.Dragon.EntityTameableDragon;
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;

public class DragonInventory implements IInventory
{
public ItemStack[] mainInventory = new ItemStack[27];

public EntityTameableDragon dragon;

public DragonInventory(EntityTameableDragon entity)
{
	dragon = entity;
}


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

@Override
public ItemStack getStackInSlot(int var1)
{
	if(var1 < mainInventory.length)
	{
		return mainInventory[var1];
	}
	return null;
}

@Override
public ItemStack decrStackSize(int var1, int var2)
{
        if (this.mainInventory[var1] != null)
        {
            ItemStack var3;

            if (this.mainInventory[var1].stackSize <= var2)
            {
                var3 = this.mainInventory[var1];
                this.mainInventory[var1] = null;
                return var3;
            }
            else
            {
                var3 = this.mainInventory[var1].splitStack(var2);

                if (this.mainInventory[var1].stackSize == 0)
                {
                    this.mainInventory[var1] = null;
                }

                return var3;
            }
        }
        else
        {
            return null;
        }
}

@Override
public ItemStack getStackInSlotOnClosing(int var1)
{
        if (this.mainInventory[var1] != null)
        {
            ItemStack var2 = this.mainInventory[var1];
            this.mainInventory[var1] = null;
            return var2;
        }
        else
        {
            return null;
        }
}

@Override
public void setInventorySlotContents(int var1, ItemStack var2)
{
        this.mainInventory[var1] = var2;

        if (var2 != null && var2.stackSize > this.getInventoryStackLimit())
        {
            var2.stackSize = this.getInventoryStackLimit();
        }
}

@Override
public String getInvName()
{
	return "Shurtugal.DragonGui";
}

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

@Override
public void onInventoryChanged()
{
	//who knows whats is supposed to do here;
}

@Override
public boolean isUseableByPlayer(EntityPlayer var1)
{
	return dragon.getOwnerName() == var1.username;
}

@Override
public void openChest()
{
	// TODO Auto-generated method stub

}

@Override
public void closeChest()
{
	// TODO Auto-generated method stub

}
    public NBTTagList writeToNBT(NBTTagList par1NBTTagList)
    {
        int var2;
        NBTTagCompound var3;

        for (var2 = 0; var2 < this.mainInventory.length; ++var2)
        {
            if (this.mainInventory[var2] != null)
            {
                var3 = new NBTTagCompound();
                var3.setByte("Slot", (byte)var2);
                this.mainInventory[var2].writeToNBT(var3);
                par1NBTTagList.appendTag(var3);
            }
        }
        return par1NBTTagList;
    }
    /**
     * Reads from the given tag list and fills the slots in the inventory with the correct items.
     */
    public void readFromNBT(NBTTagList par1NBTTagList)
    {
        this.mainInventory = new ItemStack[27];

        for (int var2 = 0; var2 < par1NBTTagList.tagCount(); ++var2)
        {
            NBTTagCompound var3 = (NBTTagCompound)par1NBTTagList.tagAt(var2);
            int var4 = var3.getByte("Slot") & 255;
            ItemStack var5 = ItemStack.loadItemStackFromNBT(var3);

            if (var5 != null)
            {
                if (var4 >= 0 && var4 < this.mainInventory.length)
                {
                    this.mainInventory[var4] = var5;
                }

            }
        }
    }
}

 

 

Container Class

 

package Shurtugal.common.entity.Dragon.Inventory;

import Shurtugal.common.entity.Dragon.EntityTameableDragon;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class ContainerDragon extends Container
{
    private final EntityPlayer thePlayer;
public ContainerDragon(EntityTameableDragon dragon, EntityPlayer player)
{
	thePlayer = player;
	DragonInventory Inventory = dragon.inventory;

	InventoryPlayer par1InventoryPlayer = thePlayer.inventory;

        int var4;
        int var5;

        for (var4 = 0; var4 < 3; ++var4)
        {
            for (var5 = 0; var5 < 9; ++var5)
            {
                this.addSlotToContainer(new Slot(Inventory, var5 + var4 * 9, 8 + var5 * 18, 82 + var4 * 18));
            }
        }

        for (var4 = 0; var4 < 3; ++var4)
        {
            for (var5 = 0; var5 < 9; ++var5)
            {
                this.addSlotToContainer(new Slot(par1InventoryPlayer, var5 + var4 * 9 + 9, 8 + var5 * 18, 150 + var4 * 18 ));
            }
        }

        for (var4 = 0; var4 < 9; ++var4)
        {
            this.addSlotToContainer(new Slot(par1InventoryPlayer, var4, 8 + var4 * 18, 208));
        }

}
    /**
     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
     */
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    {
        ItemStack var3 = null;
        Slot var4 = (Slot)this.inventorySlots.get(par2);

        if (var4 != null && var4.getHasStack())
        {
            ItemStack var5 = var4.getStack();
            var3 = var5.copy();

            if (par2 < 3 * 9)
            {
                if (!this.mergeItemStack(var5, 3 * 9, this.inventorySlots.size(), true))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(var5, 0, 3 * 9, false))
            {
                return null;
            }

            if (var5.stackSize == 0)
            {
                var4.putStack((ItemStack)null);
            }
            else
            {
                var4.onSlotChanged();
            }
        }

        return var3;
    }

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

@Override
    public ItemStack slotClick(int par1, int par2, int par3, EntityPlayer par4EntityPlayer)
    {
        ItemStack var5 = null;
        InventoryPlayer var6 = par4EntityPlayer.inventory;
        Slot var7;
        ItemStack var8;
        int var10;
        ItemStack var11;

        if ((par3 == 0 || par3 == 1) && (par2 == 0 || par2 == 1))
        {
            if (par1 == -999)
            {
                if (var6.getItemStack() != null && par1 == -999)
                {
                    if (par2 == 0)
                    {
                        par4EntityPlayer.dropPlayerItem(var6.getItemStack());
                        var6.setItemStack((ItemStack)null);
                    }

                    if (par2 == 1)
                    {
                        par4EntityPlayer.dropPlayerItem(var6.getItemStack().splitStack(1));

                        if (var6.getItemStack().stackSize == 0)
                        {
                            var6.setItemStack((ItemStack)null);
                        }
                    }
                }
            }
            else if (par3 == 1)
            {
                var7 = (Slot)this.inventorySlots.get(par1);

                if (var7 != null && var7.canTakeStack(par4EntityPlayer))
                {
                    var8 = this.transferStackInSlot(par4EntityPlayer, par1);

                    if (var8 != null)
                    {
                        int var12 = var8.itemID;
                        var5 = var8.copy();

                        if (var7 != null && var7.getStack() != null && var7.getStack().itemID == var12)
                        {
                            this.retrySlotClick(par1, par2, true, par4EntityPlayer);
                        }
                    }
                }
            }
            else
            {
                if (par1 < 0)
                {
                    return null;
                }

                System.out.println(this.inventorySlots.size());
                var7 = (Slot)this.inventorySlots.get(par1);

                if (var7 != null)
                {
                    var8 = var7.getStack();
                    ItemStack var13 = var6.getItemStack();

                    if (var8 != null)
                    {
                        var5 = var8.copy();
                    }

                    if (var8 == null)
                    {
                        if (var13 != null && var7.isItemValid(var13))
                        {
                            var10 = par2 == 0 ? var13.stackSize : 1;

                            if (var10 > var7.getSlotStackLimit())
                            {
                                var10 = var7.getSlotStackLimit();
                            }

                            var7.putStack(var13.splitStack(var10));

                            if (var13.stackSize == 0)
                            {
                                var6.setItemStack((ItemStack)null);
                            }
                        }
                    }
                    else if (var7.canTakeStack(par4EntityPlayer))
                    {
                        if (var13 == null)
                        {
                            var10 = par2 == 0 ? var8.stackSize : (var8.stackSize + 1) / 2;
                            var11 = var7.decrStackSize(var10);
                            var6.setItemStack(var11);

                            if (var8.stackSize == 0)
                            {
                                var7.putStack((ItemStack)null);
                            }

                            var7.onPickupFromSlot(par4EntityPlayer, var6.getItemStack());
                        }
                        else if (var7.isItemValid(var13))
                        {
                            if (var8.itemID == var13.itemID && var8.getItemDamage() == var13.getItemDamage() && ItemStack.areItemStackTagsEqual(var8, var13))
                            {
                                var10 = par2 == 0 ? var13.stackSize : 1;

                                if (var10 > var7.getSlotStackLimit() - var8.stackSize)
                                {
                                    var10 = var7.getSlotStackLimit() - var8.stackSize;
                                }

                                if (var10 > var13.getMaxStackSize() - var8.stackSize)
                                {
                                    var10 = var13.getMaxStackSize() - var8.stackSize;
                                }

                                var13.splitStack(var10);

                                if (var13.stackSize == 0)
                                {
                                    var6.setItemStack((ItemStack)null);
                                }

                                var8.stackSize += var10;
                            }
                            else if (var13.stackSize <= var7.getSlotStackLimit())
                            {
                                var7.putStack(var13);
                                var6.setItemStack(var8);
                            }
                        }
                        else if (var8.itemID == var13.itemID && var13.getMaxStackSize() > 1 && (!var8.getHasSubtypes() || var8.getItemDamage() == var13.getItemDamage()) && ItemStack.areItemStackTagsEqual(var8, var13))
                        {
                            var10 = var8.stackSize;

                            if (var10 > 0 && var10 + var13.stackSize <= var13.getMaxStackSize())
                            {
                                var13.stackSize += var10;
                                var8 = var7.decrStackSize(var10);

                                if (var8.stackSize == 0)
                                {
                                    var7.putStack((ItemStack)null);
                                }

                                var7.onPickupFromSlot(par4EntityPlayer, var6.getItemStack());
                            }
                        }
                    }

                    var7.onSlotChanged();
                }
            }
        }
        else if (par3 == 2 && par2 >= 0 && par2 < 9)
        {
            var7 = (Slot)this.inventorySlots.get(par1);

            if (var7.canTakeStack(par4EntityPlayer))
            {
                var8 = var6.getStackInSlot(par2);
                boolean var9 = var8 == null || var7.inventory == var6 && var7.isItemValid(var8);
                var10 = -1;

                if (!var9)
                {
                    var10 = var6.getFirstEmptyStack();
                    var9 |= var10 > -1;
                }

                if (var7.getHasStack() && var9)
                {
                    var11 = var7.getStack();
                    var6.setInventorySlotContents(par2, var11);

                    if ((var7.inventory != var6 || !var7.isItemValid(var8)) && var8 != null)
                    {
                        if (var10 > -1)
                        {
                            var6.addItemStackToInventory(var8);
                            var7.decrStackSize(var11.stackSize);
                            var7.putStack((ItemStack)null);
                            var7.onPickupFromSlot(par4EntityPlayer, var11);
                        }
                    }
                    else
                    {
                        var7.decrStackSize(var11.stackSize);
                        var7.putStack(var8);
                        var7.onPickupFromSlot(par4EntityPlayer, var11);
                    }
                }
                else if (!var7.getHasStack() && var8 != null && var7.isItemValid(var8))
                {
                    var6.setInventorySlotContents(par2, (ItemStack)null);
                    var7.putStack(var8);
                }
            }
        }
        else if (par3 == 3 && par4EntityPlayer.capabilities.isCreativeMode && var6.getItemStack() == null && par1 >= 0)
        {
            var7 = (Slot)this.inventorySlots.get(par1);

            if (var7 != null && var7.getHasStack())
            {
                var8 = var7.getStack().copy();
                var8.stackSize = var8.getMaxStackSize();
                var6.setItemStack(var8);
            }
        }

        return var5;
    }

}

 

 

GUI Class(Although this really isnt needed to find the bug, i think?)

 

package Shurtugal.client.gui;

import org.lwjgl.opengl.GL11;

import Shurtugal.common.entity.Dragon.EntityTameableDragon;
import Shurtugal.common.entity.Dragon.Inventory.ContainerDragon;
import Shurtugal.common.entity.Dragon.Inventory.DragonInventory;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.util.StatCollector;

public class GuiDragonInventory extends GuiContainer
{


public GuiDragonInventory(EntityTameableDragon dragon ,EntityPlayer player)
{
	super(new ContainerDragon(dragon, player));
	this.xSize = 175;
	this.ySize = 231;
}

    /**
     * Draw the foreground layer for the GuiContainer (everything in front of the items)
     */
    protected void drawGuiContainerForegroundLayer(int par1, int par2)
    {
        this.fontRenderer.drawString(StatCollector.translateToLocal("Shurtugal.DragonGui"), 60, 6, 4210752);
        this.fontRenderer.drawString(StatCollector.translateToLocal("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
    }
@Override
protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3)
{
        int var4 = this.mc.renderEngine.getTexture("/Shurtugal/Textures/gui/DragonGui.png");
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.mc.renderEngine.bindTexture(var4);
        int var5 = (this.width - this.xSize) / 2;
        int var6 = (this.height - this.ySize) / 2;
        this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);

}

}

 

 

GuiHandler

 

/**
* @Author Iamshortman
*/
package Shurtugal.client.gui;


import cpw.mods.fml.common.network.IGuiHandler;
import Shurtugal.IronFurnace.ContainerIronFurnace;
import Shurtugal.IronFurnace.TileEntityIronFurnace;
import Shurtugal.common.entity.Dragon.EntityBaseDragon;
import Shurtugal.common.entity.Dragon.EntityTameableDragon;
import Shurtugal.common.entity.Dragon.Inventory.ContainerDragon;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ContainerChest;
import net.minecraft.src.*;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class GuiHandler implements IGuiHandler
{

@Override
public Object getServerGuiElement(int id, EntityPlayer player, World world, int x, int y, int z)
{
	//Dont worrie about this one
	TileEntity tile_entity = world.getBlockTileEntity(x, y, z);

	if (id == 0 && tile_entity instanceof TileEntityIronFurnace)
	{

		return new ContainerIronFurnace(player.inventory, (TileEntityIronFurnace) tile_entity);
	}

	//This is the one i care about
	else if (id == 1)
	{
            Entity entity = player.ridingEntity;
            
            if (entity == null || !(entity instanceof EntityTameableDragon)) 
            {
                return null;
            }
            EntityTameableDragon dragon = (EntityTameableDragon) entity;
            
		return new ContainerDragon(dragon, player);
	}

	return null;
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World world, int x, int y, int z)
{
	TileEntity tile_entity = world.getBlockTileEntity(x, y, z);


	if (id == 0 && tile_entity instanceof TileEntityIronFurnace)
	{
		return new GuiIronFurnace(player.inventory, (TileEntityIronFurnace) tile_entity);
	}

	//This is the one i care about
	else if(id == 1)
	{
            Entity entity = player.ridingEntity;
            
            if (entity == null || !(entity instanceof EntityTameableDragon)) 
            {
                return null;
            }

		return new GuiDragonInventory((EntityTameableDragon) entity, player);
	}

	return null;
}
}

 

 

Anything else you need just ask

 

 

Thank you for time.

 

Posted

Heres the full crash Report

 

---- Minecraft Crash Report ----

// Hey, that tickles! Hehehe!

 

Time: 1/10/13 5:55 PM

Description: Ticking memory connection

 

java.lang.IndexOutOfBoundsException: Index: 58, Size: 45

at java.util.ArrayList.rangeCheck(ArrayList.java:571)

at java.util.ArrayList.get(ArrayList.java:349)

at net.minecraft.inventory.Container.slotClick(Container.java:198)

at net.minecraft.network.NetServerHandler.handleWindowClick(NetServerHandler.java:906)

at net.minecraft.network.packet.Packet102WindowClick.processPacket(Packet102WindowClick.java:46)

at net.minecraft.network.MemoryConnection.processReadPackets(MemoryConnection.java:80)

at net.minecraft.network.NetServerHandler.networkTick(NetServerHandler.java:136)

at net.minecraft.network.NetworkListenThread.networkTick(NetworkListenThread.java:57)

at net.minecraft.server.integrated.IntegratedServerListenThread.networkTick(IntegratedServerListenThread.java:108)

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:702)

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:598)

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:123)

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:497)

at net.minecraft.server.ThreadMinecraftServer.run(ThreadMinecraftServer.java:16)

 

 

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

---------------------------------------------------------------------------------------

 

-- System Details --

Details:

Minecraft Version: 1.4.6

Operating System: Linux (amd64) version 3.2.0-34-generic

Java Version: 1.6.0_24, Sun Microsystems Inc.

Java VM Version: OpenJDK 64-Bit Server VM (mixed mode), Sun Microsystems Inc.

Memory: 840518648 bytes (801 MB) / 1065025536 bytes (1015 MB) up to 1065025536 bytes (1015 MB)

JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M

AABB Pool Size: 1055 (59080 bytes; 0 MB) allocated, 890 (49840 bytes; 0 MB) used

Suspicious classes: FML and Forge are installed

IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0

FML: MCP v7.25 FML v4.6.12.511 Minecraft Forge 6.5.0.467 4 mods loaded, 4 mods active

mcp [Minecraft Coder Pack] (minecraft.jar) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available

FML [Forge Mod Loader] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available

Forge [Minecraft Forge] (coremods) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available

Surtugal [surtugal] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available

Profiler Position: N/A (disabled)

Vec3 Pool Size: 169 (9464 bytes; 0 MB) allocated, 116 (6496 bytes; 0 MB) used

Player Count: 1 / 8; [EntityPlayerMP['iamshortman'/56, l='New World', x=-380.83, y=5.70, z=1335.68]]

Type: Integrated Server (map_client.txt)

Is Modded: Definitely; Client brand changed to 'forge,fml'

 

 

Also i forgot to say im using Forge version 6.5.0.467 for minecraft 1.4.6

Posted

I found the problem, when i opened it vie dragon i was opening it from server side when all other chests and containers open from client side. Still as always thank you.

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.