Solved Getting enchantment name + level from item to string

Discussion in 'Plugin Development' started by MattexFilms, Aug 26, 2013.

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

    MattexFilms

    I'm trying to get the enchantment name and level, of an item, to a string. But the only string I get is: {Enchantment[16, DAMAGE_ALL]=1} on a sword with Sharpness 1. How can I turn this into: Sharpness 1?
     
  2. Offline

    tommycake50

    You are doing it wrong.
    use ItemStack.getEnchantments();
    iterate through Enchantment's in getEnchantments.keySet();
    and create a switch statement to decide the understandable name from the Enum name.
    getEnchantments returns a map of enchantment names and levels so .getEnchantments().get(Enchantment) gives you the level.
    and then String result = understandableName + level;

    something like this
    Code:java
    1.  
    2. private ArrayList<String> formatEnchantments(ItemStack item){
    3. ArrayList<String> formattedenchantments = new ArrayList<String>();
    4. Map<Enchantment, Integer> enchantments = item.getEnchantments();
    5. for(Enchantment e : enchantments.keySet()){
    6. String understandablename;
    7. switch(e){
    8. case DAMAGE_ALL:
    9. understandablename = "sharpness";
    10. break;
    11. etc,
    12. }
    13. formattedenchantments.add(understandablename + " " +enchantments.get(e));
    14. }
    15. return formattedenchantments;
    16. }
    17.  
     
  3. Offline

    MattexFilms

    Thanks for that, that helped a lot.
     
Thread Status:
Not open for further replies.

Share This Page