ItemStack to String

Discussion in 'Plugin Development' started by BaumwolleHD, Oct 13, 2013.

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

    BaumwolleHD

    Hello, i need to save and load ItemStacks to a db do i would be so greatful if you can give me a function to transform a ItemStack with enchantments, lore, metadata etc. to a String and back?
     
  2. Offline

    MiniDigger

    You just need to put all the data you want to save into a string, seperated with a ';' for example.
    If you cant to convert it back, split the string.
     
  3. Offline

    tommycake50

    Ok heres an example store it in a string like this.
    materialtype:amount:enchantments:lore:name
    then split it around : and create an itemstack accordingly.
     
  4. Offline

    Chinwe

    I believe ItemStacks can be saved to .ymls, as there is a method getItemStack(String) in FileConfiguration: lemme check this, one sec :)

    EDIT: Ooooh, it appears it does!
    I used yml.set("itemstack", anItemStack), and got this:


    Code:
    itemstack:
      ==: org.bukkit.inventory.ItemStack
      type: REDSTONE_LAMP_OFF
      damage: 2
      amount: 4
      meta:
        ==: ItemMeta
        meta-type: UNSPECIFIC
        display-name: A displayname
        lore:
        - Huehue
        - Potatoe
        enchants:
          DAMAGE_ALL: 10
    
    Then, you can use ItemStack itemstack = yml.getItemStack("itemstack") to get it back :)
     
    tommycake50 likes this.
  5. Offline

    tommycake50

    Ahh I forgot about that.
    However I believe the OP wants to store it in a database...
     
  6. Offline

    Chinwe

    Ah yeh, converting to a String and back:

    Code:
    // Create a random ItemStack
    ItemStack is = new ItemStack(Material.REDSTONE_LAMP_OFF, 4, (short) 2);
    is.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 5);
    ItemMeta meta = is.getItemMeta();
    meta.setDisplayName("A displayname");
    meta.setLore(Arrays.asList("Huehue", "Potatoe"));
    is.setItemMeta(meta);
     
    // ItemStack to String
    Map<String, Object> serializedMap = is.serialize();
    String itemStackString = String.valueOf(serializedMap);
     
    // Back to ItemStack
    ItemStack originalItemStack = ItemStack.deserialize(serializedMap);
     
    // Output:
    // {type=REDSTONE_LAMP_OFF, damage=2, amount=4, meta=UNSPECIFIC_META:{meta-type=UNSPECIFIC, display-name=A displayname, lore=[Huehue, Potatoe], enchants={DAMAGE_ALL=5}}}
    
    However, you may need to make a method for converting the String (that you save in the database) to a Map<String, Object> that is used to create an ItemStack :confused:
     
    tommycake50 likes this.
  7. Offline

    Ultimate_n00b

    Wait wait wait.. whaaaa.. I made a serializer for this, but yet something already exists..
     
    Chinwe likes this.
  8. Offline

    MiniDigger

    ItemStack implements ConfigurationSeriable so you can convert it easy into a Map<String,Object> and back. You dont need to make your own serializer
     
    tommycake50 likes this.
  9. Offline

    Ultimate_n00b

    If you checked out the resources thread, I made a way to transform these maps into a string, and then back into a map. I think I know how it works now.
     
Thread Status:
Not open for further replies.

Share This Page