Added config migration (#69), and upgraded version numbers for release v1.4!

This commit is contained in:
Artemis-the-gr8 2022-06-23 21:29:41 +02:00
parent e8f17afa8f
commit 3926ddcceb
7 changed files with 55 additions and 39 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.gmail.artemis-the-gr8</groupId>
<artifactId>PlayerStats</artifactId>
<version>1.3</version>
<version>1.4</version>
<build>
<plugins>
<plugin>

View File

@ -6,7 +6,7 @@
<groupId>com.gmail.artemis-the-gr8</groupId>
<artifactId>PlayerStats</artifactId>
<version>1.3</version>
<version>1.4</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -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);

View File

@ -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.
<p>1 = low (only show unexpected errors)</p>
<p>2 = medium (detail all encountered exceptions, log main tasks and show time taken)</p>
<p>3 = high (log all tasks and time taken)</p>
<p>Default: 1</p>*/
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.
<p>PlayerStats 1.1: "config-version" doesn't exist.</p>
<p>PlayerStats 1.2: "config-version" is 2.</p>
<p>PlayerStats 1.3: "config-version" is 3. </P>
<p>PlayerStats 1.4: "config-version" is 4.</p>*/
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.
<p>1 = low (only show unexpected errors)</p>
<p>2 = medium (detail all encountered exceptions, log main tasks and show time taken)</p>
<p>3 = high (log all tasks and time taken)</p>
<p>Default: 1</p>*/
public int debugLevel() {
return config.getInt("debug-level", 1);
}
/** Returns the config setting for include-whitelist-only.
<p>Default: false</p>*/
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.
<p></p>
<p>PlayerStats 1.1: "config-version" doesn't exist.</p>
<p>PlayerStats 1.2: "config-version" is 2.</p>
<p>PlayerStats 1.3: "config-version" is 3. </P>*/
private void checkConfigVersion() {
if (!config.contains("config-version") || config.getDouble("config-version") != configVersion) {
new ConfigUpdateHandler(plugin, configFile, configVersion);
reloadConfig();
}
}
}

View File

@ -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);
}
}
}

View File

@ -1,7 +1,7 @@
# ------------------------------------------------------------------------------------------------------ #
# PlayerStats Configuration #
# ------------------------------------------------------------------------------------------------------ #
config-version: 3.1
config-version: 4
# # ------------------------------- # #

View File

@ -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