AntiSwear [Source Code Included] 2015-12-06

Anti-Swear

  1. RealEmpire
    Contributors:
    RealEmpire
    Continued with updates!

    1.8 clients can no longer bypass the filter!
    New Commands!

    /filter add | filter.add
    /filter remove | filter.remove
    /filter list | filter.list
    filter.bypass

    Pretty self explanatory, enjoy!

    Source Code:
    Code (Text):
    package me.Kruciial.noswear;

    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.ChatColor;
    import org.bukkit.Server;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    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.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    public class AntiSwear
      extends JavaPlugin
      implements Listener
    {
      public void onEnable()
      {
        getServer().getPluginManager().registerEvents(this, this);
        saveDefaultConfig();
        if (getSwearWords() == null)
        {
          getConfig().set("swear-words", new ArrayList());
          saveConfig();
        }
      }

      public void onDisable() {}

      public List<String> getSwearWords()
      {
        return getConfig().getStringList("swear-words");
      }

      public String getCensorSymbol()
      {
        return getConfig().getString("censor-symbol");
      }

      public void setCensorSymbol(String symbol)
      {
        getConfig().set("censor-symbol", symbol);
        saveConfig();
      }

      public void saveSwearWord(String word)
      {
        List<String> swearWords = getSwearWords();
        if (!swearWords.contains(word.toLowerCase())) {
          swearWords.add(word.toLowerCase());
        }
        getConfig().set("swear-words", swearWords);
        saveConfig();
      }

      public void removeSwearWord(String word)
      {
        List<String> swearWords = getSwearWords();
        swearWords.remove(word.toLowerCase());
        getConfig().set("swear-words", swearWords);
        saveConfig();
      }

      public String censor(String string)
      {
        String censored = string;
        for (String swearWord : getSwearWords())
        {
          String censor = "";
          for (int i = 0; i < swearWord.length(); i++) {
            censor = censor + getCensorSymbol();
          }
          censored = censored.replaceAll("(?i)" + swearWord, censor);
        }
        return censored;
      }

      public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
      {
        if (cmd.getLabel().equalsIgnoreCase("filter")) {
          if ((args.length >= 1) && (args[0].equalsIgnoreCase("add")))
          {
            if (sender.hasPermission("filter.add"))
            {
              if (args.length == 1)
              {
                sender.sendMessage(ChatColor.RED + "Command Usage: /filter add <Swear Word>");
              }
              else
              {
                String word = "";
                for (int i = 1; i < args.length; i++) {
                  word = word + args[i] + " ";
                }
                word = word.substring(0, word.length() - 1);
                saveSwearWord(word);
                sender.sendMessage(ChatColor.GREEN + "Added swear word \"" + word + "\".");
              }
            }
            else {
              sender.sendMessage(ChatColor.RED + "You do not have permission to run that command!");
            }
          }
          else
          {
            int i;
            if ((args.length >= 1) && (args[0].equalsIgnoreCase("remove")))
            {
              if (sender.hasPermission("filter.remove"))
              {
                if (args.length == 1)
                {
                  sender.sendMessage(ChatColor.RED + "Command Usage: /filter remove <Swear Word>");
                }
                else
                {
                  String word = "";
                  for (i = 1; i < args.length; i++) {
                    word = word + args[i] + " ";
                  }
                  word = word.substring(0, word.length() - 1);
                  removeSwearWord(word);
                  sender.sendMessage(ChatColor.GREEN + "Removed swear word \"" + word + "\".");
                }
              }
              else {
                sender.sendMessage(ChatColor.RED + "You do not have permission to run that command!");
              }
            }
            else if ((args.length >= 1) && (args[0].equalsIgnoreCase("list")))
            {
              if (sender.hasPermission("filter.list"))
              {
                if (args.length != 1)
                {
                  sender.sendMessage(ChatColor.RED + "Command Usage: /filter list");
                }
                else
                {
                  sender.sendMessage(ChatColor.GREEN + "Swear Words:");
                  for (String word : getSwearWords()) {
                    sender.sendMessage(ChatColor.GRAY + "- " + word);
                  }
                }
              }
              else {
                sender.sendMessage(ChatColor.RED + "You do not have permission to run that command!");
              }
            }
            else {
              sender.sendMessage(ChatColor.RED + "Command Usage: /filter <add [Swear Word]|remove [Swear Word]|list>");
            }
          }
        }
        return true;
      }

      @EventHandler
      public void onPlayerChat(AsyncPlayerChatEvent evt)
      {
        if (!evt.getPlayer().hasPermission("filter.bypass")) {
          evt.setMessage(censor(evt.getMessage()));
        }
      }
    }
     
    Kezoq and DarksideCode like this.

Recent Updates

  1. AntiSwear

Recent Reviews

  1. GGhost
    GGhost
    3/5,
    Version: 2015-12-06
    Can't give it any more ratings as it is high on IO usage and not the best at it's job. Oh, and highly generic and just overall meh.
    1. RealEmpire
      Author's Response
      Well, this was my first plugin i'd made.