Solved How to ClearChat for Online Players Without the Bypass Permission

Discussion in 'Spigot Plugin Development' started by Yazability, May 29, 2017.

Thread Status:
Not open for further replies.
  1. Basically, I have been trying to clear the chat for all players online without the bypass permission, For example, all players without the permission clearchat.bypass will have their chat cleared and the people who have that permission will simply be sent the message that the chat has been cleared.
     
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Clearing chat is pretty much just sending a bunch of blank messages to bump previous messages beyond what players can scroll up to.
    If you just want to target specific players (in this case with a permission), just loop through all online players and if they don't have the bypass permission then send them a ton of blank messages.
     
  3. So would this work?
    Code (Text):

    for (Player all : Bukkit.getOnlinePlayers()) {
                    if (!all.hasPermission("clearchat.bypass")) {
                for (int x = 0; x < 150; x++){
                   Bukkit.broadcastMessage("");
                }
                Bukkit.broadcastMessage(chatcleared);
    }
     
     
  4. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Bukkit.broadcastMessage() sends a message to all players on the server. In this case, you only want to send it to the players that don't have the correct permission. Instead of Bukkit.broadcastMessage, use all.sendMessage().

    When you use the for loop to go through a list of elements (in this case, players) the variable you set (in this case, all) is set to the next element in the list.
     
  5. Code (Text):

                for (Player all : Bukkit.getOnlinePlayers()) {
                    if(!all.hasPermission("handychat.clearchat.bypass")) {
                        for (int x = 0; x < 150; x++){
                           all.sendMessage("");
                        }
                        all.sendMessage(chatcleared);
                        return true;
                    }  else {
                        Bukkit.broadcastMessage(chatcleared);
                    }
                }
     
    This fixed it, thank you!
     
Thread Status:
Not open for further replies.