Not reading property correctly ..?

Discussion in 'Plugin Development' started by Infernus, Mar 12, 2011.

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

    Infernus

    Heya! I'm back :p

    PHP:
    public void setup() {
            (new 
    File(directory)).mkdir();
            
    Settings = new AChatSettings(directory "config.yml");
            
    Color Settings.getString("color");
            if(
    Color.toLowerCase() == "red") {
                
    MyColor ChatColor.RED;
            } else if(
    Color.toLowerCase() == "yellow") {
                
    MyColor ChatColor.YELLOW;
            } else if(
    Color.toLowerCase() == "green") {
                
    MyColor ChatColor.GREEN;
            } else if(
    Color.toLowerCase() == "aqua") {
                
    MyColor ChatColor.AQUA;
            } else if(
    Color.toLowerCase() == "blue") {
                
    MyColor ChatColor.BLUE;
            } else if(
    Color.toLowerCase() == "darkblue") {
                
    MyColor ChatColor.DARK_BLUE;
            } else if(
    Color.toLowerCase() == "gray") {
                
    MyColor ChatColor.GRAY;
            } else if(
    Color.toLowerCase() == "white") {
                
    MyColor ChatColor.WHITE;
            } else if(
    Color.toLowerCase() == "black") {
                
    MyColor ChatColor.BLACK;
            } else {
                
    MyColor ChatColor.YELLOW;
            }
        }
    Here you can see I am loading settings from my file, click here to view the property loader.

    Configuration file:
    Code:
    #Minecraft Properties File
    #Sat Mar 12 20:34:18 CET 2011
    color=blue
    The problem is it does not load anything the string, at least I think it doesn't because it just returns to it's default color, yellow. I have also tried putting " before and after the 'blue'.

    Help would be appreciated! :)
     
  2. Offline

    MadMonkeyCo

    Your using YAML which uses semicolons like so:
    Code:
    #Minecraft Properties File
    #<Date> <Time>
    color: "blue"
    Yes me again as well...
     
  3. Offline

    Infernus

    That at least worked for a little bit, now behold this unbelievable shit:

    log (open)
    2011-03-13 11:55:18 [INFO] [AdminChat] Enabled
    2011-03-13 11:55:18 [INFO] [AdminChat] Permissions not running, all OPs will be able to send and receive Adminchat messages.
    2011-03-13 11:55:18 [INFO] [AdminChat] Color value is: blue
    2011-03-13 11:55:18 [INFO] [AdminChat] I am a fucker.

    config file (open)
    #Minecraft Properties File
    #Sat Mar 12 20:34:18 CET 2011
    color: blue


    new code:

    PHP:
        public void setup() {
            (new 
    File(directory)).mkdir();
            
    Settings = new AChatSettings(directory "config.yml");
            
    Color Settings.getString("color");
            
    log.info("[AdminChat] Color value is: " Color);
            if(
    Color == "blue") {
                
    log.info("[AdminChat] Color is indeed blue and I am sure about it!");
            } else {
                
    log.info("[AdminChat] I am a fucker.");
            }
            
    /*if(Color.toLowerCase() == "red") {
                MyColor = ChatColor.RED;
            } else if(Color.toLowerCase() == "yellow") {
                MyColor = ChatColor.YELLOW;
            } else if(Color.toLowerCase() == "green") {
                MyColor = ChatColor.GREEN;
            } else if(Color.toLowerCase() == "aqua") {
                MyColor = ChatColor.AQUA;
            } else if(Color == "blue") {
                MyColor = ChatColor.BLUE;
            } else if(Color.toLowerCase() == "darkblue") {
                MyColor = ChatColor.DARK_BLUE;
            } else if(Color.toLowerCase() == "gray") {
                MyColor = ChatColor.GRAY;
            } else if(Color.toLowerCase() == "white") {
                MyColor = ChatColor.WHITE;
            } else if(Color.toLowerCase() == "black") {
                MyColor = ChatColor.BLACK;
            } else {
                log.info("[AdminChat] Unsupported color in settings file, resetting color to default. (" + Color + ")");
                MyColor = ChatColor.YELLOW;
            }*/
        
    }
     
  4. Offline

    MadMonkeyCo

    Try using:
    Color.equalsIgnoreCase("blue");
    I can't think of anything else at the moment.
     
  5. Offline

    Sammy

    Well you are using a "pseudo-yml" file.
    If you want to use a real yml file you need to handle it like so:
    Code:
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.util.Map;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import org.yaml.snakeyaml.Yaml;
    
    /**
     *
     * @author Sammy
     */
    public class yamlHandler {
    
        private String cmd;
        private Object objd;
        private Object obju;
        BufferedReader input;
    
        public yamlHandler(String command) {
            this.cmd = command;
            try {
    
                input = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream("commands.yml")));
    
            } catch (Exception ex) {
                Logger.getLogger(yamlHandler.class.getName()).log(Level.SEVERE, null, ex);
            }
            Yaml yaml = new Yaml();
            Map<String, Map<String, Map<String, Object>>> data = (Map<String, Map<String, Map<String, Object>>>) yaml.load(input);
    
            if (command.equals(command)) {
                objd = data.get("commands").get(cmd).get("description");
                obju = data.get("commands").get(cmd).get("usage");
            }
        }
    
        public String getdescription() {
            return objd.toString();
        }
    
        public String getusage() {
            return obju.toString().replace("<command>", cmd);
        }
    }
    
    Try using MadMonkeyCo idea, the way you do it isn't wrong per-say but not the best way to do it.
     
  6. Offline

    Infernus

    Yeah, thanks for your support, but I forgot to mention that MadMonkeyCo's idea already worked. :)
     
Thread Status:
Not open for further replies.

Share This Page