Addenchantment error

Discussion in 'Plugin Development' started by Eltarion, Nov 27, 2011.

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

    Eltarion

    Hi,

    i don't know what i do wrong and i need help please.

    I know what line it is, but i don't know the error.
    (The error is where the "try-catch" is)


    The Code:

    Code:
            if(cmd.getName().equalsIgnoreCase("ench")){
                if (sender.hasPermission("enchant.ench")){
                    if (args[0].equalsIgnoreCase("add")){
                        if (args.length == 3){
                        Player player = (Player)sender;
                        ItemStack itemstack = player.getItemInHand();
                        try
                        {
                            itemstack.addEnchantment(Enchantment.getById(Integer.parseInt(args[1])), Integer.parseInt(args[2]));
                        }
                        catch (Exception ex) {
                            log.info(ex.toString());
                        }
                        player.sendMessage(ChatColor.GREEN + "Verzauberung " + ChatColor.GOLD + args[1] + ChatColor.GREEN + " auf Level " + ChatColor.GOLD + args[2] + ChatColor.GREEN + " erfolgreich zum gehaltenem Item hinzugefuegt!");
                        return true;
                        }
                    } else if (args[0].equalsIgnoreCase("del")){
                        if (args.length == 2){
                        Player player = (Player)sender;
                        ItemStack itemstack = player.getItemInHand();
                        itemstack.removeEnchantment(Enchantment.getById(Integer.parseInt(args[1])));
                        player.sendMessage(ChatColor.GREEN + "Verzauberung " + ChatColor.GOLD + args[1] + ChatColor.GREEN + " erfolgreich vom Item geloescht!");
                        return true;
                        }
                    }
                }
            }
            return false; 
    Error-Message:

    Code:
    java.lang.IllegalArgumentException: Specified enchantment cannot be applied to this itemstack
    I googled hours but don't find anything.

    *edit:
    Command i typed in was "/ench add 0 1"
    ItemInHand=Diamond_Sword
     
  2. Offline

    Zeerix

    How about reading the error message?
    Code:
    Specified enchantment cannot be applied to this itemstack
    Which enchantment did you try to add to what item?
     
  3. Offline

    bleachisback

    have you tried typing something else in besides 0?
     
  4. Offline

    Zeerix

    For swords, 16-21 should work.
     
  5. Offline

    Eltarion

    Should not go all ids?


    Example:
    Command = /ench add 1 2

    args[0]=add
    args[1]=1
    args[2]=2

    args[1] is the id of the enchantment
    args[2] is the level of the enchantment

    In the "ItemStack itemstack"-variable
    And in the "itemstack" in my example is a diamond_sword.

    And none of the ids works.

    EnchantmentID=0
    (enchantment.protect.all)
     
  6. Offline

    Zeerix

    Protection enchantments work on armor, damage enchantments work on swords.
     
Thread Status:
Not open for further replies.

Share This Page