Jump to content

Recommended Posts

Posted

I have made an item with its own inventory, and so far the GUI opens with its own slots correctly when you right click and you can put items in the slots. If you close the inventory and re-open it, all the items are gone.

I know this is an NBT issue, and I've had numerous problems with NBT before (probably just me).

 

Here is my inventory code:

 

public class BagInventory implements IInventory
{
private String NAME = "Hotbar Bag";
public static final int SIZE = 18; // Equal to 2 rows

private final ItemStack invItem;
ItemStack[] inventory = new ItemStack[size];

public BagInventory(ItemStack itemStack)
{
	this.invItem = itemStack;

	// If for any reason the itemstack has no NBT data
	if (!itemStack.hasTagCompound())
	{
		itemStack.setTagCompound(new NBTTagCompound());
	}

	// Read inventory from NBT
	readFromNBT(itemStack.getTagCompound());
}

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

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

@Override
public ItemStack decrStackSize(int slot, int amount)
{
	ItemStack stack = getStackInSlot(slot);

	if (stack != null)
	{
		if (stack.stackSize > amount)
		{
			stack = stack.splitStack(amount);
			if (stack.stackSize == 0)
			{
				setInventorySlotContents(slot, null);
			}
		}
		else
		{
			setInventorySlotContents(slot, null);
		}
		this.markDirty();
	}
	return stack;
}

@Override
public ItemStack getStackInSlotOnClosing(int slot)
{
	ItemStack stack = getStackInSlot(slot);

	if (stack != null)
	{
		setInventorySlotContents(slot, null);
	}
	return stack;
}

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

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

	this.markDirty();
}

@Override
public String getInventoryName()
{
	return this.NAME;
}

@Override
public boolean hasCustomInventoryName()
{
	return NAME.length() > 0;
}

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

@Override
public void markDirty()
{
	for(int i = 0; i < getSizeInventory(); ++i)
	{
		if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0)
		{
			inventory[i] = null;
		}
	}

	writeToNBT(invItem.getTagCompound());
}

@Override
public boolean isUseableByPlayer(EntityPlayer entityPlayer)
{
	return true;
}

@Override
public void openInventory()
{
	this.readFromNBT(this.invItem.getTagCompound());
}

@Override
public void closeInventory()
{
	this.writeToNBT(this.invItem.getTagCompound());
}

@Override
public boolean isItemValidForSlot(int slot, ItemStack itemStack)
{
	return !(itemStack.getItem() instanceof ItemBag);
}

public void readFromNBT(NBTTagCompound compound)
{
	NBTTagList items = compound.getTagList("ItemBag", NBT.TAG_COMPOUND);

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

		// Double-check has loaded correctly.
		if (slot >= 0 && slot < getSizeInventory())
		{
			inventory[slot] = ItemStack.loadItemStackFromNBT(item);
		}
	}
}

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

	for(int i = 0; i < getSizeInventory(); i++)
	{
		// Don't write empty slots
		if (getStackInSlot(i) != null)
		{
			NBTTagCompound item = new NBTTagCompound();
			item.setInteger("Slot", i);

			getStackInSlot(i).writeToNBT(item);

			items.appendTag(item);
		}
	}
	compound.setTag("ItemInventory", items);
}
}

 

 

If you need to see any other code, I will upload it. Any help is greatly appreciated.

 

I have no idea what I'm doing.

Posted

You need to do super.readFromNBT(combound) and super.writeToNBT(combound)

 

My class does not extend any class and the IInventory interface does not include these methods. There is no such thing.

I have no idea what I'm doing.

Posted

Oh - than this is the problem. Make the IInventory thing in your ItemClass i would say.

 

I have.

 

I don't mean to be rude, but have you actually looked at my code?

I have no idea what I'm doing.

Posted

 

Haha that's the tutorial I've followed to get as far as I have. For some reason the NBT is not working though. I'll have a look again.

 

Anybody who does know what's gone wrong, an answer is still greatly appreciated.

I have no idea what I'm doing.

Posted

You made a simple rookie mistake:

// Read:
NBTTagList items = compound.getTagList("ItemBag", NBT.TAG_COMPOUND);

// Write:
compound.setTag("ItemInventory", items);

Look carefully... what's different? :P

 

Also, you don't need to read/write in open/closeInventory methods - you've already read the inventory in the constructor which is called when opening the inventory, and any time something changes we already write, so those two calls are superfluous. I just leave my implementations empty.

Posted

You made a simple rookie mistake:

// Read:
NBTTagList items = compound.getTagList("ItemBag", NBT.TAG_COMPOUND);

// Write:
compound.setTag("ItemInventory", items);

Look carefully... what's different? :P

 

Also, you don't need to read/write in open/closeInventory methods - you've already read the inventory in the constructor which is called when opening the inventory, and any time something changes we already write, so those two calls are superfluous. I just leave my implementations empty.

 

Derp, forgot to change a couple of things when copy/pasting larger bits of code from your tutorial (thanks for that, by the way - very useful). I have removed the open/close code after testing it and finding it does nothing.

 

Thanks.

I have no idea what I'm doing.

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.