Solved [Bukkit 1.7.2] Sending messages to players with something the same

Discussion in 'Plugin Development' started by tamajpm, Dec 29, 2013.

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

    tamajpm

    Hello everyone,

    My problem is that if i use this code that checks the players channel and if there is someone else on the server with the same current channel i want that the message will only be send to them:

    Code:java
    1. package nl.tamajpm.channels.v1.src.events;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.AsyncPlayerChatEvent;
    8. import org.bukkit.plugin.Plugin;
    9.  
    10. public class OnPlayerChatEvent implements Listener {
    11. private Plugin plugin;
    12.  
    13. public OnPlayerChatEvent(Plugin instance) {
    14. plugin = instance;
    15. }
    16.  
    17. @EventHandler
    18. public void onPlayerChat(AsyncPlayerChatEvent e) {
    19. Player player = e.getPlayer();
    20.  
    21. for(Player players : Bukkit.getOnlinePlayers()) {
    22. if(plugin.getConfig().getString(players.getName() + ".CurrentChannel").equals(plugin.getConfig().getString(player.getName() + ".CurrentChannel"))) {
    23.  
    24. }
    25. }
    26. }
    27. }
    28.  


    Sorry, but my English is very bad at this time.
     
  2. Offline

    1Rogue Retired Staff

    You have the basis of the idea, just do players.sendMessage(/*the message content*/); . Also, you should probably load the data when you load the plugin, rather than using the config for every check.
     
  3. Offline

    tamajpm

    Yes, but if i send the message it will be send to all players without checking the channel.
     
  4. Offline

    mbaxter ʇıʞʞnq ɐ sɐɥ ı Retired Staff

    Modify the event's recipients list.
     
  5. Offline

    tamajpm

    Oké, another problem. I want this:

    - The player needs to be in the same channel as the player to receive the message.
    - The player needs to have toggled the channel chat, if he is not he gets the normal chat.
    - The player only gets the messages from his channel.

    How can i make this, i don't know how to make this. This is the last part of the plugin, so.

    Is this right:

    Code:java
    1. package nl.tamajpm.channels.v1.src.events;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.player.AsyncPlayerChatEvent;
    8. import org.bukkit.plugin.Plugin;
    9.  
    10. public class OnPlayerChat implements Listener {
    11. private Plugin plugin;
    12.  
    13. public OnPlayerChat(Plugin instance) {
    14. plugin = instance;
    15. }
    16.  
    17. @EventHandler
    18. public void onPlayerChat(AsyncPlayerChatEvent e) {
    19. Player player = e.getPlayer();
    20.  
    21. if(!plugin.getConfig().getString(player.getName() + ".CurrentChannel").equals("")) {
    22. if(plugin.getConfig().getBoolean(player.getName() + ".Toggle")) {
    23. for(Player players : Bukkit.getOnlinePlayers()) {
    24. e.getRecipients().remove(players);
    25. }
    26.  
    27. for(Player players : Bukkit.getOnlinePlayers()) {
    28. if(plugin.getConfig().getString(players.getName() + ".CurrentChannel").equals(plugin.getConfig().getString(player.getName() + ".CurrentChannel"))) {
    29. e.getRecipients().add(players);
    30. }
    31. }
    32. }
    33. }
    34. }
    35. }
    36.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  6. Offline

    CubieX

    Code:
     for(Player players : Bukkit.getOnlinePlayers()) {
    e.getRecipients().remove(players);
    }
    You can use
    Code:
    e.getRecipients().clear()
    instead of using a loop here.
    The rest of the code seems to be ok. Just try it.

    But one thing: As the name says, this event is (at least most of the time) "asynchronous".
    Means, it's run by a separate thread and not the servers main thread.
    In such async tasks you may not use any Bukkit API calls, or bad things may happen.
    So if you need to do "player.someting()" or "Bukkit.something()" you should embedd this in a synchronous timer task.

    Example:
    Code:
    plugin.getServer().getScheduler().runTask(plugin, new Runnable()
    {
        public void run()
        {
            // your code with Bukkit API calls here
        }
    });
     
  7. Offline

    tamajpm

    Thanks
     
  8. Offline

    tamajpm

    I have written the code everything is working but when i'm in a channel i can still get the messages from the public chat?

    Sorry, my code at the moment:
    Code:java
    1. package nl.tamajpm.channels.v1.src.events;
    2.  
    3. import nl.tamajpm.channels.v1.src.plugin.Channels;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.AsyncPlayerChatEvent;
    10.  
    11. public class OnPlayerChat implements Listener {
    12.  
    13. @EventHandler
    14. public void onPlayerChat(AsyncPlayerChatEvent e) {
    15. Player player = e.getPlayer();
    16.  
    17. if(!Channels.getPlugin().getConfig().getString(player.getName() + ".CurrentChannel").equals("")) {
    18. if(Channels.getPlugin().getConfig().getBoolean(player.getName() + ".Toggle") == true) {
    19. e.getRecipients().clear();
    20.  
    21. for(Player players : Bukkit.getOnlinePlayers()) {
    22. if(Channels.getPlugin().getConfig().getString(players.getName() + ".CurrentChannel").equals(Channels.getPlugin().getConfig().getString(player.getName() + ".CurrentChannel"))) {
    23. e.getRecipients().add(players);
    24. }
    25. }
    26. }
    27. }
    28. }
    29. }
    30.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  9. Offline

    tamajpm

  10. Offline

    PhilipsNostrum

    Ok, so basically you're checking if the player who sent the message is in a channel, but you also need to check if there are players on the server that are in a channel and remove them from recipients (so they don't get a message from someone In public chat or whatever you called it.
    Try something like this
    Code:
     @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            Player player = e.getPlayer();
           
            if(!Channels.getPlugin().getConfig().getString(player.getName() + ".CurrentChannel").equals("")) {
                if(Channels.getPlugin().getConfig().getBoolean(player.getName() + ".Toggle") == true) {
                    e.getRecipients().clear();
                   
                    for(Player players : Bukkit.getOnlinePlayers()) {
                        if(Channels.getPlugin().getConfig().getString(players.getName() + ".CurrentChannel").equals(Channels.getPlugin().getConfig().getString(player.getName() + ".CurrentChannel"))) {
                            e.getRecipients().add(players);
                        }
                    }
                }
            }
    //add this
    else{
    //loop through players
      for(Player players : Bukkit.getOnlinePlayers()) {
    //if that player is in a channel, remove him from recipients
                        if(!Channels.getPlugin().getConfig().getString(players.getName() + ".CurrentChannel").equals("")) {
                            e.getRecipients().remove(players);
                        }
                    }
    }
        }
    
    Also you shouldn't use the config for every check like 1Rogue said
     
  11. Offline

    tamajpm

    Thanks, that part is working now. But one problem more xD. If a player has disabled (toggled) his channel chat he is getting the channel chat and the public chat. But when he is disabled his channel chat he is should not be able to get that messages from the channel. How i fix this if you can understand me?
     
  12. Offline

    PhilipsNostrum

    Actually, I don't really understand :/ so you're saying that if they are in public chat they get messages from the channels?
     
  13. Offline

    tamajpm

    Maybe i can get your Skype i can show you.

    Thanks everyone for helping!!!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page