Question Basic Array Adding

Discussion in 'Plugin Help/Development/Requests' started by 2008Choco, Apr 8, 2015.

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


    Okay so basically my entire purpose of this, is an optional filter. I have it working, so that's great. But there's one issue. Players are clever at bypassing this swear filter. For example, using "@" instead of "a". Now I know, in that case, I can replaceall("@", "a"), but that doesn't cover all the possibilities for swears. So instead, my solution was to make a command to add a specified word to the filter. My only problem is... I have no idea how. Hah.

    So I know basic Java, yes. I do know about adding to Arrays, but for some reason, I can't figure this out. And I'm sure my mind is just blanking right now, but I would appreciate the help. Here's the code I have so far..

    Filter command: (This is a subcommand to /awolchat, hense testing if args[0].equalsIgnoreCase("filter"))
                    if (args[0].equalsIgnoreCase("filter")){
                        if (player.hasPermission("awolchat.filter")){
                            if (args.length == 1){
                                if (getConfig().getBoolean("filterChat") == false){
                                    Bukkit.broadcastMessage(ChatColor.GOLD + "AWOL " + ChatColor.YELLOW + ">> " + ChatColor.GRAY + player.getName() + " has enabled the chat filter");
                                    return true;
                                }//If chat isn't already filtered, toggle it on
                                    Bukkit.broadcastMessage(ChatColor.GOLD + "AWOL " + ChatColor.YELLOW + ">> " + ChatColor.GRAY + player.getName() + " has disabled the chat filter");
                                    return true;
                                }//If chat is already filtered, toggle it off
                            }//Close if args == 1
                            if (args.length == 2){
                                player.sendMessage(ChatColor.GOLD + "AWOL " + ChatColor.YELLOW + ">> " + ChatColor.GRAY + "Invalid Syntax: " + ChatColor.LIGHT_PURPLE + "/awolchat filter <add/remove> <word>");
                            }//Close if args == 2
                            if (args.length == 3){
                                if (args[1].equalsIgnoreCase("add")){
                                    //TODO: Add a word to an array
                                }//Add swear word to filter
                                if (args[1].equalsIgnoreCase("remove")){
                                    //TODO: Remove a word from an array
                                }//Remove swear word from filter
                            }//Close if args == 3
                        }//Close permissions == true statement
                            player.sendMessage(ChatColor.GOLD + "AWOL " + ChatColor.YELLOW + ">> " + ChatColor.RED + "ERROR: " + ChatColor.GRAY + "You do not have the proper permissions to run this command");
                            return true;
                        }//Close permissions == false statement
                    }//Close filter sub-command
    This is the filter portion of the @EventHandler: (Swears have been removed)
                String[] swearWords = {"word1", "word2", "word3" /*There are usually swears here, but I can't do that*/};
                String message = event.getMessage().toLowerCase().replaceAll("@", "a").replaceAll("\\p{Punct}", " ");
                for(String word : swearWords){
                    if(message.matches("(.* )?"+word+"( .*)?")) {
                        event.setMessage(event.getMessage().toLowerCase().replaceAll(word, "*****"));
                    else {
    Okay, cool. So obviously, I haven't implemented the methods for those add and remove commands. That's the part I need help with. Could someone show me how I could add a string into that "swearWords" array of Strings?
  2. Offline


    The easiest way would be to use something like an arraylist, that way you don't have to initialize a new array every time you want to add something. With an ArrayList, you might do something like this:

    ArrayList<String> swearWords = new ArrayList<String>();
    And then in your add command you could do:
    If you absolutely want to stick with arrays, then you can do this to add to swearWords:
    String[] temp = swearWords;
    swearWords = new String[swearWords.length+1]();
    for (int i = 0; i < temp.length; i++) {
        swearWords = temp;
    swearWords[swearWords.length-1] = args[2];
    2008Choco likes this.
  3. Offline


    @2008Choco By the way, spammers are crafty little buggers. There are other ways to swear. There are a whole set of letters that are basically just upside down - google "upside down text". There's letters in circles, spanish/french letters (ñ for instance), tiny uppercase, tiny topscript uppercase, Greek letters, so fricking many things to watch out for.
    Just saying.
    Case in point:
    don't click here (open)
    ↓ ᶠᵘᶜᵏ ᵗʰᶦˢ ᵍᵘʸ↓ ↑ ᶠᵘᶜᵏ ʸᵒᵘ ᵗᵒᵒ ↑
  4. Offline


    @pie_flavor That's not really something I'm 100% concerned about. I'm not making a Mineplex chat filter (although that would be pretty key). Sure it's an issue, but if at that point, the player is trying to bypass the filter with those, I'd be willing to kick them or ban them if due necessary.

    @DrAgonmoray I did the ArrayList thing, and it works beautifully, but the only issue is, I have to do swearWords.add every single time I start up the server. Yes the /filter add <word> command does add to the filter, but only for that session. If the server restarts, that word is removed from the filter. I think that's one of the reasons I stayed away from it in the first place and used a list.

    My goal would be to make an individual file containing all the swear words I'd like to filter. That way, I wouldn't have to have cuss words in my code :p No one wants to see that. Not even I want to see it just scrolling along my onEnable() method. But one issue... I don't know how to read and write from files just yet :3 Again, teaching myself Java and Bukkit, so I'm only basic. Would you have any tips on how to read and write to a file instead of having cusses in my code?
  5. Offline


    @2008Choco Use an ArrayList<String> and save it to an output bin file using ObjectOutputStream.
    2008Choco likes this.
Thread Status:
Not open for further replies.

Share This Page