Config help (a few questions)

Discussion in 'Plugin Development' started by Scullyking, Jul 29, 2012.

  1. Offline

    Scullyking

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I have a few questions about generating config files.

    • Is it possible to create more than one yml file?
    • If so, how can I create a yml file which has its name defined in a String var?
    Thanks, also, I am quite a noob with this, only started a week ago but I'm learning fast :)
  2. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    File configFile = new File(getDataFolder(), "fileName.yml");
    FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);

    This post has been edited 1 time. It was last edited by Firefly Jul 29, 2012.
    Scullyking likes this.
  3. Offline

    Scullyking

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Thank-you! What about creating a file with a name stored in a var?

    EDIT: Didn't seem to work for me. I put that exact code into a command and ran it. No file was made. I must be missing something obvious...

    This post has been edited 2 times. It was last edited by Scullyking Jul 29, 2012.
  4. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    String fileName = "filename.yml";
    File configFile = new File(getDataFolder(), fileName);
    Scullyking likes this.
  5. Offline

    sd5

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Put that in your main class:

    Code:
    private static final String CONFIG_NAME = "myconfig.yml";
     
    private FileConfiguration config;
    private File configFile;
     
    public void reloadConfig() {
     
        if(configFile == null) {
            configFile = new File(getDataFolder(), CONFIG_NAME);
        }
     
        config = YamlConfiguration.loadConfiguration(configFile);
     
    }
     
    public FileConfiguration getConfig() {
     
        if(config == null) {
            this.reloadConfig();
        }
     
        return config;
     
    }
     
    public void saveConfig() {
     
        if(config == null || configFile == null) {
            return;
        }
     
        try {
            getConfig().save(configFile);
        } catch(IOException e) {
            getLogger().log(Level.SEVERE, "Could not save config to " + configFile, e);
        }
     
    }
    Now you can use the custom config as the normal config with getConfig(), reloadConfig() and saveConfig()

    This post has been edited 3 times. It was last edited by sd5 Jul 29, 2012.
  6. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    you gotta save it ;) config.save(configFile);
    Scullyking likes this.
  7. Offline

    Scullyking

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    YES! Thank-you so much! Also, how can I setup the default stuff. The tutorials all say to do this:
    Code:java
    1. if(!.exists())
    2. {
    3. cfg.addDefault("config.baserep", 1);
    4. cfg.options().copyDefaults(true);
    5. saveConfig();
    6. }

    But your method is a bit different?

    This post has been edited 1 time. It was last edited by Scullyking Jul 29, 2012.
  8. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Nope. Same thing, just saveConfig is replaced with config.save(configFile)
  9. Offline

    Scullyking

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Ok, I did this and also changed the cfg to config. But the only error left is on .exists - its asking me if I want to make a method named exists()

    But if I removed the whole if line its fine...

    This post has been edited 2 times. It was last edited by Scullyking Jul 29, 2012.
  10. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    if(file.exists()) {

    You weren't checking if anything existed.
  11. Offline

    Scullyking

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Brilliant. Any idea how to check if a file with that players name already exists and cancel the file generation?

    Code:java
    1.  
    2. if file exists {
    3. cancel creation
    4. tellplayer(You are already activated)
    5. }
    6.  

    This post has been edited 5 times. It was last edited by Scullyking Jul 29, 2012.
  12. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Assuming the file name is the name of the player....

    Player p = // Your player;

    for (File f : getDataFolder().listFiles()) {
    if (f.getName().equalsIgnoreCase(p.getName()) {
    p.sendMessage("You are activated!");
    }
    }

    This post has been edited 2 times. It was last edited by Firefly Jul 29, 2012.
  13. Offline

    Scullyking

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hmm, fine in terms of java... internal error in bukkit. I'm sure I must have implemented it wrong.

    Code:java
    1. String name = sender.getName();
    2.  
    3. sender.sendMessage(ChatColor.RED + "You are set-up for reputation!");
    4.  
    5. String fileName = name + ".yml";
    6.  
    7. Player p = null;
    8.  
    9. for (File f : getDataFolder().listFiles()) {
    10. if (f.getName().equalsIgnoreCase(p.getName())) {
    11. p.sendMessage("You are already activated with reputation!");
    12. }
    13. }
    14.  
    15. File configFile = new File(getDataFolder(), fileName);
    16. FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
    17. try {
    18. if(configFile.exists())
    19. {
    20. config.addDefault("config.rep", 0);
    21. config.options().copyDefaults(true);
    22. config.save(configFile); }
    23. config.save(configFile);
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27.  

    This post has been edited 1 time. It was last edited by Scullyking Jul 29, 2012.
  14. Offline

    Firefly

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Because p is null and you are trying to execute a method on a null object, thus you got an NPE. Assign Player p to sender, after running an instanceof Player and casting it properly.
  15. Offline

    Scullyking

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Ok. More errors.


    First Run of command: Fine, config file made but nothing in it! Get message: You are set-up for reputation! As expected.
    Second run of command: The contents is added to the config file. Get message: You are set-up for reputation!
    Third run of command: Nothing changes. Get message: You are set-up for reputation!
  16. Offline

    Sagacious_Zed Bukkit Docs

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Here is a few points that need to be understood.
    1. The object in memory is a copy of the file on disk when the object was created.
    2. Changes to the object in memory are not reflected in the file unless saved.
    3. Saves are manual.

    This post has been edited 2 times. It was last edited by Sagacious_Zed Jul 29, 2012.

Share This Page