Stop some players from seeing chat

Discussion in 'Plugin Development' started by XxZHALO13Xx, Dec 18, 2014.

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

    XxZHALO13Xx

    If a player has this toggled they wont see messages.. how would i do this?
     
  2. Offline

    Rocoty

    @XxZHALO13Xx Modify the collection returned by AsyncPlayerChatEvent#getRecipients()

    Steps to find out:
    1. Reason that AsyncPlayerChatEvent should be used somehow
    2. Go to documentation.
    3. Search for AsyncPlayerChatEvent
    4. Quickly scan all methods.
    5. Find the correct method and read documentation.

    The next time you have a similar question, follow above steps before posting a thread on the forums.
     
  3. Offline

    XxZHALO13Xx

    @Rocoty i have. i just dont know how to cancel the msg for some
     
  4. Offline

    Rocoty

    Funergy likes this.
  5. Offline

    XxZHALO13Xx

    @Rocoty ... And how does one do that
     
  6. Offline

    567legodude

    Create a list of players.
    Code:
    ArrayList<Player> nochat = new ArrayList<Player>();
    Add players to that list when they toggle.
    Look for the event and remove them.
    Code:
    @EventHandler
    public void onChat(AsyncPlayerChatEvent event) {
        for (Player p : nochat) {
            event.getRecipients().remove(p);
        }
    }
     
  7. Offline

    Rocoty

  8. Offline

    XxZHALO13Xx

    @567legodude @Rocoty when i do it when i chat nothing pops up but it shows it to others

    @Rocoty @567legodude

    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent e){
    3. Player p = e.getPlayer();
    4. String msg = e.getMessage();
    5.  
    6. if(toggledChat.contains(p)){
    7. for (Player players : toggledChat) {
    8. e.getRecipients().remove(players);
    9. }
    10. }
    11. else{
    12. if(!toggledChat.contains(p)){
    13. for (Player playerson : toggledChat) {
    14. e.getRecipients().add(playerson);
    15. }
    16. }
    17. }


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

    Webbeh

    Wow that's some serious code.

    Remove the whole "else { }" block, that is 100% useless. Also if you have the exact invert of what you're trying to achieve, this would mean that toggledChat contains the muted players, not the ones who receive them :)
     
  10. Offline

    567legodude

    That means that you put yourself in no-chat mode. Which means it works. Because you said.
     
  11. Offline

    XxZHALO13Xx

    @567legodude @Webbeh like they wont see other players messages. not there own. i still see messages just not ones i type
     
  12. Offline

    Rocoty

    @XxZHALO13Xx I think your issue isn't necessarily the way you handle chat events, but is probably related to other parts of your code. If you'd post your whole code that would be really helpful towards us helping you.
     
  13. Offline

    XxZHALO13Xx

    @Rocoty i dont have an issue with the code i just don't know how to hide the chat from other players
     
  14. Offline

    Rocoty

    @XxZHALO13Xx Well, it has been explained to you numerous times. I will try again, step by step.

    You need to handle AsyncPlayerChatEvent (which you probably already do).
    When handling this event, get the set of all recipients with event.getRecipients().
    This returns a mutable set in regular cases.
    With the returned set you can add or remove recipients at your leisure.
    What you want to do is remove from the set those players that have chat toggled off.
    This can be achieved by calling the remove(Player) method on the set.
    If you have a collection of players you want to remove from the set, use removeAll(Collection).
    You do NOT need to add any players to the set as all players on the server are in the set by default.

    If you are still confused, please explain as thoroughly as you can what you are confused about. Tell the details, please.
     
  15. Offline

    567legodude

    @XxZHALO13Xx I already provided you with a very simple answer.
    Except you did something like this.
    Ill tell you that what I posted, any player in the list will not receive chat.
     
  16. Offline

    teej107

  17. Offline

    Rocoty

    WARNING: Unnecessary (and negligible) performance overhead and inevitably imminent ConcurrentModificationException detected.

    Iterate through the toggled chat collection and remove each entry from the recipients. Or better yet. recipients.removeAll(collectionOfPlayersWhoWontSeeChat);
     
    teej107 and AdamQpzm like this.
Thread Status:
Not open for further replies.

Share This Page