Jump to content

[1.8][Solved]Custom Inventory NBT Not Saving


Toxicshadow

Recommended Posts

Alright, so I fixed my issues from earlier. I'm so close to being done with this inventory work. But, after going through CoolAlias' tutorial on inventory, updates and all, my new custom slots don't save. You can place an item in whichever slot, but when you close and reopen the GUI, the item is deleted forever. So sad  :'( hehe.

 

So I did some searching, saw some previous posts on this issue but never found any solutions for my issue.

 

Inventory Class:

 

 

// I removed a bunch of the code because you shouldn't need most of it.

// Some variables
protected ItemStack[] inventory = new ItemStack[3];
public int slotsCount = 3;
private String tagName = Rot.MOD_ID + "TrinketInventory";

// Read/Write NBT Methods
public void writeToNBT(NBTTagCompound tagcompound)
{
	NBTTagList nbttaglist = new NBTTagList();
	for (int i = 0; i < this.getSizeInventory(); ++i)
	{
		if (this.getStackInSlot(i) != null)
		{
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte) i);
			this.getStackInSlot(i).writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}
	}
	// We're storing our items in a custom tag list using our 'tagName' from
	// above
	// to prevent potential conflicts
	tagcompound.setTag(tagName, nbttaglist);
}

public void readFromNBT(NBTTagCompound compound) {
	// now you must include the NBTBase type ID when getting the list;
	// NBTTagCompound's ID is 10
	NBTTagList items = compound.getTagList(tagName, compound.getId());
	for (int i = 0; i < items.tagCount(); ++i) {
		// tagAt(int) has changed to getCompoundTagAt(int)
		NBTTagCompound item = items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");
		if (slot >= 0 && slot < getSizeInventory()) {
			setInventorySlotContents(slot,
					ItemStack.loadItemStackFromNBT(item));
		}
	}
}

 

 

 

ExtendPlayer:

 

 

// Some Variables
public final RotTrinketInventory inventory = new RotTrinketInventory();

// get and register
public static final ExtendPlayer get(EntityPlayer player)
{
	try
	{
		return (ExtendPlayer) player.getExtendedProperties(EXT_PROP_NAME);
	}
	catch (Exception e)
	{
		return null;
	}
}

public static final void register(EntityPlayer player)
{
	player.registerExtendedProperties(ExtendPlayer.EXT_PROP_NAME, new ExtendPlayer(player));
}


// Load and Save NBT Data
@Override
public void loadNBTData(NBTTagCompound compound)
{
	// Here we fetch the unique tag compound we set for this class of
	// Extended Properties
	NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
	// Snipped out a bunch of other loads you don't need.
	this.inventory.readFromNBT(properties);

}

@Override
public void saveNBTData(NBTTagCompound compound)
{
	// We need to create a new tag compound that will save everything for
	// our Extended Properties
	NBTTagCompound properties = new NBTTagCompound();

	// Again I removed a ton of saves you don't need.

	this.inventory.writeToNBT(properties);

	compound.setTag(EXT_PROP_NAME, properties);
}

 

 

 

Should be all the code you'd need; I don't think it's a container issue, but idk.

 

Once more, any help is appreciated.  :-\

Link to comment
Share on other sites

That looks like it should be saving and loading correctly - are you providing a proper server-side gui element, i.e. a Container, when opening your gui? Is that container using the player's custom inventory field?

 

Show your IGuiHandler code as well as your Container constructor.

 

Container:

 

 

	public ContainerPseudoPlayer(InventoryPlayer realPlayerInventory, EntityPlayer player)
{
	int i;
        int j;
        
        // Inventory
	for (i = 0; i < 3; ++i)
        {
            for (j = 0; j < 9; ++j)
            {
                this.addSlotToContainer(new Slot(realPlayerInventory, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18));
            }
        }

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

	// Trinkets
	for (i = 0; i < 3; i++)
	{
		this.addSlotToContainer(new Slot(new RotTrinketInventory(), i, 26, 8 + i * 18));
	}

	// Armor, literally copy-pasted from container-player
	 for (i = 0; i < 4; ++i)
        {
            final int k = i;
            this.addSlotToContainer(new Slot(realPlayerInventory, realPlayerInventory.getSizeInventory() - 1 - i, 8, 8 + i * 18)
            {
                public int getSlotStackLimit()
                {
                    return 1;
                }
                public boolean isItemValid(ItemStack stack)
                {
                    if (stack == null) return false;
                    return stack.getItem().isValidArmor(stack, k, player);
                }
                @SideOnly(Side.CLIENT)
                public String getSlotTexture()
                {
                    return ItemArmor.EMPTY_SLOT_NAMES[k];
                }
            });
        }
}

 

 

 

GuiHandler:

 

 

public class GuiHandler implements IGuiHandler {
@Override
public Object getClientGuiElement(int guiId, EntityPlayer player, World world, int x,
		int y, int z) {
	// -snip-
	switch (guiId) {
		case 0 :
			// -snip-
		case 1 : {
			return new GuiClassSelection(player);
		}
		default :
			return null;
	}
}
// 1 is class Menu
@Override
public Object getServerGuiElement(int guiId, EntityPlayer player, World world, int x,
		int y, int z) {
	// -snip-

	switch (guiId) {
		case 0 :
			// -snip -
		case 1 : 
			return new ContainerPseudoPlayer(player.inventory, player);
		default :
			return null;
	}
}

}

Snipped out some code for a different gui :P

 

 

Link to comment
Share on other sites

Your issue is that you are not actually passing in the custom player inventory, only the vanilla inventory.

 

Your code should look like this:

return new ContainerCustomPlayer(player, player.inventory, ExtendedPlayer.get(player).inventory);

Note the last parameter is the custom inventory, which you need in your Container class to add the custom inventory slots:

// add one slot to the container for every slot in your custom inventory:
addSlotToContainer(new SlotCustom(inventoryCustom, 0, 80, );

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.