From 6d1f0fe561887189d487a74c4929ae57b2fc7809 Mon Sep 17 00:00:00 2001 From: hyperdefined Date: Thu, 29 Aug 2024 00:16:22 -0400 Subject: [PATCH] update config from 6 to 7 --- .../java/lol/hyper/toolstats/ToolStats.java | 2 +- .../toolstats/tools/config/ConfigUpdater.java | 8 ++ .../tools/config/versions/Version7.java | 73 +++++++++++++++++++ src/main/resources/config.yml | 2 +- 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/main/java/lol/hyper/toolstats/tools/config/versions/Version7.java diff --git a/src/main/java/lol/hyper/toolstats/ToolStats.java b/src/main/java/lol/hyper/toolstats/ToolStats.java index e0817cc..80dc721 100644 --- a/src/main/java/lol/hyper/toolstats/ToolStats.java +++ b/src/main/java/lol/hyper/toolstats/ToolStats.java @@ -113,7 +113,7 @@ public final class ToolStats extends JavaPlugin { */ public final NamespacedKey originType = new NamespacedKey(this, "origin"); - public final int CONFIG_VERSION = 6; + public final int CONFIG_VERSION = 7; public final Logger logger = this.getLogger(); public final File configFile = new File(this.getDataFolder(), "config.yml"); diff --git a/src/main/java/lol/hyper/toolstats/tools/config/ConfigUpdater.java b/src/main/java/lol/hyper/toolstats/tools/config/ConfigUpdater.java index cd28c1a..afc4d2b 100644 --- a/src/main/java/lol/hyper/toolstats/tools/config/ConfigUpdater.java +++ b/src/main/java/lol/hyper/toolstats/tools/config/ConfigUpdater.java @@ -36,6 +36,7 @@ package lol.hyper.toolstats.tools.config; import lol.hyper.toolstats.ToolStats; import lol.hyper.toolstats.tools.config.versions.Version6; +import lol.hyper.toolstats.tools.config.versions.Version7; public class ConfigUpdater { @@ -49,9 +50,16 @@ public class ConfigUpdater { int version = toolStats.config.getInt("config-version"); // this will be a switch in the future + // Upgrade 5 to 6 if (version == 5) { Version6 version6 = new Version6(toolStats); version6.update(); } + + // Upgrade 6 to 7 + if (version == 6) { + Version7 version7 = new Version7(toolStats); + version7.update(); + } } } diff --git a/src/main/java/lol/hyper/toolstats/tools/config/versions/Version7.java b/src/main/java/lol/hyper/toolstats/tools/config/versions/Version7.java new file mode 100644 index 0000000..f8746af --- /dev/null +++ b/src/main/java/lol/hyper/toolstats/tools/config/versions/Version7.java @@ -0,0 +1,73 @@ +/* + * This file is part of ToolStats. + * + * ToolStats is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * ToolStats is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with ToolStats. If not, see . + */ + +package lol.hyper.toolstats.tools.config.versions; + +import lol.hyper.toolstats.ToolStats; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Version7 { + + private final ToolStats toolStats; + + /** + * Used for updating from version 6 to 7. + * + * @param toolStats ToolStats instance. + */ + public Version7(ToolStats toolStats) { + this.toolStats = toolStats; + } + + /** + * Perform the config update. + */ + public void update() { + // save the old config first + try { + toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config-6.yml"); + } catch (IOException exception) { + toolStats.logger.severe("Unable to save config-5.yml!"); + throw new RuntimeException(exception); + } + + // we make this super verbose so that admins can see what's being added + toolStats.logger.info("Updating config.yml to version 7."); + toolStats.config.set("config-version", 7); + + toolStats.logger.info("Adding messages.flight-time to config.yml."); + toolStats.config.set("messages.flight-time", "&7Flight time: &8{time}"); + + toolStats.logger.info("Adding enabled.flight-time to config.yml."); + toolStats.config.set("enabled.flight-time", true); + + // save the config and reload it + try { + toolStats.config.save("plugins" + File.separator + "ToolStats" + File.separator + "config.yml"); + } catch (IOException exception) { + toolStats.logger.severe("Unable to save config.yml!"); + throw new RuntimeException(exception); + } + toolStats.loadConfig(); + toolStats.logger.info("Config has been updated to version 7. A copy of version 5 has been saved as config-6.yml"); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index af5abb7..45df9e4 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -136,4 +136,4 @@ number-formats: # This has no use currently, but can be used for future features for dupe detection. generate-hash-for-items: true -config-version: 6 \ No newline at end of file +config-version: 7 \ No newline at end of file