Posted September 1, 201510 yr 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); }
September 1, 201510 yr 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
September 1, 201510 yr Author 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
September 1, 201510 yr 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
September 1, 201510 yr 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]
September 2, 201510 yr 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. http://i.imgur.com/NdrFdld.png[/img]
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.