mirror of
https://github.com/proxiodev/RedisBungee.git
synced 2024-11-05 04:48:02 +00:00
split some functions from config loader to be common
This commit is contained in:
parent
9b54ca93db
commit
383e647c87
@ -101,7 +101,7 @@ then in your project plugin.yml add `RedisBungee` to `depends` like this
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
name: "yourplugin"
|
name: "yourplugin"
|
||||||
main: your.main.class
|
main: your.loaders.class
|
||||||
version: 1.0.0-SNAPSHOT
|
version: 1.0.0-SNAPSHOT
|
||||||
author: idk
|
author: idk
|
||||||
depends: [ RedisBungee ]
|
depends: [ RedisBungee ]
|
||||||
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.imaginarycode.minecraft.redisbungee.api.config;
|
||||||
|
|
||||||
|
public class LangConfiguration {
|
||||||
|
}
|
@ -18,7 +18,6 @@ import java.util.List;
|
|||||||
|
|
||||||
public class RedisBungeeConfiguration {
|
public class RedisBungeeConfiguration {
|
||||||
|
|
||||||
public static final int CONFIG_VERSION = 2;
|
|
||||||
private final String proxyId;
|
private final String proxyId;
|
||||||
private final List<InetAddress> exemptAddresses;
|
private final List<InetAddress> exemptAddresses;
|
||||||
private final boolean registerCommands;
|
private final boolean registerCommands;
|
||||||
|
@ -8,13 +8,13 @@
|
|||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* 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.google.common.reflect.TypeToken;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
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.JedisClusterSummoner;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
|
import com.imaginarycode.minecraft.redisbungee.api.summoners.JedisPooledSummoner;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.summoners.Summoner;
|
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.ClusterConnectionProvider;
|
||||||
import redis.clients.jedis.providers.PooledConnectionProvider;
|
import redis.clients.jedis.providers.PooledConnectionProvider;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public interface ConfigLoader {
|
public interface ConfigLoader extends GenericConfigLoader {
|
||||||
|
|
||||||
default void loadConfig(RedisBungeePlugin<?> plugin, File dataFolder) throws IOException {
|
int CONFIG_VERSION = 2;
|
||||||
loadConfig(plugin, dataFolder.toPath());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
default void loadConfig(RedisBungeePlugin<?> plugin, Path dataFolder) throws IOException {
|
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();
|
final YAMLConfigurationLoader yamlConfigurationFileLoader = YAMLConfigurationLoader.builder().setPath(configFile).build();
|
||||||
ConfigurationNode node = yamlConfigurationFileLoader.load();
|
ConfigurationNode node = yamlConfigurationFileLoader.load();
|
||||||
if (node.getNode("config-version").getInt(0) != RedisBungeeConfiguration.CONFIG_VERSION) {
|
if (node.getNode("config-version").getInt(0) != CONFIG_VERSION) {
|
||||||
handleOldConfig(dataFolder);
|
handleOldConfig(dataFolder, "config.yml", "config.yml");
|
||||||
node = yamlConfigurationFileLoader.load();
|
node = yamlConfigurationFileLoader.load();
|
||||||
}
|
}
|
||||||
final boolean useSSL = node.getNode("useSSL").getBoolean(false);
|
final boolean useSSL = node.getNode("useSSL").getBoolean(false);
|
||||||
@ -145,29 +140,5 @@ public interface ConfigLoader {
|
|||||||
|
|
||||||
void onConfigLoad(RedisBungeeConfiguration configuration, Summoner<?> summoner, RedisBungeeMode mode);
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
package com.imaginarycode.minecraft.redisbungee.api.config.loaders;
|
||||||
|
|
||||||
|
public interface LangConfigLoader {
|
||||||
|
}
|
@ -14,7 +14,7 @@ import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager;
|
|||||||
import com.imaginarycode.minecraft.redisbungee.api.ProxyDataManager;
|
import com.imaginarycode.minecraft.redisbungee.api.ProxyDataManager;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
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.config.RedisBungeeConfiguration;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent;
|
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent;
|
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent;
|
||||||
|
@ -15,7 +15,7 @@ import com.imaginarycode.minecraft.redisbungee.api.PlayerDataManager;
|
|||||||
import com.imaginarycode.minecraft.redisbungee.api.ProxyDataManager;
|
import com.imaginarycode.minecraft.redisbungee.api.ProxyDataManager;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeeMode;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
|
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.config.RedisBungeeConfiguration;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent;
|
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerChangedServerNetworkEvent;
|
||||||
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent;
|
import com.imaginarycode.minecraft.redisbungee.api.events.IPlayerJoinedNetworkEvent;
|
||||||
|
2
gradlew
vendored
2
gradlew
vendored
@ -160,7 +160,7 @@ fi
|
|||||||
|
|
||||||
# Collect all arguments for the java command, stacking in reverse order:
|
# Collect all arguments for the java command, stacking in reverse order:
|
||||||
# * args from the command line
|
# * args from the command line
|
||||||
# * the main class name
|
# * the loaders class name
|
||||||
# * -classpath
|
# * -classpath
|
||||||
# * -D...appname settings
|
# * -D...appname settings
|
||||||
# * --module-path (only if needed)
|
# * --module-path (only if needed)
|
||||||
|
Loading…
Reference in New Issue
Block a user