From a5127e67a314c27e756f649f07389ecd4b056ef2 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Nov 2021 18:26:32 +0000 Subject: [PATCH] Add config updater --- .../bukkit/listener/BukkitRedisListener.java | 2 +- .../bungeecord/config/ConfigLoader.java | 30 +++++++++++++++++-- .../bungeecord/config/ConfigManager.java | 8 +++++ .../src/main/resources/bungee-config.yml | 1 - 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/bukkit/src/main/java/me/william278/husksync/bukkit/listener/BukkitRedisListener.java b/bukkit/src/main/java/me/william278/husksync/bukkit/listener/BukkitRedisListener.java index c6423c8a..c647eee2 100644 --- a/bukkit/src/main/java/me/william278/husksync/bukkit/listener/BukkitRedisListener.java +++ b/bukkit/src/main/java/me/william278/husksync/bukkit/listener/BukkitRedisListener.java @@ -56,7 +56,7 @@ public class BukkitRedisListener extends RedisListener { switch (message.getMessageType()) { case REQUEST_DATA_ON_JOIN -> { UUID playerUUID = UUID.fromString(message.getMessageDataElements()[1]); - switch (me.william278.husksync.redis.RedisMessage.RequestOnJoinUpdateType.valueOf(message.getMessageDataElements()[0])) { + switch (RedisMessage.RequestOnJoinUpdateType.valueOf(message.getMessageDataElements()[0])) { case ADD_REQUESTER -> HuskSyncBukkit.bukkitCache.setRequestOnJoin(playerUUID); case REMOVE_REQUESTER -> HuskSyncBukkit.bukkitCache.removeRequestOnJoin(playerUUID); } diff --git a/bungeecord/src/main/java/me/william278/husksync/bungeecord/config/ConfigLoader.java b/bungeecord/src/main/java/me/william278/husksync/bungeecord/config/ConfigLoader.java index b117a39b..b2bb4ec6 100644 --- a/bungeecord/src/main/java/me/william278/husksync/bungeecord/config/ConfigLoader.java +++ b/bungeecord/src/main/java/me/william278/husksync/bungeecord/config/ConfigLoader.java @@ -1,14 +1,40 @@ package me.william278.husksync.bungeecord.config; -import me.william278.husksync.util.MessageManager; +import me.william278.husksync.HuskSyncBungeeCord; import me.william278.husksync.Settings; +import me.william278.husksync.util.MessageManager; import net.md_5.bungee.config.Configuration; import java.util.HashMap; public class ConfigLoader { - public static void loadSettings(Configuration config) throws IllegalArgumentException { + private static final HuskSyncBungeeCord plugin = HuskSyncBungeeCord.getInstance(); + + private static Configuration copyDefaults(Configuration config) { + // Get the config version and update if needed + String configVersion = config.getString("config_file_version", "1.0"); + if (configVersion.contains("-dev")) { + configVersion = configVersion.replaceAll("-dev", ""); + } + if (!configVersion.equals(plugin.getDescription().getVersion())) { + if (configVersion.equalsIgnoreCase("1.0")) { + config.set("check_for_updates", true); + } + if (configVersion.equalsIgnoreCase("1.0") || configVersion.equalsIgnoreCase("1.0.1") || configVersion.equalsIgnoreCase("1.0.2") || configVersion.equalsIgnoreCase("1.0.3")) { + config.set("clusters.main.player_table", "husksync_players"); + config.set("clusters.main.data_table", "husksync_data"); + } + config.set("config_file_version", plugin.getDescription().getVersion()); + } + // Save the config back + ConfigManager.saveConfig(config); + return config; + } + + public static void loadSettings(Configuration loadedConfig) throws IllegalArgumentException { + Configuration config = copyDefaults(loadedConfig); + Settings.language = config.getString("language", "en-gb"); Settings.serverType = Settings.ServerType.BUNGEECORD; diff --git a/bungeecord/src/main/java/me/william278/husksync/bungeecord/config/ConfigManager.java b/bungeecord/src/main/java/me/william278/husksync/bungeecord/config/ConfigManager.java index bf6371ea..f3f94477 100644 --- a/bungeecord/src/main/java/me/william278/husksync/bungeecord/config/ConfigManager.java +++ b/bungeecord/src/main/java/me/william278/husksync/bungeecord/config/ConfigManager.java @@ -32,6 +32,14 @@ public class ConfigManager { } } + public static void saveConfig(Configuration config) { + try { + ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, new File(plugin.getDataFolder(), "config.yml")); + } catch (IOException e) { + plugin.getLogger().log(Level.CONFIG, "An exception occurred loading the configuration file", e); + } + } + public static void loadMessages() { try { if (!plugin.getDataFolder().exists()) { diff --git a/bungeecord/src/main/resources/bungee-config.yml b/bungeecord/src/main/resources/bungee-config.yml index e561c22a..b54d463e 100644 --- a/bungeecord/src/main/resources/bungee-config.yml +++ b/bungeecord/src/main/resources/bungee-config.yml @@ -22,6 +22,5 @@ clusters: main: player_table: 'husksync_players' data_table: 'husksync_data' - check_for_updates: true config_file_version: 1.1 \ No newline at end of file