Jump to content

Recommended Posts

Posted

Hey guys,

I started creating my own backpack and stumpled upon a problem.

I created the Inventory for it (not posting all methods because unnecessary)

Now I wonder. I can save and read it from nbt. Do I need to create the inventory from the nbt every time I want to access it, and save it after I created it? If not where do I store it? Since I cant store it in the item class im confused..

 

public class InventoryBackpack implements IInventory{

public static InventoryBackpack readFromNBT(NBTTagCompound compound)
{
	InventoryBackpack backpack = new InventoryBackpack();
	NBTTagList list = compound.getTagList(LIST_TAG, 10);

	if(list!=null)
	{	
		for( byte b=0; b<list.tagCount(); b++)
		{
			NBTTagCompound comp = (NBTTagCompound) list.get(b);
			backpack.setInventorySlotContents(comp.getByte("id"), ItemStack.loadItemStackFromNBT(comp));
		}
	}	
	return backpack;
}
private static final String LIST_TAG="BACKPACK_TAG";

private ItemStack[] items;

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

	for (byte b = 0; b < items.length; b++) {
		if(items[b]==null) continue;
		NBTTagCompound comp = new NBTTagCompound();
		comp.setByte("id", b);
		items[b].writeToNBT(comp);
		tag.appendTag(comp);
	}

	compound.setTag(LIST_TAG, tag);
}

Posted

There are a couple of ways to handle this.

 

You could associate it with the player like enderchests.  I don't like the for backpacks.

 

On mine, i stored it in the itemstack for the backpack.  In which case, yes you have read/write it out of nbt each time.

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

U sure? :/ bc i wanted my backpacks to store specific items on pickup, which means that i need to check every time a player picks an item up if there is space in the backpack. which means a lot of nbt reading. rip processor

Posted

I spelled out two routes for you.  If you are really concerned about processor power then go the extended properties route.  It has its drawbacks though.  You won't be able to store the backpack in a chest or somewhere else.  Any backpack you pick up will have the same inventory.

 

 

To be honest though, you are worrying about nothing.  There won't be enough item pickups occurring fast enough to cause an issue.

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

Usually the contents of the backpack are stored in the itemstack's NBT data, storing it in the player would mean that every backpack would have the same items, and taking an item from one bag will take it from another - vice versa with putting in items.

Development of Plugins [2012 - 2014] Development of Mods [2012 - Current]

Posted

See here for a tutorial that explains both methods (IEEP and ItemStack NBT).

 

As others have said, don't worry about performance. Neither implementation will cause any sort of performance issue at all; just choose the one that makes sense for your project.

 

I.e., if you want the inventory tied to the player, like an extra armor slot, use IEEP; if you want an item that stores an inventory, such as a backpack, use ItemStack NBT. That is the only way to design - worry about performance only after you implement it in a logical fashion and notice issues.

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.