PlayerInteractEvent Help!

Discussion in 'Plugin Development' started by GEETAFF, Jul 24, 2014.

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

    GEETAFF

    So the problem is when i right click the item it wont activate the command. Heres my code:

    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event)
        {
            Player player = event.getPlayer();
     
            if(event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR)
            {
                if(event.getItem().getType() == Material.APPLE)
                {
                    {
                        player.playSound(player.getLocation(), Sound.BURP, 3F, 1F);
                        player.getWorld().playEffect(player.getLocation(), Effect.EXTINGUISH, 500);
                        player.setVelocity(player.getEyeLocation().getDirection().multiply(2.0));
                        player.sendMessage(ChatColor.GREEN + "You farted loudly and proudly!");
                    }
                }
            }
        }
     
  2. Offline

    Gnat008

    GEETAFF
    You appear to have an extra set of { }.
     
  3. Offline

    GEETAFF

    Oh I think that was just a mess up cause I had to quickly re-code this.. Ill try though

    Ok, I just tried it, that didnt fix it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    Dragonphase

    Gnat008
    Nothing wrong with that, other than to provide a scope limit.
     
  5. Offline

    Gnat008

    Dragonphase
    Interesting. Was not aware of that. Thanks!

    GEETAFF
    Disregard what I said earlier.
     
    Dragonphase likes this.
  6. Offline

    DannyDog

    GEETAFF
    Can you show us any error or does it just not do anything?
    One of your checks if probably false.
    Add some flags to check.
     
  7. Offline

    Gamesareme

    I would have thought you would get what the player was holding rather than what you have. Like this.
    Code:
    player.getItemInHand().getType() == Material.APPLE
     
  8. Offline

    DannyDog

    GEETAFF
    Just realized that you don't need to check if the action was left clicking a block or right clicking air because the event already has that.
    It will fire if they right or left click.

    Also Gamesareme is right.
    You should be getting the item that the player is holding.
     
  9. Offline

    fireblast709

  10. Offline

    Gamesareme

    fireblast709
    I do, but I have not seen any other plugins use that way of getting what item was used before. That is why I suggested what I did.
     
  11. Offline

    DannyDog

    fireblast709
    Yea I do javadoc, why you think i don't javadoc in my life?
     
  12. Offline

    fireblast709

    Gamesareme I could potentially set the item in hand before your eventhandler gets called ;)

    DannyDog the fact that you suggested that it was correct should already indicate that you missed that getItem() is the same as getItemInHand(). In fact, as stated above, it could be considered more robust.
     
    AoH_Ruthless likes this.
  13. Offline

    DannyDog

    fireblast709
    Lol looks like I don't javadoc
    Never read what event.getItem() returned.
    My life is a lie.
     
Thread Status:
Not open for further replies.

Share This Page