Solved getItenInHand error

Discussion in 'Plugin Development' started by rhunter, Oct 13, 2016.

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

    rhunter

    Hey so I get this error when I try to getItemIn hand for a player. It strikesthrough ".getItemInHand":
    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if(player.hasPermission("drugs.use")){
        if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
            if(player.getItemInHand().getType() == Material.SUGAR){
              
            }
        }}
    
    }
    I'm sure its an easy fix, thanks!
     
  2. Offline

    Zombie_Striker

    @rhunter
    It is. Add "Main" at the end.

    GetItemInHand was deprecated because now players can hold items in two hands. Because of that ,they had to specify which hand you were getting.

    BTW: Always remember to null check. The item can be null. Make sure the item is not null before getting the type.
     
  3. Offline

    rhunter

    Ah, forgot about duel wielding! Thank you.
    EDIT: Wait, what do I add "Main" to the end of?
     
  4. Offline

    Zombie_Striker

  5. Offline

    rhunter

    Thats what i'm trying to do but it says it is undefined for the type player
     
  6. Offline

    Zombie_Striker

    @rhunter
    What version of bukkit/spigot are you using?
     
  7. Offline

    rhunter

    spigot-1.10.2-R0.1-SNAPSHOT-latest.jar
    EDIT: heres the code:
    Code:
    @EventHandler
    
    public void onCocaineUse(PlayerInteractEvent event) {
    
    Player player = event.getPlayer();
    
    if(player.hasPermission("drugs.use")){
    
    if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    
    if(player.getItemInMainHand().getType() == Material.SUGAR){
    
    player.getInventory().remove(Material.SUGAR);
    
    player.addPotionEffect(new org.bukkit.potion.PotionEffect(PotionEffectType.SPEED, 20000, 2));
    
    player.addPotionEffect(new org.bukkit.potion.PotionEffect(PotionEffectType.FAST_DIGGING, 20000, 2));
    
    player.addPotionEffect(new org.bukkit.potion.PotionEffect(PotionEffectType.GLOWING, 20000, 3));
    
    }
    
    }}
    
    }
    
     
  8. Offline

    Zombie_Striker

    @rhunter
    After reading the deprecated message found HERE, it turns out you need to get the player's inventory before using that method. Add ".getInventory()" before using that method.
     
  9. Offline

    rhunter

    Thank you!
     
Thread Status:
Not open for further replies.

Share This Page