Distances

Discussion in 'Resources' started by nitrousspark, Apr 4, 2013.

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

    nitrousspark

    heres a class in my plugin that gets you a bunch of different stuff, such as getting the distance from a location to another location(rough or exact), and getting all the players in a radius. hope it helps you

    the entire class:
    http://pastie.org/7323876

    theres code for getting the distance from a location to another

    Code:
     
     
    public static int roughDistanceFromLocation(Location loc, Location loc1) {
    int x2 = (int) loc.getX();
    int x1 = (int) loc1.getX();
    int y2 = (int) loc.getY();
    int y1 = (int) loc1.getY();
    int z2 = (int) loc.getZ();
    int z1 = (int) loc1.getZ();
    int ad = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1)
    * (z2 - z1);
    int d = (int) Math.sqrt(ad);
    return d;
    }
     
    public static double exactDistanceFromLocation(World world, Location loc,
    Location loc1) {
    double x2 = loc.getX();
    double x1 = loc1.getX();
    double y2 = loc.getY();
    double y1 = loc1.getY();
    double z2 = loc.getZ();
    double z1 = loc1.getZ();
    double ad = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1)
    * (z2 - z1);
    double d = Math.sqrt(ad);
    return d;
    }
     
     
    public static double exactDistanceFromSpawn(World world, Location loc) {
    double x2 = loc.getX();
    double x1 = world.getSpawnLocation().getX();
    double y2 = loc.getY();
    double y1 = world.getSpawnLocation().getY();
    double z2 = loc.getZ();
    double z1 = world.getSpawnLocation().getZ();
    double ad = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1)
    * (z2 - z1);
    double d = Math.sqrt(ad);
    return d;
    }
    i have code to get all the players in a radius of a location

    Code:
     
     
    public static ArrayList<Player> playersDistance(Location loc, double radius) {
     
    ArrayList<Player> players = new ArrayList<Player>();
     
    double i1 = loc.getX();
    double j1 = loc.getY();
    double k1 = loc.getZ();
     
    for (Player player : Bukkit.getOnlinePlayers()) {
     
    if (player.getWorld().equals(loc.getWorld())) {
     
    double i2 = player.getLocation().getX();
    double j2 = player.getLocation().getY();
    double k2 = player.getLocation().getZ();
     
    double ad = Math.sqrt((i2 - i1) * (i2 - i1) + (j2 - j1)
    * (j2 - j1) + (k2 - k1) * (k2 - k1));
     
    if (ad < radius) {
    players.add(player);
    }
     
    }
     
    }
     
    return players;
     
    }
    i also have code to get the closest entity and player

    Code:
     
     
    public Entity getClosestEntity(Player player, double radius) {
    double minimalDistance = Math.pow(radius, 2);
    double curDist;
    Entity closest = null;
    for (Entity p : player.getNearbyEntities(1000, 1000, 1000)) {
    if (!p.equals(player)) {
    curDist = player.getLocation().distanceSquared(p.getLocation());
    if (curDist < minimalDistance) {
    minimalDistance = curDist;
    closest = p;
    }
    }
    }
    return closest;
    }
     
    public static Player getClosestPlayer(Player player, double radius) {
    double minimalDistance = Math.pow(radius, 2);
    double curDist;
    Player closest = null;
    for (Player p : Bukkit.getOnlinePlayers()) {
    curDist = player.getLocation().distanceSquared(p.getLocation());
    if (curDist < minimalDistance) {
    minimalDistance = curDist;
    closest = p;
    }
    }
    return closest;
    }
    there were a couple of other things there but i didnt put them in this little list of them because they werent really useful.
     
  2. Offline

    chasechocolate

    For locations you can just use <location 1>.distance(<location 2>).
     
    MrBluebear3 and mncat77 like this.
  3. Offline

    nitrousspark

    since when was that added?
     
  4. Offline

    chasechocolate

    I think since 1.4.5, don't remember exactly.
     
  5. dont use Math.sqrt inside a loop (getting al player near some player) its slow and can be avoided by multipling the other argument by it self inside the comparison
     
    bobacadodl likes this.
  6. Offline

    nitrousspark

  7. Offline

    nitrousspark

    does the .distance() use a sqrt in it? and is that why theres distanceSquared()? so that you dont have to use the sqrt
     
  8. Offline

    Me4502

    That has been there long before 1.4.5, it's been there atleast a year.
     
Thread Status:
Not open for further replies.

Share This Page