From 3926ddccebde3956bb1d750321f7377398447c2e Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Thu, 23 Jun 2022 21:29:41 +0200 Subject: [PATCH] Added config migration (#69), and upgraded version numbers for release v1.4! --- dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- .../gr8/playerstats/commands/StatCommand.java | 6 --- .../gr8/playerstats/config/ConfigHandler.java | 54 +++++++++---------- .../config/ConfigUpdateHandler.java | 26 ++++++++- src/main/resources/config.yml | 2 +- src/main/resources/plugin.yml | 2 +- 7 files changed, 55 insertions(+), 39 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 75dafb4..7cab65a 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.gmail.artemis-the-gr8 PlayerStats - 1.3 + 1.4 diff --git a/pom.xml b/pom.xml index 32d5f64..d61f77e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.gmail.artemis-the-gr8 PlayerStats - 1.3 + 1.4 UTF-8 diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java index 9f15e07..c67ed3d 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -53,12 +53,6 @@ public class StatCommand implements CommandExecutor { adventure.sender(sender).sendMessage(messageFactory.usageExamples(sender instanceof ConsoleCommandSender)); return true; } - else if (args[0].equalsIgnoreCase("test")) { - String selection = (args.length > 1) ? args[1] : null; - printTranslatableNames(sender, selection); - return true; - } - else { //part 1: collecting all relevant information from the args StatRequest request = generateRequest(sender, args); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java index 72065ab..097f6c9 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigHandler.java @@ -22,19 +22,29 @@ public class ConfigHandler { saveDefaultConfig(); config = YamlConfiguration.loadConfiguration(configFile); - configVersion = 3.1; + configVersion = 4; checkConfigVersion(); MyLogger.setDebugLevel(debugLevel()); } - /** Returns the desired debugging level. -

1 = low (only show unexpected errors)

-

2 = medium (detail all encountered exceptions, log main tasks and show time taken)

-

3 = high (log all tasks and time taken)

-

Default: 1

*/ - public int debugLevel() { - return config.getInt("debug-level", 1); + /** Checks the number that "config-version" returns to see if the config needs updating, and if so, send it to the Updater. +

PlayerStats 1.1: "config-version" doesn't exist.

+

PlayerStats 1.2: "config-version" is 2.

+

PlayerStats 1.3: "config-version" is 3.

+

PlayerStats 1.4: "config-version" is 4.

*/ + private void checkConfigVersion() { + if (!config.contains("config-version") || config.getDouble("config-version") != configVersion) { + new ConfigUpdateHandler(plugin, configFile, configVersion); + reloadConfig(); + } + } + + /** Create a config file if none exists yet (from the config.yml in the plugin's resources). */ + private void saveDefaultConfig() { + config = plugin.getConfig(); + plugin.saveDefaultConfig(); + configFile = new File(plugin.getDataFolder(), "config.yml"); } /** Reloads the config from file, or creates a new file with default values if there is none. @@ -54,6 +64,15 @@ public class ConfigHandler { } } + /** Returns the desired debugging level. +

1 = low (only show unexpected errors)

+

2 = medium (detail all encountered exceptions, log main tasks and show time taken)

+

3 = high (log all tasks and time taken)

+

Default: 1

*/ + public int debugLevel() { + return config.getInt("debug-level", 1); + } + /** Returns the config setting for include-whitelist-only.

Default: false

*/ public boolean whitelistOnly() { @@ -251,23 +270,4 @@ public class ConfigHandler { } } } - - /** Create a config file if none exists yet (from the config.yml in the plugin's resources). */ - private void saveDefaultConfig() { - config = plugin.getConfig(); - plugin.saveDefaultConfig(); - configFile = new File(plugin.getDataFolder(), "config.yml"); - } - - /** Checks the number that "config-version" returns to see if the config needs updating, and if so, send it to the Updater. -

-

PlayerStats 1.1: "config-version" doesn't exist.

-

PlayerStats 1.2: "config-version" is 2.

-

PlayerStats 1.3: "config-version" is 3.

*/ - private void checkConfigVersion() { - if (!config.contains("config-version") || config.getDouble("config-version") != configVersion) { - new ConfigUpdateHandler(plugin, configFile, configVersion); - reloadConfig(); - } - } } diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java index 55db79f..2eb04d1 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/config/ConfigUpdateHandler.java @@ -14,21 +14,43 @@ public class ConfigUpdateHandler { public ConfigUpdateHandler(Main plugin, File configFile, double configVersion) { YamlConfiguration configuration = YamlConfiguration.loadConfiguration(configFile); updateTopListDefault(configuration); + updateDefaultColors(configuration); configuration.set("config-version", configVersion); try { configuration.save(configFile); ConfigUpdater.update(plugin, configFile.getName(), configFile); - plugin.getLogger().info("Your config has been updated to version " + configVersion + - "! This should have migrated your settings, but double-check your config.yml if you suspect something went wrong."); + plugin.getLogger().warning("Your config has been updated to version " + configVersion + + ". This version includes some slight changes in the default color scheme, but none of your custom settings should have been changed!"); } catch (IOException e) { e.printStackTrace(); } } + /** Adjusts the value for "top-list" to migrate the config file from versions 1 or 2 to version 3.*/ private void updateTopListDefault(YamlConfiguration configuration) { String oldTitle = configuration.getString("top-list-title"); if (oldTitle != null && oldTitle.equalsIgnoreCase("Top [x]")) { configuration.set("top-list-title", "Top"); } } + + /** Adjusts some of the default colors to migrate from versions 2 or 3 to version 4.*/ + private void updateDefaultColors(YamlConfiguration configuration) { + updateColor(configuration, "top-list.title", "yellow", "#FFD52B"); + updateColor(configuration, "top-list.stat-names", "yellow", "#FFD52B"); + updateColor(configuration, "top-list.sub-stat-names", "#FFD52B", "yellow"); + updateColor(configuration, "individual-statistics.stat-names", "yellow", "#FFD52B"); + updateColor(configuration, "individual-statistics.sub-stat-names", "#FFD52B", "yellow"); + updateColor(configuration, "total-server.title", "gold", "#55AAFF"); + updateColor(configuration, "total-server.server-name", "gold", "#55AAFF"); + updateColor(configuration, "total-server.stat-names", "yellow", "#FFD52B"); + updateColor(configuration, "total-server.sub-stat-names", "#FFD52B", "yellow"); + } + + private void updateColor(YamlConfiguration configuration, String path, String oldValue, String newValue) { + String configString = configuration.getString(path); + if (configString != null && configString.equalsIgnoreCase(oldValue)) { + configuration.set(path, newValue); + } + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 33a364d..9154e48 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,7 +1,7 @@ # ------------------------------------------------------------------------------------------------------ # # PlayerStats Configuration # # ------------------------------------------------------------------------------------------------------ # -config-version: 3.1 +config-version: 4 # # ------------------------------- # # diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8f8733c..5f8e8c1 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,6 +1,6 @@ main: com.gmail.artemis.the.gr8.playerstats.Main name: PlayerStats -version: 1.3 +version: 1.4 api-version: 1.18 description: adds commands to view player statistics in chat author: Artemis_the_gr8