Getting all players within a radius, and something else

Discussion in 'Plugin Development' started by C0nsole, May 17, 2013.

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

    C0nsole

    Okay, so when a player executes a command I want to get all players within a certain radius of the sender, and spit them back out to the sender, along with each players exact coordinates (Only really X and Z but meh :p )
    Thanks alot!
     
  2. Offline

    caseif

    There's a method in the Player class called getNearbyEntities(). It returns a List which you can iterate through and then add the players to another list.
     
  3. Offline

    C0nsole

    AngryNerd
    Thanks, I'll work on that and see if I have any more problems
    Is the correct context player.getNearbyEntites ? If so what type of variable is it best to store it as?
     
  4. Offline

    caseif

    The method would return an Entity List, and thus you would want to store it as such. I may have been mistaken when I said it was in the Player class; try getting an Entity instance from the Player and calling it on that.
     
  5. Offline

    C0nsole

    AngryNerd
    I've got the following:
    Code:java
    1. List<Entity> nbe = new ArrayList<Entity>();
    2. nbe = player.getNearbyEntities(1000, 1000, 1000);

    And how do you suggest I format it and send it to the player? Never worked with this type of thing before...
     
  6. AngryNerd C0nsole
    I wouldn't use getNearbyEntites() if you are only concerned with players as that method would loop over every entity in the world.

    Instead i'd recommend using Bukkit.getOnlinePlayers() and then compare the distance of each players location to the point you are checking against.
     
  7. Offline

    EcMiner

    Code:java
    1. for(Entity e : player.getNearbyEntities(100,100,100)) {
    2. if(e instanceof Player) {
    3. Player target = (Player)e;
    4. }
    5. }


    I think this should work
     
  8. Offline

    caseif

    Well, you would first need to figure out which entities are players with a foreach loop, then add the players to a separate list (preferably a String List which stores the player's usernames). Once you have that done, you can iterate through that list and append to it using either the += operator or a StringBuilder. In each loop, add the value of the current index, then add ", ". After the loop, you can trim the last comma and space off by calling .substring(0, string.length()) on the String you've built and send it to the player.
     
  9. Offline

    C0nsole

    And then what would be the best way to send it?
    Alright, getting slightly lost here. Just by me saying that don't think I'm a total noob. Just, like I said:
     
  10. Offline

    caseif

    I would recommend using a slightly modified version of EcMiner 's code, where you store ((Player)e).getName() to a List defined outside of the foreach. Then, conduct another foreach on that List that you've been adding the player names to, and append a String (also defined outside of this foreach) with each index followed by ", ". It should look something like this:
    Code:
    String message = "";
    for (String s : players){
        message += s;
        message += ", ";
    }
    message = message.substring(0, message.length() - 2); // to trim off the last two characters
     
  11. C0nsole
    Code:java
    1.  
    2. Player sender = null;
    3.  
    4. List<Player> list = new ArrayList<Player>();
    5.  
    6. Location source = sender.getLocation();
    7.  
    8. for (Player p : Bukkit.getOnlinePlayers()) {
    9. // chance 100 to whatever you need
    10. if (p.getLocation().distance(source) < 100) {
    11. list.add(p);
    12. // never store the Player object for prolonged periods of time,
    13. // it causes memory leaks.
    14. // in this instance we will clear the list after so it's OK to
    15. // do so.
    16. }
    17. }
    18.  
    19. StringBuilder b = new StringBuilder().append("Nearby Players : ");
    20.  
    21. for (Player p : list) {
    22. if (p != null && p.isOnline()) {
    23. Location l = p.getLocation();
    24. b.append(p.getName()).append("{").append(l.getBlockX()).append(", ").append(l.getBlockZ()).append("}, ");
    25. }
    26. }
    27.  
    28. if (!list.isEmpty()) {
    29. sender.sendMessage(b.toString());
    30. } else {
    31. sender.sendMessage("No nearby players.");
    32. }
    33.  
    34. list.clear();
    35. list = null; // remove player references
    36.  
     
  12. Offline

    C0nsole

    Adamki11s
    I get this error when running the command
    Code:
    18:05:38 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'track' in plugin Track v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:189)
        at org.bukkit.craftbukkit.v1_5_R3.CraftServer.dispatchCommand(CraftServer.java:523)
        at net.minecraft.server.v1_5_R3.PlayerConnection.handleCommand(PlayerConnection.java:965)
        at net.minecraft.server.v1_5_R3.PlayerConnection.chat(PlayerConnection.java:883)
        at net.minecraft.server.v1_5_R3.PlayerConnection.a(PlayerConnection.java:840)
        at net.minecraft.server.v1_5_R3.Packet3Chat.handle(Packet3Chat.java:44)
        at net.minecraft.server.v1_5_R3.NetworkManager.b(NetworkManager.java:292)
        at net.minecraft.server.v1_5_R3.PlayerConnection.d(PlayerConnection.java:109)
        at net.minecraft.server.v1_5_R3.ServerConnection.b(SourceFile:35)
        at net.minecraft.server.v1_5_R3.DedicatedServerConnection.b(SourceFile:30)
        at net.minecraft.server.v1_5_R3.MinecraftServer.r(MinecraftServer.java:581)
        at net.minecraft.server.v1_5_R3.DedicatedServer.r(DedicatedServer.java:226)
        at net.minecraft.server.v1_5_R3.MinecraftServer.q(MinecraftServer.java:477)
        at net.minecraft.server.v1_5_R3.MinecraftServer.run(MinecraftServer.java:410)
        at net.minecraft.server.v1_5_R3.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
        at me.C0nsole.Track.Track.onCommand(Track.java:56)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
        ... 15 more
    
    This is my code:
    Code:java
    1. if(player.hasPermission("track.track")){
    2. if(args.length == 0){
    3. Player sender = null;
    4.  
    5. List<Player> list = new ArrayList<Player>();
    6.  
    7. Location source = sender.getLocation();
    8.  
    9. for (Player p : Bukkit.getOnlinePlayers()) {
    10. if (p.getLocation().distance(source) < 100) {
    11. list.add(p);
    12. }
    13. }
    14.  
    15. StringBuilder b = new StringBuilder().append("Nearby Players : ");
    16.  
    17. for (Player p : list) {
    18. if (p != null && p.isOnline()) {
    19. Location l = p.getLocation();
    20. b.append(p.getName()).append("{").append(l.getBlockX()).append(", ").append(l.getBlockZ()).append("}, ");
    21. }
    22. }
    23.  
    24. if (!list.isEmpty()) {
    25. sender.sendMessage(b.toString());
    26. } else {
    27. sender.sendMessage("No nearby players.");
    28. }
    29.  
    30. list.clear();
    31. list = null;
    32. }
    33. }

    Is this a problem?:
    [​IMG]
     
  13. Offline

    EcMiner

    It's not smart to get the location of a sender, the sender could also be the Console, do Location source = player.getLocation();
     
  14. C0nsole
    You can't leave the sender (Player) variable null.

    If running this from onCommand you need to check if the sender is an instanceof a Player and then cast the sender to a Player object and then use that.
     
    C0nsole likes this.
Thread Status:
Not open for further replies.

Share This Page