How to cast string to itemmeta

Discussion in 'Plugin Development' started by william9518, Jan 11, 2013.

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

    william9518

    so I just did (ItemMeta)string
    but it returned in stackttrace:
    cannot cast from java.lang.string to org.bukkit.inventory.meta.itemmeta.
    halp
     
  2. Offline

    CubixCoders

    What are you trying to do?
     
  3. Offline

    william9518

    trying to return itemmeta from a configsection
     
  4. Offline

    CubixCoders

    Like names and lore?
     
  5. Offline

    william9518

    not just those, armomr color, potion effects, ALL the itemmetas
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    ItemMeta is ConfigurationSerializable, it should be treated like all other objects that are ConfigurationSerializable to make your life easier.
     
  7. Offline

    william9518

    so how do i do it
     
  8. Offline

    TheTrixsta

    LOL! You would do something like this.

    Code:
    ItemStack item = new ItemStack(Material.IRON_SWORD);
    ItemMeta meta = item.getItemMeta();
    String name = "My sword :)";
    meta.setDisplayName(name);
    item.setItemMeta(meta);
    player.getInventory().addItem(item);
    
    This will allow you to manipulate the item's meta then set it to the item
     
  9. Offline

    william9518

    Ya, no. I want to read all the metas like potion, leather armor, etc
     
  10. Offline

    TheTrixsta

    then instead of using
    Code:
    ItemMeta meta = item.getItemMeta();
    use
    Code:
    PotionMeta meta = item.getItemMeta();
    and so and and so forth check the javadocs for the other instances of ItemMeta
     
  11. Offline

    william9518

    So how can i get the meta from string? Did u read my question?!
     
  12. Offline

    finalblade1234

    Are you just trying to get items into a players Inv?
    if so there are simpler ways...
     
  13. Offline

    william9518

    NO DAMMIT im saving metadata into a config and i need to read it off!
     
  14. Offline

    EnvisionRed

    Stop being an idiot. You can't cast String to ItemMeta because ItemMeta isn't a subclass of string. If you're saving it into a config you can save each component with a separator character and use String.split to get all the args when you load from it again.
     
  15. Offline

    desht

    1. Do not swear at people who are trying to help you.
    2. Go back and read what Sagacious_Zed wrote. ItemMeta implements ConfigurationSerializable so you can save it directly to your config and read it directly back. You don't need to worry about serialising it yourself; Bukkit will do it for you.

    PHP:
    config.set("meta"thisItem.getMeta());
     
    gomeow likes this.
  16. Offline

    Sagacious_Zed Bukkit Docs

    william9518
    And to add on to what desht said. You can get it back by using the get method, be aware that some casting will be involved as type information is lost at runtime.

    Code:
    ItemMeta itemmeta = (config.get("meta") instanceof ItemMeta) ? (ItemMeta) config.get("meta") : null)
    edit fixed code
     
  17. Offline

    william9518

    Sorry, got impaitient... How do i read it back from the config?
     
  18. Offline

    desht

    See the post directly above yours :)

    Although there's a minor syntax error in that code - try this:
    PHP:
    ItemMeta itemmeta config.get("meta") instanceof ItemMeta ? (ItemMetaconfig.get("meta") : null;
    // or maybe you want to throw an exception if the saved config item isn't what you're expecting
    And note this will work equally well for subclasses of ItemMeta such as BookMeta etc.
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    desht
    nice catch looks like i failed to remove everything once i started typing.
     
Thread Status:
Not open for further replies.

Share This Page