Plugin still accessing default config?

Discussion in 'Plugin Development' started by Kakaroot, Feb 26, 2021.

  1. Offline

    Kakaroot

    I'm making a swear filter plugin, and when I remove a word from my config list (one which was originally in the default config) and reload, it still filters that word. However, I've tried adding a word of my own onto the server directly and that worked, then I also removed that word and that worked.

    So I think the plugin is still using the old config somehow?
    (Sorry if I'm being stupid, sort of new to this.)


    onEnable/onDisable:
    Code:
        public void onEnable() {
            System.out.println("KFilter has loaded successfully.");
            getServer().getPluginManager().registerEvents(new SwearFilter(), this);
            this.saveDefaultConfig();
            instance = this;
    
            this.getCommand("kf").setExecutor((CommandExecutor) new BaseCommand());
    
        }
    
        public void onDisable() {
        }
    The part that replaces:
    Code:
    public class SwearFilter implements Listener {
    
        @EventHandler
        public static void onPlayerChatEvent(final AsyncPlayerChatEvent e) {
            String msg = e.getMessage();
            String[] split_message = msg.split(" ");
            Main mainClass = Main.getInstance();
            FileConfiguration config = mainClass.getConfig();
            ConfigurationSection banned_words = config.getConfigurationSection("banned_words");
            for (String word : split_message) {
                String word_lower = word.toLowerCase();
                if (banned_words.contains(word_lower)) {
                    msg = msg.replace(word, config.getString("banned_words." + word_lower));
                    e.setMessage(msg);
                    break;
                }
            }
        }
    }
    Any help would be really appreciated :)
     
  2. Offline

    Strahan

    Code:
    System.out.println("KFilter has loaded successfully.");
    This is not necessary; Spigot already does it so this just results in log clutter.
    Code:
    this.saveDefaultConfig();
    This should be before any classes are created.
    Code:
    this.getCommand("kf").setExecutor((CommandExecutor) new BaseCommand());
    This shouldn't need to be cast if you setup the class properly.
    Code:
    public void onDisable() {
    }
    There is no point to having an empty method.
    Code:
    public static void onPlayerChatEvent(final AsyncPlayerChatEvent e) {
    The static keyword is not necessary here.
    Code:
    Main mainClass = Main.getInstance();
    Personally, I think dependency injection would be the better way to go but that's just personal preference.
    Code:
    ConfigurationSection banned_words = config.getConfigurationSection("banned_words");
    You should always null check the return from getConfigurationSection, as it may be null.
    Code:
    if (banned_words.contains(word_lower)) {
    banned_words is a ConfigurationSection, not a collection of Strings so this will not work as you expect. What you likely should be doing is getting a List<String> from the config.
    Code:
    msg = msg.replace(word, config.getString("banned_words." + word_lower));
    OK, so I see what you are doing now. So your config has the word to match and it's value is what to swap? So like:
    Code:
    banned_words:
      shit: poop
      bitch: female dog
    If that's the case, you don't need to get a ConfigurationSection at all. Just do like:
    Code:
    for (String word : msg.split(" ")) {
      msg = msg.replace(word, config.getString("banned_words." + word, word));
    }
    That said, this is pointless because it will be stupid simple to break this filter.
     

Share This Page