Configuration - Doing more work than I have to - Not generating booleans in .yml

Discussion in 'Plugin Development' started by Butkicker12, Oct 17, 2011.

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

    Butkicker12

    @feildmaster Said I was doing more work than I have to

    It creates the file but throws a IE exception when saving. The file is blank too. What am I doing wrong.

    http://pastie.org/2714742
    http://pastie.org/2714748



    Code:
     @Override
      public void onEnable() {
        // ----Getting the plugin manager----
        PluginManager pm = this.getServer().getPluginManager();
        // ----Register Event----
        pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener,
            Priority.Normal, this);
        pm.registerEvent(Event.Type.PROJECTILE_HIT, entityListener,
            Priority.Normal, this);
        log.info("[Shotgun] Enabled version " + getDescription().getVersion());
        log.info("[Shotgun] Made by butkicker12!");
    
        // Configuration
        File configFile = new File("plugins/Shotgun/config.yml");
        if(!configFile.exists()){
          log.info("[Shotgun] Setting up config!");
          new File("plugins/Shotgun").mkdir();
          if(!configFile.exists()){
              try { configFile.createNewFile(); }
              catch(Exception e){
                log.info("[Shotgun] Error when creating config file.");
                }
          }
          setupConfig();
        }
        try {
          config.save(configFile);
        } catch (IOException e) {
          log.severe("[Shotgun] IOException: " + e.getMessage());
          e.printStackTrace();
        }
        loadConfig();
        }
    Code:
    public void setupConfig(){
        File file = new File("plugins/Shotgun/config.yml");
        getConfig().set("weapons.shotgun", true);
          getConfig().set("weapons.smokebomb", false);
          getConfig().set("weapons.rocketlauncher", false);
          getConfig().set("weapons.grenade", false);
          getConfig().set("weapons.rocketlauncher.explosion", false);
          getConfig().set("weapons.nuke", false);
          getConfig().set("weapons.airstrike", true);
    
          try{
            config.save(file);
          }catch(Exception e){
              log.info("[Shotgun] Error when saving plugin/Shotgun/config.yml.");
              }
      }
      
    public void loadConfig(){
        boolean Shotgun = getConfig().getBoolean("weapons.shotgun", true);
        boolean SmokeBomb = getConfig().getBoolean("weapons.smokebomb", false);
        boolean RocketLauncher = getConfig().getBoolean("weapons.rocketlauncher", false);
        boolean RocketLauncherExplosion = getConfig().getBoolean("weapons.rocketlauncher.explosion", false);
        boolean Grenade = getConfig().getBoolean("weapons.grenade", false);
        boolean Nuke = getConfig().getBoolean("weapons.nuke", false);
        boolean Airstrike = getConfig().getBoolean("weapons.airstrike", true);
        //boolean DebugSettingEnabled = getConfig().getBoolean("debug", true);
    
        // Send configuration settings to variables
        this.Shotgun = Shotgun;
        this.SmokeBomb = SmokeBomb;
        this.RocketLauncher = RocketLauncher;
        this.Grenade = Grenade;
        this.RocketLauncherExplosion = RocketLauncherExplosion;
        this.Nuke = Nuke;
        this.Airstrike = Airstrike;
      }
     
      private YamlConfiguration config;
      boolean Shotgun;
      boolean SmokeBomb;
      boolean RocketLauncher;
      boolean RocketLauncherExplosion;
      boolean Grenade;
      boolean Nuke;
      boolean Airstrike;
      boolean Debug;
     
  2. Offline

    feildmaster

    Code:
    File configFile = new File("plugins/Shotgun/config.yml");
    if(!configFile.exists()){
            getConfig().save();
    }
    to create the file.

    Also, you shouldn't use getConfig that often. Every time you do it reloads the config. (waste of time)

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

    Sagacious_Zed Bukkit Docs

    Code:
    public FileConfiguration getConfig() {
        if (newConfig == null) {
            reloadConfig();
        }
        return newConfig;
    }
    
    public void reloadConfig() {
        newConfig = YamlConfiguration.loadConfiguration(configFile);
    
        InputStream defConfigStream = getResource("config.yml");
        if (defConfigStream != null) {
            YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
    
            newConfig.setDefaults(defConfig);
        }
    It should not be the case, if I am reading this correctly.
     
  4. Offline

    Butkicker12


    What I was using this before the RB update

    Code:
            // Configuration
            getConfig().options().copyDefaults(true);
            saveConfig();
    Is that the same thing. I just put the config.yml in the same place as the plugin.yml right.
     
  5. Offline

    feildmaster

    I forgot he optimized the new config.

    either way, you're not saving correctly.

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

    Butkicker12

    Ok so how should I save. If it help it throws my exveption when it tries to save
     
  7. Offline

    Technius

    Use this:
    Don't copy/paste, spacing is a bit off.
    Code:Java
    1.  
    2. File config = new File (this.getDataFolder().getPath() + File.separator + "config.yml");
    3. if (!config.exists())
    4. {
    5. try
    6. {
    7. fos.flush();
    8. fos.close();
    9. }
    10. catch (IOException ioe)
    11. {
    12. log.info("Error");
    13. }
    14. }
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    My vote is to do this.
    Code:
    @Override
    public void onEnable() {
      // ----Getting the plugin manager----
      PluginManager pm = this.getServer().getPluginManager();
      // ----Register Event----
      pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener,
          Priority.Normal, this);
      pm.registerEvent(Event.Type.PROJECTILE_HIT, entityListener,
          Priority.Normal, this);
      log.info("[Shotgun] Enabled version " + getDescription().getVersion());
      log.info("[Shotgun] Made by butkicker12!");
    
      // Configuration
      FileConfiguration config = this.getConfig();
      
      config.options().copyDefaults(true);
      saveConfig();
    }
    then put the following config.yml at the root of your plugin, i.e. where plugin.yml resides
    Code:
    weapons:
      shotgun: true
      smokebomb: false
      rocketlauncher: false
      rocketlauncherexplosion: false
      grenade: false
      nuke: false
      airstrike: true
    Actually, while handcrafting your yaml, i noticed you were actually constructing an invalid yaml file.
    you were trying to do this
    Code:
    weapons:
      rocketlaucher: false
        explosion: true
    which is not valid yaml. Which probably highlights the need for better error reporting on a non valid map.
     
  9. Offline

    feildmaster

    WHY!

    plugin.saveConfig();
    .saveConfig()! DO IT!

    EDIT: (Didn't read Zed's, That's how you should do it)

    EDIT2: By the way, you should do "Enabled" at the end... Silly. :p
     
  10. Offline

    Butkicker12

    I have it this way now. Not liking it though.
     
  11. Offline

    Sagacious_Zed Bukkit Docs

    The whole point is to separate code from the values. If you really wanted too, you can supply defaults in the code, but it is definitely not the preferred way of doing, judging from how to API is designed.
     
  12. Offline

    Butkicker12

    Wait how would I do with the new RB?
    setBoolean(weapons.shotgun.use)?

    I have it loading with the

    boolean SmokeBomb = getConfig().getBoolean("weapons.smokebomb", false);
     
  13. Offline

    Sagacious_Zed Bukkit Docs

  14. Offline

    Butkicker12

    Last edited by a moderator: May 20, 2016
  15. Offline

    Sagacious_Zed Bukkit Docs

    Why do you still have a setup config?
    Lines 70 - 78 can be modified to make line 79 - 86 and makes it easier to understand, instead of going through this roundabout way of setting fields.
    SetupConfig does not do what you think it does. Anytime it is called, it will override whatever values were in the config with the ones that are in the set methods. Then it will proceed to encounter an NPE as config has not been defined.
     
  16. Offline

    Butkicker12

    if(!config.Exists(){ ? Around this part?

    Code:
        public void setupConfig(){
            File file = new File("plugins/Shotgun/config.yml");
            getConfig().set("weapons.shotgun", true);
                getConfig().set("weapons.smokebomb", false);
                getConfig().set("weapons.rocketlauncher", false);
                getConfig().set("weapons.grenade", false);
                getConfig().set("weapons.rocketlauncher.explosion", false);
                getConfig().set("weapons.nuke", false);
                getConfig().set("weapons.airstrike", true);
            try{
                config.save(file);
            }catch(Exception e){
                    log.severe("[Shotgun] Error when saving plugin/Shotgun/config.yml.");
                    e.printStackTrace();
                    }
        }
     
  17. Offline

    Sagacious_Zed Bukkit Docs

    However you plan to use setup config, is how you should fix it. If you are already providing a default config.yml then you do not have to do any setup.
     
  18. Offline

    Butkicker12

    I'm pretty sure im going to do it this way. http://pastie.org/2721182
    It is ok to put a try/catch around
    getConfig().options().copyDefaults(true);
    saveConfig();
    Right?
     
Thread Status:
Not open for further replies.

Share This Page