How to check which player is being looked at

Discussion in 'Plugin Development' started by chrisman0091, Aug 22, 2013.

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

    chrisman0091

    Hello guys, I am currently making a plugin where when you right click with a stick it will take certain things from the inventory of the player you were looking at. I have the event registered for right click and everything:
    Code (open)
    Code:java
    1. @EventHandler()
    2. public void onInteract(PlayerInteractEvent event) {
    3. Player player = event.getPlayer();
    4. if (event.getAction() == Action.RIGHT_CLICK_AIR) {
    5. if (player.getItemInHand().getType() == Material.STICK) {
    6. //will do stuff here
    7. }
    8. }
    9. }


    I was wondering how I would grab the player being looked at(kind of like finding the block the player is looking at) so I can continue. How would I go about doing this?
     
  2. Offline

    metalhedd

    its pretty hard... the general idea is to use player.getLineOfSight() and for every block along that path, determine if there are any players standing such that their body is inside the block, this doesn't take into consideration other entities that might be blocking the line of sight, so you should really check for any/all entities.
     
  3. Offline

    chrisman0091


    But this would pick up all players, correct? If multiple players are in the line of sight anyway.
     
  4. Offline

    metalhedd


    you have to loop through the blocks from closest to furthest, and just for good measure, do a distance check between the 2 player locations as well (to break the tie when 2 players are standing in the same 'block')
     
  5. Offline

    chrisman0091

    So all in all it would be a lot simply to say throw a snowball at the player and see who it hits or actually have to hit the player with the stick?
     
  6. Offline

    The_Doctor_123

    I created this code in my Sonic Screwdriver plugin(Note: Things have been removed to improve visibility of code you'd want):

    Code:java
    1.  
    2. List<Block> Blocks = event.getPlayer().getLineOfSight(null, 100);
    3. Blocks = repairLineOfSight(Blocks, event.getPlayer().getLocation().getPitch());
    4. for (Block block : Blocks)
    5. {
    6. List<Player> Players = event.getPlayer().getWorld().getPlayers();
    7. for (Player player : Players)
    8. {
    9. if ((player.getLocation().distance(block.getLocation()) < 0.75 || player.getEyeLocation().distance(block.getLocation()) < 0.75) && !(event.getPlayer() == player)) && !(event.getPlayer() == player))
    10. {
    11. // Do stuff
    12.  
    13. // If you'd like to stop checking for players, just break the for loop in here.
    14. }
    15. }
    16.  
    17. // This "repairs" the line of sight. If you use it without my method, it can do some weird stuff like go down through the ground and go to the top of the world..
    18. public List<Block> repairLineOfSight(List<Block> Blocks, float Pitch)
    19. {
    20. List<Block> NewBlocks = new List<Block>();
    21.  
    22. int PrevY = Blocks.get(0).getY();
    23. boolean Rising;
    24. if (Pitch < 0)
    25. {
    26. Rising = true;
    27. }
    28. else
    29. {
    30. Rising = false;
    31. }
    32. for (Block block : Blocks)
    33. {
    34. if ((block.getY() >= PrevY && Rising == true) || (block.getY() <= PrevY && Rising == false))
    35. {
    36. NewBlocks.add(block);
    37. }
    38. else
    39. {
    40. break;
    41. }
    42. }
    43. return NewBlocks;
    44. }
     
    metalhedd likes this.
  7. Offline

    Plo124

    How about the PlayerInteractEntity event?

    Code:java
    1. @EventHandler
    2. public void takeItems(PlayerInteractEntityEvent e){
    3. Player pl = e.getPlayer();
    4. if (e.getEntity() instanceof Player){
    5. //code
    6. }
    7. }


    I probably mucked up somewhere, and maybe someone else can fix up my code :p
     
Thread Status:
Not open for further replies.

Share This Page