From 383e647c871c56c5a2ad82e7bd422e5e7af5baee Mon Sep 17 00:00:00 2001 From: mohammed jasem alaajel Date: Sat, 30 Sep 2023 13:34:21 +0400 Subject: [PATCH] split some functions from config loader to be common --- README.md | 2 +- .../api/config/LangConfiguration.java | 4 ++ .../api/config/RedisBungeeConfiguration.java | 1 - .../config/{ => loaders}/ConfigLoader.java | 45 +++------------ .../config/loaders/GenericConfigLoader.java | 57 +++++++++++++++++++ .../api/config/loaders/LangConfigLoader.java | 4 ++ .../minecraft/redisbungee/RedisBungee.java | 2 +- .../RedisBungeeVelocityPlugin.java | 2 +- gradlew | 2 +- 9 files changed, 77 insertions(+), 42 deletions(-) create mode 100644 RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/LangConfiguration.java rename RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/{ => loaders}/ConfigLoader.java (82%) create mode 100644 RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/GenericConfigLoader.java create mode 100644 RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/LangConfigLoader.java diff --git a/README.md b/README.md index 97ba182..bb0a4ee 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ then in your project plugin.yml add `RedisBungee` to `depends` like this ```yaml name: "yourplugin" -main: your.main.class +main: your.loaders.class version: 1.0.0-SNAPSHOT author: idk depends: [ RedisBungee ] diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/LangConfiguration.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/LangConfiguration.java new file mode 100644 index 0000000..0d421e8 --- /dev/null +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/LangConfiguration.java @@ -0,0 +1,4 @@ +package com.imaginarycode.minecraft.redisbungee.api.config; + +public class LangConfiguration { +} diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/RedisBungeeConfiguration.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/RedisBungeeConfiguration.java index ffb44c5..31a0aee 100644 --- a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/RedisBungeeConfiguration.java +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/RedisBungeeConfiguration.java @@ -18,7 +18,6 @@ import java.util.List; public class RedisBungeeConfiguration { - public static final int CONFIG_VERSION = 2; private final String proxyId; private final List exemptAddresses; private final boolean registerCommands; diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/ConfigLoader.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/ConfigLoader.java similarity index 82% rename from RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/ConfigLoader.java rename to RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/ConfigLoader.java index 9bf84e2..346f3ee 100644 --- a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/ConfigLoader.java +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/ConfigLoader.java @@ -8,13 +8,13 @@ * http://www.eclipse.org/legal/epl-v10.html */ -package com.imaginarycode.minecraft.redisbungee.api.config; +package com.imaginarycode.minecraft.redisbungee.api.config.loaders; -import com.google.common.collect.ImmutableMap; import com.google.common.reflect.TypeToken; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; +import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisClusterSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner; import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner; @@ -26,26 +26,21 @@ import redis.clients.jedis.*; import redis.clients.jedis.providers.ClusterConnectionProvider; import redis.clients.jedis.providers.PooledConnectionProvider; -import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.StandardCopyOption; import java.util.*; -public interface ConfigLoader { +public interface ConfigLoader extends GenericConfigLoader { - default void loadConfig(RedisBungeePlugin plugin, File dataFolder) throws IOException { - loadConfig(plugin, dataFolder.toPath()); - } + int CONFIG_VERSION = 2; + @Override default void loadConfig(RedisBungeePlugin plugin, Path dataFolder) throws IOException { - Path configFile = createConfigFile(dataFolder); + Path configFile = createConfigFile(dataFolder, "config.yml", "config.yml"); final YAMLConfigurationLoader yamlConfigurationFileLoader = YAMLConfigurationLoader.builder().setPath(configFile).build(); ConfigurationNode node = yamlConfigurationFileLoader.load(); - if (node.getNode("config-version").getInt(0) != RedisBungeeConfiguration.CONFIG_VERSION) { - handleOldConfig(dataFolder); + if (node.getNode("config-version").getInt(0) != CONFIG_VERSION) { + handleOldConfig(dataFolder, "config.yml", "config.yml"); node = yamlConfigurationFileLoader.load(); } final boolean useSSL = node.getNode("useSSL").getBoolean(false); @@ -145,29 +140,5 @@ public interface ConfigLoader { void onConfigLoad(RedisBungeeConfiguration configuration, Summoner summoner, RedisBungeeMode mode); - default Path createConfigFile(Path dataFolder) throws IOException { - if (Files.notExists(dataFolder)) { - Files.createDirectory(dataFolder); - } - Path file = dataFolder.resolve("config.yml"); - if (Files.notExists(file)) { - try (InputStream in = getClass().getClassLoader().getResourceAsStream("config.yml")) { - Files.createFile(file); - assert in != null; - Files.copy(in, file, StandardCopyOption.REPLACE_EXISTING); - } - } - return file; - } - - default void handleOldConfig(Path dataFolder) throws IOException { - Path oldConfigFolder = dataFolder.resolve("old_config"); - if (Files.notExists(oldConfigFolder)) { - Files.createDirectory(oldConfigFolder); - } - Path oldConfigPath = dataFolder.resolve("config.yml"); - Files.move(oldConfigPath, oldConfigFolder.resolve(UUID.randomUUID() + "_config.yml")); - createConfigFile(dataFolder); - } } diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/GenericConfigLoader.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/GenericConfigLoader.java new file mode 100644 index 0000000..e5f845d --- /dev/null +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/GenericConfigLoader.java @@ -0,0 +1,57 @@ +package com.imaginarycode.minecraft.redisbungee.api.config.loaders; + +import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; +import java.time.Instant; +import java.util.UUID; + +public interface GenericConfigLoader { + + // CHANGES on every reboot + String RANDOM_OLD = "backup-" + Instant.now().getEpochSecond(); + + default void loadConfig(RedisBungeePlugin plugin, File dataFolder) throws IOException { + loadConfig(plugin, dataFolder.toPath()); + } + + + void loadConfig(RedisBungeePlugin plugin, Path path) throws IOException; + + default Path createConfigFile(Path dataFolder, String configFile, @Nullable String defaultResourceID) throws IOException { + if (Files.notExists(dataFolder)) { + Files.createDirectory(dataFolder); + } + Path file = dataFolder.resolve(configFile); + if (Files.notExists(file) && defaultResourceID != null) { + try (InputStream in = getClass().getClassLoader().getResourceAsStream(defaultResourceID)) { + Files.createFile(file); + assert in != null; + Files.copy(in, file, StandardCopyOption.REPLACE_EXISTING); + } + } + return file; + } + + default void handleOldConfig(Path dataFolder, String configFile, @Nullable String defaultResourceID) throws IOException { + Path oldConfigFolder = dataFolder.resolve("old_config"); + if (Files.notExists(oldConfigFolder)) { + Files.createDirectory(oldConfigFolder); + } + Path randomStoreConfigDirectory = oldConfigFolder.resolve(RANDOM_OLD); + if (Files.notExists(randomStoreConfigDirectory)) { + Files.createDirectory(randomStoreConfigDirectory); + } + Path oldConfigPath = dataFolder.resolve(configFile); + + Files.move(oldConfigPath, randomStoreConfigDirectory.resolve(configFile)); + createConfigFile(dataFolder, configFile, defaultResourceID); + } + +} diff --git a/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/LangConfigLoader.java b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/LangConfigLoader.java new file mode 100644 index 0000000..edb41f6 --- /dev/null +++ b/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/config/loaders/LangConfigLoader.java @@ -0,0 +1,4 @@ +package com.imaginarycode.minecraft.redisbungee.api.config.loaders; + +public interface LangConfigLoader { +} diff --git a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java index 8b8d263..4db37df 100644 --- a/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java +++ b/RedisBungee-Bungee/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungee.java @@ -14,7 +14,7 @@ import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager; import com.imaginarycode.minecraft.redisbungee.api.ProxyDataManager; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; -import com.imaginarycode.minecraft.redisbungee.api.config.ConfigLoader; +import com.imaginarycode.minecraft.redisbungee.api.config.loaders.ConfigLoader; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent; diff --git a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java index cdad87e..e63aadb 100644 --- a/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java +++ b/RedisBungee-Velocity/src/main/java/com/imaginarycode/minecraft/redisbungee/RedisBungeeVelocityPlugin.java @@ -15,7 +15,7 @@ import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager; import com.imaginarycode.minecraft.redisbungee.api.ProxyDataManager; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode; import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin; -import com.imaginarycode.minecraft.redisbungee.api.config.ConfigLoader; +import com.imaginarycode.minecraft.redisbungee.api.config.loaders.ConfigLoader; import com.imaginarycode.minecraft.redisbungee.api.config.RedisBungeeConfiguration; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent; import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent; diff --git a/gradlew b/gradlew index fcb6fca..c24bf85 100755 --- a/gradlew +++ b/gradlew @@ -160,7 +160,7 @@ fi # Collect all arguments for the java command, stacking in reverse order: # * args from the command line -# * the main class name +# * the loaders class name # * -classpath # * -D...appname settings # * --module-path (only if needed)