Solved getItemInMainHand() Undefined

Discussion in 'Spigot Plugin Help' started by adamthesmartie, May 19, 2016.

  1. Hi there! I'm learning to code plugins and am trying to duplicate results I have seen in other places. Right now, I am trying to do a certain thing when an item is used (like a fishing rod). I saw it described briefly in the Bukkit Plugin Tutorial, however, the command that it says to use (getItemInHand()) is deprecated (due to 1.9, as I'm sure you'll know). I have replaced it with getItemInMainHand(), but that gives the error "The method getItemInMainHand() is undefined for the type Player". I saw on another thread to use Player#getEquipment().getItemInMainHand() instead, but that also gives an error (I may be using it in the wrong syntax though, like I said I am a beginner). I couldn't find any help for this anywhere else, so I decided to post here. This is my code so far:

    package io.github.adamthesmartie.Lightning;

    import java.util.Set;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Strike extends JavaPlugin
    {
    @EventHandler
    public void onPlayerInteractBlock(PlayerInteractEvent event)
    {
    Player player = event.getPlayer();
    if (player.getItemInMainHand().getType() == Material.FISHING_ROD)
    {
    //do something
    }
    }
    }

    I have attached my referenced libraries too (I tried importing all of the ones I could get from BuildTools 1.9.4, but still nothing works)

    It is probably a very easy fix, but I can't seem to find what wrong myself... so thanks to anyone who comments :)
     

    Attached Files:

    #1 adamthesmartie, May 19, 2016
    Last edited: May 21, 2016
  2. player.getInventory().getItemInMainHand()
     
  3. Oh... I feel really dumb now. Thanks!