How to Mute a player from chat.

Discussion in 'Resources' started by DocRedstone, Jul 7, 2012.

  1. Offline

    DocRedstone

    First create a String List.
    PHP:
    1. List<String> bannedFromChat = new ArrayList<String>();
    Then you want to register the chat event
    PHP:
    1. //Class Declaration
    2. public class Main extends JavaPlugin implements Listener {
    3.  
    4. //On enable
    5. getServer().getPluginManager().registerEvents(this, this);
    And finally your chat listener
    PHP:
    1. public void onPlayerChat(PlayerChatEvent event) {
    2.         Player p = event.getPlayer();
    3.         for (String s : bannedFromChat) {
    4.             if (s.equals(p.getName())) {
    5.                 event.setCancelled(true);
    6.                 p.sendMessage(ChatColor.RED + "[BanVote] You have been muted. You can not chat.");
    7.             }
    8.         }
    9.     }
    to ban someone just add the following code after the onCommand(...) Method.

    PHP:
    1. bannedFromChat.add(args[0]);
    to unban someone just add the following code after the onCommand(...) Method.
    PHP:
    1. bannedFromChat.remove(args[0]);
     
  2. Code (Text):
    1. [...]
    2. if (s.equalsIgnoreCase(p.getDisplayName())) {
    3. [...]
    You're gonna have some problems with that.
     
  3. Offline

    DocRedstone

    Yeh quick fingers. I fixed it to just include equals.
     
  4. I was actually talking about the .getDisplayName(), since it can change AND contain colors so there's a way to bypass this mute.
     
    DocRedstone likes this.
  5. Offline

    DocRedstone

    you can make it use getName() instead and it will work just fine.
     
  6. I know, that's my point. Hence, it would be nice if you could edit your post so other people, who might use this, won't run into the issue.
     
    McLuke500 likes this.
  7. Offline

    DocRedstone

    Done and Done.
     
  8. bannedFromChat really needs to be a HashSet, not an ArrayList.
     
  9. Offline

    Blackside921

    I would do something like: if(bannedFromChat.contains(p.getName())
    instead of looping through the whole List.
     
  10. Yeah, you will cause pretty bad chat lag if a large server ever tries to use something based on this.
     

Share This Page