Compass Points to Nearest Player

Discussion in 'Plugin Development' started by Xx_LeetGamer_xX, Sep 26, 2013.

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

    Xx_LeetGamer_xX

    How would I make a compass point to the nearest player?
     
  2. Offline

    Scizzr

    Untested, but should work.
    Tailor it to your needs.
    Side note: This will need to be ran in some sort of a loop (maybe every 1 or 2 seconds?) because the compass target is a location, which would change when the nearest player moves.
    Code:
    public Player getNearestPlayer(Player player) {
        double distNear = 0.0D;
        Player playerNear = null;
        for (Player player2 : Bukkit.getOnlinePlayers()) {
            //don't include the player that's checking
            if (player == player2) { continue; }
            //make sure same world (cannot measure distance between worlds)
            if (player.getWorld() != player2.getWorld()) { continue; }
         
            Location location2 = player.getLocation();
            double dist = location.distance(location2);
            if (playerNear == null || dist < distNear) {
                playerNear = player2;
                distNear = dist;
            }
        }
        return playerNear;
    }
     
    //you need to define the player variable
    Player playerNear = getNearestPlayer(player);
    if (playerNear != null) {
        player.setCompassTarget(playerNear);
    }
    
     
  3. Offline

    chasechocolate

    Scizzr being a bit picky, but:
    • Loop through player.getWorld().getPlayers() instead of all online players, it will save that check.
    • player.setCompassTarget(playerNear.getLocation())
     
    The_Doctor_123 likes this.
  4. Offline

    Scizzr

    Thanks for advice on first one, and oversight on that last one. Typing in the forum, so didnt check it at all xD
     
  5. Offline

    xTrollxDudex

    chasechocolate
    I think he should use getNearbyEntities() within a certain distance and check if it is a player. And use distanceSquared.
     
  6. Offline

    chasechocolate

    xTrollxDudex with that, if he wanted to truly get the nearest player out of all the other players in the world, he would have to use a very large number. Also, that method returns ALL entities, so he could improve the search by only filtering players (world.getPlayers()).
     
  7. Offline

    xTrollxDudex

  8. Offline

    Licio123

    How can i make tHis plugin with point just for enimies like /friend playername
    Then don't point to friended player but only if the another player is also friended ?
     
  9. Offline

    candyfloss20

    I Know this is old, but im making a Game and want to know if this would work ??
    xTrollxDudex chasechocolate

    Code:java
    1. public xCraftPlayer getNearestUser(xCraftPlayer xUser) {
    2. double distNear = 0.0D;
    3. xCraftPlayer playerNear = null;
    4. for (xCraftPlayer xAll : allplayers) {
    5.  
    6. if (xAll == xUser) { continue; }
    7. if (Specplayers.contains(xAll)) { continue; }
    8.  
    9. Location location2 = xUser.getLocation();
    10. double dist = xAll.getLocation().distance(location2);
    11. if (playerNear == null || dist < distNear) {
    12. playerNear = xAll;
    13. distNear = dist;
    14. }
    15. }
    16. return playerNear;
    17. }
     
  10. Offline

    Jaaakee224

  11. Offline

    FabeGabeMC

  12. Offline

    fireblast709

  13. Offline

    chasechocolate

    FabeGabeMC use .distanceSquared() for code efficiency
     
    candyfloss20 likes this.
  14. Offline

    eyamaz

    Just because it is getting close to Halloween does not mean you are allowed to practice the rites of Necromancy.

    Thread locked
     
    es359, mine-care, teej107 and 2 others like this.
Thread Status:
Not open for further replies.

Share This Page