getItemInMainHand() is undefined! Need help =(

Discussion in 'Spigot Plugin Development' started by Zendovo, May 30, 2017.

  1. So I have been trying to update my plugin to 1.11 because "getItemInHand" is deprecated.

    So I used "getItemInMainHand()" but doesnt seem to work either.

    Here is my code:

    Code (Text):
    package me.zendovio.clickpick;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin implements Listener , CommandExecutor{
       
        @Override
        public void onEnable() {
            getLogger().info("CPS has been enabled");
            Bukkit.getServer().getPluginManager().registerEvents((Listener)this, this);
            if (!new File(getDataFolder(), "config.yml").exists())
            {
            saveDefaultConfig();
            this.getCommand("cpsreload").setExecutor(new Main());
        }
        }
       
        @Override
        public void onDisable() {
            getLogger().info("CPS has been disabled");
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            if (player.getItemInMainHand().getType() == Material.DIAMOND_PICKAXE)
            {
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
                {
                    if(player.getWorld().getName().equalsIgnoreCase(getConfig().getString("WorldName")))
                    {
                        player.performCommand(getConfig().getString("command"));
                    }      
                }
            }
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
            if (sender instanceof Player){
                reloadConfig();
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a[ClickPickSell] &bConfig reloaded!"));
                return true;
            } else {
                reloadConfig();
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a[ClickPickSell] &bConfig reloaded!"));
                return true;
            }
           
        }
    }
     
  2. You need to get the first the inventory of the player
    player.getInventory().getItemInMainHand()
     
  3. Ok I will try it out!

    Thanks It worked but now when I try to right click on ground it does it twice...... =(

    New Code:
    Code (Text):
    package me.zendovio.clickpick;

    import java.io.File;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin implements Listener , CommandExecutor{
       
        @Override
        public void onEnable() {
            getLogger().info("CPS has been enabled");
            Bukkit.getServer().getPluginManager().registerEvents((Listener)this, this);
            if (!new File(getDataFolder(), "config.yml").exists())
            {
            saveDefaultConfig();
            this.getCommand("cpsreload").setExecutor(new Main());
        }
        }
       
        @Override
        public void onDisable() {
            getLogger().info("CPS has been disabled");
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
            if (player.getInventory().getItemInMainHand().getType() == Material.DIAMOND_PICKAXE)
            {
                if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
                {
                    if(player.getWorld().getName().equalsIgnoreCase(getConfig().getString("WorldName")))
                    {
                        player.performCommand(getConfig().getString("command"));
                    }      
                }
            }
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
            if (sender instanceof Player){
                reloadConfig();
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a[ClickPickSell] &bConfig reloaded!"));
                return true;
            } else {
                reloadConfig();
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a[ClickPickSell] &bConfig reloaded!"));
                return true;
            }
           
        }
    }

    :( Please Help!
     
  4. According to me, I think that "if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)"
    it thinks that when I'm clicking on blocks I'm also clicking in air is there anyway to fix it?
     
  5. Because the event is fired twice, once per hand. Check if the EquipmentSlot is HAND (e.getHand() == EquipmentSlot.HAND)
     
    • Informative Informative x 1