Solved Giving knock back to a tool

Discussion in 'Spigot Plugin Development' started by Poofyjerry, Feb 22, 2020.

  1. I would like to give the player a shovel with knockback on it but everytime I try it says that type of enchantment cant be added to a tool. I know there is a way to override this ive seen knockback on some weird items, how can i do this?

    Code (Text):

    package me.cadeheinberg.ItemsCustom;

    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    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;
    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin {

      @Override
      public void onEnable() {
       
      }

      @Override
      public void onDisable() {
     
      }
     
      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        //if console return
        if(sender instanceof ConsoleCommandSender) {
          return true;
        }
        //Booster axe
        if(label.equalsIgnoreCase("customaxe")) {
          Player player = (Player) sender;
          ItemStack item = new ItemStack(Material.IRON_AXE, 1);
          ItemMeta meta = item.getItemMeta();
          meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Booster Axe");
          item.setItemMeta(meta);
          player.getInventory().addItem(item);
        }
        //Shotgun
        if(label.equalsIgnoreCase("customshovel")) {
          Player player = (Player) sender;
          ItemStack item = new ItemStack(Material.GOLDEN_SHOVEL, 1);
          ItemMeta meta = item.getItemMeta();
          meta.setDisplayName(ChatColor.GOLD + "LIL SHOTTY");
          item.setItemMeta(meta);
          item.addEnchantment(Enchantment.KNOCKBACK, 2);
          player.getInventory().addItem(item);
        }
        return false;
     
      }
    }

     
     
    #1 Poofyjerry, Feb 22, 2020
    Last edited: Feb 22, 2020
  2. Try to use ItemStack#addUnsafeEnchantment(Enchantment, int) instead.
     
  3. It's been some time since I last dealt with custom items but iirc you should be able to use player.getItemInHand().equals(youCustomItem) for comparing.
     
  4. Worked thank you dude!