Messaging Near Players

Discussion in 'Plugin Development' started by The_BloodHound, Jun 30, 2015.

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

    The_BloodHound

    Okay, so I'm making a plugin and I need to message all the players within a certain radius of a block. How would I do this? And there's no point in asking for my code because it'd be no help to you. All answers are appreciated whether they work or not :)
     
  2. @The_BloodHound Player#getNearbyEntitites check if they're a player and then message them.
     
  3. Offline

    nj2miami

    @The_BloodHound It is not possible directly to get entities around a block, however you can get nearby entities from other entities. If you have a block location, spawn an arrow or something at that location, gather the nearby entities then remove the arrow or other "holder" entity.
     
    Shortninja66 likes this.
  4. @nj2miami
    OR just iterate over all entities in the world and check their distance to the location :/
     
    Shortninja66 likes this.
  5. Offline

    Hawktasard

    @The_BloodHound
    Code:java
    1. Block b = null;
    2. for(Player player : b.getWorld().getPlayers()) {
    3. if(player.getLocation().distanceSquared(b.getLocation()) <= (radius*radius))
    4. player.sendMessage("A message.");
    5. }


    Something like that, haven't looked at the code for distanceSquared so maybe the radius * radius should be just radius but it should work.
     
  6. Offline

    nj2miami

  7. @nj2miami
    You should learn some more :). It's the same method entity.getNearbyEntities() uses but easier. If you look through the code of getNearbyEntities() it ends up doing the same thing
     
    Hawktasard and _Filip like this.
  8. Offline

    The_BloodHound

    Thanks for the reply everyone, I'll look over the code and test these.
    Also, maybe I can spawn a pig at a certain location and get the nearby entities within a radius and message them.
     
  9. @The_BloodHound Spawning a pig could alter the result. Not by much, but it still can. Try a non-moving entity that is small, such as an arrow. All you would have to do is spawn the arrow above the block and make sure player's aren't allowed to pick it up.
     
    The_BloodHound likes this.
  10. Offline

    teej107

    I actually think it's better because
    1. You don't need to create an Entity
    2. You aren't iterating over every entity. Only the players
    @The_BloodHound You don't need to iterate over Entities that you don't need. Just iterate over the players in the world and compare their distances.
     
    Hawktasard likes this.
  11. Offline

    The_BloodHound

    What does "iterate" or "iterating" mean? There are some terms that I'm unfamiliar with in coding :p
    Also, I was wondering if place a command block with the tell raw command and powering it with a redstone block would be another way.
     
    Last edited: Jul 2, 2015
  12. @The_BloodHound Iterate basically means to loop through something. So, if you're iterating through a list, you're looping through the entire list.
     
    The_BloodHound likes this.
  13. Offline

    WPM

  14. Offline

    teej107

    I don't see how that would have to do with Bukkit. I don't know if there is an in-game command to get entities in a radius.
     
  15. Offline

    The_BloodHound

    You can do /tellraw @a[r=Radius] {text:"Blah blah text here",color:aqua} and I was thinking I can just place a command block with that command inside it (Meta data I guess I'd be using)
     
  16. Offline

    teej107

    @The_BloodHound What's wrong with doing it with code? If you use command blocks, Bukkit isn't needed.
     
    dlange likes this.
  17. Offline

    The_BloodHound

    I'm not sure how to place it with the command in it. (with bukkit)
     
  18. Offline

    teej107

  19. Offline

    mythbusterma

    @The_BloodHound

    You don't need a command block to do this, however, and that's a terrible solution.
     
  20. Offline

    The_BloodHound

    Okay thanks for the feedback.
     
  21. Offline

    nj2miami

    LOL. Yah totally efficient to constantly have to iterate over every entity in a world to check if they are within a relative distance to a location. *smh*
     
  22. @nj2miami
    http://pastebin.com/U3QyUvie
     
    Hawktasard likes this.
  23. Offline

    mythbusterma

    > Being this ignorant

    That is exactly what Entity#getNearbyEntities(...) does.

    Not that iterating over a few thousand things and performing a simple calculation is that expensive, anyway. The fastest way to is iterate over every Player in the world and check their distance, however.
     
    Last edited: Jul 4, 2015
    Hawktasard and teej107 like this.
  24. Offline

    nj2miami

    I know what getNearby does, my point stands that if you wanted to message Players near a specific location, which of these would you choose? I don't really see why you would write the additional lines of code to do a or b when Bukkit already can do the calculations for you in one line of code.

    a) Scan all entities and calculate if they are within a radius of a location
    b) Scan all players and calculate if they are within a radius of a location
    c) Allow Bukkit to do all the lifting for you, spawn an arrow at a location, grab nearby entities, find which are players and blast a message to them?
     
  25. Offline

    teej107

    @nj2miami You still have to do as much iterating with option C as you do with A and B. B is just the most efficient out of the rest.
     
  26. Offline

    Hawktasard

    @nj2miami
    I don't get it, what's so bad about my solution?
    I also don't understand why spawning an arrow would make it any better?
     
  27. Offline

    The_BloodHound

    Can a moderator lock the thread because it's causing arguments. Thanks :)
     
  28. Locked.

    @The_BloodHound Please use the report button for things like that.
     
  29. Offline

    Hawktasard

    MaTaMoR_ likes this.
Thread Status:
Not open for further replies.

Share This Page