Get what entity player clicked

Discussion in 'Plugin Development' started by Xp10d3, Sep 24, 2020.

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

    Xp10d3

    How would I check whether a player clicked, like, a cow vs an armor stand? I know there is something called entity damage event, but hypothetically let's say I cancel the event for that and I just want to check whether the player left clicked that entity whether they damage them or not. Note that I'm using version 1.8, and I don't have any code that is relevant at the moment :)
     
  2. Offline

    Shqep

  3. Offline

    gochi9

    Actually looking at the 1.8.8 java docs you can see that it only works for right click[​IMG]
    So if you want left click then you must use @Shqep and use EntityDamageByEntityEvent (i mean there is not other way around).You could check if the event is cancelled then uncancel it then get the entity and cancel the event again when you are done
     
    Xp10d3 likes this.
  4. Offline

    Shqep

    It's the same thing on the latest version by the way, that's why I still linked them despite him asking in 1.8. And the javadocs there are more detailed and updated, so it's easier to understand too.

    Why u gotta use me man I can't listen for ur events.
     
    Xp10d3 likes this.
  5. Offline

    gochi9

    Well yes i must admit that sending a ss is probably an overkill but still since he said he is using 1.8.8 he might think that there is a chance than it would've been different on some older versions so just to make sure
     
  6. Offline

    Xp10d3

    Thanks! Sorry, I meant left click not right click :/ I'll try this out and see if it works :)
    EDIT: by the way, if I try and listen on EntityDamageByEntityEvent, won't that destroy the armor stand/entity? I don't want to do that, but rather cancel the event. However if I do that won't that just return false?
    Ex.
    Code:java
    1.  
    2. // Anti-aura/anti-killaura
    3. @EventHandler
    4. public void click(EntityDamageByEntityEvent event) {
    5. Player player = (Player) event.getDamager(); // Get who left-clicked the NPC
    6. Entity entity = event.getEntity();
    7. Location loc = player.getLocation().add(player.getLocation().getDirection().multiply(-1));
    8. if (entity.getName().equalsIgnoreCase("RGBArmor") && entity.getLocation().equals(loc)) {
    9. int checks = core.getConfig().getInt("killaura." + player.getUniqueId().toString()); // Get the amount of checks a player has in the config file
    10. if (event.isCancelled()) {
    11. event.setCancelled(false);
    12. }
    13.  
     
  7. Offline

    gochi9

    No this won't do.If the event is cancelled this won't do a thing.First you should give the event the highest priority, so the plugin will run it last, after that you should start the event with the check and after do your thing
    Example
    Code:
        @EventHandler (priority = EventPriority.HIGHEST) //Run the event last
        public void click(EntityDamageByEntityEvent event) {
            if (event.isCancelled()) event.setCancelled(false);//Check if the event is cancelled and enable it again
           
            if(!(event.getDamager() instanceof Player)) return; //Check if the damager is a player
           
            Player player = (Player) event.getDamager(); // Get who left-clicked the NPC
            Entity entity = event.getEntity();
            Location loc = player.getLocation().add(player.getLocation().getDirection().multiply(-1));
           
            if (entity.getName().equalsIgnoreCase("RGBArmor") && entity.getLocation().equals(loc)) {
                //Do your thing
                event.setCancelled(true);//Cancel it again if you want to
            }
        }
    
     
  8. Offline

    Xp10d3

    Ah, I see. That makes sense, thanks! I understand it now :) I'll test this out and let you know whether it works or not before marking this as solved :D
    EDIT: Hmmm, I seem to have an issue in that the armor stand I'm trying to spawn doesn't teleport to the player. Bit confused on that, and I don't think an infinite loop works for this either. I'll do some more testing first.
     
    Last edited: Sep 25, 2020
Thread Status:
Not open for further replies.

Share This Page