Getting players in a circle radius

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

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

    kreashenz

    Hey.. I've come across a thing where I have to get players in circle and give them and effect. Also, in that circle I have to create the smoke effect.

    I have these methods that help, but I was told the getPlayers(...) wouldn't work.

    Code:java
    1. public static List<Block> getBlocks(Location center, int radius, boolean hollow, boolean sphere){
    2. List<Location> locs = circle(center, radius, radius, hollow, sphere, 0);
    3. List<Block> blocks = new ArrayList<Block>();
    4.  
    5. for(Location loc : locs){
    6. blocks.add(loc.getBlock());
    7. }
    8.  
    9. return blocks;
    10. }
    11.  
    12. public static List<Player> getPlayers(Location center, int radius, boolean hollow, boolean sphere){
    13. List<Location> locs = circle(center, radius, radius, hollow, sphere, 0);
    14. List<Player> players = new ArrayList<Player>();
    15.  
    16. for(Location loc : locs){
    17. for(Player p : Bukkit.getOnlinePlayers()){
    18. if(p.getLocation().equals(loc)){
    19. players.add(p);
    20. }
    21. }
    22. }
    23.  
    24. return players;
    25. }
    26.  
    27. private static List<Location> circle(Location loc, int radius, int height, boolean hollow, boolean sphere, int plusY){
    28. List<Location> circleblocks = new ArrayList<Location>();
    29. int cx = loc.getBlockX();
    30. int cy = loc.getBlockY();
    31. int cz = loc.getBlockZ();
    32.  
    33. for(int x = cx - radius; x <= cx + radius; x++){
    34. for (int z = cz - radius; z <= cz + radius; z++){
    35. for(int y = (sphere ? cy - radius : cy); y < (sphere ? cy + radius : cy + height); y++){
    36. double dist = (cx - x) * (cx - x) + (cz - z) * (cz - z) + (sphere ? (cy - y) * (cy - y) : 0);
    37.  
    38. if(dist < radius * radius && !(hollow && dist < (radius - 1) * (radius - 1))){
    39. Location l = new Location(loc.getWorld(), x, y + plusY, z);
    40. circleblocks.add(l);
    41. }
    42. }
    43. }
    44. }
    45.  
    46. return circleblocks;
    47. }


    Hopefully someone understands what I'm trying to do. Thanks in advanced!
     
  2. Offline

    1Rogue

    Code:java
    1. public List<String> getPlayersInRange(double radius, Location loc) {
    2. List<String> players = new LinkedList();
    3. for(Player p : Bukkit.getOnlinePlayers()){
    4. if(p.getLocation().distance(loc) <= radius){
    5. players.add(p.getName());
    6. }
    7. }
    8. return players;
    9. }


    Then do whatever to the corresponding players.
     
  3. Offline

    kreashenz

    1Rogue What's "loc" meant to be..?
     
  4. Offline

    1Rogue

    Sorry about that, I meant to pass it in the method. But you should be able to just pass the center of the circle using this.
     
Thread Status:
Not open for further replies.

Share This Page