Development Assistance Sending chat to a certain radius around you.

Discussion in 'Plugin Help/Development/Requests' started by HCMatt, Apr 2, 2015.

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

    HCMatt

    Hello. I am making a plugin that has different chat channels. I want the normal chat to be a max of 16 blocks from the sender of the chat message. So....

    Player 1 - Sends message.
    Player 2 - 10 blocks from Player 1, sees the message.
    Player 3 - 15 blocks from Player 1, sees the message.
    Player 4 - 17 blocks from Player 1, can't see the message.

    Here is what I have so far....

    Code:
        @EventHandler
        public void onChat(AsyncPlayerChatEvent e){
            Player p = e.getPlayer();
            Location pLocation = p.getLocation();
            String message = e.getMessage();
          
            for (Player op : e.getRecipients()){
                if(op.getLocation().distance(pLocation) <= 16){
                }else {
                    e.getRecipients().remove(op);
                    return;
                }
            }
          
        }

    I got 3 accounts on a test server, stood 2 of them next to each other and ran over 16 blocks away with the other one. The message is still sending to one of the two players that are next to each other. Please help.
     
    Last edited: Apr 2, 2015
  2. @HCMatt Apart from the empty if it seems fine.

    Why not just do if(!locationStuff){ e.getRecipents.remove(); } It is less code and more efficient than running an empty if.
     
  3. Online

    timtower Administrator Administrator Moderator

    @HCMatt You probably don't want to return out of the event.....
     
    the133448 likes this.
  4. Offline

    HCMatt


    So like this?....

    Code:
    for (Player op : e.getRecipients()){
                if(!(op.getLocation().distance(pLocation) <= 16)){
                    e.getRecipients().remove(op);
                }
            }
     
  5. Offline

    HCMatt

    I did that, and also took out the return part, and the same thing is happening :/.
     
  6. Online

    timtower Administrator Administrator Moderator

    @HCMatt Did you register the event?
     
  7. @timtower In his tests people received the message so I would say so.

     
  8. Offline

    HCMatt

    Yes. I did this in my onEnable().

    Code:
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
     
  9. Online

    timtower Administrator Administrator Moderator

    @bwfcwalshy That isn't a test, that is the wanted result.
     
  10. Offline

    HCMatt

    What is happening so far is it is only removing one person from e.getRecipients(), even though they are both further than 16 blocks away.... Anyone know ho to fix this ?
     
  11. Online

    timtower Administrator Administrator Moderator

    @HCMatt What is the exact distance? Did you recompile it? Exporting to the right location?
     
  12. @timtower Ah yes, sorry.

    @HCMatt Print a message with everyone's distance and see what it says.
     
  13. Offline

    RuthlessRage

    Are you registering this is the main class; Is the code in the main class? If you have the chat shit in a listener you need to change bukkit.getserver().getpluginmanager().registerevents(this,this); and id redcommend using this code to register instead

    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(new yourlistenerclassname(this), this);
     
  14. Offline

    HCMatt

    @timtower The distance I don't want them to recieve the message at is 16 blocks. It works if one other player is online, but when another person is on, making it 3 online, it only stops the message for 1 person.
     
  15. @HCMatt Try printing a message with everyone's distance and see what it says
     
  16. Offline

    HCMatt

    Wait... When i type in chat, this error message comes up in console:

    http://imgur.com/rXOnumZ (click the link)

    @bwfcwalshy I did this, only one of the people showed up. The other persons location didn't.

    EDIT by Timtower: merged posts
     
  17. Online

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives.
    @HCMatt Try to make an additional list. If not in the distance: put the player in second list, then in another loop, remove them from the set
     
  18. Offline

    HCMatt

    [SOLVED]
    Code:
    for {Player op : e.getRecipients() { 
    was giving me an error in console. I kept everything the same but changed
    Code:
    e.getRecipients()
    to
    Code:
    Bukkit.getOnlinePlayers()
    and it now works fine. Thanks for all the help.
     
Thread Status:
Not open for further replies.

Share This Page