Doesn't do if statement if action is right click air

Discussion in 'Plugin Development' started by Blackwing_Forged, Oct 28, 2017.

Thread Status:
Not open for further replies.
  1. I have this event with a method and if i shift right click in the air it doesn't print out the "You must be holding something to do this" message but if i am on a block it does print out the message but it prints it twice, whats wrong?
    Code:
    @EventHandler
        public void hatEvent(PlayerInteractEvent e)
        {
            Player p = e.getPlayer();
           
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR) && p.isSneaking() || e.getAction().equals(Action.RIGHT_CLICK_BLOCK) && p.isSneaking())
            {
                ItemStack is = p.getInventory().getItemInMainHand();
                setHat(p, is);
                e.setCancelled(true);
            }
        }
       
        public void setHat(Player p, ItemStack is)
        {
            if(is.getType() == Material.AIR || is == null)
            {
                p.sendMessage(ChatColor.RED + "You must be holding something to do this");
            }
            else
            {
                if(p.getInventory().getHelmet() == null)
                {
                    if(is.getAmount() > 1)
                    {
                        ItemStack copy = is.clone();
                        copy.setAmount(1);
                        p.getInventory().setHelmet(copy);
                        is.setAmount(is.getAmount() - 1);
                        p.sendMessage(ChatColor.GREEN + "Enjoy your hat!");
                    }
                    else
                    {
                        p.getInventory().setHelmet(is);
                        p.getInventory().remove(is);
                        p.sendMessage(ChatColor.GREEN + "Enjoy your hat!");
                    }
                }
                else
                {
                    p.sendMessage(ChatColor.RED + "Your head must be empty to use this");
                }
            }
        }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Blackwing_Forged It seems that you have grouped conditions, but brackets around those.
     
  3. Offline

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page