Is it possible to detect if a player left/right clicked an item?

Discussion in 'Plugin Development' started by uksspy, Jan 29, 2015.

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

    uksspy

    I have been trying to detect if a player right clicks an item entity with no luck.
    I tried the PlayerInteractEntityEvent and tested if .getRightClicked was an instance of Item.

    Here is the snippet of code showing what I did:
    Code:
    @EventHandler
        public void onEntityInteract(PlayerInteractEntityEvent event){
            if(event.getRightClicked() instanceof Item){
    Is this even possible with bukkit?
     
  2. Offline

    Xtreme727

    If you are testing items and only items, use PlayerInteractEvent. Then use event.getItem() instead of getRightCLicked(). To test if it was right or left click, use something like: if (event.getAction() != Action.<something here>) return;
     
  3. Offline

    uksspy

    @Xtreme727
    Doesn't PlayerInteractEvent's event.getItem() return the ItemStack the player is holding? If so, I believe you misunderstood my question. I am looking for a way to detect if a player clicked and Item entity, so like a dropped item.
     
  4. Offline

    Xtreme727

    @uksspy Ohhhh I see what you mean, sorry ;P Using Item, and the org.bukkit.entity.Item(i think thats it) should possibly work
     
  5. Offline

    Konato_K

    @uksspy I remember testing this back in 1.6 and it was not possible, if something changed since then or there is a way to do it it's beyond my knowledge.
     
  6. Offline

    FerusGrim

    PlayerInteractEntityEvent#getRightClicked returns an Entity object.

    An Entity can be an Item - and you would need to do some testing to prove what I'm about to say - but I believe that the Entity Item refers to ItemStacks that are on the ground and, perhaps, in ItemFrames.
     
  7. Offline

    LegacyGaming

    Like this?
    Code:
    @EventHandler
    public void PlayerInteract(PlayerInteractEvent event) {
    if(event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    //Code here
    }
    
     
  8. Offline

    nverdier

    @LegacyGaming No, you would only check for the needed Action(s)...
     
  9. Offline

    FerusGrim

    Not to mention that that's the wrong event, and those methods wouldn't work inside of the one OPs attempting to use, as one does not extend the other.
     
  10. Offline

    nverdier

    @FerusGrim haha didn't catch that. What I get for just skimming :p
     
  11. Offline

    uksspy

    My thinking is that while the Entity technically can be an Item it is impossible, or even just really hard (meaning you have to click it exactly), to interact with an Item entity.
     
  12. Offline

    FerusGrim

    All Item objects extend Entity, meaning all Items are Entity objects.
     
  13. Offline

    uksspy

    I was referring to the Entity that event.getRightClicked() returns. All Items are Entities but not all Entities are Items. :p
     
  14. Offline

    FerusGrim

    @uksspy I guess I'm confused as to your meaning. That's what the:
    Code:java
    1. if (event.getRightClicked() instanceof Item)
    check if for, non?
     
Thread Status:
Not open for further replies.

Share This Page