Inventories and NBTTags

Discussion in 'Plugin Development' started by CorrieKay, Nov 14, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    CorrieKay

    Im trying to utilize the NBT api(ish) that comes in craftbukkit as a way to store additional player data. Thing is, my server has a remote chest system, that i need to migrate over to my new data storage system.

    Anyways, the problem im having is with creating/setting the remote chest inventory. Theres a bunch of inventory types, and im not sure which one im supposed to use.

    I figured i could use the InventoryLargeChest, with two TileEntityChests inside.. But how do i get the NBTTagList from that? Should i be using another type of inventory that is easier to work with? And how would i go about creating that inventory from the NBTTagList of item stacks?

    Thanks :3
     
  2. Offline

    one4me

    However you choose to get the Inventory instance, I'd think something like this would work -
    Code:
    public NBTTagList inventoryToList(Inventory inventory) {
      NBTTagList nbttaglist = new NBTTagList();
      ItemStack[] is = inventory.getContents();
      for(int i = 0; i < is.length; i++) {
        net.minecraft.server.ItemStack mis = CraftItemStack.createNMSItemStack(is[i]);
        if(mis != null) {
          NBTTagCompound nbttagcompound = new NBTTagCompound();
          nbttagcompound.setByte("Slot", (byte)i);
          mis.save(nbttagcompound);
          nbttaglist.add(nbttagcompound);
        }
      }
      return nbttaglist;
    }
    public Inventory listToInventory(NBTTagList nbttaglist) {
      TileEntityChest tileentitychest = new TileEntityChest();
      for(int i = 0; i < nbttaglist.size(); i++) {
        NBTTagCompound nbttagcompound = (NBTTagCompound)nbttaglist.get(i);
        int j = nbttagcompound.getByte("Slot") & 0xFF;
        if(j >= 0 && j < 27) {
          tileentitychest.setItem(j, net.minecraft.server.ItemStack.a(nbttagcompound));
        }
      }
      return new CraftInventory(tileentitychest);
    }
    
     
    Derthmonuter likes this.
  3. Offline

    CorrieKay

    Thanks, thats exactly what i needed!

    Can you explain this line though?
    nbttagcompound1.getByte("Slot") & 0xFF;

    Also, another question about NBTTags. Whenever creating an NBTTag type, such as NBTTagString or NBTTagByte, what is the string in the parameter?

    edit: also, how do i set the size of the inventory? im assuming that the tile entity chest dynamically resizes itsself, right?
     
  4. Offline

    one4me

    That line gets the slot number. Read this for an explanation of & 0xFF.

    The string in the parameter would be to set the name variable, as far as I know it's not used for anything important, and it isn't needed.

    The size of a TileEntityChest is 27, and there are no methods included in the class to change the private variable. I'm not sure what would happen but you could probably use reflection to change the size.

    Also I updated the second method in my previous post. I had forgotten to remove two unnecessary lines from when I was messing around.
     
  5. Offline

    CorrieKay

    Lets say i have a tag list, and im gonna fill it with tag strings. If all of the strings have the same "name", is that going to cause issues?
     
  6. Offline

    one4me

    You could, the name is just a variable in the NBTTagString instance.
    Code:
    NBTTagList list = new NBTTagList();
    list.add(new NBTTagString("NAME", "String1"));
    list.add(new NBTTagString("NAME", "String2"));
    System.out.println(((NBTTagString)list.get(0)).getName());
    System.out.println(((NBTTagString)list.get(0)).data);
    System.out.println(((NBTTagString)list.get(1)).getName());
    System.out.println(((NBTTagString)list.get(1)).data);
    //Prints out
    NAME
    String1
    NAME
    String2
    
     
Thread Status:
Not open for further replies.

Share This Page