How to Mute a player from chat.

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

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

    DocRedstone

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

    PHP:
    bannedFromChat.add(args[0]);
    to unban someone just add the following code after the onCommand(...) Method.
    PHP:
    bannedFromChat.remove(args[0]);
     
  2. Code:
    [...]
    if (s.equalsIgnoreCase(p.getDisplayName())) {
    [...]
    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. Offline

    desht

    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. Offline

    md_5

    Yeah, you will cause pretty bad chat lag if a large server ever tries to use something based on this.
     
Thread Status:
Not open for further replies.

Share This Page