Detect nearby players

Discussion in 'Plugin Development' started by Liutenantpickle, Sep 1, 2012.

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

    Liutenantpickle

    Trying to make a plugin for my Hunger games server, Max distance is about 40-60 Blocks
    This is all I got for my "if" statement
    if (other.getLocation().distance(player.getLocation()) <= maxDist) {
    other.sendMessage("Someone is nearby.");

    }

    Can someone help me with this? Thanks :)
     
  2. Offline

    WarmakerT

    for(Entity entity : Entity.getNearbyEntities(arg, arg, arg)){
    if(entity instanceof Player){
    Player player = (Player) entity;
    player.sendMessage(ChatColor.GOLD + player.getName() + "§e is nearby");
    }
    }
     
  3. Offline

    amitlin14

    from my rpg plugin


    Code:java
    1. public void AlertNearby(Player player) {
    2. for (Player p : Bukkit.getOnlinePlayers()) {
    3. if (p != player && p.getWorld() == player.getWorld()
    4. && p.getLocation().distance(player.getLocation()) < 16)
    5. p.sendMessage(ChatColor.DARK_RED
    6. + "[ALERT]"
    7. + ChatColor.RED
    8. + " "
    9. + player.getDisplayName()
    10. + " died in an area close to you and has become a zombie!");
    11. }
    12. }


    tells the nearby players that a player died and turned to a zombie.

    just change some stuff and itll work fine.

    this is better from war marker's since it iterates through the online players rather then the entities that are nearby the player thus saving a fair amount of memory usage. combine this with warmaker's and itll work perfectly.
     
  4. Offline

    Liutenantpickle

    What do I put on Warmarker's args? on Entity.getNearbyEntities(arg, arg, arg)){
     
  5. Offline

    caldabeast

    the radius to check for in x, y, z
    I usually just have them all the same.

    Entity.getNearbyEntities(5, 5, 5) would return any Entity within 5 blocks, any direction
    Entity.getNearbyEntities(50, 50, 50) would return any Entity within 50 blocks, any direction
     
  6. Offline

    amitlin14

    dont use that, that searches for ALL entities (xp orbs, drops, rain, mobs, etc), mine just searches in the online players so it saves a fair amount of memory

    Code:java
    1.  
    2. public void AlertNearby(Player player) {
    3. for (Player p : Bukkit.getOnlinePlayers()) {
    4. if (p != player && p.getWorld() == player.getWorld()
    5. && p.getLocation().distance(player.getLocation()) < 25) {
    6. player.sendMessage("shhh. your not alone.");
    7. }
    8. }
    9. }
    10.  


    I just use this inside an event, but if you want it to be on its own, just impement PlayerMoveEvent evt, so itll turn out like this:

    Code:java
    1.  
    2. public void AlertNearby(PlayerMoveEvent evt) {
    3. Player player = evt.getPlayer();
    4. for (Player p : Bukkit.getOnlinePlayers()) {
    5. if (p != player && p.getWorld() == player.getWorld()
    6. && p.getLocation().distance(player.getLocation()) < 25) {
    7. player.sendMessage("shhh. your not alone.");
    8. }
    9. }
    10. }
    11.  



    Im writing this on the browser, so i havnt tested it, but it should work.

    also, if you dont want it to post the message for each player in the radius, just do this:

    Code:java
    1.  
    2. public void AlertNearby(PlayerMoveEvent evt) {
    3. Player player = evt.getPlayer();
    4. boolean notAlone = false;
    5. for (Player p : Bukkit.getOnlinePlayers()) {
    6. if (p != player && p.getWorld() == player.getWorld()
    7. && p.getLocation().distance(player.getLocation()) < 25) {
    8. notAlone = true;
    9. }
    10. }
    11. if (notAlone) {
    12. player.sendMessage("shhh. your not alone.");
    13. }
    14. }
    15.  


    all these search in a radius of 25 blocks (50 by 50 square) so they fit exactly wat u want, just change the radius to wat u want.
    That should work ^-^ enjoy.
     
  7. Offline

    Liutenantpickle

    Thanks guys! :) Appreciated all your help :)
     
  8. Offline

    amitlin14

    np, any time.
     
Thread Status:
Not open for further replies.

Share This Page