Remove enchantments

Discussion in 'Plugin Development' started by leonh, Oct 29, 2014.

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

    leonh

    I'm pretty nub at programming, this is one of my first programs so bear with me.

    Code:java
    1. package me.leon1717.undone;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.enchantments.Enchantment;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.inventory.ItemStack;
    9. import org.bukkit.inventory.meta.ItemMeta;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class Undone extends JavaPlugin{
    13.  
    14. protected ItemStack itemInHand;
    15. 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,
    16. Enchantment.ARROW_INFINITE,
    17. Enchantment.ARROW_KNOCKBACK,
    18. Enchantment.PROTECTION_ENVIRONMENTAL,
    19. Enchantment.PROTECTION_EXPLOSIONS,
    20. Enchantment.PROTECTION_FALL,
    21. Enchantment.PROTECTION_FIRE,
    22. Enchantment.PROTECTION_PROJECTILE,
    23. Enchantment.LURE,
    24. Enchantment.LUCK,
    25. Enchantment.OXYGEN,
    26. Enchantment.THORNS,
    27. Enchantment.WATER_WORKER };
    28.  
    29. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[]args){
    30. if(!(sender instanceof Player)){
    31. sender.sendMessage(ChatColor.RED +"Console cannot do this!");
    32. return true;
    33. }
    34. Player e=(Player) sender;
    35.  
    36. if(cmd.getName().equalsIgnoreCase("unenchant")){
    37. ItemStack item=e.getInventory().getItemInHand();
    38. ItemMeta itemMeta=item.getItemMeta();
    39. for(Enchantment en: enchant){
    40. if(itemMeta.hasEnchants()){
    41. itemMeta.removeEnchant(en);
    42. item.setItemMeta(itemMeta);
    43. }
    44. }
    45. }
    46.  
    47. return true;
    48. }
    49. }


    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..?
     
  2. Offline

    fireblast709

  3. Offline

    Abs0rbed

    your removeEnchantment method doesn't accept an Enchantment argument, it accepts a Player...
    I think your main problem is here:
    Code:java
    1. for(Enchantment ench: enchant){
    2. if(itemMeta.hasEnchants()){
    3. itemMeta.removeEnchant(Enchantment /*<-- Right here*/ );
    4.  
    5. }
    6.  
    7. }

    You're giving it the enchant class, when you should be giving it the ench variable you made for your for-each loop :eek:
     
  4. Offline

    OffLuffy

    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
    1. for (Enchantment e : itemMeta.getEnchants().keySet()) {
    2. itemMeta.removeEnchant(e);
    3. }
     
  5. Offline

    Abs0rbed

    I was talking specifically about the removeEnchant method that the OP wrote. Maybe I misread the question.

     
  6. Offline

    OffLuffy

    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.
     
  7. Offline

    Abs0rbed

    OffLuffy lol, makes much more sense now. Must've misunderstood something xD.
     
  8. Offline

    leonh

  9. Offline

    fireblast709

    leonh well if you need it explained, just ask :D!
     
  10. Offline

    leonh

    Thanks for the help so far guys, but I'm stumped again... more info in Edit.
     
  11. Offline

    Skionz

    leonh Debug to see where the issue is
     
  12. Offline

    leonh

    I have never debugged before... I'll look up some tutorials, I guess. Thanks.
     
  13. Offline

    fireblast709

    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.
     
  14. Offline

    leonh

    bump
     
Thread Status:
Not open for further replies.

Share This Page