Disable chat for a player

Discussion in 'Plugin Development' started by ryr11, Jan 10, 2014.

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

    ryr11

    I am making a hub plugin, and I want to disable chat when chatOn is false, I know how to do that, but I don't know how do disable chat (I don't know how to do that in general) for only the players with chatOn false (I don't know that either).
     
  2. Offline

    jusjus112

    ryr11
    1. make an hashmap with an boolean and set it to tue
    2. make an event called syncChatEvent, and set if the boolean is true cancel the event.
    3. if you type for example /chatoff <Player> put the player in the hashmap
    4. done
     
  3. Offline

    ryr11

    I have done that (thanks), but how do I get the data from a hashmap. Also I have heard that array lists might better, is that true?

    So like this (I decided to use permissions for a completely different reason):

    Code:java
    1. @EventHandler
    2. public void onPlayerChat(AsyncPlayerChatEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. if ((!p.hasPermission("chat.off"))) {
    6. event.setCancelled(true);
    7. }
    8.  
    9. }


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

    Wizehh

    Code:java
    1.  
    2. ArrayList<String> mutedPlayers = new ArrayList<String>();
    3. @EventHandler
    4. public void onPlayerChat(AsyncPlayerChatEvent e) {
    5. Player p = e.getPlayer();
    6.  
    7. if (mutedPlayers.contains(p.getName)) {
    8. event.setCancelled(true);
    9. }
    10.  
    11. }

    *freehand
    **If you want to add players to the list,
    Code:java
    1. mutedPlayers.add(p.getName)
    will suffice.
     
  5. Offline

    ryr11

    Is that for muting a player, or making it so that certain players don't receive chat.
     
  6. Offline

    Regagames

    ryr11 That is making it so a player can't chat. The AsyncPlayerChatEvent gets if the player types something in chat, then he is checking if mutedPlayers ArrayList contains the person typing the message, if it does, it doesn't globally broadcast their chat message.
     
  7. Offline

    ryr11

    Could you make something to make a player not receive chat>?
     
  8. Offline

    Regagames

    ryr11 I will not make it for you, but I will explain how. First you still use the 'AsyncPlayerChatEvent' but instead of cancelling the event if they are in that ArrayList. Make a for loop (shown below, this will loop through all online players)
    Code:java
    1.  
    2. for(Player players : Bukkit.getOnlinePlayers()){
    3.  
    4. }
    5.  

    and then check if 'players' are in the ArrayList or don't have the permission to receive chat and then do this:
    Code:java
    1. event.getRecipients().remove(players);

    This should remove 'players' from the list of people that can receive the message. Basically, it is getting the recipients that can receive the message and removing the looped through 'players' (If you have any questions tahg me, I'm pretty bad at explaining stuff.)
     
  9. Offline

    Wizehh

    That will disable chat for the player (meaning they cannot type in chat, but they can still see chat); did you want something else?
     
  10. Offline

    Regagames

Thread Status:
Not open for further replies.

Share This Page