Solved Locking chat help!

Discussion in 'Spigot Plugin Development' started by JacksonHogan, Jun 4, 2016.

Thread Status:
Not open for further replies.
  1. I'm trying to lock my chat, But with the code i'm using right now, When ever the plugin enables, and someone trys talking it doesn't show there chat or anything.

    Code (Text):
    package xxlightbulbxx.fantasycore.chat;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import xxlightbulbxx.fantasycore.main.FantasyCore;

    public class Lock implements Listener{
       
        public static boolean chatLocked;
       
        public static boolean isChatLocked() {
            return chatLocked;
        }

        public static void setChatLocked(boolean chatLocked) {
            Lock.chatLocked = chatLocked;
        }

        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            Player player = e.getPlayer();
           
            if(isChatLocked() && player.hasPermission("fantasycore.chat.bypass")){
                e.setCancelled(false);
                return;
            }else{
                if(isChatLocked()){
                    e.setCancelled(true);
                    player.sendMessage(FantasyCore.prefix
                            + ChatColor.GOLD + "Chat is currently locked!");
                }else{
                    if(isChatLocked() == false){
                        return;
                    }
                }
            }
        }
       
        public static void lockChat(Player player){
            if(!isChatLocked()){
                setChatLocked(true);
                Bukkit.broadcastMessage(FantasyCore.prefix
                        + ChatColor.GOLD + player.getName() + " Locked chat!");
            }else{
                player.sendMessage(FantasyCore.prefix
                        + ChatColor.RED + "Chat is already locked! Use "
                        + ChatColor.GOLD + "/chat unlock " + ChatColor.GREEN + "to unlock chat!");
            }
        }
       
        public static void unlockChat(Player player){
            if(isChatLocked()){
                setChatLocked(false);
                Bukkit.broadcastMessage(FantasyCore.prefix
                        + ChatColor.GOLD + player.getName() + " Unlocked chat!");
            }else{
                player.sendMessage(FantasyCore.prefix
                        + ChatColor.RED + "Chat is already unlocked! Use "
                        + ChatColor.GOLD + "/chat lock " + ChatColor.GREEN + "to lock chat!");
            }
        }
    }
     
    onEnable:

    Code (Text):
        public void onEnable(){
            //Commands
            Bukkit.getServer().getPluginCommand("chat").setExecutor(new ChatManager(this));
            Bukkit.getServer().getPluginCommand("freeze").setExecutor(new FreezeManager(this));
            Bukkit.getServer().getPluginCommand("unfreeze").setExecutor(new FreezeManager(this));
    //        Bukkit.getServer().getPluginCommand("list").setExecutor(new ListManager(this));
           
            //Register Events
            Bukkit.getServer().getPluginManager().registerEvents(new Lock(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new ChatHover(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new FreezePlayer(), this);
        }
     
  2. Change
    Code (Text):
    if(isChatLocked() && player.hasPermission("fantasycore.chat.bypass")){
    to
    Code (Text):
    if(!isChatLocked() || player.hasPermission("fantasycore.chat.bypass")){
     
  3. It would be better for him to just check
    Code (Text):
    if (isChatLocked() && !player.hasPermission("fantasycore.chat.bypass")){}
    In addition to this
    Code (Text):
        public static boolean chatLocked;

        public static boolean isChatLocked() {
            return chatLocked;
        }
    Both of these can be private and not static
     
    • Like Like x 1
  4. Also please apply a minimal amount of quality in your code. This is painful to read and completely meaningless.
     
    • Like Like x 1
  5. When i try debugging it by using this: It still doesn't let me talk... it just sends the msg "Test1".
    Code (Text):
    if(isChatLocked() == false){
                e.setCancelled(false);
                Bukkit.broadcastMessage("Test1");
            }
     
  6. Code (Text):
     @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            Player player = e.getPlayer();
           
            if(!isChatLocked() || player.hasPermission("fantasycore.chat.bypass")){
                e.setCancelled(false);
             } else {
               p.sendMessage("Chat is locked");
               e.setCancelled(true);
             }
        }
    For god sakes, be careful with static abuse... I have no clue what you're doing right now.
     
    • Agree Agree x 2
  7. Code (Java):
        public static boolean chatLocked;
       
        public static boolean isChatLocked() {
            return chatLocked;
        }

        public static void setChatLocked(boolean chatLocked) {
            Lock.chatLocked = chatLocked;
        }

        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            Player player = e.getPlayer();
           
            if(isChatLocked() == false){
                e.setCancelled(false);
                Bukkit.broadcastMessage("Test1");
            }
           
            if(isChatLocked() && player.hasPermission("fantasycore.chat.bypass")){
                e.setCancelled(false);
                Bukkit.broadcastMessage("Test2");
            }else{
                if(isChatLocked()){
                    e.setCancelled(true);
                    Bukkit.broadcastMessage("Test3");
                    player.sendMessage(FantasyCore.prefix
                            + ChatColor.RED + "Chat is locked!");
                }
            }
        }
    This is my updated code.

    Everythings working fine... The broadcasts get sent. It just doesnt let me talk.
     
  8. Did you try the code I just sent..?
     
  9. Yea i tried it... It did the same thing my first code did.
     
  10. What about this
    Code (Text):
    @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            Player player = e.getPlayer();
           
            if(isChatLocked() && !player.hasPermission(fantasycore.chat.bypass))
              event.setCancelled(true);
        }
     
  11. Nope, That doesn't work.
     
  12. Try broadcasting the value of chatLocked before the if statement
     
  13. I did... It returned false.
     
  14. Cool what about formatting the code like this it's working for me
    Code (Java):
    if(isChatLocked() && (!player.hasPermission(String node)));
     
  15. A ; at the end of an if statement? Ew
     
  16. When did u setChatLocksetChatLocked(true or false);
     
  17. Interface? Joke I'm at phone doesn't realize
     
  18. I don't think its my code for some reason...
    When i remove all code from the chat listener... It still doesn't let me chat.

    EDIT: But when i disable the plugin i can chat.
     
  19. Thread solved!
    I had another listener for chat that was messing with it.
     
    • Funny Funny x 1
Thread Status:
Not open for further replies.