Storing data in an ItemStack?

Discussion in 'Plugin Development' started by DarkBladee12, May 21, 2013.

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

    DarkBladee12

    Hey guys, is it possible to store data like strings or anything else in an itemstack? Unfortunately the .setMetadata method just works for entities and I don't think you can store data in the MaterialData or can you? Please help me ;)

    EDIT: I don't want to use the name/lore to store information!!!!
     
  2. Offline

    Cybermaxke

  3. Offline

    DarkBladee12

    Cybermaxke That looks good, I won't change the itemmeta, but do you have an example of using this?
     
  4. Offline

    Cybermaxke

    Sure
    Code:
    ItemStack is = new ItemStack(Material.DIAMOND_SWORD);
     
    /**
    * Getting tag from the itemstack. (A copy.)
    */
    TagCompound tag = TagUtils.getTag(is);
    if (tag == null) {
        tag = new TagCompound();
    }
     
    /**
    * Creating or getting a component.
    */
    TagCompound mytag = new TagCompound();
    if (tag.hasKey("MyCompound")) {
        mytag = tag.getCompound("MyCompound");
    }
     
    /**
    * Adding custom data.
    */
    mytag.setInteger("MyCustomData1", 5);
    mytag.setString("MyCustomData2", "Data");
     
    /**
    * Setting the component to the tag.
    */
    tag.setCompound("MyCompound", mytag);
     
    /**
    * Setting the tag for the itemstack. (Returns a cloned itemstack.)
    */
    is = TagUtils.setTag(is, tag);
     
Thread Status:
Not open for further replies.

Share This Page