[SOLVED]read from config

Discussion in 'Plugin Development' started by XxFuNxX, Aug 27, 2011.

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

    XxFuNxX

    Hello! i need some help, i need to be able to read from an config.

    now my plugin has
    Code:
    Integer amount = Integer.parseInt(args[1]);
                    if(amount >= 2) {
    
    }
    wich means that if it is two or higher, something will happen
    but how do i get if its 2 or higher from a config property? like
    Code:
    maxamount: 2
    please help!
     
  2. Let's say that line is in the String foo:
    Code:
    String[] split = foo.split(": ");
    if(split[0].equalsIgnoreCase("maxamount"))
    {
      Integer amount = Integer.parseInt(split[1]);
      if(amount >= 2) {}
    }
    I'll bet you could also use YML but I never did that so I can't help here.

    //EDIT: A side note: My code example would crash if there's no "<something>: <something>" in foo. But future checks are up to you... ;)
     
  3. Offline

    XxFuNxX

    Hmm, i try that when i come home :) thanks

    cant get it to work :( any other ideas?

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

    Arkel

  5. Offline

    XxFuNxX

    mm.... :(
     
  6. Offline

    RROD

    Try this:

    Code:
    import org.bukkit.util.config.Configuration;
    
    public class random extends JavaPlugin {
        // Proper way to do config (should be). ^^
        // Creates
        public Integer configInt;
        public Configuration config;
    
        @Override
        public void onEnable() {
            //Initialises and will write config if it doesn't exist.
            configInt = config.getInt("maxamount", 2);
            config.save(); //Save the config.
    
            //doOnEnable Code...
        }
    
        //To grab this data from the config (located at plugins/pluginName/config.yml)
        //Do this code:
        public void doConfig() {
            int maxamount = config.getInt("maxamount", 2);
            if(maxamount <= 2) {}
        }
    
        @Override
        public void onDisable() {
            // TODO Auto-generated method stub
    
        }
    }
    And yes, it's YAML.
     
  7. Offline

    XxFuNxX

    :( , the
    Code:
    if(amount >= 2)
    is gonna be editable in an YML file, @RROD that dosent help :/ srry but thanks anyway.
    the >= 2 is gonna be editable, like
    Code:
    if(amount >= (YML FILE CONFIG)
    hope you understand me right!
     
  8. Well, what do you think the line
    is for? You just have to change one line to do what you want:
    Code:
    if(amount <= maxamount) {}
     
  9. Offline

    XxFuNxX

    no, the 2 in maxamount is still strict, wich means that the player cant edit it.
     
  10. Offline

    XxFuNxX

    Okay srry :)

    I cant get it to work :(
    i got this:
    in the command sender class
    Code:
    int maxamount = plugin.kitConf.getInt("maxamount", 2);
                    Integer amount = Integer.parseInt(args[1]);
                    if(amount >= maxamount) {
                        sender.sendMessage("That amount is to much!");
                    }
    else {
    //////some othe code
    }
    in the main class:
    Code:
    public Integer test;
    
    public void onEnable() {
     kitConf = new Configuration(file);
    test = kitConf.getInt("maxamount", 2);
                kitConf.save();
    }
    am i doing something wrong?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  11. remoe the test = .. line and replace the kitConf.save() with kitConf.load()

    //EDIT: This is just an idea, as I sayed I didn't work with YAML before and I don't want to read the docs for you..
     
  12. Offline

    XxFuNxX

    thanks, i try that

    Well, THANK YOU! it worked :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
Thread Status:
Not open for further replies.

Share This Page