Swear Filter

Discussion in 'Plugin Development' started by RebzO1, Aug 1, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    RebzO1

    I have a plugin that has a list off banned words, it works for the most part.
    add the word manually into the config.yml and then reload the config in game

    But now i am trying to make it so you can add the word ingame and make it work

    Code:
                    if (CommandLabel.equalsIgnoreCase("curseadd") && p.isOp()){
                        if(args.length == 1){
                            getConfig().getStringList("cursewords").add(args[0]);
                            saveDefaultConfig();
                            p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!");
                        }
                        else p.sendMessage(ChatColor.RED + "Usage /curse bannedword");
                       
                        return true;
                    }
    I thought that this may work when i /curseadd test
    Could I get a little help with this please?
     
  2. Offline

    PDKnight

    well, everything's great, but when you get the string list and add something to it, you have to add it to config then, I'll show you an example:
    Code:java
    1. if (CommandLabel.equalsIgnoreCase("curseadd") && p.isOp()){
    2. if(args.length == 1){
    3. getConfig().set("cursewords", getConfig().getStringList("cursewords").add(args[0]));
    4. // ^ I've changed this line ^
    5. saveDefaultConfig();
    6. p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!");
    7. }
    8. else p.sendMessage(ChatColor.RED + "Usage /curse bannedword");
    9.  
    10. return true;
    11. }

    So for next programming, use getConfig().set(path,data) :)
     
    RebzO1 likes this.
  3. Offline

    RebzO1

    So close lol
    It is still not adding the word to the config file
     
  4. Offline

    PDKnight

    I don't really now, but you should do saveConfig() if you're using default config.yml :)
     
  5. Offline

    RebzO1

    i dunno i guess ill figure it out at some point
     
  6. Offline

    MCMatters

    @RebzO1 use saveConfig instead of saveDefaultConfig.
     
  7. Offline

    RebzO1

    I changed the code too:

    Code:
                    if (CommandLabel.equalsIgnoreCase("curseadd") && p.isOp()){
                        if(args.length == 1){
                            getConfig().set("cursewords", getConfig().getStringList("cursewords").add(args[0]));
                            saveConfig();
                            p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!");
                        }
                        else p.sendMessage(ChatColor.RED + "Usage /curse bannedword");
                    
                        return true;
                    }
                
                }
    Now when i look in my config file the list of banned words is gone and it says:
    cursewords: true

    I am at a complete loss now
    And when i type '/curseword test' it isn't banning that word

    I have changed it back to saveDefaultConfig() temporarily so that i can at least modify the config then '/cursereload' to load the new list
     
    Last edited: Aug 1, 2015
  8. Offline

    MCMatters

  9. Offline

    RebzO1

    Code:
    package me.rebz.anticurse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    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 org.bukkit.plugin.java.JavaPlugin;
    
       
        public class Main extends JavaPlugin implements Listener{
    
            public void onEnable() {
                saveDefaultConfig();
                Bukkit.getServer().getPluginManager().registerEvents(this, this);
                getLogger().info("CC-AnitiCurse Enabled!");
            }
            public void onDisable() {
                saveDefaultConfig();   
                getLogger().info("CC-AntiCurse Disabled!");
            }
    
            @EventHandler
            public void onPlayerChat(AsyncPlayerChatEvent e){
                Player p = e.getPlayer();
                for (String text : e.getMessage().split(" ")){
                    if (getConfig().getStringList("cursewords").contains(text)){
                        e.setCancelled(true);
                        Bukkit.broadcastMessage(ChatColor.DARK_AQUA + p.getName() + ChatColor.RED + " Please watch your language!");
                    }
                }
            }
           
            public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
                Player p = (Player) sender;{
    
                    if (CommandLabel.equalsIgnoreCase("cursereload") && p.isOp()){
                        reloadConfig();
                        p.sendMessage(ChatColor.GREEN + "Configuration Reloaded!");
                        return true;
                    }
                   
                    if (CommandLabel.equalsIgnoreCase("curseadd") && p.isOp()){
                        if(args.length == 1){
                            getConfig().set("cursewords", getConfig().getStringList("cursewords").add(args[0]));
                            saveDefaultConfig();
                            p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!");
                        }
                        else p.sendMessage(ChatColor.RED + "Usage /curse bannedword");
                       
                        return true;
                    }
                   
                }
                return false;
            }
    }
    
     
  10. Offline

    MCMatters

    @RebzO1 please do my changes and use Command#getName
     
  11. @RebzO1 Remove the saveDefaultConfig on disable and you don't need messages for enable and disable. Also use cmd.getName() not commandLabel and always check before casting (Look at @mine-care's signature if needed) Use saveConfig when saving in a command.
     
    mine-care likes this.
  12. Offline

    RebzO1

    Code:
    package me.rebz.anticurse;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    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 org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin implements Listener{
    
        public void onEnable() {
            saveDefaultConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
        public void onDisable() {
        }
    
        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            Player p = e.getPlayer();
            for (String text : e.getMessage().split(" ")){
                if (getConfig().getStringList("cursewords").contains(text)){
                    e.setCancelled(true);
                    Bukkit.broadcastMessage(ChatColor.DARK_AQUA + p.getName() + ChatColor.RED + " Please watch your language!");
                }
            }
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String CommandLabel, String[] args){
            Player p = (Player) sender;{
    
                if (cmd.getName().equalsIgnoreCase("cursereload") && p.isOp()){
                    reloadConfig();
                    p.sendMessage(ChatColor.GREEN + "Configuration Reloaded!");
                    return true;
                }
    
                if (cmd.getName().equalsIgnoreCase("curseadd") && p.isOp()){
                    if(args.length == 1){
                        getConfig().set("cursewords", getConfig().getStringList("cursewords").add(args[0]));
                        saveConfig();
                        p.sendMessage(ChatColor.RED + (args[0]) + ChatColor.DARK_AQUA + " added to banned words!");
                    }
                    else p.sendMessage(ChatColor.RED + "Usage /curse bannedword");
                    return true;
                }
    
            }
            return false;
        }
    }
    
    made the changes u wanted could you check through it and lemme know
     
  13. @RebzO1
    And why is there a open bracket after sender; apart from that it looks fine
     
  14. Offline

    RebzO1

    Ran the changes on the test server and again it changed my config.yml
    when I type /curseadd test.

    The config.yml changed from the list of words to cursewords:true.

    I really appreciate the replies btw sorry to be a pain I just have no clue what is going on.

    My bad, i didn't even notice that

    Removed it.

    Issue is still the same as my post above
     
  15. @RebzO1 Try doing List<String> words = getTheListOfStrings
    Add the new swear and then reset the list of strings that that list
     
  16. Offline

    Zombie_Striker

    1. Remove the onDisabled, since it does nothing.
    2. Your a broadcasting to everyone on the server by using getServer.broadcastMessage(), but from the text it seems you just want to send it to that player.
    3. The ArrayList#.add method returns a boolean, not the array instance. Create an instance of the array, add the word, then set it in the config.
     
    mine-care likes this.
Thread Status:
Not open for further replies.

Share This Page