I'm pretty nub at programming, this is one of my first programs so bear with me. Code:java package me.leon1717.undone; import org.bukkit.ChatColor;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.enchantments.Enchantment;import org.bukkit.entity.Player;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.plugin.java.JavaPlugin; public class Undone extends JavaPlugin{ protected ItemStack itemInHand; private Enchantment enchant[] = { Enchantment.SILK_TOUCH, Enchantment.DURABILITY, Enchantment.DIG_SPEED, Enchantment.LOOT_BONUS_BLOCKS, Enchantment.DAMAGE_ALL, Enchantment.KNOCKBACK, Enchantment.FIRE_ASPECT, Enchantment.DAMAGE_ARTHROPODS, Enchantment.DAMAGE_UNDEAD, Enchantment.LOOT_BONUS_MOBS, Enchantment.ARROW_DAMAGE, Enchantment.ARROW_FIRE, Enchantment.ARROW_INFINITE, Enchantment.ARROW_KNOCKBACK, Enchantment.PROTECTION_ENVIRONMENTAL, Enchantment.PROTECTION_EXPLOSIONS, Enchantment.PROTECTION_FALL, Enchantment.PROTECTION_FIRE, Enchantment.PROTECTION_PROJECTILE, Enchantment.LURE, Enchantment.LUCK, Enchantment.OXYGEN, Enchantment.THORNS, Enchantment.WATER_WORKER }; public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[]args){ if(!(sender instanceof Player)){ sender.sendMessage(ChatColor.RED +"Console cannot do this!"); return true; } Player e=(Player) sender; if(cmd.getName().equalsIgnoreCase("unenchant")){ ItemStack item=e.getInventory().getItemInHand(); ItemMeta itemMeta=item.getItemMeta(); for(Enchantment en: enchant){ if(itemMeta.hasEnchants()){ itemMeta.removeEnchant(en); item.setItemMeta(itemMeta); } } } return true; }} EDIT: I have no errors now, but I'm sad to inform you all, it doesn't work. I have no idea where to start to find the problem... Help? EDIT 2: Progress, although I feel like this should work, I'm missing something..?
your removeEnchantment method doesn't accept an Enchantment argument, it accepts a Player... I think your main problem is here: Code:java for(Enchantment ench: enchant){ if(itemMeta.hasEnchants()){ itemMeta.removeEnchant(Enchantment /*<-- Right here*/ ); } } You're giving it the enchant class, when you should be giving it the ench variable you made for your for-each loop
How is it accepting a Player? The JavaDoc linked above you specifies it is an Enchantment to remove from the ItemStack. The OP is using ItemMeta, but the JavaDoc for the two is the same, although I'm not sure if removing the enchant from the ItemStack works quite right. Seem to recall having issues if not using the meta. Also, if you're looking to remove all enchantments from the ItemMeta or ItemStack, iterate over a list of it's enchantments, then remove them one by one. i.e. Code:java for (Enchantment e : itemMeta.getEnchants().keySet()) { itemMeta.removeEnchant(e);}
I was talking specifically about the removeEnchant method that the OP wrote. Maybe I misread the question.
Even in his code sample, it's referring to the one for the the ItemMeta class, which is pretty much the same as far as JavaDocs are concerned as the ItemStack, although they may function differently. I just can't imagine why a removeEnchantment() method would ever need a Player as they aren't Enchantments XD There's no removeEnchantment() method for the Player class either, just removeAchievement(). Just confused me a bit is all here somehow. The reason he left the area you specified blank (removeEnchantment(Enchantment )) is because he didn't know what argument to put there instead.
leonh two things: No need to wrap the ItemStack that player.getItemInHand() returns in a new ItemStack. After modifying ItemMeta, you need to reapply it to the ItemStack.