Solved How to find the type of potion a player is holding.

Discussion in 'Plugin Development' started by webbhead, Apr 28, 2015.

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

    webbhead

    I want to find the type of potion the player is holding in their hand and stop the usage I was looking on the internet and found some stuff that didn't work here is my code...
    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            ItemStack hand = p.getItemInHand();
            Action a = e.getAction();
            short pot = 16428;
            short pot1 = 8297;
            short pot2 = 16489;
            if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK && hand != null) {
                if (hand.getType() == Material.POTION) {
                    if (hand.getDurability() == pot) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(ChatColor.RED + "You cannot throw those!");
                        return;
                    }
                }
            } else if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK && hand != null) {
                if (hand.getType() == Material.POTION) {
                    if (hand.getDurability() == pot1) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(ChatColor.RED + "You cannot throw those!");
                        return;
                    }
                }
            } else if (a == Action.RIGHT_CLICK_AIR || a == Action.RIGHT_CLICK_BLOCK && hand != null) {
                if (hand.getType() == Material.POTION) {
                    if (hand.getDurability() == pot2) {
                        p.setItemInHand(new ItemStack(Material.AIR));
                        p.sendMessage(ChatColor.RED + "You cannot throw those!");
                        return;
                    }
                }
            } else {
                return;
            }
        }
     
  2. Offline

    Garnetty

    First of all, why are you doing 2 else if statements when both of them are the same. That's basically like doing this
    Code:
    if (p.getName().equalsIgnoreCase("Garnetty")) { return true; }
    
    else if (p.getName().equalsIgnoreCase("Garnetty")) { return false; }
    
    else if (p.getName().equalsIgnoreCase("Garnetty")) { return null; }
    
    // Will always return true, and will NEVER return false or null
    
     
  3. Offline

    webbhead

    They are checking for 2 different potions.
     
  4. Offline

    Garnetty

    Code:
    if(a ==Action.RIGHT_CLICK_AIR|| a ==Action.RIGHT_CLICK_BLOCK&& hand !=null)
    else if(a ==Action.RIGHT_CLICK_AIR|| a ==Action.RIGHT_CLICK_BLOCK&& hand !=null)
    else if(a ==Action.RIGHT_CLICK_AIR|| a ==Action.RIGHT_CLICK_BLOCK&& hand !=null)
    
     
  5. Offline

    SuperOriginal

    else if's only run if the preceding ifs return false. Since the first if statement (the one checking the interact action) is returning true, no other code blocks will run. So you end up only checking for 'pot'.

    Edit: ^ Garnetty shows it here
     
  6. Offline

    webbhead

    Ohhh.... Now I see haha, I wasn't looking at the first line. So I should be checking the pot "Durability"(DataID) on the first line.
     
  7. Offline

    Garnetty

    It's fine, it happens. Just let us know if it worked alright
     
  8. Offline

    SuperOriginal

    @webbhead You could do that or just surround the entire InteractEvent with the action check instead of each individual potion check.
     
  9. Offline

    Garnetty

    I think that going with what @SuperOriginal said is your best bet
     
Thread Status:
Not open for further replies.

Share This Page