Solved Removing some enchantments

Discussion in 'Plugin Development' started by maestro34, Sep 17, 2014.

Thread Status:
Not open for further replies.
  1. Hi guys, I'm trying to remove some enchantments for a PvP Server, I tried many ways but I have not found the right way yet :/ I hope someone will help me :)

    Code:
    Code:java
    1. private List<Enchantment> allowed = Arrays.asList(PROTECTION_ENVIRONMENTAL, FIRE_ASPECT, LOOT_BONUS_MOBS, DIG_SPEED, DAMAGE_ALL, PROTECTION_FALL,
    2. ARROW_DAMAGE, ARROW_INFINITE, ARROW_FIRE, DURABILITY, LOOT_BONUS_BLOCKS, SILK_TOUCH);
    3.  
    4. @EventHandler
    5. public void onEnchantItem(EnchantItemEvent e) {
    6. Map<Enchantment, Integer> enchants = e.getEnchantsToAdd();
    7. Iterator<Enchantment> iterator = enchants.keySet().iterator();
    8. while(iterator.hasNext()) {
    9. Enchantment en = iterator.next();
    10. if(!(allowed.contains(en))) enchants.remove(en);
    11. }
    12. }


    Error: http://pastebin.com/AWHgKRUP
     
  2. Offline

    mythbusterma

    maestro34

    You can't modify a list while you're iterating over it, hence the CocurrentModificationException, simply replace enchants.remove(en) with iterator.remove()
     
  3. maestro34
    Use iterator.remove() instead. Also I don't think removing the enchantment from the map will do anything.
     
  4. Thanks, It's working :) Yes, it do something, it's written in the doc
     
Thread Status:
Not open for further replies.

Share This Page