Solved Plugin Config loading help?

Discussion in 'Plugin Development' started by eNnillaMS, Oct 13, 2013.

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

    eNnillaMS

    Okay, so the plugin I've been working on is loading the default config file instead of the file that gets made when the plugin starts. No idea why. It causes a bunch of errors whenever someone runs a command too until I physically delete the default config.yml from the plugin jar file.
    Here's my onEnable function, (yes it has @Override above it). Was there something I've missed for actually loading the config file?

    Code:java
    1. public void onEnable(){
    2. saveDefaultConfig();
    3. getConfig();
    4. getServer().getPluginManager().registerEvents(this, this);
    5. if (!setupEconomy() ) {
    6. log.severe(String.format("[%s] - Disabled due to dependancy (Vault) not being found!", getDescription().getName()));
    7. getServer().getPluginManager().disablePlugin(this);
    8. return;
    9. }
    10. setupPermissions();
    11. setupChat();
    12. }
     
  2. Offline

    MiniDigger

    eNnillaMS what is the different between the 'default config' and 'the file that gets made when the plugin starts'?
    And i think getConfig(); is useless if you dont do anything with the object you get.
     
  3. Offline

    eNnillaMS


    The 'default config' was the config.yml that I package with the plugin, 'the file that gets made when the plugin starts' being the version that staff members edit and gets changed around. When the plugin loads, or when someone does anything the plugin handles, the plugin gets values from the packaged config.yml instead of the one that the server staff change around.
    I do plenty with it in different EventHandlers, but it's still just things like
    Code:java
    1. if (!getConfig().getStringList("players").contains("eNnillaMS")){
    2. //Do Stuff
    3. }


    EDIT: As an example, removing saveDefaultConfig(); and deleting the modifiable config.yml results in a command saying that I have a job, but there aren't any jobs in existence. I'm listed in the default config as having a job, and there are jobs there; but this:
    Code:java
    1. } else if (args[0].equalsIgnoreCase("listJobs")){
    2. if (sender.hasPermission("MineJobs.viewJobs")){
    3. String plyrJobs = " ";
    4. int i = getConfig().getStringList("players." + sender.getName()).size();
    5. for (String job:getConfig().getStringList("players." + sender.getName()).toArray(new String)) {
    6. plyrJobs += job + " ";
    7. }
    8. String Jobs = " ";
    9. int j = getConfig().getConfigurationSection("jobs").getKeys(false).size();
    10. for (String job:getConfig().getConfigurationSection("jobs").getKeys(false).toArray(new String[j])) {
    11. Jobs += job + " ";
    12. }
    13. sender.sendMessage(".oOo___________________MineJobs___________________oOo.");
    14. sender.sendMessage(" Your jobs:");
    15. sender.sendMessage(plyrJobs);
    16. sender.sendMessage(" To quit a job, use '/mj quitJob <jobName>'");
    17. sender.sendMessage(" Availible Jobs:");
    18. sender.sendMessage(Jobs);
    19. sender.sendMessage(" To get a job, use '/mj getJob <jobName>'");
    20. sender.sendMessage(".oOo______________________________________________oOo.");
    21. }

    results in plyrJobs containing "Miner", but Jobs is empty.
     
  4. Offline

    MrSparkzz

    eNnillaMS
    If you don't want it to save the "default config," why are you telling it to save the default config?
    Code:java
    1.  
    2. public void onEnable() {
    3. saveDefaultConfig();
    4.  
    5. // code
    6. }
    7.  
     
    Ultimate_n00b likes this.
  5. Offline

    eNnillaMS

    I want it to save the default config to disk if the config.yml isn't there already. I read on the Plugin building tutorial that if the file already exists then the method would just fail and nothing would happen.
    My problem is less that than the fact that I don't know how to read values specifically from the config.yml on the disk instead of letting the plugin decide to read default values out of itself first.
     
  6. Offline

    MiniDigger

    eNnillaMS it should always get the values from the config, loaded into the memory. And that should be the config on the disk. But you can try to load that one with reloadConfig()
     
  7. Offline

    eNnillaMS

    MiniDigger That's whats strange though!
    I tried that and it didn't so anything!
     
  8. Offline

    eNnillaMS

    I didn't find an answer on how to better use getConfig(); but I found an alternate method of config loading; normally used for secondary files that fixes my problem.

    For loading: (Instead of getConfig(); )
    Code:java
    1. import java.io.File;
    2. import org.bukkit.configuration.file.YamlConfiguration;
    3. //initialize class//
    4. public File configfile;
    5. public YamlConfiguration conFig;
    6. @Override public void onEnable(){
    7. if (!(new File(getDataFolder() + "config.yml").exists())) {
    8. saveDefaultConfig();
    9. }
    10. getServer().getPluginManager().registerEvents(this, this);
    11. configfile = new File(new File(getDataFolder().getParentFile(), "MineJobs"), "config.yml");
    12. conFig = YamlConfiguration.loadConfiguration(configfile);
    13. }
    14. }

    That lets you use conFig instead of getConfig().
    Technically it's exactly the same thing, only I'm forcing the code to select the file from the disk.

    Saving: (Replace YOURPLUGINNAME with your plugin name..) ( Instead of saveConfig(); )
    Code:java
    1. try {
    2. conFig.save(new File(new File(getDataFolder().getParentFile(), "YOURPLUGINNAME"), "config.yml"));
    3. } catch(IOException e) {
    4. log.severe("MineJobs: Could not save config to YOURPLUGINNAME/config.yml.");
    5. }


    And Reloading: ( Instead of reloadConfig(); )
    Code:java
    1. configfile = new File(new File(getDataFolder().getParentFile(), "MineJobs"), "config.yml");
    2. conFig = YamlConfiguration.loadConfiguration(configfile);
     
Thread Status:
Not open for further replies.

Share This Page