Need help with radius

Discussion in 'Plugin Development' started by Burnsalan18, Mar 30, 2013.

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

    Burnsalan18

    Ive been looking everywhere for a good tutorial on how to do this, Ive even asked my friends but they dont know the bukkit api and I really dont have anywhere else to turn.

    I need to check if a player is within the radius of a block that is configured in another part of the plugin. The problem is, is that I dont know how to check the radius. If anyone can help it would be greatly appreciated. (Ive posted here once before about the problem but the only response I got was not helpful at all, examples of what I need to do would make me extremely greatful)
     
  2. Offline

    LazyLemons

    Code:
    public static boolean isInBorder(Location center, Location toCheck,
    int range) {
    int x = center.getBlockX(), z = center.getBlockZ();
    int x1 = toCheck.getBlockX(), z1 = toCheck.getBlockZ();
     
    if (x1 >= (x + range) || z1 >= (z + range) || x1 <= (x - range)
    || z1 <= (z - range)) {
    return false;
    }
    return true;
    }
    
     
  3. Offline

    Burnsalan18

  4. Offline

    LazyLemons

    The isInBorder method checks a location "toCheck" to see if it is in a circle of radius "range" where "center" is the circle's center.
    You can do
    Code:
    Location loc = player2.getLocation();
    Location loca = player.getLocation();
    int radius = 5;
    if (isInBorder(loc, loca, 5) {
    //do whatever
    } 
    
    That takes player2's location(loc), as the center, and checks if player(loca) is withing a 5 block radius around them.
     
  5. Offline

    skipperguy12

    LazyLemons
    Can you also explain how the code works? I'm horrible with loops and math D:

    Edit: Drew a grid on some paper, and worked it out. Thanks, also, why do you check the last two || statements? You don't need it, it's doing the same thing as the first 2 statements but...well...reversed
     
  6. Offline

    JazzaG

  7. Offline

    iZanax

  8. Offline

    Burnsalan18

    Okay, I get it now, but how would I make the center a specific coord? And would I be able to put this in a Blockbreakevent?
     
  9. Offline

    LazyLemons

    Code:
    Location center = new Location(e.getPlayer().getWorld(), XCOORD, YCOORD, YZOORD);
    
     
  10. Offline

    nitrousspark

    i have code for this, and can you explain to me exacly what lengthSquared() does?

    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;
     
    }
     
Thread Status:
Not open for further replies.

Share This Page