Weird error with checking for enchants

Discussion in 'Plugin Development' started by PeregrineX, Aug 22, 2014.

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

    PeregrineX

    Hello, I have a problem with my code (like everybody else here lol).

    My plugin checks to see if there are any enchantments on an item, using this:

    Code:java
    1. int plusOne = Integer.parseInt(args[0]) + 1;
    2.  
    3. //...
    4.  
    5. Map<Enchantment, Integer> enchNull = new HashMap<Enchantment, Integer>();
    6. enchNull.put(null, null);
    7. ItemStack slot = player.getInventory().getItem(plusOne);
    8. if(slot.getEnchantments() != enchNull && !slot.getEnchantments().containsValue(-1))
    9. {
    10. //-snip-
    11. }
    12. else
    13. {
    14. sender.sendMessage(ChatColor.RED + "The selected item slot must contain an enchanted item!");
    15. }


    But when I run /enhance (the command), I get this error:

    Code:
    Error: Enchantment PROTECTION_FALL: Enchantment Level is either too low or too high (given -1, bounds are 1 to 4)
    
    Does anyone know how to fix this? I can't post the stack trace because this was displayed in-game :(
     
  2. Offline

    Dragonphase

    PeregrineX

    The error message produced literally tells you how to fix this.
     
  3. Offline

    PeregrineX


    That's the problem; I'm not adding an enchantment yet, I'm just checking to see if there are any enchantments on an item.

    EDIT: So yea, Feather Falling levels go from I to IV, according to the wiki. I believe it's -1 because I can't enchant that item with it (I was testing on a sword). Or if I'm being incredibly stupid and ignorant, then I'm sorry.

    Solved, sort of. I had to use a 'for' loop to remove all the keys/values in the map getEnchantments that had a value of "-1", then I was able to use if(!enchSet.isEmpty()) to determine whether it had enchantments or not.

    Here is my code, for future reference:

    Code:java
    1. int minusOne = Integer.parseInt(args[0]) - 1; //I changed this because of a stupid error on my part.
    2.  
    3. //...
    4.  
    5. ItemStack slot = player.getInventory().getItem(minusOne);
    6. Map<Enchantment, Integer> enchMap = slot.getEnchantments();
    7. Set<Enchantment> enchSet = enchMap.keySet();
    8. for(int badValue = -1; enchMap.containsValue(badValue); badValue = -1)
    9. {
    10. enchMap.remove(enchSet);
    11. }
    12. if(!enchSet.isEmpty())
    13. {
    14. //-snip-
    15. }


    So, there's this workaround, if anyone has a REAL solution, I would be more than happy to know.

    Thanks!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    Dragonphase

    PeregrineX

    Why not just check to see if the enchantment can be applied to the ItemStack?

    Code:java
    1. boolean canEnchant = myEnchantment.canEnchantItem(myItemStack);
     
Thread Status:
Not open for further replies.

Share This Page