Solved Locking Chat

Discussion in 'Plugin Development' started by Markyroson, Jun 14, 2015.

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

    Markyroson

    How would I go about locking (and unlocking if locked) chat so that only someone with permissions to do so could speak in it?

    Code:
    if(args[0].equalsIgnoreCase("lock")) {
          //lock chat
    }
    else if(args[0].equalsIgnoreCase("unlock")) {
         //unlock chat
    }
    I realize the code is only a section of the onCommand boolean but it is the only part relevant to this question on this time ;)
     
    Last edited: Jun 14, 2015
  2. Offline

    RainoBoy97

    A boolean should do the trick.
     
    Markyroson likes this.
  3. Offline

    Reynergodoy

    you can use mine ;)
    Code:
    package *[COLOR=#8000ff]PUT YOU PACKAGE HERE[/COLOR]*;
    
    import * [COLOR=#00ff80]YOUR MAIN CLASS HERE[/COLOR] *;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class * [COLOR=#00ff80]THE NAME OF YOUR CLASS HERE[/COLOR] *
      implements CommandExecutor, Listener
    {
      public Main plugin;
      public static boolean globalmute = false;
    
      public * [COLOR=#00ff80]THE NAME AGAIN [/COLOR]*(* [COLOR=#ff4dff]YOUR MAIN CLASS[/COLOR] * instance)
      {
        this.plugin = instance;
      }
    
      public * [COLOR=#00ff80]THE NAME AGAIN[/COLOR] *() {
    }
    
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        if (!(sender instanceof Player))
        {
          sender.sendMessage(ChatColor.RED + "Only players can use this!");
          return true;
        }
        Player p = (Player)sender;
        if ((cmd.getName().equalsIgnoreCase("chatclose")) &&
          (p.hasPermission("Server.Staff"))) {
          if (!globalmute)
          {
            Bukkit.broadcastMessage(" §4§lChat closed!");
            globalmute = true;
          }
          else if (globalmute)
          {
            Bukkit.broadcastMessage(" §a§lChat opened!");
            globalmute = false;
          }
        }
        return false;
      }
    
      @EventHandler
      public void OnChat(AsyncPlayerChatEvent event)
      {
        Player player = event.getPlayer();
        if ((!player.hasPermission("Server.Staff")) && (globalmute))
        {
          event.setCancelled(true);
          player.sendMessage("§6Chat closed!");
        }
      }
    }
    
    ready to be edited
     
    Markyroson likes this.
  4. Offline

    Markyroson

    thanks man! :)
     
  5. Offline

    Reynergodoy

    you're welcome
     
    Markyroson likes this.
  6. Offline

    Markyroson

    Only problem I have is that any player can still send messages when the chat is supposedly locked :(
     
  7. Note: boolean locked = false;

    @EventHandler
    public void onPlayerChat(AsyncPlayerChatEvent e) {
    if(locked) {
    e.setCancelled(true);
    }
    }

    (onCommand)
    if(cmd.equalsIgn....) {
    set boolean to true.
    }
     
    Markyroson likes this.
  8. Offline

    Markyroson

    I am confused. I tried what you said and I could still chat. I am not opped on my test server and have no permissions on it (to test it) and I can still chat.
     
  9. Are you registering your events?
     
  10. Offline

    Markyroson

    Yes. Here is the code for it :

    Code:
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
    The code is within the onEnable in the same class.
     
  11. Offline

    Reynergodoy

    Code:
     if ((!player.hasPermission("Server.Staff")) && (globalmute))
    are you sure that you're not an operator(OP)?
    if the player has Server.Staff the event of chatlocking will be bypassed '-'
    i tested on my test server, and all works fine '-'
     
  12. Offline

    Markyroson

    I am definitely not operator as I cannot use basically any commands (ie /heal etc) and I cleared any group manager data so as I do not have literally any permissions.
     
  13. Offline

    Irantwomiles

    @Markyroson could you post updated code? Let us take a look
     
    Markyroson likes this.
  14. Offline

    Markyroson

    Code:
    package me.Markyroson.utils;
    
    import java.util.logging.Logger;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    //import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener{
         public final Logger logger = Logger.getLogger("Minecraft");
         public Main plugin;
         public boolean globalMute = false;
        // public static Main plugin;
    
        // public Main(Main instance) {
            // this.plugin = instance;
        // }
        public void OnEnable() {
            PluginDescriptionFile pdfFile = getDescription();
            this.logger.info(pdfFile.getName() + "Successfully started up" + pdfFile.getVersion());
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            //this.logger.info(pdfFile.getName() + " Succesfully " + PluginStatus.ON + pdfFile.getVersion());    //start up message
        }
        public void OnDisable() {
            PluginDescriptionFile pdfFile = getDescription();
            this.logger.info(pdfFile.getName() + "Successfully shut down" + pdfFile.getVersion());
        //    this.logger.info(pdfFile.getName() + " Succesfully " + PluginStatus.OFF);    //shut down message
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            //if(!(sender instanceof Player)) {
             //   sender.sendMessage(ChatColor.RED + "You must be a player to execute this command!");
           // } else {
                if(label.equalsIgnoreCase("chat")) {
                    sender.sendMessage("The commands available are:");
                    sender.sendMessage("/chat clear");
                    sender.sendMessage("/chat lock");
                    sender.sendMessage("/chat unlock");
                    if(args.length == 1) {
                        if(args[0].equalsIgnoreCase("clear")) {
                            if(sender.hasPermission("Utils.chat.clear")) {
                                for(int i = 0; i < 100; i++) {
                                    Bukkit.broadcastMessage("");
                                }
                            Bukkit.broadcastMessage(ChatColor.BLUE + "" + ChatColor.BOLD + sender.getName() + ChatColor.GRAY + " has just cleared the chat!");
                            return true;
                            }
                        }
                        else if(args[0].equalsIgnoreCase("lock") && sender.hasPermission("Utils.chat.lock")) {
                             if (globalMute != true)
                             {
                               Bukkit.broadcastMessage(" §4§lChat closed!");
                               globalMute = true;
                             }
                             else if (globalMute == true)
                             {
                               Bukkit.broadcastMessage(" §a§lChat opened!");
                               globalMute = false;
                             }
                        }
                    }
                }
           // }
            return false;
        }
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e) {
            if(globalMute) {
                e.setCancelled(true);
            }
        }
    }
    I commented out the checking for console as the console is the only way I can do it if I don't have any permissions (to test it) ;)
     
  15. Offline

    Irantwomiles

    Hey I thought it was something you did so I tried to make a simple one myself and I can't get it to work either. I even tried to change the boolean to int and checking if the right number was in place but that didn't work either. If you find the solution please tag me in it cause im curious.
     
    Markyroson likes this.
  16. Offline

    Markyroson

    alright. I will. Let me know if you find something as well okay?
    Could you please post your class relating to this (in full)?
     
  17. I did this for a plugin of mine, how I did it is below. You can also decompile SOS as long as you don't just repost it as your own.

    My way:
    I made a book with a get and set. When they do the command it toggles the Boolean. On chat it checks if it is locked, if so and they don't have the permission to talk the event is then canceled and a message is sent.
     
    Markyroson likes this.
  18. Offline

    Irantwomiles

    @Markyroson Well I fixed my issue. For some reason I spelled onEnable onEnalbe (I guess I should have used @Override). Well either way if you still haven't figured it out yet I can show you my code.
     
    Markyroson likes this.
  19. Offline

    Markyroson

    Please do post it. I am currently not able to get near my PC to try so I would like to see what you managed to get working.
     
  20. Offline

    Irantwomiles

    Code:
    package me.iran.bukkit;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MuteChat extends JavaPlugin implements Listener {
    
        boolean isLocked = false;
        CharSequence[] name = {"TEST", "hi", "well"};
    
        @Override
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
        }
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            Player player = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("lockchat")) {
                if (player.hasPermission("iran.chat.mute")) {
                    if (isLocked != true) {
                        isLocked = true;
                        Bukkit.broadcastMessage(ChatColor.RED
                                + "Chat has been muted by: " + ChatColor.YELLOW
                                + player.getName());
    
                    } else {
                        player.sendMessage(ChatColor.RED + "Chat is already Muted");
                    }
    
                } else {
                    player.sendMessage(ChatColor.RED
                            + "You do Not have Permission to do this Command");
                }
            }
    
            if (cmd.getName().equalsIgnoreCase("unlockchat")) {
                if (player.hasPermission("iran.chat.unmute")) {
                    if (isLocked != false) {
                        isLocked = false;
                        Bukkit.broadcastMessage(ChatColor.GREEN
                                + "Chat has been Un-Muted by: " + ChatColor.AQUA
                                + player.getName());
                    } else {
                        player.sendMessage(ChatColor.RED
                                + "Chat is currently not Muted");
                    }
                } else {
                    player.sendMessage(ChatColor.RED
                            + "You do Not have Permission to do this Command");
                }
            }
            return true;
        }
    
        @EventHandler
        public void onType(AsyncPlayerChatEvent event) {
            if (isLocked != false
                    && !event.getPlayer().hasPermission("iran.chat.bypass")) {
                event.setCancelled(true);
                event.getPlayer()
                        .sendMessage(
                                ChatColor.RED
                                        + "Chat is currently muted and only Staff may Speak!");
            }
           
            if(event.getMessage().contains("fuck")) {
                event.setMessage(event.getMessage().replace("fuck", "freak"));
            }
        }
    }
    
    Ok here it is. I dont like to spoon feed because you dont learn from it but it seems like you actually tried it out.

    @Markyroson dont look at the onType lol. I forgot I was messing with that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
    Markyroson likes this.
  21. Offline

    Markyroson

    Yes, I did try and spent quite a bit of time here and there trying to figure it out yesterday (just managed to get to my development computer now). You did also kind of say that you would post it so....lol haha ;)

    Anyways, your code worked! :) Thanks man! :) Only issue is that if the console tries the commands it generates an error but I can figure that one out ;) Thanks again!

    EDIT: I did fix that error. Just had to set it from player.whatever to sender.whatever (have encountered error before and know how to deal with it).

    Also, what was the CharSequence[] name = {"TEST", "hi", "well"}; there for? It isn't being called anywhere.

    no problem :) ;)
     
    Last edited: Jun 15, 2015
  22. Offline

    Irantwomiles

    Youre welcome :D, ya I know I didn't do much checks in the code so I was expecting some errors if done in console. and the CharSequence was just something I was messing with :p
     
    Markyroson likes this.
  23. Offline

    Markyroson

    ah, okay haha ;)
     
  24. You never check if the sender was a player.
     
    Markyroson likes this.
  25. Offline

    Markyroson

    I wanted it to be available to console as well and sender encompasses both. It was deliberate and works fine.
     
  26. Offline

    Zombie_Striker

    @Markyroson
    It may looks like it "Work fine" to you, but that is really bad and you should never blindly cast. If someone hired you to do a job and you did that, they would fire you instantly.
     
  27. Offline

    teej107

    Go ahead. Try it with the console.
     
  28. Offline

    Markyroson

    Do you mean via checking if it is the console that sent it? If not and you mean in game, I did that and it works fine.

    EDIT:
    There, I added the check, no more alarm bells going off?

    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(sender instanceof ConsoleCommandSender || sender instanceof Player) {
            if(label.equalsIgnoreCase("chat")) {
                sender.sendMessage("The commands available are:");
                sender.sendMessage("/chat clear");
                sender.sendMessage("/chat lock");
                sender.sendMessage("/chat unlock");
                if(args.length == 1) {
                 if(args[0].equalsIgnoreCase("clear")) {    //clears the chat
                     if(sender.hasPermission(Permissions.Chat.CLEAR)) {
                         for(int i = 0; i < 100; i++) {
                             Bukkit.broadcastMessage("");
                         }
                     Bukkit.broadcastMessage(ChatColor.BLUE + "" + ChatColor.BOLD + sender.getName() + ChatColor.GRAY + " has just cleared the chat!");
                     return true;
                     }
                 }
                 if (args[0].equalsIgnoreCase("lock")) {    // locks the chat so that no one other than console, operator (OP), and those with bypass permission can send messages ("talk") in chat
                     if (sender.hasPermission(Permissions.Chat.LOCK)) {
                         if(this.plugin.getConfig().getBoolean("chat_locked") != true) {
                             this.plugin.getConfig().set("chat_locked", true);
                             this.plugin.saveConfig();
                             this.plugin.reloadConfig();
                             Bukkit.broadcastMessage(ChatColor.RED
                                     + "Chat has been locked by: " + ChatColor.YELLOW
                                     + sender.getName());
                             return true;
                         } else {
                             sender.sendMessage(ChatColor.RED + "Chat is already locked");
                             return false;
                         }
         
                     } else {
                         sender.sendMessage(ChatColor.RED
                                 + "You do not have Permission to do this Command");
                         return false;
                     }
                 }
         
                 if (args[0].equalsIgnoreCase("unlock")) {    // unlocks the chat so that everyone/anyone can send messaged ("talk") in it
                     if (sender.hasPermission(Permissions.Chat.UNLOCK)) {
                         if(this.plugin.getConfig().getBoolean("chat_locked") != false) {
                             this.plugin.getConfig().set("chat_locked", false);
                             this.plugin.saveConfig();
                             this.plugin.reloadConfig();
                             Bukkit.broadcastMessage(ChatColor.GREEN
                                     + "Chat has been Un-locked by: " + ChatColor.AQUA
                                     + sender.getName());
                             return true;
                         } else {
                             sender.sendMessage(ChatColor.RED
                                     + "Chat is currently not locked");
                             return false;
                         }
                     } else {
                         sender.sendMessage(ChatColor.RED
                                 + "You do Not have Permission to do this Command");
                         return false;
                     }
                 }
                }
             }
            }
                return false;
            }
    @teej107 See above code as I added the checks. Any more alarm bells going off for you?
     
    Last edited: Jun 17, 2015
  29. Offline

    Irantwomiles

    I wasn't making this plugin to the full extent. It was just something to show this guy how it would work. I'm sure if he wants to add extra checks he could so I didn't bother.
     
    Markyroson likes this.
  30. In my opinion, if you spoon feed, spoon feed right
     
    Drkmaster83 likes this.
Thread Status:
Not open for further replies.

Share This Page