Command failing to trigger event

Discussion in 'Spigot Plugin Development' started by DecisionsYT, May 15, 2016.

  1. Hey!

    In my lockchat plugin. When doing /lockchat lock, it's failing to cancel out the messages. It sends you the message "LockChat-Locked" but NOT cancelling the message.

    CODE:

    Code (Text):
    package ChatFilter;

    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;
    import me.DecisionsYT.Main;


    public class LockChat implements Listener, CommandExecutor {
       
       
    Main plugin;
       
        public LockChat(Main passedPlugin) {
            this.plugin = passedPlugin;
        }
       
       
      protected boolean lockchat;

     
      @EventHandler
      public void onPlayerChat(AsyncPlayerChatEvent e) {
         
          Player player = e.getPlayer();
         
        if (!lockchat) {
            return;
            }
       
       
        if (lockchat) {
        e.setCancelled(true);
        player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Locked").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                }
            }

      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
         
          if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Non-Player")));
                return true;
            }
           
            Player player = (Player) sender;
       
          if (args.length == 0) {
              sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Usage").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
          }
         
          if (args.length == 1) {
              if (sender.hasPermission("ChatX.LockChat")) {
                  if (args[0].equalsIgnoreCase("Lock")) {
                      sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Lock").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                      Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&',plugin.getConfig().getString("LockChat-Lock-Broadcast").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                      lockchat = true;
                     
                     
                  } else if (args[0].equalsIgnoreCase("Unlock")) {
                      sender.sendMessage(ChatColor.translateAlternateColorCodes('&',plugin.getConfig().getString("LockChat-Unlock").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                      Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&',plugin.getConfig().getString("LockChat-Unlock-Broadcast").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
                      lockchat = false;
                  }
              }
          }
         
         
          if (!sender.hasPermission("ChatX.LockChat")) {
              sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message").replace("{player}", player.getName()).replace("{display_name}", player.getDisplayName())));
          }
       
         
         
          return true;
         
       
              }
      }
     
  2. I have
     
  3. I noticed after I re-read your post.
     
  4. So, any suggestions then?
     
  5. The only thing that makes sense is the event is getting uncancelled by another plugin. Set the priority to HIGHEST.
     
  6. It's the only plugin being run on my server...
     
  7. I'll mess about with it
     
  8. Having changed the code, it still doesn't work

    CODE NOW:

    Code (Text):
    package ChatFilter;

    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.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import me.DecisionsYT.Main;


    public class LockChat implements Listener, CommandExecutor {
       
       
    Main plugin;
       
        public LockChat(Main passedPlugin) {
            this.plugin = passedPlugin;
        }
       
       
        protected boolean lockchat = true;

     
        @EventHandler
          public void lc(AsyncPlayerChatEvent e)
          {
            if (e.getPlayer().hasPermission("ChatX.LockChat.Bypass")) return;
            if (this.lockchat) return;
            e.setCancelled(true);
            e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Locked")));
          }

      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
         
     
        if (!sender.hasPermission("ChatX.LockChat")) {
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message")));
                return true;
        }
       
        if (sender.hasPermission("ChatX.LockChat")) {
        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Status").replace("{status}", lcStatus()).replace("{player}", sender.getName())));
        Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Broadcast").replace("{status}", lcStatus()).replace("{sender}", sender.getName())));
        this.lockchat = (!this.lockchat);
       
       
        return true;
        }
        return true;
      }

      private final String lcStatus()
      {
        return this.lockchat ? "locked" : "unlocked";
      }
    }
     
  9. Code (Text):
     @EventHandler
          public void lc(AsyncPlayerChatEvent e)
          {
            if (e.getPlayer().hasPermission("ChatX.LockChat.Bypass")) return;
            if (this.lockchat) return;
            e.setCancelled(true);
            e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Locked")));
          }
    That will check if the player has a permission even when "lockchat" isn't being called.
     
  10. Okay, so what should it be, to see if a player has a bypass perm and it's in LOCKED mode, then they can talk, if they DON'T have the bypass perm when it is LOCKED, then cancel the message and send them a message.

    EDIT:

    CURRENT CODE:

    Code (Text):
    package ChatFilter;

    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.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    import me.DecisionsYT.Main;


    public class LockChat implements Listener, CommandExecutor {
       
       
    Main plugin;
       
        public LockChat(Main passedPlugin) {
            this.plugin = passedPlugin;
        }
       
       
        protected boolean lockchat = true;

     
        @EventHandler(priority = EventPriority.HIGHEST)
          public void lc(AsyncPlayerChatEvent e) {
           
            if (e.getPlayer().hasPermission("ChatX.LockChat.Bypass")) return;
            if (this.lockchat) return;
            e.setCancelled(true);
            e.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Locked")));
          }

      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
         
     
        if (!sender.hasPermission("ChatX.LockChat")) {
            sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("No-Perm-Message")));
                return true;
        }
       
        if (sender.hasPermission("ChatX.LockChat")) {
        sender.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Status").replace("{status}", lcStatus()).replace("{player}", sender.getName())));
        Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("LockChat-Broadcast").replace("{status}", lcStatus()).replace("{sender}", sender.getName())));
        this.lockchat = (!this.lockchat);
       
       
        return true;
        }
        return true;
      }

      private final String lcStatus()
      {
        return this.lockchat ? "locked" : "unlocked";
      }
    }
     
  11. I'm on my phone so I can't really see what's in your code, but shouldn't this boolean be inverted? You're returning when lockchat is true.
     
  12. Okay, doing so, when /lockchat LOCK (SO CHAT IS LOCKED) I can't speak. when /lockchat UNLOCK (SO CHAT IS UNLOCKED) I still can't speak.
     
  13. This issue has been solved many many times, please learn Java before you request a ton of help from the Spigot community, you've been posting like 2 threads a day asking basic simple Java based questions and it's quite obvious that you don't know Java well so please learn Java before asking for help. Once you learn Java most of these issues you can problem solve on your own, if you want to learn Spigot plugin coding first than that is going to set you up to fail. It's gotten to the point that the Spigot community is actually making your plugins. Yes we will continue to help, yes you don't need to learn Java to continue, but you're just setting yourself up to fail, I feel like if I gave you a medium-sized plugin's code than you wouldn't know what 75% or more of it is doing. I'm not saying this to be mean, I'm just trying to set you up to succeed instead of fail, I started like you and only coded Spigot but once I learned Java I could problem solve 99% of my issues by myself and I could make plugins much cleaner and I could make them work with 1 or 2 tries without tests (of course with a little bug-fixing if it doesn't work the first time), I'm looking for you to become better than that. With that said learning Java will really help you in coding these plugins and your life all together, I hope you take this advice seriously and try to follow it.
    Here is a good place to know how to learn Java and where to start
    The thread pointed to above will help you greatly.
     
    • Agree Agree x 2
  14. A boolean won't do much for args, if it's just a /lockchat cmd, then the boolean will work. Otherwise, you need args..
     
  15. a boolean will do well for args if you can know how to set it up for example if(arg[1].equals('c')) bool = true else bool = false
     
  16. I know how to use args.
     
  17. True, however. It's obvious he doesn't know that method.
     
  18. not saying you don't, please refer to my previous post.
     
  19. I know.
     
  20. yeah, in my previous post I tried to help him but I doubt he'll take the help, you could put up a sign for free Java lessons for 5 years and he'd be like "Rather do Spigot API coding" but I'm not trying to start a flame war
     
    • Agree Agree x 1
    • Funny Funny x 1