[SOLVED]Config hell! And IP filter regex

Discussion in 'Plugin Development' started by Lukeer32, Aug 3, 2012.

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

    Lukeer32

    Hi there. Just as I have been updating my plugin (Attempting to again). Well where to start.
    Firstly My code does not copy out the config file properly (misses out the comments) And it misses out a lot of the things in the list and replaces them with null

    Secondly when i try to access some string lists it does not work, and gives me null, which of course throws off the rest of my code.

    I have never had a config file which has been simple to do. Here is my code (mostly empty as i am re-building my entire plugin)

    REGEX PROBLEM:

    I am trying to build an IP filter into my plugin, however the regex im using does not work, i am not sure why, but i am very new to reg expression

    CODE: (Everything is nice and commented, because i naturally program like that :) So no problems to read )

    Code:
    package com.gmail.luker491;
    import java.util.List;
    import java.util.logging.Logger;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerChatEvent;
     
    public class PedantiChat extends JavaPlugin implements Listener {
        Logger log;
     
        public void onEnable(){
            //register the events
            getServer().getPluginManager().registerEvents(this, this);
            //get the logger
            Logger log = getLogger();
         
          //CONFIG SECTION
            getConfig().options().copyDefaults();
            saveConfig();
            log.info("Pedantic chat started successfully");
     
        }
     
        //What happens when the plugin is unloaded
        public void onDisable(){
            Logger log = getLogger();
            log.info("PedanticChat is disabled. Thanks for using it!");
         
        }
     
        @EventHandler
     
        public void onPlayerChat(final PlayerChatEvent event){
         
         
     
            //Start by making all the relevant player variables
         
            String inmessage = event.getMessage();
            int messagelength = inmessage.length();
            String lastletter = inmessage.substring(messagelength - 1, messagelength);
         
                         
            //Filter out any ip addresses straight away before anything else is done using a regex
            if(getConfig().getString("ipfilter").equals("true")){
                //if the message matches the regex it will simply set the persons message
                if(inmessage.contains("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.")){
                    event.setMessage(getConfig().getString("noobmessage"));
                }
                //The rest of the code to be executed if there is no IP in the message
                else{
                                 
                    //First de-caps everything if enabled
                    if(getConfig().getString("capsfilter").equals("true")){
                     
                        inmessage = inmessage.toLowerCase(); 
                    }
                 
                 
                 
                 
                    //Replace the words to imrpove the grammar, for example i with I ive with I've
                    if(getConfig().getStringList("replacewords") != null){
                     
                        //get the list from the config
                        List<String> wordlist = getConfig().getStringList("replacewords");
                        int listlength = wordlist.size();
                     
                        //this was just to confirm the config file was at fault, ignore
                        log.info(wordlist.toString());
                     
                        //convert the list to a string
                        String[] wordsin = wordlist.toArray(new String[listlength]);
                        log.info(wordsin.toString());
                        //loop over the whole array seperating the words  i , I  using split then replacing them
                        for(int i = 0 ; i<wordsin.length; i++){
                            String[] words = wordsin[i].toString().split(",");
                         
                            //This is just for debugging will be removed
                         
                            log.info(words.toString());
                         
                         
                            String before = words[0];
                            String after = words[1];
                            inmessage = inmessage.replace(before, after);
                                                 
                        }
                     
                    }
                         
                    //Adding the full stop
                    if(getConfig().getStringList("nofullstop") != null){
                     
                        List<String> charsin = getConfig().getStringList("replacewords");
                        String[] chararray = charsin.toArray(new String[0]);
                             
                        if(!in_array(chararray, lastletter)){             
                            inmessage = inmessage + ".";
                            }
                            }
                 
                                 
                 
                    //lastly add the capital letter to the text if enabled
                    if(getConfig().get("capital").equals("true")){
                                                     
                        inmessage = inmessage.substring(0, 1).toUpperCase() + inmessage.substring(1,messagelength);
                                 
                    }
                 
                }
            }
        }             
             
                                     
                 
         
             
        //All funtions for the code go down here
     
        //the compare array against string function, searching for a needle in a haystack
            public static boolean in_array(String[] haystack, String needle) {
                for(int i=0;i<haystack.length;i++) {
                    if(haystack[i].toString().equals(needle)) {
                        return true;
                    }
                }
                return true;
            }     
    }
         
    
    Thanks for any help you have to give me!

    Bump. Badly need this help! :)

    Sorry i forgot to add the config file!
    Code:
    #Pedantic Chat config file. Take care when editing
    #Letters not suitable for having a full stop. Add your own if you find any DO NOT ADD ANY BRACKETS OR . BECAUSE THIS IS AGAINST THE YAML SYNTAX
    version: 2
    nofullstop:
    - O
    - S
    - P
    - D
    - ?
    - !
    - ?
    - ;
    - -
    - $
    - £
    - ^
    - (
    - )
    - +
    - >
    - <
    - |
    - \
    #smilies which will be converted. Add your own in the same format smiliebefore,smilieafter
    smilies:
    - :d,:D
    - :p,:P
    - :s,:S
    - o_o,O_O
    - :o,:O
    - xd,XD
    #Whether making the first letter capital is allowed
    capital: true
    ##Whether to add a full stop or not
    fullstop: true
    #The minimum length of text needed for a fullstop
    minlength: 5
    #Whether to filter out all caps rage or not
    capsfilter: true
    #The words that you want to be replaced to remove bad grammar format: wordbefore,worldafter (Note, spaces count)
    replacewords:
    - i , I
    - ive , I've
    - im , I'm
    - u , you
    - uve , you've
    #words which are censored
    censors:
    - crap
    - shit
    - wank
    #Whether the ip address filter is enabled or not
    ipfilter: true
    #The message to send if someone tries to advertise
    noobmessage: I am a massive noob and just tried to advertise!
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  2. Offline

    pzxc

    Here's an IP regex for you

    "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
     
    Lukeer32 likes this.
  3. Offline

    joshua katz

    ("25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?")
     
    Lukeer32 likes this.
  4. Offline

    Lukeer32

    pzxc
    joshua katz
    Thanks for the regex i will give it a try (when i have worked out how to fix this damn config)
     
  5. Offline

    davejavu

    Lukeer32
    Your config problems;
    Firstly, vhange this:
    Code:java
    1.  
    2. getConfig().options().copyDefaults();
    3.  

    To this:
    Code:java
    1.  
    2. getConfig().options().copyDefaults(true);
    3. getConfig().options().copyHeader(true);
    4.  

    By not putting true there, all you've done is got the boolean, which is false by default.
    Also, you'll want the second line there, which will mean it will copy the HEADER - the text after the #.
    Furthermore, you don't actually save the config in onDisable(), I suggest you do this;
    Code:java
    1.  
    2. reloadConfig();
    3. saveConfig();
    4.  

    This will mean if the users have edited the config it will be reloaded, then saved.
     
    gvlfm78 and Lukeer32 like this.
  6. Offline

    Lukeer32

    davejavu Thanks so much for the help! I will give it a try now. You're awesome!
     
  7. Offline

    davejavu

    Lukeer32 No problem, if I've helped you, hit the "Like" button.
     
    Lukeer32 likes this.
  8. Offline

    Lukeer32

    davejavu Ok i the config file is copying correctly now, and i have no null point errors but it seems to not be reading from it correctly for some reason, i will look into it. and get back to you about it

    davejavu I have a problem now where the rest of my code seems not to be executing, my IP filter works and it does it job, but if the chat doesn't have an ip in it does not appear to do its job.
    CODE
    Code:
    package com.gmail.luker491;
    import java.util.List;
    import java.util.logging.Logger;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.event.player.PlayerChatEvent;
     
    public class PedantiChat extends JavaPlugin implements Listener {
        Logger log;
       
        public void onEnable(){
            //register the events
            getServer().getPluginManager().registerEvents(this, this);
            //get the logger
            Logger log = getLogger();
           
          //CONFIG SECTION
            getConfig().options().copyDefaults(true);
            getConfig().options().copyHeader(true);
            saveConfig();
            log.info("Pedantic chat started successfully");
     
        }
       
        //What happens when the plugin is unloaded
        public void onDisable(){
            reloadConfig();
            saveConfig();
            log.info("Config saved succesfully");
            Logger log = getLogger();
            log.info("PedanticChat is disabled. Thanks for using it!");
           
        }
       
        @EventHandler
       
        public void onPlayerChat(final PlayerChatEvent event){
           
           
     
            //Start by making all the relevant player variables
           
            String inmessage = event.getMessage();
            int messagelength = inmessage.length();
            String lastletter = inmessage.substring(messagelength - 1, messagelength);
           
                           
           
            //Filter out any ip addresses straight away before anything else is done using a regex
            if(getConfig().getString("ipfilter").equals("true")){
                //if the message matches the regex it will simply set the persons message
               
                Pattern pattern = Pattern.compile("(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])");
                Matcher matcher = pattern.matcher(inmessage);
                if(matcher.find()){
                    event.setMessage(getConfig().getString("noobmessage"));
                }
               
    }
           
            //First de-caps everything if enabled
            if(getConfig().getString("capsfilter").equals("true")){
               
                inmessage = inmessage.toLowerCase();   
            }
           
           
           
           
            //Replace the words to imrpove the grammar, for example i with I ive with I've
            if(getConfig().getStringList("replacewords") != null){
     
           
                //convert the list to a string
                String[] wordsin = getConfig().getStringList("replacewords").toArray(new String[0]);
               
               
                //loop over the whole array seperating the words  i , I  using split then replacing them
                for(int i = 0 ; i<wordsin.length; i++){
                   
                   
                    String[] words = wordsin[i].toString().split(",");
                 
                    String before = words[0];
                    String after = words[1];
                    inmessage = inmessage.replace(before, after);
                                           
                }
               
            }
           
            //Replace the smilies just like the words
            if(getConfig().getStringList("smilies") != null){
               
           
                //get the list then convert the list to a string
                String[] simliesin = getConfig().getStringList("smilies").toArray(new String[0]);
               
               
                //loop over the whole array seperating the words  i , I  using split then replacing them
                for(int i = 0 ; i<simliesin.length; i++){
                   
                   
                    String[] words = simliesin[i].toString().split(",");
                 
                    String before = words[0];
                    String after = words[1];
                    inmessage = inmessage.replace(before, after);
                                           
                }
               
            }
           
            //Censor out any words that are in the list
            //if(getConfig().getString("censors") != null){
                //List<String> rudewords = getConfig().getStringList("censors");
                //to be continued
               
            //}
           
           
           
                   
            //Adding the full stop if it is enabled
            if(getConfig().getString("fullstop").equals("true")){
                //check the message is longer than the minimum length
                if(messagelength > getConfig().getInt("minlength")){
                    //check that the nonfull stop list has items in it
                    if(getConfig().getStringList("nofullstop") != null){
                       
                        List<String> charsin = getConfig().getStringList("replacewords");
                        String[] chararray = charsin.toArray(new String[0]);
                                //check that the last letter is not something not suitable for a full stop
                        if(!in_array(chararray, lastletter)){               
                            inmessage = inmessage + ".";
                            }
                        else{
                            inmessage = inmessage + ".";
                           
                        }
                            }
                }
     
            }
                           
           
            //lastly add the capital letter to the text if enabled
            if(getConfig().get("capital").equals("true")){
                                               
                inmessage = inmessage.substring(0, 1).toUpperCase() + inmessage.substring(1,messagelength);
                           
            }
           
           
                }
               
           
     
               
           
           
           
           
                   
        //All funtions for the code go down here
       
        //the compare array against string function, searching for a needle in a haystack
            public static boolean in_array(String[] haystack, String needle) {
                for(int i=0;i<haystack.length;i++) {
                    if(haystack[i].toString().equals(needle)) {
                        return true;
                    }
                }
                return true;
            }
     
    }
           
    
    It would help if i remembered to put event.setmessage -_- Thanks for the help guys!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  9. Offline

    davejavu

    @Lukerr32 Please put [SOLVED] in the thread name. thanks.
     
Thread Status:
Not open for further replies.

Share This Page