/** * Method to setup the configuration. If the configuration file doesn't exist it will be created * by {@link #defaultConfig()} After that the configuration is loaded {@link #loadConfig()} We * than check if an configuration update is necessary {@link #updateNecessary()} and set {@link * #configAvailable} to true * * @see #defaultConfig() * @see #loadConfig() * @see #updateNecessary() * @see #updateConfig() */ void setupConfig() { // Checking if config file exists, if not create it if (!(new File(main.getDataFolder(), configFile)).exists()) { attackControlLogger.info("Creating default configuration file"); defaultConfig(); } // adding the default values customDefaultConfig(); try { config.load(pluginPath + configFile); } catch (IOException e) { attackControlLogger.severe("Can't read the " + configFile + " File!", e); } catch (InvalidConfigurationException e) { attackControlLogger.severe("Problem with the configuration in " + configFile + "!", e); } // Loading the config from file loadConfig(); // Checking internal configCurrent and config file configVer setupCommands(); setupListeners(); updateNecessary(); // If config file has new options update it if enabled if (mainConfig.autoUpdateConfig) { updateConfig(); } configAvailable = true; }
/** * Method for loading the configuration from disk. First we get the config object from disk, than * we read in the standard configuration part. We also log a message if #debugLogEnabled and we * produce some debug logs. After that we load the custom configuration part #loadCustomConfig() * * @see #loadCustomConfig() */ void loadConfig() { // Starting to update the standard configuration configVer = config.getString("configVer"); // Debug OutPut NOW! attackControlLogger.debug("configCurrent", configCurrent); attackControlLogger.debug("configVer", configVer); loadCustomConfig(); attackControlLogger.info("Configuration v." + configVer + " loaded."); }
/** * Method to write and create the default configuration. The custom configuration variables are * added via #setupCustomDefaultVariables() Than we write the configuration to disk #writeConfig() * Than we get the config object from disk We are adding the default configuration for the * variables and load the defaults for the custom variables #customDefaultConfig() * * @see #setupCustomDefaultVariables() * @see #customDefaultConfig() */ private void defaultConfig() { setupCustomDefaultVariables(); if (!writeConfig()) { attackControlLogger.severe("Problems writing default config!"); attackControlLogger.info("Using internal Defaults!"); } else { attackControlLogger.debug("DefaultConfig written"); } config.addDefault("configVer", configVer); }
/** * Method to check if the configuration version are different. will set #configRequiresUpdate to * true if versions are different */ void updateNecessary() { if (configVer.equalsIgnoreCase(configCurrent)) { attackControlLogger.info("Config is up to date"); } else { attackControlLogger.warning("Config is not up to date!"); attackControlLogger.warning("Config File Version: " + configVer); attackControlLogger.warning("Internal Config Version: " + configCurrent); attackControlLogger.warning("It is suggested to update the config.yml!"); configRequiresUpdate = true; } }
/** Method to update the configuration if it is necessary. */ void updateConfig() { if (configRequiresUpdate) { configVer = configCurrent; if (writeConfig()) { attackControlLogger.info("Configuration was updated with new default values."); attackControlLogger.info("Please change them to your liking."); } else { attackControlLogger.warning("Configuration file could not be auto updated."); attackControlLogger.warning("Please rename " + configFile + " and try again."); } } }
/** Method to load the configuration into the config variables */ void loadCustomConfig() { playerCanAttack = config.getBoolean(PLAYER_CAN_ATTACK); if (playerCanAttack) { attackControlLogger.info("an angel cried!"); } }
/** * Method for writing the configuration file. First we write the standard configuration part, than * we write the custom configuration part via #writeCustomConfig() * * @return true if writing the config was successful * @see #writeCustomConfig(java.io.PrintWriter) */ public boolean writeConfig() { attackControlLogger.debug("creating config"); boolean success = false; try { PrintWriter stream; File folder = main.getDataFolder(); if (folder != null) { folder.mkdirs(); } PluginDescriptionFile pdfFile = main.getDescription(); stream = new PrintWriter(pluginPath + configFile); attackControlLogger.debug("starting contents"); // Let's write our config ;) stream.println( "# " + pdfFile.getName() + " " + pdfFile.getVersion() + " by " + pdfFile.getAuthors().toString()); stream.println("#"); stream.println("# Configuration File for module [" + MODULE_NAME + "]"); stream.println("#"); stream.println("# For detailed assistance please visit: " + mainConfig.getPluginSlug()); stream.println(); stream.println("# Configuration Version"); stream.println("configVer: \"" + configVer + "\""); stream.println(); // Getting the custom config information from the top of the class attackControlLogger.debug("going for customConfig"); writeCustomConfig(stream); stream.println(); stream.close(); success = true; } catch (FileNotFoundException e) { attackControlLogger.warning("Error saving the " + configFile + "."); } return success; }
/** * Method to reload the configuration. * * @return msg with the status of the reload */ public String reloadConfig() { configAvailable = false; try { config.load(pluginPath + configFile); configAvailable = true; } catch (IOException e) { attackControlLogger.severe("Can't read the " + configFile + " File!", e); } catch (InvalidConfigurationException e) { attackControlLogger.severe("Problem with the configuration in " + configFile + "!", e); } String msg; if (configAvailable) { loadConfig(); attackControlLogger.info("Config reloaded"); msg = MODULE_NAME + " Config was reloaded"; } else { attackControlLogger.severe("Reloading Config before it exists."); attackControlLogger.severe("Flog the developer!"); msg = "Something terrible terrible did go really really wrong, see console log!"; } return msg; }