Looping trough enchantments

Discussion in 'Plugin Development' started by MordorKing78, Nov 22, 2016.

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

    MordorKing78

    How would I loop trough enchantments? I know they have ids but these are not in a right order from 1 to 10. How would I loop trough all enchantments?

    I'm trying to get a list of enchantments that a player can put on his items.

    Thanks for the help!

     
    Last edited: Nov 22, 2016
  2. Offline

    danielporsh9

    What do you mean loop through enchantments?
     
  3. Offline

    DoggyCode™

    PHP:
    // creating item
    ItemStack itemStack = new ItemStack(Material.DIAMOND_SWORD);

    // adding enchantments for testing
    itemStack.addEnchantment(Enchantment.DURABILITY2);
    itemStack.addEnchantment(Enchantment.DAMAGE_UNDEAD4);

    // create map with enchantment as key and level(int) as value
    Map<EnchantmentIntegeritemEnchants itemStack.getEnchantments();

    // iretate through the map
    Iterator it itemEnchants.entrySet().iterator();

    int i 0;
    while (
    it.hasNext()) {
      
    // get the pair
      
    Map.Entry pair = (Map.Entry)it.next();
      
    // using WordUtils.capitalize to produce a nice output like "Durability" instead of "DURABILITY"
      // the pair's key would be the Enchantment object and the value would be the level in the map.
      // you can probably use some util online if you wanna convert that int to a roman number
      
    System.out.println("[" "] " WordUtils.capitalize(((Enchantment)pair.getKey()).getName()) + " " pair.getValue());
      
    i++;
      
    it.remove(); // avoids a ConcurrentModificationException
    }
    This would print the following for my itemStack:
    [0] Durability 2
    [1] TheUndeadEnchantment 4
     
  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    MordorKing78

    (@danielporsh9) @DoggyCode™ Sorry if I wasn't being clear but I'm trying to get all the enchantmens that can be put on an certain item, e.g. sharpness can go on swords but not on armor.

    (It won't let me edit my previous post, sorry)
    @timtower but these ids aren't listed from 1 to x are they? makes it very hard to loop trough all of the enchantments by using a standard loop.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  6. Offline

    DoggyCode™

    I don't get what u mean from 0-10?
     
  7. Offline

    MordorKing78

    @DoggyCode™ Well, this was my first plan:
    Code:
    int maxEnchantInteger = <max enchantment id here>;
           
            for(int i=0; i<maxEnchantInteger; i++){
                Enchant e = Enchantment.getById(i);
               
                if(e.canEnchantItem(i)){
                    /*
                     * do stuff
                     */
                }
    
            }
    But since the enchantment id's don't go like 1, 2, 3, 4, 5 (I believe they go like 12 43 etc.) it's very hard to loop trough them using this method.
     
  8. Online

    timtower Administrator Administrator Moderator

    @MordorKing78 Why not use for(Enchantment enc : Enchantment.values())
    And then check the ID if needed
     
  9. Offline

    MordorKing78

    @timtower That does work perfectly, I'm now using this as my code:
    PHP:
        public HashSet<EnchantmentcheckPossibilities(ItemStack item){
            
    HashSet<Enchantmentpos = new HashSet<Enchantment>();

            for(
    Enchantment enc Enchantment.values()){
                if(
    enc.canEnchantItem(item)){
                    
    pos.add(enc);
                }
            }
           
            return 
    pos;
        }
    is that fine?
     
  10. Offline

    DoggyCode™

    If it works, it sure looks fine!


    Sent from my iPhone using Tapatalk
     
    MordorKing78 likes this.
Thread Status:
Not open for further replies.

Share This Page