Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with Java Language Filter

  1. Oct 13, 2008 #1
    I have run into a problem... The language filter we have been using is out of date and it periodically does not work. I was hoping I could get some help on making a new one.

    The old one:
    Code (Text):

    import java.io.File;
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.net.SocketAddress;
    import java.util.ArrayList;
    import java.util.Map;
    import java.util.Scanner;
    import java.util.StringTokenizer;

    import pokeglobal.server.object.Char;
    import pokeglobal.server.object.PlayerChar;

    /*
     *
     *
     * Filters bad language along with Game Moderator commands
     *
     */
    public class Moderator {
        private ArrayList<Filter> CensorList = new ArrayList<Filter>();
        private Map<String, PlayerChar> playerList;

           /**
            * Default Constructor which loads bad language database
            */
        public Moderator()
        {
            loadFilter();
            playerList = ClientHandler.getPlayerList();
           
        }
       
           /**
            * Adds a new bad word to the database and restarts the moderating system without a server shutdown.
            * @param String wordToFilter - badword:goodword , The word to be filtered will be replaced with the good word
            */
        //Allows mods to add a new word to the filter
        void addFilter(String wordToFilter)
        {
            try
            {  
                if (!wordToFilter.contains(":")) throw new Exception();
                PrintWriter filterWord = new PrintWriter(new FileWriter(
                        "res/badwords.txt"));
                for(int i = 0; i < CensorList.size(); i++)
                {
                    filterWord.println(CensorList.get(i).getOriginal() + ":" + CensorList.get(i).getNew());
                }
                filterWord.println(wordToFilter);
                //Close PrintWriters
                filterWord.close();
               
                //Reload filter list
                loadFilter();
            }
            catch (Exception e)
            {
                System.out.println("Could not add word to filter database");
            }
        }
       
           /**
            * Reloads the moderating database without restarting the server
            */
        //Reloads the filter list
        void loadFilter()
        {
            try
            {
                CensorList.clear();
                Scanner originalWords = new Scanner(new File("res/badwords.txt"));
                while(originalWords.hasNextLine())
                {
                    String s = originalWords.nextLine();
                    Filter f = new Filter();
                    if(s.indexOf("*") == 0)
                        f.set(s.substring(1), true);
                    else
                        f.set(s, false);
                    CensorList.add(f); 
                }
                originalWords.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }      
        }

           /**
            * Filters words/commands appropriately
            * @param String messageSent - The message sent by the player
            * @param PlayerChar username - The player who sent the message
            */
        //Returns the filtered word and executes any mods commands sent by mods
        String filter(String messageSent, PlayerChar username)
        {
            String output = "";
            if(!username.isMod())
                output = removeBad(messageSent);
            else
            {
                boolean playerFound = false;
                if(messageSent.indexOf("/addWord") == 0)
                {
                    addFilter(messageSent.substring(8));
                    output = username.getName() + " updated the Moderating System";
                }
                else if(messageSent.indexOf("/kick ") == 0)
                {
                    for (String key : playerList.keySet())
                    {
                        if(key.equalsIgnoreCase(messageSent.substring(5).trim()))
                        {
                            if (!playerList.get(key).isMod()){
                                playerFound = true;
                                kickPlayer(key);
                                username.getMap().sendToAll("CServer: " + messageSent.substring(5).trim() + " was kicked!");
                            }
                            break;
                        }
                    }
                    if (!playerFound)
                        username.getIoSession().write("CServer: " + messageSent.substring(5).trim() + " does not exist!");
                }
                else if(messageSent.indexOf("/kickall") == 0)
                {
                    for (String key : playerList.keySet())
                    {
                        kickPlayer(key);
                        username.getMap().sendToAll("CServer: " + messageSent.substring(5).trim() + " was kicked!");
                    }
                }
                else if(messageSent.indexOf("/mute") == 0)
                {
                        for (String key : playerList.keySet())
                        {
                            if(key.equalsIgnoreCase(messageSent.substring(5).trim()))
                            {
                                if (!playerList.get(key).isMod()){
                                    playerFound = true;
                                    mutePlayer(key);
                                    username.getMap().sendToAll("CServer: " + messageSent.substring(5).trim() + " was muted!");
                                }
                                break;
                            }
                        }
                        if (!playerFound)
                            username.getIoSession().write("CServer: " + messageSent.substring(5).trim() + " does not exist!");
                }
                else if(messageSent.indexOf("/silentmute") == 0)
                {
                        for (String key : playerList.keySet())
                        {
                            if(key.equalsIgnoreCase(messageSent.substring(11).trim()))
                            {
                                if (!playerList.get(key).isMod()){
                                    playerFound = true;
                                    silentmutePlayer(key);
                                   
                                }
                                break;
                            }
                        }
                        if (!playerFound)
                            username.getIoSession().write("CServer: " + messageSent.substring(5).trim() + " does not exist!");
                }
                else if(messageSent.indexOf("/disable") == 0)
                {
                        for (String key : playerList.keySet())
                        {
                            if(key.equalsIgnoreCase(messageSent.substring(8).trim()))
                            {
                                if (!playerList.get(key).isMod()){
                                    playerFound = true;
                                    disablePlayer(key);
                                    username.getMap().sendToAll("CServer: " + messageSent.substring(8).trim() + " was disabled!");
                                }
                                break;
                            }
                        }
                        if (!playerFound)
                            username.getIoSession().write("CServer: " + messageSent.substring(8).trim() + " does not exist!");
                }
                else if(messageSent.indexOf("/enable") == 0)
                {
                        for (String key : playerList.keySet())
                        {
                            if(key.equalsIgnoreCase(messageSent.substring(7).trim()))
                            {
                                if (!playerList.get(key).isMod()){
                                    playerFound = true;
                                    enablePlayer(key);
                                    username.getMap().sendToAll("CServer: " + messageSent.substring(7).trim() + " was freed from all moderator restrictions!");
                                }
                                break;
                            }
                        }
                        if (!playerFound)
                            username.getIoSession().write("CServer: " + messageSent.substring(7).trim() + " does not exist!");
                }
                else if(messageSent.indexOf("/unmute") == 0)
                {
                        for (String key : playerList.keySet())
                        {
                            if(key.equalsIgnoreCase(messageSent.substring(7).trim()))
                            {
                                if (!playerList.get(key).isMod()){
                                    playerFound = true;
                                    unmutePlayer(key);
                                    username.getMap().sendToAll("CServer: " + messageSent.substring(7).trim() + " was unmuted!");
                                }
                                break;
                            }
                        }
                        if (!playerFound)
                            username.getIoSession().write("CServer: " + messageSent.substring(7).trim() + " does not exist!");
                }
                else if(messageSent.indexOf("/freeze") == 0)
                {
                    for (String key : playerList.keySet())
                    {
                        if(key.equalsIgnoreCase(messageSent.substring(7).trim()))
                        {
                            if (!playerList.get(key).isMod()){
                                playerFound = true;
                                freezePlayer(key);
                                username.getMap().sendToAll("CServer: " + messageSent.substring(7).trim() + " was frozen!");
                            }
                            break;
                        }
                    }
                    if (!playerFound)
                        username.getIoSession().write("CServer: " + messageSent.substring(7).trim() + " does not exist!");
                }
                /*else if(messageSent.indexOf("/trade") == 0)
                {
                    for (String key : playerList.keySet())
                    {
                        if(key.equalsIgnoreCase(messageSent.substring(6).trim()))
                        {
                                playerFound = true;
                                PlayerChar player = playerList.get(key);
                                player.addTradePokemon(username.getParty()[0]);
                                username.removePokemon(username.getParty()[0]);
                                username.getMap().sendToAll("CServer: ");
                            break;
                        }
                    }
                    if (!playerFound)
                        username.getIoSession().write("CServer: " + messageSent.substring(5).trim() + " does not exist!");
                }*/
                else if(messageSent.indexOf("/unfreeze") == 0)
                {
                    for (String key : playerList.keySet())
                    {
                        if(key.equalsIgnoreCase(messageSent.substring(9).trim()))
                        {
                            if (!playerList.get(key).isMod()){
                                playerFound = true;
                                unfreezePlayer(key);
                                username.getMap().sendToAll("CServer: " + messageSent.substring(9).trim() + " was unfrozen!");
                            break;
                            }
                        }
                    }
                    if (!playerFound)
                        username.getIoSession().write("CServer: " + messageSent.substring(9).trim() + " does not exist!");
                }
                else if(messageSent.indexOf("/who") == 0)
                {
                    int playerCount = 0;
                    for (String key : playerList.keySet())
                    {
                        playerCount = playerCount + 1;
                        output = output + key + ", ";
                    }
                    username.getIoSession().write("C" + playerCount + " Players Online: " + output);
                    output = "";
                } else if (messageSent.indexOf("/antilag") == 0) {
                    playerList.get(username).getIoSession().close();
                }


                /*else if(messageSent.indexOf("/setsprite") == 0){
                   
                    username.getName();
                    username.setSprite(messageSent.substring(10));
                    username.getMap().sendToAll("CServer: GM changed his sprite and needs to reload.");
                   
                }*/
               
               
                // /pos command gets server to tell your position in chat.
                else if(messageSent.indexOf("/pos") == 0){     
                   
                    for (String key : playerList.keySet())
                    {
                        if(key.equalsIgnoreCase(messageSent.substring(4).trim()))
                        {
                            if (!playerList.get(key).isMod()){
                                playerFound = true;
                                posPlayer(key, username);
                            }
                            break;
                        }
                    }
                    if (!playerFound)
                        username.getIoSession().write("CServer: " + messageSent.substring(4).trim() + " does not exist!");
                   
                }
                else if(messageSent.indexOf("/teletoplayer") == 0){    
                   
                    for (String key : playerList.keySet())
                    {
                        if(key.equalsIgnoreCase(messageSent.substring(13).trim()))
                        {
                            if (!playerList.get(key).isMod()){
                                playerFound = true;
                                teleToPlayerPlayer(key, username);
                            }
                            break;
                        }
                    }
                    if (!playerFound)
                        username.getIoSession().write("CServer: " + messageSent.substring(4).trim() + " does not exist!");
                   
                }
                else if(messageSent.indexOf("/getip") == 0){
                    for(String key : playerList.keySet()){
                        if(key.equalsIgnoreCase(messageSent.substring(13).trim())){
                            PlayerChar player = playerList.get(key);
                            if(!playerList.get(key).isMod()){
                                username.getIoSession().write("CServer: The player " + player.getName() + " ip is " + player.getIoSession().getLocalAddress());
                            }else{
                                username.getIoSession().write("CServer: You cannot try to get game moderator ip address.");
                            }
                               
                        }
                    }
                }
                else if(messageSent.indexOf("/announce") == 0){
                    for (PlayerChar p : playerList.values())
                        p.getIoSession().write("C<Server Announcement>: " + messageSent.substring(9).trim());
                   
                }
                else
                    output = removeBad(messageSent);
            }
            return output;
        }
       
           /**
            * Removes all bad language
            * @param String messageSent
            */
        String removeBad(String messageSent)
        {
            String temp = messageSent.trim();
            StringTokenizer st3 = new StringTokenizer(temp);
            String output = "";
            while(st3.hasMoreTokens())
            {
                String check = st3.nextToken();
                for(int i = 0; i < CensorList.size(); i++)
                {
                    if(CensorList.get(i).hasWildCard)
                    {
                        if(check.toLowerCase().indexOf(CensorList.get(i).getOriginal().toLowerCase()) >= 0)
                        {
                            check = CensorList.get(i).getNew();
                            break;
                        }              
                    }
                    else
                    {
                        if(check.equalsIgnoreCase(CensorList.get(i).getOriginal()))
                        {
                            check = CensorList.get(i).getNew();
                            break;
                        }                      
                    }
                }
                output = output + check + " ";
            }
            return output;
        }
       
           /**
            * Kicks a player from the server and informs the player they have been kicked
            * @param String playerName - Player's name
            */
        void kickPlayer(String playerName)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    player.getIoSession().write("mYou have been kicked from Pokemon Global.");
                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    player.getIoSession().close();
                    System.out.println(playerName + " was kicked!");
                }
            }
        }
       
           /**
            * Mutes a player (disables them from using chat) and informs them they have been muted
            * @param String playername - Player's name
            */
        void mutePlayer(String playerName)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    player.getIoSession().write("mYou have been muted. You may no longer use the chat functions.");
                    player.setMuted(true);
                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    System.out.println(playerName + " was muted!");
                }
            }
        }
        void silentmutePlayer(String playerName)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    player.setMuted(true);
                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
            }
        }
       
           /**
            * Unmutes a player and informs them they have been unmuted
            * @param String playername - Player's name
            */
        void unmutePlayer(String playerName)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    player.getIoSession().write("mYou have been unmuted. You may now use the chat functions again.");
                    player.setMuted(false);
                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    System.out.println(playerName + " was unmuted!");
                }
            }
        }
       
           /**
            * Freezes and Mutes a player
            * @param String playername - Player's name
            */
        void disablePlayer(String playerName)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    player.setFrozen(true);
                    player.setMuted(true);
                    player.getIoSession().write("mYou have been disabled from using Pokemon Global.");
                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    player.getIoSession().close();
                    System.out.println(playerName + " was disabled!");
                }
            }
        }
       
           /**
            * Unfreezes and unmutes a player
            * @param String playername - Player's name
            */
        void enablePlayer(String playerName)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    player.setFrozen(false);
                    player.setMuted(false);
                    player.getIoSession().write("mYou have been freed from all moderator restrictions.");
                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    System.out.println(playerName + " was enabled!");
                }
            }
        }
           /**
            * Disables player movement
            * @param String playername - Player's name
            */
            void freezePlayer(String playerName)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    player.getIoSession().write("mYou have been frozen. You may no longer move. A moderator may require your attention.");
                    player.setFrozen(true);
                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    System.out.println(playerName + " was frozen!");
                }
            }
        }
       
               /**
                * Re-enables player's movement
                * @param String playername - Player's name
                */
        void unfreezePlayer(String playerName)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    player.getIoSession().write("mYou have been unfrozen. You may move again.");
                    player.setFrozen(false);
                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    System.out.println(playerName + " was unfrozen!");
                }
            }
        }
       
        void posPlayer(String playerName, PlayerChar username)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    username.getIoSession().write("C" +
                    "------------------------" +
                    player.getName() +
                    "'s current map position is: " +
                    player.getMapX() +
                    ", " +
                    player.getMapY() +
                    ". " +
                    player.getName() +
                    "'s current map co-ord's are: " +
                    player.getX() +
                    ", " +
                    player.getY() +
                    ". " +
                    "------------------------");

                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    System.out.println("Found the position for " + playerName);
                }
            }
        }
        void teleToPlayerPlayer(String playerName, PlayerChar username)
        {
            PlayerChar player = playerList.get(playerName);
            if (player != null && !player.isMod()) {
                try
                {
                    username.setMap(player.getMap());
                    username.setX(player.getX());
                    username.setY(player.getY());
                    username.getIoSession().write("C" + "You have teleported to player" + playerName);

                    Thread.sleep(500);
                }
                catch (Exception e)
                {
                }
                finally
                {
                    System.out.println("Found the position for " + playerName);
                }
            }
        }
    }

    class Filter
    {
        private String f;
        boolean hasWildCard;
       
        void set(String o1, boolean wild)
        {
            f = o1;
            hasWildCard = wild;    
        }
       
        String getOriginal()
        {
            return f.substring(0, f.indexOf(':'));
        }
       
        String getNew()
        {
            return f.substring(f.indexOf(':') + 1);
        }
    }
     
    The "/res/badwords.txt" is:

    Badword:GoodWord

    Badword is any bad word that you want filtered out
    Goodword is what you are replacing it with (like !@#$)

    Now this system does work sometimes, but recently the server was updated and this system has fallen apart. If anyone has any concept ideas or anything please let me know. Thank you.
     
    Last edited: Oct 13, 2008
  2. jcsd
  3. Oct 26, 2008 #2
    Do you get paid for fixing this? We don't.

    We're here to help, so you need to help us to help you.

    Most people would look at a post, and give an answer in a matter of a minute. If it takes more time than that, they move on. Guess why there is not a single answer since your post ten days ago.

    Here are some suggestions:
    1. Be precise about your problem.
    does not tell us much unless you indicate what makes you think it does not work, error messages, etc.
    2. Extract the part of the code that you think is causing the problem.
    If you think people have nothing better to do than to study your precisely 650 lines of code to look for your problem, think twice. This is your responsibility to help people to help you.
    If you are not a programmer, or if you did not write the programme, hire one.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Help with Java Language Filter
  1. Java help (Replies: 19)

  2. Java help (Replies: 3)

  3. Java help (Replies: 6)

Loading...