Solved Get entity player is looking

Discussion in 'Plugin Development' started by au2001, Aug 7, 2014.

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

    au2001

    Hello Bukkit,

    I'm developing a plugin for my server (not open yet).
    I wanted to make a feature where when you hit a player it throws him in the air.
    I used the EntityDamageByEntityEvent for that, it worked fine.
    But because admins are in gm 1, the "EntityDamageByEntityEvent" doesn't fire...
    So I used the PlayerAnimationEvent but the problem is:
    How do I get the entity the player is looking ? (Entity would be better then Player)

    So if anyone knows how to get the entity a player is looking, would be great !
    You might want to use player.getEyeLocation().getDirection() and get entity on this line (but how!)

    Thanks for helping,
    - au2001
     
  2. Offline

    au2001

    Bump :)
     
  3. Offline

    TheWolfBadger

    au2001 Checking from eye level and iterating through the blocks in the direction they are looking. Make sure to subtract 2 from the y for each of those blocks though. Then I would cycle through the players on the server's locations and see if they match up. With this method you can make a distance for the iterator also.
     
  4. Offline

    teej107

  5. Offline

    Androm

    Code:java
    1. import org.bukkit.entity.Player;
    2. import org.bukkit.util.Vector;
    3. import org.bukkit.entity.Entity;
    4.  
    5. public class Targeter {
    6.  
    7. public static Player getTargetPlayer(final Player player) {
    8. return getTarget(player, player.getWorld().getPlayers());
    9. }
    10.  
    11. public static Entity getTargetEntity(final Entity entity) {
    12. return getTarget(entity, entity.getWorld().getEntities());
    13. }
    14.  
    15. public static <T extends Entity> T getTarget(final Entity entity,
    16. final Iterable<T> entities) {
    17. if (entity == null)
    18. return null;
    19. T target = null;
    20. final double threshold = 1;
    21. for (final T other : entities) {
    22. final Vector n = other.getLocation().toVector()
    23. .subtract(entity.getLocation().toVector());
    24. if (entity.getLocation().getDirection().normalize().crossProduct(n)
    25. .lengthSquared() < threshold
    26. && n.normalize().dot(
    27. entity.getLocation().getDirection().normalize()) >= 0) {
    28. if (target == null
    29. || target.getLocation().distanceSquared(
    30. entity.getLocation()) > other.getLocation()
    31. .distanceSquared(entity.getLocation()))
    32. target = other;
    33. }
    34. }
    35. return target;
    36. }
    37.  
    38. }

    This code is not from me, but since I develop plugins only for my server I don´t know who made this code^^
    to get the entity the player is facing at simple call:
    Code:java
    1. getTargetEntity(your_player)//or
    2. getTargetEntity(your_entity)
    3.  

    To get a player replace Entity with player. It returns null if there is no entity.
     
  6. Offline

    Necrodoom

    Wouldnt a playerinteractentity event fire? (On rightclick)
     
  7. Offline

    au2001

    TheWolfBadger That would be really buggy and would take all lot of server ressources... Looping through each entity 5 times...
    And players could click every tick or so... that would make the server lag a lot...
    I think there's another way to get the entity...

    @Necrodoom Maybe it does ! I'll try that ! Thanks :D

    @Androm if Necrodoom's code doesn't work I'll try yours, looks fine

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

    Androm

    I used my code to sitch place with entites if you right-click and sneak with a special item and it´s fast because it returns the first (LivingEntity/Creature..) entity in the direction a player is facing. But for your things Necrodoom ´s code is better^^.
     
  9. Offline

    Garris0n

  10. Offline

    teej107

  11. Offline

    au2001

    For right clicking, @Necrodoom's code is working perfect :D
    For left clicking, Garris0n is right :p but @Androm's code works fine :)

    Thanks everyone for helping ;)
     
    bennie3211 likes this.
Thread Status:
Not open for further replies.

Share This Page