forked from public-mirrors/HuskSync
Migrate config to Annotaml
parent
8c0f7a295f
commit
acd97a1cb0
@ -1,276 +1,190 @@
|
|||||||
package net.william278.husksync.config;
|
package net.william278.husksync.config;
|
||||||
|
|
||||||
import dev.dejvokep.boostedyaml.YamlDocument;
|
import net.william278.annotaml.YamlComment;
|
||||||
|
import net.william278.annotaml.YamlFile;
|
||||||
|
import net.william278.annotaml.YamlKey;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Arrays;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Settings used for the plugin, as read from the config file
|
* Plugin settings, read from config.yml
|
||||||
*/
|
*/
|
||||||
|
@YamlFile(header = """
|
||||||
|
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
|
||||||
|
┃ HuskSync Config ┃
|
||||||
|
┃ Developed by William278 ┃
|
||||||
|
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
|
||||||
|
┣╸ Information: https://william278.net/project/husksync
|
||||||
|
┗╸ Documentation: https://william278.net/docs/husksync""",
|
||||||
|
|
||||||
|
versionField = "config_version", versionNumber = 2)
|
||||||
public class Settings {
|
public class Settings {
|
||||||
|
|
||||||
/**
|
// Top-level settings
|
||||||
* Map of {@link ConfigOption}s read from the config file
|
public String language = "en-gb";
|
||||||
*/
|
|
||||||
private final Map<ConfigOption, Object> configOptions;
|
|
||||||
|
|
||||||
// Load the settings from the document
|
|
||||||
private Settings(@NotNull YamlDocument config) {
|
|
||||||
this.configOptions = new HashMap<>();
|
|
||||||
Arrays.stream(ConfigOption.values()).forEach(configOption -> configOptions
|
|
||||||
.put(configOption, switch (configOption.optionType) {
|
|
||||||
case BOOLEAN -> configOption.getBooleanValue(config);
|
|
||||||
case STRING -> configOption.getStringValue(config);
|
|
||||||
case DOUBLE -> configOption.getDoubleValue(config);
|
|
||||||
case FLOAT -> configOption.getFloatValue(config);
|
|
||||||
case INTEGER -> configOption.getIntValue(config);
|
|
||||||
case STRING_LIST -> configOption.getStringListValue(config);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Default constructor for empty settings
|
@YamlKey("check_for_updates")
|
||||||
protected Settings(@NotNull Map<ConfigOption, Object> configOptions) {
|
public boolean checkForUpdates = true;
|
||||||
this.configOptions = configOptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@YamlKey("cluster_id")
|
||||||
* Get the value of the specified {@link ConfigOption}
|
public String clusterId = "";
|
||||||
*
|
|
||||||
* @param option the {@link ConfigOption} to check
|
|
||||||
* @return the value of the {@link ConfigOption} as a boolean
|
|
||||||
* @throws ClassCastException if the option is not a boolean
|
|
||||||
*/
|
|
||||||
public boolean getBooleanValue(@NotNull ConfigOption option) throws ClassCastException {
|
|
||||||
return (Boolean) configOptions.get(option);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@YamlKey("debug_logging")
|
||||||
* Get the value of the specified {@link ConfigOption}
|
public boolean debugLogging = false;
|
||||||
*
|
|
||||||
* @param option the {@link ConfigOption} to check
|
|
||||||
* @return the value of the {@link ConfigOption} as a string
|
|
||||||
* @throws ClassCastException if the option is not a string
|
|
||||||
*/
|
|
||||||
public String getStringValue(@NotNull ConfigOption option) throws ClassCastException {
|
|
||||||
return (String) configOptions.get(option);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the value of the specified {@link ConfigOption}
|
|
||||||
*
|
|
||||||
* @param option the {@link ConfigOption} to check
|
|
||||||
* @return the value of the {@link ConfigOption} as a double
|
|
||||||
* @throws ClassCastException if the option is not a double
|
|
||||||
*/
|
|
||||||
public double getDoubleValue(@NotNull ConfigOption option) throws ClassCastException {
|
|
||||||
return (Double) configOptions.get(option);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Database settings
|
||||||
* Get the value of the specified {@link ConfigOption}
|
@YamlComment("Database connection settings")
|
||||||
*
|
@YamlKey("database.credentials.host")
|
||||||
* @param option the {@link ConfigOption} to check
|
public String mySqlHost = "localhost";
|
||||||
* @return the value of the {@link ConfigOption} as a float
|
|
||||||
* @throws ClassCastException if the option is not a float
|
|
||||||
*/
|
|
||||||
public double getFloatValue(@NotNull ConfigOption option) throws ClassCastException {
|
|
||||||
return (Float) configOptions.get(option);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@YamlKey("database.credentials.port")
|
||||||
* Get the value of the specified {@link ConfigOption}
|
public int mySqlPort = 3306;
|
||||||
*
|
|
||||||
* @param option the {@link ConfigOption} to check
|
|
||||||
* @return the value of the {@link ConfigOption} as an integer
|
|
||||||
* @throws ClassCastException if the option is not an integer
|
|
||||||
*/
|
|
||||||
public int getIntegerValue(@NotNull ConfigOption option) throws ClassCastException {
|
|
||||||
return (Integer) configOptions.get(option);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
@YamlKey("database.credentials.database")
|
||||||
* Get the value of the specified {@link ConfigOption}
|
public String mySqlDatabase = "HuskSync";
|
||||||
*
|
|
||||||
* @param option the {@link ConfigOption} to check
|
@YamlKey("database.mysql.credentials.username")
|
||||||
* @return the value of the {@link ConfigOption} as a string {@link List}
|
public String mySqlUsername = "root";
|
||||||
* @throws ClassCastException if the option is not a string list
|
|
||||||
*/
|
@YamlKey("database.credentials.password")
|
||||||
@SuppressWarnings("unchecked")
|
public String mySqlPassword = "pa55w0rd";
|
||||||
public List<String> getStringListValue(@NotNull ConfigOption option) throws ClassCastException {
|
|
||||||
return (List<String>) configOptions.get(option);
|
@YamlKey("database.credentials.parameters")
|
||||||
|
public String mySqlConnectionParameters = "?autoReconnect=true&useSSL=false";
|
||||||
|
|
||||||
|
@YamlComment("MySQL connection pool properties")
|
||||||
|
@YamlKey("database.connection_pool.maximum_pool_size")
|
||||||
|
public int mySqlConnectionPoolSize = 10;
|
||||||
|
|
||||||
|
@YamlKey("database.connection_pool.minimum_idle")
|
||||||
|
public int mySqlConnectionPoolIdle = 10;
|
||||||
|
|
||||||
|
@YamlKey("database.connection_pool.maximum_lifetime")
|
||||||
|
public long mySqlConnectionPoolLifetime = 1800000;
|
||||||
|
|
||||||
|
@YamlKey("database.connection_pool.keepalive_time")
|
||||||
|
public long mySqlConnectionPoolKeepAlive = 0;
|
||||||
|
|
||||||
|
@YamlKey("database.connection_pool.connection_timeout")
|
||||||
|
public long mySqlConnectionPoolTimeout = 5000;
|
||||||
|
|
||||||
|
@YamlKey("database.table_names")
|
||||||
|
public Map<String, String> tableNames = TableName.getDefaults();
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
public String getTableName(@NotNull TableName tableName) {
|
||||||
|
return Optional.ofNullable(tableNames.get(tableName.name().toLowerCase()))
|
||||||
|
.orElse(tableName.defaultName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
// Redis settings
|
||||||
* Load the settings from a BoostedYaml {@link YamlDocument} config file
|
@YamlComment("Redis connection settings")
|
||||||
*
|
@YamlKey("redis.credentials.host")
|
||||||
* @param config The loaded {@link YamlDocument} config.yml file
|
public String redisHost = "localhost";
|
||||||
* @return the loaded {@link Settings}
|
|
||||||
*/
|
@YamlKey("redis.credentials.port")
|
||||||
public static Settings load(@NotNull YamlDocument config) {
|
public int redisPort = 6379;
|
||||||
return new Settings(config);
|
|
||||||
|
@YamlKey("redis.credentials.password")
|
||||||
|
public String redisPassword = "";
|
||||||
|
|
||||||
|
@YamlKey("redis.use_ssl")
|
||||||
|
public boolean redisUseSsl = false;
|
||||||
|
|
||||||
|
|
||||||
|
// Synchronization settings
|
||||||
|
@YamlComment("Synchronization settings")
|
||||||
|
@YamlKey("synchronization.max_user_data_snapshots")
|
||||||
|
public int maxUserDataSnapshots = 5;
|
||||||
|
|
||||||
|
@YamlKey("synchronization.save_on_world_save")
|
||||||
|
public boolean saveOnWorldSave = true;
|
||||||
|
|
||||||
|
@YamlKey("synchronization.save_on_death")
|
||||||
|
public boolean saveOnDeath = false;
|
||||||
|
|
||||||
|
@YamlKey("synchronization.compress_data")
|
||||||
|
public boolean compressData = true;
|
||||||
|
|
||||||
|
@YamlKey("synchronization.save_dead_player_inventories")
|
||||||
|
public boolean saveDeadPlayerInventories = true;
|
||||||
|
|
||||||
|
@YamlKey("synchronization.network_latency_milliseconds")
|
||||||
|
public int networkLatencyMilliseconds = 500;
|
||||||
|
|
||||||
|
@YamlKey("synchronization.features")
|
||||||
|
public Map<String, Boolean> synchronizationFeatures = SynchronizationFeature.getDefaults();
|
||||||
|
|
||||||
|
public boolean getSynchronizationFeature(@NotNull SynchronizationFeature feature) {
|
||||||
|
return Optional.ofNullable(synchronizationFeatures.get(feature.name().toLowerCase()))
|
||||||
|
.orElse(feature.enabledByDefault);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an option stored by a path in config.yml
|
* Represents the names of tables in the database
|
||||||
*/
|
*/
|
||||||
public enum ConfigOption {
|
public enum TableName {
|
||||||
LANGUAGE("language", OptionType.STRING, "en-gb"),
|
USERS("husksync_users"),
|
||||||
CHECK_FOR_UPDATES("check_for_updates", OptionType.BOOLEAN, true),
|
USER_DATA("husksync_user_data");
|
||||||
|
|
||||||
CLUSTER_ID("cluster_id", OptionType.STRING, ""),
|
|
||||||
DEBUG_LOGGING("debug_logging", OptionType.BOOLEAN, false),
|
|
||||||
|
|
||||||
DATABASE_HOST("database.credentials.host", OptionType.STRING, "localhost"),
|
|
||||||
DATABASE_PORT("database.credentials.port", OptionType.INTEGER, 3306),
|
|
||||||
DATABASE_NAME("database.credentials.database", OptionType.STRING, "HuskSync"),
|
|
||||||
DATABASE_USERNAME("database.credentials.username", OptionType.STRING, "root"),
|
|
||||||
DATABASE_PASSWORD("database.credentials.password", OptionType.STRING, "pa55w0rd"),
|
|
||||||
DATABASE_CONNECTION_PARAMS("database.credentials.params", OptionType.STRING, "?autoReconnect=true&useSSL=false"),
|
|
||||||
DATABASE_CONNECTION_POOL_MAX_SIZE("database.connection_pool.maximum_pool_size", OptionType.INTEGER, 10),
|
|
||||||
DATABASE_CONNECTION_POOL_MIN_IDLE("database.connection_pool.minimum_idle", OptionType.INTEGER, 10),
|
|
||||||
DATABASE_CONNECTION_POOL_MAX_LIFETIME("database.connection_pool.maximum_lifetime", OptionType.INTEGER, 1800000),
|
|
||||||
DATABASE_CONNECTION_POOL_KEEPALIVE("database.connection_pool.keepalive_time", OptionType.INTEGER, 0),
|
|
||||||
DATABASE_CONNECTION_POOL_TIMEOUT("database.connection_pool.connection_timeout", OptionType.INTEGER, 5000),
|
|
||||||
DATABASE_USERS_TABLE_NAME("database.table_names.users_table", OptionType.STRING, "husksync_users"),
|
|
||||||
DATABASE_USER_DATA_TABLE_NAME("database.table_names.user_data_table", OptionType.STRING, "husksync_user_data"),
|
|
||||||
|
|
||||||
REDIS_HOST("redis.credentials.host", OptionType.STRING, "localhost"),
|
|
||||||
REDIS_PORT("redis.credentials.port", OptionType.INTEGER, 6379),
|
|
||||||
REDIS_PASSWORD("redis.credentials.password", OptionType.STRING, ""),
|
|
||||||
REDIS_USE_SSL("redis.use_ssl", OptionType.BOOLEAN, false),
|
|
||||||
|
|
||||||
SYNCHRONIZATION_MAX_USER_DATA_SNAPSHOTS("synchronization.max_user_data_snapshots", OptionType.INTEGER, 5),
|
|
||||||
SYNCHRONIZATION_SAVE_ON_WORLD_SAVE("synchronization.save_on_world_save", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_COMPRESS_DATA("synchronization.compress_data", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_NETWORK_LATENCY_MILLISECONDS("synchronization.network_latency_milliseconds", OptionType.INTEGER, 500),
|
|
||||||
SYNCHRONIZATION_SAVE_DEAD_PLAYER_INVENTORIES("synchronization.save_dead_player_inventories", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_INVENTORIES("synchronization.features.inventories", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_ENDER_CHESTS("synchronization.features.ender_chests", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_HEALTH("synchronization.features.health", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_MAX_HEALTH("synchronization.features.max_health", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_HUNGER("synchronization.features.hunger", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_EXPERIENCE("synchronization.features.experience", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_POTION_EFFECTS("synchronization.features.potion_effects", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_ADVANCEMENTS("synchronization.features.advancements", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_GAME_MODE("synchronization.features.game_mode", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_STATISTICS("synchronization.features.statistics", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_PERSISTENT_DATA_CONTAINER("synchronization.features.persistent_data_container", OptionType.BOOLEAN, true),
|
|
||||||
SYNCHRONIZATION_SYNC_LOCATION("synchronization.features.location", OptionType.BOOLEAN, true);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The path in the config.yml file to the value
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public final String configPath;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The {@link OptionType} of this option
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public final OptionType optionType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The default value of this option if not set in config
|
|
||||||
*/
|
|
||||||
@Nullable
|
|
||||||
private final Object defaultValue;
|
|
||||||
|
|
||||||
ConfigOption(@NotNull String configPath, @NotNull OptionType optionType, @Nullable Object defaultValue) {
|
|
||||||
this.configPath = configPath;
|
|
||||||
this.optionType = optionType;
|
|
||||||
this.defaultValue = defaultValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
ConfigOption(@NotNull String configPath, @NotNull OptionType optionType) {
|
private final String defaultName;
|
||||||
this.configPath = configPath;
|
|
||||||
this.optionType = optionType;
|
|
||||||
this.defaultValue = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the value at the path specified (or return default if set), as a string
|
|
||||||
*
|
|
||||||
* @param config The {@link YamlDocument} config file
|
|
||||||
* @return the value defined in the config, as a string
|
|
||||||
*/
|
|
||||||
public String getStringValue(@NotNull YamlDocument config) {
|
|
||||||
return defaultValue != null
|
|
||||||
? config.getString(configPath, (String) defaultValue)
|
|
||||||
: config.getString(configPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
TableName(@NotNull String defaultName) {
|
||||||
* Get the value at the path specified (or return default if set), as a boolean
|
this.defaultName = defaultName;
|
||||||
*
|
|
||||||
* @param config The {@link YamlDocument} config file
|
|
||||||
* @return the value defined in the config, as a boolean
|
|
||||||
*/
|
|
||||||
public boolean getBooleanValue(@NotNull YamlDocument config) {
|
|
||||||
return defaultValue != null
|
|
||||||
? config.getBoolean(configPath, (Boolean) defaultValue)
|
|
||||||
: config.getBoolean(configPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private Map.Entry<String, String> toEntry() {
|
||||||
* Get the value at the path specified (or return default if set), as a double
|
return Map.entry(name().toLowerCase(), defaultName);
|
||||||
*
|
|
||||||
* @param config The {@link YamlDocument} config file
|
|
||||||
* @return the value defined in the config, as a double
|
|
||||||
*/
|
|
||||||
public double getDoubleValue(@NotNull YamlDocument config) {
|
|
||||||
return defaultValue != null
|
|
||||||
? config.getDouble(configPath, (Double) defaultValue)
|
|
||||||
: config.getDouble(configPath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@SuppressWarnings("unchecked")
|
||||||
* Get the value at the path specified (or return default if set), as a float
|
private static Map<String, String> getDefaults() {
|
||||||
*
|
return Map.ofEntries(Arrays.stream(values())
|
||||||
* @param config The {@link YamlDocument} config file
|
.map(TableName::toEntry)
|
||||||
* @return the value defined in the config, as a float
|
.toArray(Map.Entry[]::new));
|
||||||
*/
|
|
||||||
public float getFloatValue(@NotNull YamlDocument config) {
|
|
||||||
return defaultValue != null
|
|
||||||
? config.getFloat(configPath, (Float) defaultValue)
|
|
||||||
: config.getFloat(configPath);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the value at the path specified (or return default if set), as an int
|
* Represents enabled synchronisation features
|
||||||
*
|
*/
|
||||||
* @param config The {@link YamlDocument} config file
|
public enum SynchronizationFeature {
|
||||||
* @return the value defined in the config, as an int
|
|
||||||
*/
|
INVENTORIES(true),
|
||||||
public int getIntValue(@NotNull YamlDocument config) {
|
ENDER_CHESTS(true),
|
||||||
return defaultValue != null
|
HEALTH(true),
|
||||||
? config.getInt(configPath, (Integer) defaultValue)
|
MAX_HEALTH(true),
|
||||||
: config.getInt(configPath);
|
HUNGER(true),
|
||||||
|
EXPERIENCE(true),
|
||||||
|
POTION_EFFECTS(true),
|
||||||
|
ADVANCEMENTS(true),
|
||||||
|
GAME_MODE(true),
|
||||||
|
STATISTICS(true),
|
||||||
|
PERSISTENT_DATA_CONTAINER(false),
|
||||||
|
LOCATION(false);
|
||||||
|
|
||||||
|
private final boolean enabledByDefault;
|
||||||
|
|
||||||
|
SynchronizationFeature(boolean enabledByDefault) {
|
||||||
|
this.enabledByDefault = enabledByDefault;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private Map.Entry<String, Boolean> toEntry() {
|
||||||
* Get the value at the path specified (or return default if set), as a string {@link List}
|
return Map.entry(name().toLowerCase(), enabledByDefault);
|
||||||
*
|
|
||||||
* @param config The {@link YamlDocument} config file
|
|
||||||
* @return the value defined in the config, as a string {@link List}
|
|
||||||
*/
|
|
||||||
public List<String> getStringListValue(@NotNull YamlDocument config) {
|
|
||||||
return config.getStringList(configPath, new ArrayList<>());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@SuppressWarnings("unchecked")
|
||||||
* Represents the type of the object
|
private static Map<String, Boolean> getDefaults() {
|
||||||
*/
|
return Map.ofEntries(Arrays.stream(values())
|
||||||
public enum OptionType {
|
.map(SynchronizationFeature::toEntry)
|
||||||
BOOLEAN,
|
.toArray(Map.Entry[]::new));
|
||||||
STRING,
|
|
||||||
DOUBLE,
|
|
||||||
FLOAT,
|
|
||||||
INTEGER,
|
|
||||||
STRING_LIST
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue