Solved ItemStack Internal

Discussion in 'Spigot Plugin Development' started by Eliminator, May 25, 2016.

  1. When I print out an ItemStack, I get:
    Code (Text):
    ItemStack{SKULL_ITEM x 1, SKULL_META:{meta-type=SKULL, internal=H4sIAAAAAAAAAE2Py26DMBREbytVoqif0S2Sw8MkiyyqpmqMAhTCI/aOh1EAm0YJpCHf1Q+sl53d0ZxZjA6gw8u+n4T4On83reAaPJIaXl3XVGmWBi6bwrC5bRpLF9dG2bjItZsSVxXSQVejEz+PLb88gzby2zid+UUHgAcNnrJCTBx++ewhdjii+uCJaiZYcbJHIiTdySVDNpfvBBOp+u0b3s2rf64zFrkjqOUd2RBNpczQzooF38aLSqbXwEydQAaCffo27VIz3AQtyz9+/IRa/r2/B5KYipXDRJhENk28VvltsImQ2sxhnt6YVG0X975MZ2pSRIbFqonWa/UA/gDgMOu2HAEAAA==}}
    How do I get the "internal" value from an item stack?

    Thank you.
  2. The internal value is when the item has NBT data. If you want the NBT data (the internal value is just a base64 encode of raw NBT data, useless). Here is an NMS solution:
    Code (Java):
    CraftItemStack craftItemStack = CraftItemStack.asCraftCopy(itemStack);
    if (craftItemStack.getType() != Material.AIR) { // AIR cannot contain NBT
        net.minecraft.server.ItemStack nmsItemStack = CraftItemStack.asNMSCopy(craftItemStack);
        NBTTagCompound compound = nmsItemStack.getTag();

        if (compound == null)
            nmsItemStack.setTag(compound = new NBTTagCompound()); // Just incase it was null

        // Do whatever with your NBT data

        itemStack = CraftItemStack.asBukkitCopy(nmsItemStack); // All done
    It is necessary to check if the CraftItemStack type (the Material) is not AIR, because there are items that can exist as an ItemStack, but not as a CraftItemStack (don't ask me why, just something I noticed).

    Good luck.
    #2 CraftedFury, May 25, 2016
    Last edited: May 25, 2016
    • Informative Informative x 1
  3. Thanks, when I get the NBTTagCompound and print it out I see:
    Code (Text):
    NBT: {SkullOwner:{Id:"9c919b83-f3fe-456f-a824-7d1d08cc8bd2",Properties:{textures:[0:{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOTU1ZDYxMWE4NzhlODIxMjMxNzQ5YjI5NjU3MDhjYWQ5NDI2NTA2NzJkYjA5ZTI2ODQ3YTg4ZTJmYWMyOTQ2In19fQ=="}]}},display:{Lore:[0:"§0Editable:19:8"],Name:"§rCheese"}}
    How do I get the Properties of the NBTTagCompound?

    Is it just nbt.get("SkullOwner")?

    What I am looking to do is save the NBTBase of SkullOwner, then load it and put it back on the NBTTagCompound of another item.
    #3 Eliminator, May 27, 2016
    Last edited: May 27, 2016
  4. Using pure NMS NBT access it would just be nbt.getString("SkullOwner"). This way you can save the string value instead of an NBTBase.
  5. nbt.getString("SkullOwner"); returns an empty String.
  6. NBT#toString returns that json text.
    MojangsonParser (or so)#parse returns an NBTTagCompound from one of those json strings.

    Of course getString("Properties") returns null, because "Properties" resembles just another tag with the array "textures"
  7. In the case of your exact NBT sample, it would be:
    Code (Java):
    NBTTagCompound skullOwner = nbt.getCompound("SkullOwner");
    String id = skullOwner.getString("Id"); // 9c919b83-f3fe-456f-a824-7d1d08cc8bd2
    NBTTagCompound properties = skullOwner.getCompound("Properties");
    NBTTagList textures = properties.getList("textures");
    String value = textures.get(0).getString("Value"); // eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvOTU1ZDYxMWE4NzhlODIxMjMxNzQ5YjI5NjU3MDhjYWQ5NDI2NTA2NzJkYjA5ZTI2ODQ3YTg4ZTJmYWMyOTQ2In19fQ==
    String decodedValue = Base64Coder.decodeLines(data); // Requires Library: org.yaml.snakeyml
    JsonObject json = new JsonParser().parse(decodedValue).getAsJsonObject(); // Requires Library:
    JsonObject skin = json.getAsJsonObject("textures").getAsJsonObject("SKIN");
    String skinUrl = skin.get("url").getAsString(); //
    Hopefully you can learn from this.
    #7 CraftedFury, May 27, 2016
    Last edited: May 27, 2016
  8. Thanks, I just used the .toString(), and MojangsonParser.parse() and it worked.