Getting the item in a player's hand

Discussion in 'Plugin Development' started by Calebizzthaman, Jul 28, 2017.

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

    Calebizzthaman

    I've been trying to make this part of my plugin give the player regeneration when they right click with a red rose in their hand. But it allows everything to give the player regeneration when right clicked. Help?


    Code:
        @EventHandler
        public void onRightClickDandelion(PlayerInteractEvent e)
        {
            Player player = e.getPlayer();
            Location loc = player.getLocation();
            World world = player.getWorld();
            Material material = player.getInventory().getItemInMainHand().getType();
          
          
            if((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK) && (material == Material.RED_ROSE))
            {
              
              
                    player.getInventory().getItemInMainHand().setAmount
                        (player.getInventory().getItemInMainHand().getAmount()-1);
              
                    player.addPotionEffect
                        (new PotionEffect(PotionEffectType.REGENERATION, 80, 3));
              
                    world.playEffect(loc,Effect.POTION_BREAK, 0 );
              
            }
          
    
        } 
     
  2. Offline

    Machine Maker

    @Calebizzthaman
    1. You may need to add another set of parentheses around the OR statement checking the action. It should be
    Code:
    if (((action == Action.RIGHT_CLICK_AIR) || (action == Action.RIGHT_CLICK_BLOCK)) && (material == Material.RED_ROSE))
     
  3. Offline

    Calebizzthaman

    Now nothing happens when I right click with anything or the red rose.
     
  4. Offline

    Machine Maker

    @Calebizzthaman
    That's interesting. It should work. Try doing two if checks instead, first check if the action is what you want, then check the material.
     
  5. Offline

    i3ick

    @Calebizzthaman
    @X1machinemaker1X

    YAY!! Learning time!

    ||
    is a logical statement
    | is bitwise

    Logical statements only move to the next statement if the first one is true. However the player can't both be right clicking the air and the block, so you wanna check them both at the same time. Also, a bitwise statement has more power than a logical, so it will be evaluated first.

    The code would then be

    if((e.getAction() == Action.RIGHT_CLICK_AIR) | (e.getAction() == Action.RIGHT_CLICK_BLOCK) && (material == Material.RED_ROSE))

    no parenthesis needed. This code is correct, but it won't work either. After some testing I found out that you are indeed holding a RED_ROSE, but if you log the action, the thing you are actually putting down appears to be DOUBLE_PLANT

    therfore:

    if((e.getAction() == Action.RIGHT_CLICK_AIR) | (e.getAction() == Action.RIGHT_CLICK_BLOCK) && (material == Material.DOUBLE_PLANT))

    Cheers,
    i3ick
     
    Reflxction and Zombie_Striker like this.
  6. Offline

    Reflxction

    Am I too bad at Bukkit API, or do I think that you have to do "&& p.getItemInHand() == Material.DOUBLE_PLANT"?
     
  7. Offline

    Machine Maker

    @xTechno_
    1. p.getItemInHand() is a deprecated method because there are now two hands in minecraft. Developers should use
    Code:
    p.getInventory().getItemInMainHand(); //or
    p.getInventory().getItemInOffHand();
    2. Also, you are comparing an itemstack to a Material enum.
     
    Reflxction likes this.
Thread Status:
Not open for further replies.

Share This Page