Turn storage-enum into a registry

This commit is contained in:
Lukas Rieger (Blue) 2024-02-24 14:11:56 +01:00
parent 79ea7baba7
commit 35c236e9ce
No known key found for this signature in database
GPG Key ID: AA33883B1BBA03E6
6 changed files with 45 additions and 11 deletions

View File

@ -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."); "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 = storageConfig.createStorage();
storage.initialize(); storage.initialize();

View File

@ -25,25 +25,44 @@
package de.bluecolored.bluemap.common.config.storage; package de.bluecolored.bluemap.common.config.storage;
import de.bluecolored.bluemap.api.debug.DebugDump; 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.storage.Storage;
import de.bluecolored.bluemap.core.util.Key;
import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import java.util.Locale;
@SuppressWarnings("FieldMayBeFinal") @SuppressWarnings("FieldMayBeFinal")
@DebugDump @DebugDump
@ConfigSerializable @ConfigSerializable
public class StorageConfig { public class StorageConfig {
private StorageType storageType = StorageType.FILE; private Key storageType = StorageType.FILE.getKey();
public StorageType getStorageType() { public Key getStorageTypeKey() {
return storageType; 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 { public Storage createStorage() throws Exception {
if (this.getClass().equals(StorageConfig.class)) if (this.getClass().equals(StorageConfig.class))
throw new UnsupportedOperationException("Can not create a Storage from the StorageConfig superclass."); 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);
} }
} }

View File

@ -27,20 +27,35 @@
import de.bluecolored.bluemap.core.storage.Storage; import de.bluecolored.bluemap.core.storage.Storage;
import de.bluecolored.bluemap.core.storage.file.FileStorage; import de.bluecolored.bluemap.core.storage.file.FileStorage;
import de.bluecolored.bluemap.core.storage.sql.SQLStorage; 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), public static final StorageType FILE = new StorageType( Key.bluemap("file"), FileConfig.class, FileStorage::new );
SQL (SQLConfig.class, SQLStorage::create); public static final StorageType SQL = new StorageType( Key.bluemap("sql"), SQLConfig.class, SQLStorage::create );
public static final Registry<StorageType> REGISTRY = new Registry<>(
FILE,
SQL
);
private final Key key;
private final Class<? extends StorageConfig> configType; private final Class<? extends StorageConfig> configType;
private final StorageFactory<? extends StorageConfig> storageFactory; private final StorageFactory<? extends StorageConfig> storageFactory;
<C extends StorageConfig> StorageType(Class<C> configType, StorageFactory<C> storageFactory) { public <C extends StorageConfig> StorageType(Key key, Class<C> configType, StorageFactory<C> storageFactory) {
this.key = key;
this.configType = configType; this.configType = configType;
this.storageFactory = storageFactory; this.storageFactory = storageFactory;
} }
@Override
public Key getKey() {
return key;
}
public Class<? extends StorageConfig> getConfigType() { public Class<? extends StorageConfig> getConfigType() {
return configType; return configType;
} }

View File

@ -887,7 +887,7 @@ public int storagesCommand(CommandContext<S> context) {
source.sendMessage(Text.of(TextColor.BLUE, "Storages loaded by BlueMap:")); source.sendMessage(Text.of(TextColor.BLUE, "Storages loaded by BlueMap:"));
for (var entry : plugin.getBlueMap().getConfig().getStorageConfigs().entrySet()) { for (var entry : plugin.getBlueMap().getConfig().getStorageConfigs().entrySet()) {
source.sendMessage(Text.of(TextColor.GRAY, " - ", TextColor.WHITE, entry.getKey()) 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()) .setClickAction(Text.ClickAction.RUN_COMMAND, "/bluemap storages " + entry.getKey())
); );
} }

View File

@ -6,7 +6,7 @@
# The storage-type of this storage. # The storage-type of this storage.
# Depending on this setting, different config-entries are allowed/expected in this config file. # 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) # 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 path to the folder on your file-system where bluemap will save the rendered map
# The default is: "bluemap/web/maps" # The default is: "bluemap/web/maps"

View File

@ -6,7 +6,7 @@
# The storage-type of this storage. # The storage-type of this storage.
# Depending on this setting, different config-entries are allowed/expected in this config file. # 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) # 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 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] # The format for this url is usually something like: jdbc:[driver]://[host]:[port]/[database]