How to not make new config when there is already one

Discussion in 'Plugin Development' started by william9518, Jan 5, 2013.

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

    william9518

    Now this worked before. My thing did not generate a new config or anything but it does now and it just keeps replacing the old one. Help, how can I make it so that if there is a config it wont make a new one?!
    my loadconfig method(THIS WORKED BEFORE):
    Code:
     public void loadConfig(){
          getConfig().options().copyDefaults(true);
          saveConfig();
    List<String> players = new ArrayList<String>();
        for (Player player : Bukkit.getOnlinePlayers()) {
          players.add(player.getName());
          String name = player.getName();
          if (!getConfig().contains(name)) {
            getConfig().set(name, Integer.valueOf(0));
            credits.put(name, Integer.valueOf(getConfig().getInt(name)));
            saveConfig();
          } else {
            credits.put(name, Integer.valueOf(getConfig().getInt(name)));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtsoldier")) {
            getConfig().set(name + "boughtsoldier", Boolean.valueOf(true));
            boughtsoldier.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtsoldier")));
            saveConfig();
          } else {
            boughtsoldier.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtsoldier")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtstandard")) {
            getConfig().set(name + "boughtstandard", Boolean.valueOf(true));
            boughtstandard.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtstandard")));
            saveConfig();
          } else {
            boughtstandard.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtstandard")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtpumpkinhead")) {
            getConfig().set(name + "boughtpumpkinhead", Boolean.valueOf(false));
            boughtpumpkinhead.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtpumpkinhead")));
            saveConfig();
          } else {
            boughtpumpkinhead.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtpumpkinhead")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtarcher")) {
            getConfig().set(name + "boughtarcher", Boolean.valueOf(false));
            boughtarcher.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtarcher")));
            saveConfig();
          } else {
            boughtarcher.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtarcher")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtwizard")) {
            getConfig().set(name + "boughtwizard", Boolean.valueOf(false));
            boughtwizard.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtwizard")));
            saveConfig();
          } else {
            boughtwizard.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtwizard")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtscout")) {
            getConfig().set(name + "boughtscout", Boolean.valueOf(false));
            boughtscout.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtscout")));
            saveConfig();
          } else {
            boughtscout.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtscout")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtblacksmith")) {
            getConfig().set(name + "boughtblacksmith", Boolean.valueOf(false));
            boughtblacksmith.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtblacksmith")));
            saveConfig();
          } else {
            boughtblacksmith.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtblacksmith")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtjuggernaut")) {
            getConfig().set(name + "boughtjuggernaut", Boolean.valueOf(false));
            boughtjuggernaut.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtjuggernaut")));
            saveConfig();
          } else {
            boughtjuggernaut.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtjuggernaut")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtjumper")) {
            getConfig().set(name + "boughtjumper", Boolean.valueOf(false));
            boughtjumper.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtjumper")));
            saveConfig();
          } else {
            boughtjumper.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtjumper")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtsnowman")) {
            getConfig().set(name + "boughtsnowman", Boolean.valueOf(false));
            boughtsnowman.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtsnowman")));
            saveConfig();
          } else {
            boughtsnowman.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtsnowman")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtsniper")) {
            getConfig().set(name + "boughtsniper", Boolean.valueOf(false));
            boughtsniper.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtsniper")));
            saveConfig();
          } else {
            boughtsniper.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtsniper")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtdemoman")) {
            getConfig().set(name + "boughtdemoman", Boolean.valueOf(false));
            boughtdemoman.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtdemoman")));
            saveConfig();
          } else {
            boughtdemoman.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtdemoman")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtviper")) {
            getConfig().set(name + "boughtviper", Boolean.valueOf(false));
            boughtviper.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtviper")));
            saveConfig();
          } else {
            boughtviper.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtviper")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtmedic")) {
            getConfig().set(name + "boughtmedic", Boolean.valueOf(false));
            boughtmedic.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtmedic")));
            saveConfig();
          } else {
            boughtmedic.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtvip")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtvip")) {
            getConfig().set(name + "boughtvip", Boolean.valueOf(false));
            boughtvip.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtvip")));
            saveConfig();
          } else {
            boughtvip.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtvip")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtmvp")) {
            getConfig().set(name + "boughtmvp", Boolean.valueOf(false));
            boughtmvp.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtmvp")));
            saveConfig();
          } else {
            boughtmvp.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtmvp")));
            saveConfig();
          }
          if (!getConfig().contains(name + "boughtomg")) {
            getConfig().set(name + "boughtomg", Boolean.valueOf(false));
            boughtomg.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtomg")));
            saveConfig();
          } else {
            boughtomg.put(name, Boolean.valueOf(getConfig().getBoolean(name + "boughtomg")));
            saveConfig();
          }
        }
        saveConfig();
      }
    and in the onEnable it says loadconfig(); then saveconfig();.
     
  2. Offline

    ZeusAllMighty11 Retired Staff

    Java files have a built in method to see if they exist


    if(someFile.exists()){


    } else {
    // load file here, create defaults, etc
    }
     
  3. Offline

    william9518

    I can't do that with getConfig()
     
  4. Offline

    hockeygoalie5

  5. Offline

    william9518

    I dont know which part is wrong that deletes the config but it is in onEnable because when i open the server the plugin just deletes the config and makes a new one. here's my onEnable:
    Code:
     public void onEnable() {
        getLogger().info("OMGPvP by william9518 is now enabled");
        PluginManager manager = getServer().getPluginManager();
        manager.registerEvents(new OMGPvPEventListener(this), this);
        getCommand("shop").setExecutor(new OMGPvPCommandExecutor(this));
        getCommand("credit").setExecutor(new OMGPvPCommandExecutor(this));
        getCommand("buy").setExecutor(new OMGPvPCommandExecutor(this));
        getCommand("faq").setExecutor(new OMGPvPCommandExecutor(this));
        getCommand("hg").setExecutor(new OMGPvPCommandExecutor(this));
        getCommand("givekit").setExecutor(new OMGPvPKitCommandExecutor(this));
        getCommand("givehead").setExecutor(new OMGPvPCommandExecutor(this));
        mainDirectory = getDataFolder().getPath();
        config = new File(mainDirectory + "/config.yml");
        if(!config.exists()){
            loadConfig();
            saveConfig();
        } else {
            saveConfig();
        }
      }
     
  6. Offline

    CorrieKay

    Use defaults instead of checking if they exist. It will do it for you.

    You can either set the defaults in your onEnable method, or you can load a file as a default (which is much easier to do, ill show you how to do that)

    Make a file next to your plugin.yml called "defaultConfig.yml" (or whatever you wish to name it, the name doesnt matter)

    Write it like a standard YML file, with all of the default configuration items inside of it.

    Then do this, on enable (before loading your preferences)

    Code:
    Configuration defaultConfig = YamlConfiguration.loadConfiguration(getResource("defaultConfig.yml"));
    getConfig.setDefaults(defaultConfig);
    getConfig().options().copyDefaults(true);
    saveConfig();
    
     
  7. Offline

    william9518

    Didnt work!
     
  8. Offline

    Sagacious_Zed Bukkit Docs

    Why are you going in such a round about way to set defaults.
     
  9. Offline

    william9518

    How would you do it?
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    If you want to write the entire file into the 'data folder' saveResource method is a much better alternative.
     
Thread Status:
Not open for further replies.

Share This Page