Solved e.setCancelled(true) failing to work..

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

Thread Status:
Not open for further replies.
  1. Hey guys!

    In this anti advertising plugin, When I go to say something that should be blocked, for instance mineplex.com it allows the message to go through, and it doesn't message the advertising player "Advertising-Message"

    CODE:

    Code (Text):
    package ChatFilter;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    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 me.DecisionsYT.Main;

    public class AntiAdvertise implements Listener {
       
       
    Main plugin;
       
        public AntiAdvertise(Main passedPlugin) {
            this.plugin = passedPlugin;
           
       
          }
       
        public static AntiAdvertise instance = null;

      Boolean filter = Boolean.valueOf(true);

         

         
      @EventHandler
      public void onAdvertiseChat(AsyncPlayerChatEvent e)
      {
        if (this.filter.booleanValue())
        {
          Player player = e.getPlayer();
          if (player.hasPermission("ChatX.AntiAd.Bypass")) {
            return;
          }
         
          if (!player.hasPermission("ChatX.AntiAd.Bypass")) {
              return;
          }
         
          if (!player.hasPermission("ChatX.AntiAd.Alert")) {
              return;
          }
         
            Pattern pattern = Pattern.compile("(?i)(((([a-zA-Z0-9-]+\\.)+(gs|ts|adv|no|uk|us|de|eu|com|net|noip|to|gs|me|info|biz|tv|au))+(\\:[0-9]{2,5})?))");
            Pattern pattern2 = Pattern.compile("(?i)(((([0-9]{1,3}\\.){3}[0-9]{1,3})(\\:[0-9]{2,5})?))");
            Matcher matcher = pattern.matcher(e.getMessage());
            Matcher matcher2 = pattern2.matcher(e.getMessage());

            if (matcher.find()) {
               
              e.setCancelled(true);
              player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Advertising-Message")));
             
             
              for (Player Staff : Bukkit.getOnlinePlayers()) {
                if (Staff.hasPermission("ChatX.AntiAd.Alert")) {
                  Staff.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Advertising-Alert").replace("{player}", e.getPlayer().getName().replace("{message}", e.getMessage()))));
                }
              }
              return;
            }
            if (matcher2.find())
            {
              e.setCancelled(true);
                   }
            }
          }
    }
     
  2. Choco

    Moderator

    (1) First of all, use primitive data types instead of their components
    Code (Java):
    boolean filter = true;
    (2) Second of all... you realize what you're doing here, right?
    Code (Java):
          if (player.hasPermission("ChatX.AntiAd.Bypass")) {
            return;
          }
     
          if (!player.hasPermission("ChatX.AntiAd.Bypass")) {
              return;
          }
    If the player has the permission, return. If the person doesn't have the permission, return. So basically, whether they have the permission or not, it's not going to run the code after those two if statements

    (3) This piece of code is never initialized, is never used, and is always null (Unless I just missed the line where it was initialized, but I don't see it)
    Code (Java):
    public static AntiAdvertise instance = null;
    (4) Variable names (even temporary ones) should be lowerCamelCase as per Java conventions
    Code (Java):
    for (Player Staff : Bukkit.getOnlinePlayers()) {
    i.e., use "staff" vs. "Staff"
     
    • Agree Agree x 4
    • Winner Winner x 1
  3. 2) and 3) Yea I guess, 2) My brain = Shutdown 3) Lack of brain usage xD


    Sent from my iPod touch using Tapatalk
     
    • Like Like x 1
  4. Okay, doing so resulted in nothing working at all... No cancelling of messages or sending messages to the player...
     
  5. For 4) unless you tell me it's nessicary, I'll just keep as is, as I prefer it that way, also, got any suggestions now?
     
  6. Choco

    Moderator

    Your preferences go against java conventions ;) It's a good thing to keep in mind for next time.
    Check if your listener is actually registered. Perhaps you just didn't register it at all.

    Also, just in case you misunderstood what I was specifying in my first reply, let's see what you've got now
     
  7. CODE NOW:

    Code (Text):
    package ChatFilter;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    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 me.DecisionsYT.Main;

    public class AntiAdvertise implements Listener {
     
     
    Main plugin;
     
        public AntiAdvertise(Main passedPlugin) {
            this.plugin = passedPlugin;
         
     
          }
     

      Boolean filter = true;

       

       
      @EventHandler
      public void onAdvertiseChat(AsyncPlayerChatEvent e)
      {
        if (this.filter.booleanValue())
        {
          Player player = e.getPlayer();
          if (player.hasPermission("ChatX.AntiAd.Bypass")) {
            return;
          }
       
       
          if (!player.hasPermission("ChatX.AntiAd.Alert")) {
              return;
          }
       
            Pattern pattern = Pattern.compile("(?i)(((([a-zA-Z0-9-]+\\.)+(gs|ts|adv|no|uk|us|de|eu|com|net|noip|to|gs|me|info|biz|tv|au))+(\\:[0-9]{2,5})?))");
            Pattern pattern2 = Pattern.compile("(?i)(((([0-9]{1,3}\\.){3}[0-9]{1,3})(\\:[0-9]{2,5})?))");
            Matcher matcher = pattern.matcher(e.getMessage());
            Matcher matcher2 = pattern2.matcher(e.getMessage());

         
            if (!player.hasPermission("ChatX.AntiAd.Bypass")) {
                if (matcher.find()) {
                 
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Advertising-Message")));
                }
                 
                    for (Player Staff : Bukkit.getOnlinePlayers()) {
                      if (Staff.hasPermission("ChatX.AntiAd.Alert")) {
                        Staff.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Advertising-Alert").replace("{player}", e.getPlayer().getName().replace("{message}", e.getMessage()))));
                      }
                    }
                    return;
                  }
                  if (matcher2.find())
                  {
                    e.setCancelled(true);
                         }
                  }
            }
          }
    MAIN CLASS:

    Code (Text):
    package me.DecisionsYT;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import ChatChannels.DonorChat;
    import ChatChannels.StaffChat;
    import ChatFilter.AntiAdvertise;
    import ChatFilter.AntiSpam;
    import ChatFilter.AntiSwear;
    import ChatFilter.LockChat;
    import ChatFilter.Mute;
    import ClearChat.ClearChat;
    import Events.Join;
    import Events.Quit;
    import HelpCommand.Help;
    import Links.Shop;


    public class Main extends JavaPlugin implements Listener {

        public void onEnable() {
         
              saveDefaultConfig();
         
           
              //Events
              PluginManager pm = Bukkit.getServer().getPluginManager();
              LockChat lockChat = new LockChat(this);
              pm.registerEvents(this, this);
              pm.registerEvents(new AntiSwear(this), this);
              pm.registerEvents(lockChat, this);
              pm.registerEvents(new Join(this), this);
              pm.registerEvents(new Quit(this), this);
              pm.registerEvents(new Mute(this), this);
              pm.registerEvents(new AntiSpam(this), this);
              pm.registerEvents(new AntiAdvertise(this), this);
           
              //Chat Channels
              getCommand("SC").setExecutor(new StaffChat(this));
              getCommand("StaffChat").setExecutor(new StaffChat(this));
              getCommand("DC").setExecutor(new DonorChat(this));
              getCommand("DonorChat").setExecutor(new DonorChat(this));
           
              //ClearChat
              getCommand("ClearChat").setExecutor(new ClearChat(this));
              getCommand("CC").setExecutor(new ClearChat(this));
           
              //LockChat
              getCommand("LockChat").setExecutor(lockChat);
           
              //Help
              getCommand("ChatX").setExecutor(new Help(this));
           
              //Links
              getCommand("Buy").setExecutor(new Shop(this));
              getCommand("Shop").setExecutor(new Shop(this));
              getCommand("Rules").setExecutor(new Shop(this));
              getCommand("Website").setExecutor(new Shop(this));
           
              //Mute
              getCommand("Mute").setExecutor(new Mute(this));
           
           
         
           
              Bukkit.getServer().getConsoleSender().sendMessage("§b§lChatX has been enabled!");
        }
     
        public void onDisable() {
         
            Bukkit.getServer().getConsoleSender().sendMessage("§b§lChatX has been disabled!");
         
        }
     
     
    }
     
     
  8. MiniDigger

    Supporter

    1) this check is redundant because you returned if the player has Bypass perms earlier already so this is always true.
    2) you are still using the wrapper object instead of the primitive type.
    3) I am not sure if you want to have that there. If understood it right, you want the Alert perm to identify staff members. But now you abort the whole filter stuff if the player who chatted is not staff.
    4) you are alerting the staff everytime. you want to altert only if the matcher matched.
     
  9. Okay so, what issues are left from doing this:

    Code (Text):
    package ChatFilter;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    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 me.DecisionsYT.Main;

    public class AntiAdvertise implements Listener {
       
       
    Main plugin;
       
        public AntiAdvertise(Main passedPlugin) {
            this.plugin = passedPlugin;
           
       
          }
       

      Boolean filter = true;

         

         
      @EventHandler
      public void onAdvertiseChat(AsyncPlayerChatEvent e)
      {
        if (this.filter.booleanValue())
        {
          Player player = e.getPlayer();
          if (player.hasPermission("ChatX.AntiAd.Bypass")) {
            return;
          }
         
         
         
            Pattern pattern = Pattern.compile("(?i)(((([a-zA-Z0-9-]+\\.)+(gs|ts|adv|no|uk|us|de|eu|com|net|noip|to|gs|me|info|biz|tv|au))+(\\:[0-9]{2,5})?))");
            Pattern pattern2 = Pattern.compile("(?i)(((([0-9]{1,3}\\.){3}[0-9]{1,3})(\\:[0-9]{2,5})?))");
            Matcher matcher = pattern.matcher(e.getMessage());
            Matcher matcher2 = pattern2.matcher(e.getMessage());

           
            if (!player.hasPermission("ChatX.AntiAd.Bypass")) {
                if (matcher.find()) {
                   
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Advertising-Message")));
                }
                   
                    return;
                  }
                  if (matcher2.find())
                  {
                    e.setCancelled(true);
                         }
                  }
            }
          }
     
  10. Anyone have suggestions?
     
  11. MiniDigger

    Supporter

    1 and 2 are still there
     
  12. Okay, now??

    Code (Text):
    package ChatFilter;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    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 me.DecisionsYT.Main;

    public class AntiAdvertise implements Listener {
       
       
    Main plugin;
       
        public AntiAdvertise(Main passedPlugin) {
            this.plugin = passedPlugin;
           
       
          }
       

      Boolean filter = true;

         

         
      @EventHandler
      public void onAdvertiseChat(AsyncPlayerChatEvent e)
      {
        if (this.filter.booleanValue())
        {
          Player player = e.getPlayer();
          if (player.hasPermission("ChatX.AntiAd.Bypass")) {
            return;
          }
         
         
         
            Pattern pattern = Pattern.compile("(?i)(((([a-zA-Z0-9-]+\\.)+(gs|ts|adv|no|uk|us|de|eu|com|net|noip|to|gs|me|info|biz|tv|au))+(\\:[0-9]{2,5})?))");
            Pattern pattern2 = Pattern.compile("(?i)(((([0-9]{1,3}\\.){3}[0-9]{1,3})(\\:[0-9]{2,5})?))");
            Matcher matcher = pattern.matcher(e.getMessage());
            Matcher matcher2 = pattern2.matcher(e.getMessage());

           
                if (matcher.find()) {
                   
                    e.setCancelled(true);
                    player.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Advertising-Message")));
                }
                   
                  if (matcher2.find())
                  {
                    e.setCancelled(true);
                     }
                  }
               }
        }
    I haven't done anything to do with 2) as I don't understand what you want me to do to make it a primitive type...
     
  13. MiniDigger

    Supporter

    in have, use boolean not Boolean. Boolean is an object, boolean is an primitive type (like int, long, float, byte, char, idk)
     
  14. Ahh!

    I see, so, doing so gives me this error: http://prntscr.com/b38u5c
     
  15. MiniDigger

    Supporter

    remove the method call, it is not needed. if takes an boolean. You gave it a Boolean so you needed to call booleanValue (which returns a boolean)
     
  16. Okay, I have removed
    Code (Text):
    if (this.filter.booleanValue())
    Lets test it.
     
  17. OMG YAY!!! IT WORKED THANKS SOOOO MUCH!!!!

    I REALLY APPRECIATE IT!!!

    OH WOW! JUST REALIZED CAPS LOCK WAS ON!! OH WELL...
     
    • Funny Funny x 1
Thread Status:
Not open for further replies.