From 35c236e9ce48f610aa2efcd5fd08d2ebd4a11219 Mon Sep 17 00:00:00 2001 From: "Lukas Rieger (Blue)" Date: Sat, 24 Feb 2024 14:11:56 +0100 Subject: [PATCH] Turn storage-enum into a registry --- .../bluemap/common/BlueMapService.java | 2 +- .../common/config/storage/StorageConfig.java | 25 ++++++++++++++++--- .../common/config/storage/StorageType.java | 23 ++++++++++++++--- .../common/plugin/commands/Commands.java | 2 +- .../bluemap/config/storages/file.conf | 2 +- .../bluemap/config/storages/sql.conf | 2 +- 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/BlueMapService.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/BlueMapService.java index 94c39c08..839118b6 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/BlueMapService.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/BlueMapService.java @@ -287,7 +287,7 @@ public synchronized Storage getOrLoadStorage(String storageId) throws Configurat "You will either need to define that storage, or change the map-config to use a storage-config that exists."); } - Logger.global.logInfo("Initializing Storage: '" + storageId + "' (Type: " + storageConfig.getStorageType() + ")"); + Logger.global.logInfo("Initializing Storage: '" + storageId + "' (Type: '" + storageConfig.getStorageType().getKey() + "')"); storage = storageConfig.createStorage(); storage.initialize(); diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/StorageConfig.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/StorageConfig.java index a617df53..ba850570 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/StorageConfig.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/StorageConfig.java @@ -25,25 +25,44 @@ package de.bluecolored.bluemap.common.config.storage; import de.bluecolored.bluemap.api.debug.DebugDump; +import de.bluecolored.bluemap.common.config.ConfigurationException; import de.bluecolored.bluemap.core.storage.Storage; +import de.bluecolored.bluemap.core.util.Key; import org.spongepowered.configurate.objectmapping.ConfigSerializable; +import java.util.Locale; + @SuppressWarnings("FieldMayBeFinal") @DebugDump @ConfigSerializable public class StorageConfig { - private StorageType storageType = StorageType.FILE; + private Key storageType = StorageType.FILE.getKey(); - public StorageType getStorageType() { + public Key getStorageTypeKey() { return storageType; } + public StorageType getStorageType() throws ConfigurationException { + StorageType type = StorageType.REGISTRY.get(storageType); + + if (type == null) { + // try legacy config format + Key legacyFormatKey = Key.bluemap(storageType.getValue().toLowerCase(Locale.ROOT)); + type = StorageType.REGISTRY.get(legacyFormatKey); + } + + if (type == null) + throw new ConfigurationException("No storage-type found for key: " + storageType + "!"); + + return type; + } + public Storage createStorage() throws Exception { if (this.getClass().equals(StorageConfig.class)) throw new UnsupportedOperationException("Can not create a Storage from the StorageConfig superclass."); - return storageType.getStorageFactory(this.getClass()).provide(this); + return getStorageType().getStorageFactory(this.getClass()).provide(this); } } diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/StorageType.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/StorageType.java index 20a24e94..469062fe 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/StorageType.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/config/storage/StorageType.java @@ -27,20 +27,35 @@ import de.bluecolored.bluemap.core.storage.Storage; import de.bluecolored.bluemap.core.storage.file.FileStorage; import de.bluecolored.bluemap.core.storage.sql.SQLStorage; +import de.bluecolored.bluemap.core.util.Key; +import de.bluecolored.bluemap.core.util.Keyed; +import de.bluecolored.bluemap.core.util.Registry; -public enum StorageType { +public class StorageType implements Keyed { - FILE (FileConfig.class, FileStorage::new), - SQL (SQLConfig.class, SQLStorage::create); + public static final StorageType FILE = new StorageType( Key.bluemap("file"), FileConfig.class, FileStorage::new ); + public static final StorageType SQL = new StorageType( Key.bluemap("sql"), SQLConfig.class, SQLStorage::create ); + public static final Registry REGISTRY = new Registry<>( + FILE, + SQL + ); + + private final Key key; private final Class configType; private final StorageFactory storageFactory; - StorageType(Class configType, StorageFactory storageFactory) { + public StorageType(Key key, Class configType, StorageFactory storageFactory) { + this.key = key; this.configType = configType; this.storageFactory = storageFactory; } + @Override + public Key getKey() { + return key; + } + public Class getConfigType() { return configType; } diff --git a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java index 20f550ff..4c7078e5 100644 --- a/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java +++ b/BlueMapCommon/src/main/java/de/bluecolored/bluemap/common/plugin/commands/Commands.java @@ -887,7 +887,7 @@ public int storagesCommand(CommandContext context) { source.sendMessage(Text.of(TextColor.BLUE, "Storages loaded by BlueMap:")); for (var entry : plugin.getBlueMap().getConfig().getStorageConfigs().entrySet()) { source.sendMessage(Text.of(TextColor.GRAY, " - ", TextColor.WHITE, entry.getKey()) - .setHoverText(Text.of(entry.getValue().getStorageType().name())) + .setHoverText(Text.of(entry.getValue().getStorageTypeKey().getFormatted())) .setClickAction(Text.ClickAction.RUN_COMMAND, "/bluemap storages " + entry.getKey()) ); } diff --git a/BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/file.conf b/BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/file.conf index e54f6514..f0fc2388 100644 --- a/BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/file.conf +++ b/BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/file.conf @@ -6,7 +6,7 @@ # The storage-type of this storage. # Depending on this setting, different config-entries are allowed/expected in this config file. # Don't change this value! (If you want a different storage-type, check out the other example-configs) -storage-type: FILE +storage-type: "bluemap:file" # The path to the folder on your file-system where bluemap will save the rendered map # The default is: "bluemap/web/maps" diff --git a/BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/sql.conf b/BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/sql.conf index 236698de..7f4eee3a 100644 --- a/BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/sql.conf +++ b/BlueMapCommon/src/main/resources/de/bluecolored/bluemap/config/storages/sql.conf @@ -6,7 +6,7 @@ # The storage-type of this storage. # Depending on this setting, different config-entries are allowed/expected in this config file. # Don't change this value! (If you want a different storage-type, check out the other example-configs) -storage-type: SQL +storage-type: "bluemap:sql" # The JDBC-Connection URL that is used to connect to the database. # The format for this url is usually something like: jdbc:[driver]://[host]:[port]/[database]