Teleporting a player on your cross-hair to you

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

Thread Status:
Not open for further replies.
  1. Hello. Is it possible to teleport a player that you are looking at, to you? If so, could I see maybe an example? I was told it was pretty difficult to do.
    Thanks.
     
  2. Isn't that something with player.hasLineOfSight(other player);?
     
  3. Offline

    Chinwe

    Possibley get the block at player.getTargetBlock(), then spawn an entity (invisible?), getNearbyEntites(), sort out the players from the list, and teleport the nearest one?

    Though this wouldn't work if they were looking directly at them :<
     
  4. Offline

    Zarkopafilis

    Yes , it returns a boolean, get the nearby entitiees and tp the closest player(or a specified player)?
     
  5. Offline

    KingNyuels

    Try this:
    Code:java
    1. public LivingEntity getTarget(Player player) {
    2. int range = 60;
    3. List<Entity> nearbyE = player.getNearbyEntities(range, range, range);
    4. ArrayList<LivingEntity> livingE = new ArrayList<LivingEntity>();
    5.  
    6. for (Entity e : nearbyE) {
    7. if (e instanceof LivingEntity) {
    8. livingE.add((LivingEntity) e);
    9. }
    10. }
    11.  
    12. LivingEntity target = null;
    13. BlockIterator bItr = new BlockIterator(player, range);
    14. Block block;
    15. Location loc;
    16. int bx, by, bz;
    17. double ex, ey, ez;
    18. // loop through player's line of sight
    19. while (bItr.hasNext()) {
    20. block = bItr.next();
    21. bx = block.getX();
    22. by = block.getY();
    23. bz = block.getZ();
    24. // check for entities near this block in the line of sight
    25. for (LivingEntity e : livingE) {
    26. loc = e.getLocation();
    27. ex = loc.getX();
    28. ey = loc.getY();
    29. ez = loc.getZ();
    30. if ((bx - .75 <= ex && ex <= bx + 1.75)
    31. && (bz - .75 <= ez && ez <= bz + 1.75)
    32. && (by - 1 <= ey && ey <= by + 2.5)) {
    33. // entity is close enough, set target and stop
    34. target = e;
    35. break;
    36. }
    37. }
    38. }
    39. return target;
    40.  
    41. }


    Then call the method and teleport the player:
    Code:java
    1. getTarget(player).teleport(player.getLocation());


    KingNyuels
     
  6. Offline

    Chinwe

    KingNyuels
    Almost exactly what I thought of a minute ago, BlockIterators :)
    Noice solution :rolleyes:
     
  7. KingNyuels
    It works. Thank you very much :)

    Wait, if they don't look at a player, it throws a NPE.
     
  8. Offline

    metalhedd

    KingNyuels Thx for posting that. First time seeing BlockIterator. very cool
     
  9. Offline

    Chinwe

    You'd better check if getTarget(player) != null before teleporting then :)
     
Thread Status:
Not open for further replies.

Share This Page