Jump to content

[UNSOLVED]java.lang.IndexOutOfBoundsException


saxon564

Recommended Posts

I am having a problem where some of the lots of the inventory aren't registering correctly and the ones that do copy the entire stack and when the Gui is closed it doesn't save the inventory. I have been trying to figure this out for a while with no avail.

 

Item Code:

 

package mods.betterFarming.items;

import mods.betterFarming.betterFarming;
import mods.betterFarming.inventory.inventorySeedPouch;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class itemSeedPouch extends Item {
//Big thanks to Eydamos for his backpack mod open source

public itemSeedPouch(int id) {
	super(id);
	setMaxStackSize(1);
	this.setCreativeTab(CreativeTabs.tabMisc);
}

public void updateIcons(IconRegister iconreg){	
	iconIndex = iconreg.registerIcon("betterFarming:seedPouch");
}

    @Override
    public ItemStack onItemRightClick(ItemStack is, World world, EntityPlayer player) {
        // if world.isRemote than we are on the client side
        if(world.isRemote) {
        	player.openGui(betterFarming.instance, 1, player.worldObj, 0, 0, 0);
        	return is;
        }
        return is;
    }
    
    @Override
    public String getItemDisplayName(ItemStack itemstack) {
        return "Seed Pouch";
    }

    public static IInventory getSeedPouchInv(EntityPlayer player) {
    	IInventory InventorySeedPouch = null;
        ItemStack seedPouch = player.getCurrentEquippedItem();
        
        if(seedPouch != null && seedPouch.getItem() instanceof itemSeedPouch) {
        	InventorySeedPouch = new inventorySeedPouch(player, seedPouch);
        }
        return InventorySeedPouch;
    }

}

 

 

Container Code:

 

package mods.betterFarming.inventory;

import mods.betterFarming.items.itemSeedPouch;
import mods.betterFarming.misc.NBTUtil;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class containerSeedPouch extends Container {
//Big thanks to Eydamos for his backpack mod open source

    private int numRows;
    private IInventory lowerChestInventory;

    public containerSeedPouch(IInventory playerInventory, IInventory seedPouchInventory, ItemStack seedPouch) {
    	this.numRows = seedPouchInventory.getSizeInventory() / 9;
        seedPouchInventory.openChest();
        int i = (this.numRows - 4) * 18;
        int j;
        int k;

        for (j = 0; j < this.numRows; ++j)
        {
            for (k = 0; k < 9; ++k)
            {
                this.addSlotToContainer(new Slot(seedPouchInventory, k + j * 9, 8 + k * 18, 18 + j * 18));
            }
        }

        for (j = 0; j < 3; ++j)
        {
            for (k = 0; k < 9; ++k)
            {
                this.addSlotToContainer(new Slot(playerInventory, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
            }
        }

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

    /**
     * True is the current equipped item is the opened item otherwise false.
     */
    @Override
    public boolean canInteractWith(EntityPlayer player) {
    	return this.lowerChestInventory.isUseableByPlayer(player);
    }

    /**
     * Called when a player shift-clicks on a slot.
     */
    @Override
    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int slotPos) {
        ItemStack returnStack = null;
        Slot slot = (Slot) inventorySlots.get(slotPos);

        if(slot != null && slot.getHasStack()) {
            ItemStack itemStack = slot.getStack();
            if(itemStack.getItem() instanceof itemSeedPouch) {
                return returnStack;
            }
            returnStack = itemStack.copy();

            if(slotPos < numRows * 9) {
                if(!mergeItemStack(itemStack, numRows * 9, inventorySlots.size(), true)) {
                    return null;
                }
            } else if(!mergeItemStack(itemStack, 0, numRows * 9, false)) {
                return null;
            }

            if(itemStack.stackSize == 0) {
                slot.putStack((ItemStack) null);
            } else {
                slot.onSlotChanged();
            }
        }

        return returnStack;
    }

    @Override
    public void onCraftGuiClosed(EntityPlayer player) {
        super.onCraftGuiClosed(player);
        this.lowerChestInventory.closeChest();
    }
    
    public IInventory getLowerChestInventory()
    {
        return this.lowerChestInventory;
    }
}

 

 

Slot Code:

 

package mods.betterFarming.inventory;

import mods.betterFarming.items.itemSeedPouch;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

public class slotSeedPouch extends Slot {
//Big thanks to Eydamos for his backpack mod open source

    public slotSeedPouch(IInventory inventory, int slotIndex, int xPos, int yPos) {
        super(inventory, slotIndex, xPos, yPos);
    }

    /**
     * Check if the stack is a valid item for this slot. Always true beside for
     * backpacks.
     */
    @Override
    public boolean isItemValid(ItemStack is) {
        return is != null && is.getItem() instanceof itemSeedPouch ? false : true;
    }
}

 

 

Gui Code:

 

package mods.betterFarming.gui;

import mods.betterFarming.inventory.containerSeedPouch;
import mods.betterFarming.misc.NBTUtil;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.util.StatCollector;

import org.lwjgl.opengl.GL11;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class GUISeedPouch extends GuiContainer {
//Big thanks to Eydamos for his backpack mod open source

    private IInventory upperInventory;
    private IInventory lowerInventory;
    private int inventoryRows;

    public GUISeedPouch(IInventory inventoryPlayer, IInventory inventorySeedPouch) {
        super(new containerSeedPouch(inventoryPlayer, inventorySeedPouch, null));
        upperInventory = inventorySeedPouch;
        lowerInventory = inventoryPlayer;
        short short1 = 222;
        int i = short1 - 108;
        inventoryRows = inventorySeedPouch.getSizeInventory() / 9;
        ySize = i + inventoryRows * 18;
    }

    /**
     * Draw the foreground layer for the GuiContainer (everything in front of
     * the items)
     */
    @Override
    protected void drawGuiContainerForegroundLayer(int par1, int par2) {
        fontRenderer.drawString(StatCollector.translateToLocal(upperInventory.getInvName()), 8, 6, 4210752);
        fontRenderer.drawString(StatCollector.translateToLocal(lowerInventory.getInvName()), 8, ySize - 96 + 2, 4210752);
    }

    /**
     * Draw the background layer for the GuiContainer (everything behind the
     * items)
     */
    @Override
    protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        mc.renderEngine.bindTexture("/gui/container.png");
        int var5 = (width - xSize) / 2;
        int var6 = (height - ySize) / 2;
        drawTexturedModalRect(var5, var6, 0, 0, xSize, inventoryRows * 18 + 17);
        drawTexturedModalRect(var5, var6 + inventoryRows * 18 + 17, 0, 126, xSize, 96);
    }

    @Override
    public void onGuiClosed() {
        super.onGuiClosed();
        if(mc.thePlayer != null) {
            EntityPlayer player = mc.thePlayer;

            ItemStack itemStack = player.getCurrentEquippedItem();
            if(itemStack != null) {
                if(NBTUtil.hasTag(itemStack, "seedPouch")) {
                    NBTUtil.removeTag(itemStack, "seePouch");
                }
            }
        }
    }
}

 

 

Gui Handler Code:

 

package mods.betterFarming.gui;

import mods.betterFarming.inventory.containerSeedPouch;
import mods.betterFarming.items.itemSeedPouch;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;

public class guiHandler implements IGuiHandler {
        //returns an instance of the Container you made earlier
        @Override
        public Object getServerGuiElement(int id, EntityPlayer player, World world,
                        int x, int y, int z) {
        	return new containerSeedPouch(player.inventory, itemSeedPouch.getSeedPouchInv(player), player.getCurrentEquippedItem());
        }

        //returns an instance of the Gui you made earlier
        @Override
        public Object getClientGuiElement(int id, EntityPlayer player, World world,
                        int x, int y, int z) {
        	return new GUISeedPouch(player.inventory, itemSeedPouch.getSeedPouchInv(player));
        }
}

 

 

inventory code:

 

package mods.betterFarming.inventory;

import mods.betterFarming.items.itemSeedPouch;
import mods.betterFarming.misc.NBTUtil;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

public class inventorySeedPouch extends InventoryBasic {
//Big thanks to Eydamos for his backpack mod open source

    // the title of the SeedPouch
    private String inventoryTitle;

    // an instance of the player to get the inventory
    private EntityPlayer playerEntity;
    // the original ItemStack to compare with the player inventory
    private ItemStack originalIS;

    // if class is reading from NBT tag
    private boolean reading = false;

    /**
     * Takes a player and an ItemStack.
     * 
     * @param player
     *            The player which has the SeedPouch.
     * @param is
     *            The ItemStack which holds the SeedPouch.
     */
    public inventorySeedPouch(EntityPlayer player, ItemStack is) {
        super("", false, getInventorySize(is));

        playerEntity = player;
        originalIS = is;

        // check if inventory exists if not create one
        if(!hasInventory()) {
            createInventory();
        }

        loadInventory();
    }

    /**
     * Is called whenever something is changed in the inventory.
     */
    @Override
    public void onInventoryChanged() {
        super.onInventoryChanged();
        // if reading from NBT don't write
        if(!reading) {
            saveInventory();
        }
    }

    /**
     * This method is called when the chest opens the inventory. It loads the
     * content of the inventory and its title.
     */
    @Override
    public void openChest() {
        loadInventory();
    }

    /**
     * This method is called when the chest closes the inventory. It then throws
     * out every SeedPouch which is inside the SeedPouch and saves the inventory.
     */
    @Override
    public void closeChest() {
        saveInventory();
    }

    /**
     * Returns the name of the inventory.
     */
    @Override
    public String getInvName() {
        return inventoryTitle;
    }

    // ***** custom methods which are not in IInventory *****
    /**
     * Returns the size of the inventory based on the ItemStack.
     * 
     * @param is
     *            The ItemStack to check for the size.
     * @return The number of slots the inventory has.
     */
    protected static int getInventorySize(ItemStack is) {
        return 9 * 3;
    }

    /**
     * Returns if an Inventory is saved in the NBT.
     * 
     * @return True when the NBT is not null and the NBT has key "Inventory"
     *         otherwise false.
     */
    private boolean hasInventory() {
        return NBTUtil.hasTag(originalIS, "Inventory");
    }

    /**
     * Creates the Inventory Tag in the NBT with an empty inventory.
     */
    private void createInventory() {
        setInvName(originalIS.getDisplayName());
        writeToNBT();
    }

    /**
     * Sets the name of the inventory.
     * 
     * @param name
     *            The new name.
     */
    public void setInvName(String name) {
        inventoryTitle = name;
    }

    /**
     * Searches the SeedPouch in players inventory and saves NBT data in it.
     */
    private void setNBT() {
        if(!NBTUtil.getBoolean(originalIS, "seedPouchOpen")) {
            for(ItemStack itemStack : playerEntity.inventory.mainInventory) {
                if(itemStack != null && itemStack.getItem() instanceof itemSeedPouch) {
                    if(itemStack.isItemEqual(originalIS)) {
                        itemStack.setTagCompound(originalIS.getTagCompound());
                    }
                }
            }
        }
    }

    /**
     * If there is no inventory create one. Then load the content and title of
     * the inventory from the NBT
     */
    public void loadInventory() {
        readFromNBT();
    }

    /**
     * Saves the actual content of the inventory to the NBT.
     */
    public void saveInventory() {
        writeToNBT();
        setNBT();
    }

    /**
     * Writes a NBT Node with inventory.
     * 
     * @param outerTag
     *            The NBT Node to write to.
     * @return The written NBT Node.
     */
    private void writeToNBT() {
        // save name in display->Name
        NBTTagCompound name = new NBTTagCompound();
        name.setString("Name", getInvName());
        NBTUtil.setCompoundTag(originalIS, "display", name);

        NBTTagList itemList = new NBTTagList();
        for(int i = 0; i < getSizeInventory(); i++) {
            if(getStackInSlot(i) != null) {
                NBTTagCompound slotEntry = new NBTTagCompound();
                slotEntry.setByte("Slot", (byte) i);
                getStackInSlot(i).writeToNBT(slotEntry);
                itemList.appendTag(slotEntry);
            }
        }
        // save content in Inventory->Items
        NBTTagCompound inventory = new NBTTagCompound();
        inventory.setTag("Items", itemList);
        NBTUtil.setCompoundTag(originalIS, "Inventory", inventory);
        // return outerTag;
    }

    /**
     * Reads the inventory from a NBT Node.
     * 
     * @param outerTag
     *            The NBT Node to read from.
     */
    private void readFromNBT() {
        reading = true;
        // TODO for backwards compatibility
        if(NBTUtil.getCompoundTag(originalIS, "Inventory").hasKey("title")) {
            setInvName(NBTUtil.getCompoundTag(originalIS, "Inventory").getString("title"));
        } else {
            setInvName(NBTUtil.getCompoundTag(originalIS, "display").getString("Name"));
        }

        NBTTagList itemList = NBTUtil.getCompoundTag(originalIS, "Inventory").getTagList("Items");
        for(int i = 0; i < itemList.tagCount(); i++) {
            NBTTagCompound slotEntry = (NBTTagCompound) itemList.tagAt(i);
            int j = slotEntry.getByte("Slot") & 0xff;

            if(j >= 0 && j < getSizeInventory()) {
                setInventorySlotContents(j, ItemStack.loadItemStackFromNBT(slotEntry));
            }
        }
        reading = false;
    }
}

crash report:

 

 

---- Minecraft Crash Report ----

// On the bright side, I bought you a teddy bear!

 

Time: 4/25/13 4:17 PM

Description: Ticking memory connection

 

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

at java.util.ArrayList.rangeCheck(Unknown Source)

at java.util.ArrayList.get(Unknown Source)

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

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

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

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

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

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

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

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

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

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

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

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

Operating System: Windows 7 (amd64) version 6.1

Java Version: 1.7.0_07, Oracle Corporation

Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation

Memory: 883360600 bytes (842 MB) / 1038876672 bytes (990 MB) up to 1038876672 bytes (990 MB)

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

AABB Pool Size: 2407 (134792 bytes; 0 MB) allocated, 2252 (126112 bytes; 0 MB) used

Suspicious classes: FML and Forge are installed

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

FML: MCP v7.44 FML v5.1.8.611 Minecraft Forge 7.7.1.611 4 mods loaded, 4 mods active

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

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

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

betterFarming [better Farming] (bin) Unloaded->Constructed->Pre-initialized->Initialized->Post-initialized->Available->Available->Available->Available

Profiler Position: N/A (disabled)

Vec3 Pool Size: 1061 (59416 bytes; 0 MB) allocated, 1030 (57680 bytes; 0 MB) used

Player Count: 1 / 8; [EntityPlayerMP['Player768'/217, l='New World', x=37.30, y=23.00, z=25.72]]

Type: Integrated Server (map_client.txt)

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

 

Link to comment
Share on other sites

Its become evedent that no one seems to know how to fix this since only one person has made a suggestion but no one else has. Its sad that i cant even go to a community of modders to get an answer for this. Dies, thanks for at least trying.

Link to comment
Share on other sites

  • 1 month later...

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.