1
0
mirror of https://github.com/lucko/LuckPerms.git synced 2025-09-25 21:38:59 +02:00

Refactor storage type config read

This commit is contained in:
Luck
2019-04-23 21:57:43 +01:00
parent 494ab6787f
commit 2c62de9658
6 changed files with 25 additions and 55 deletions

View File

@@ -89,10 +89,8 @@ public class MigrationPowerfulPerms extends SubCommand<Object> {
return CommandResult.STATE_ERROR; return CommandResult.STATE_ERROR;
} }
String method = plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD); StorageType type = plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
StorageType type = StorageType.parse(method); if (type != StorageType.MYSQL) {
if (type == null || type != StorageType.MYSQL) {
// We need to load the Hikari/MySQL stuff. // We need to load the Hikari/MySQL stuff.
plugin.getDependencyManager().loadStorageDependencies(ImmutableSet.of(StorageType.MYSQL)); plugin.getDependencyManager().loadStorageDependencies(ImmutableSet.of(StorageType.MYSQL));
} }

View File

@@ -72,7 +72,7 @@ public class ApiConfiguration implements LPConfiguration {
@Override @Override
public @NonNull String getStorageMethod() { public @NonNull String getStorageMethod() {
return this.handle.get(ConfigKeys.STORAGE_METHOD); return this.handle.get(ConfigKeys.STORAGE_METHOD).getName();
} }
@Override @Override
@@ -83,7 +83,7 @@ public class ApiConfiguration implements LPConfiguration {
@Override @Override
public @NonNull Map<String, String> getSplitStorageOptions() { public @NonNull Map<String, String> getSplitStorageOptions() {
return this.handle.get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream() return this.handle.get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream()
.collect(ImmutableCollectors.toMap(e -> e.getKey().name().toLowerCase(), Map.Entry::getValue)); .collect(ImmutableCollectors.toMap(e -> e.getKey().name().toLowerCase(), e -> e.getValue().getName()));
} }
@Override @Override

View File

@@ -43,6 +43,7 @@ import me.lucko.luckperms.common.primarygroup.AllParentsByWeightHolder;
import me.lucko.luckperms.common.primarygroup.ParentsByWeightHolder; import me.lucko.luckperms.common.primarygroup.ParentsByWeightHolder;
import me.lucko.luckperms.common.primarygroup.PrimaryGroupHolder; import me.lucko.luckperms.common.primarygroup.PrimaryGroupHolder;
import me.lucko.luckperms.common.primarygroup.StoredHolder; import me.lucko.luckperms.common.primarygroup.StoredHolder;
import me.lucko.luckperms.common.storage.StorageType;
import me.lucko.luckperms.common.storage.implementation.split.SplitStorageType; import me.lucko.luckperms.common.storage.implementation.split.SplitStorageType;
import me.lucko.luckperms.common.storage.misc.StorageCredentials; import me.lucko.luckperms.common.storage.misc.StorageCredentials;
import me.lucko.luckperms.common.util.ImmutableCollectors; import me.lucko.luckperms.common.util.ImmutableCollectors;
@@ -474,7 +475,9 @@ public final class ConfigKeys {
/** /**
* The name of the storage method being used * The name of the storage method being used
*/ */
public static final ConfigKey<String> STORAGE_METHOD = enduringKey(lowercaseStringKey("storage-method", "h2")); public static final ConfigKey<StorageType> STORAGE_METHOD = enduringKey(customKey(c -> {
return StorageType.parse(c.getString("storage-method", "h2"), StorageType.H2);
}));
/** /**
* If storage files should be monitored for changes * If storage files should be monitored for changes
@@ -489,13 +492,13 @@ public final class ConfigKeys {
/** /**
* The options for split storage * The options for split storage
*/ */
public static final ConfigKey<Map<SplitStorageType, String>> SPLIT_STORAGE_OPTIONS = enduringKey(customKey(c -> { public static final ConfigKey<Map<SplitStorageType, StorageType>> SPLIT_STORAGE_OPTIONS = enduringKey(customKey(c -> {
EnumMap<SplitStorageType, String> map = new EnumMap<>(SplitStorageType.class); EnumMap<SplitStorageType, StorageType> map = new EnumMap<>(SplitStorageType.class);
map.put(SplitStorageType.USER, c.getString("split-storage.methods.user", "h2").toLowerCase()); map.put(SplitStorageType.USER, StorageType.parse(c.getString("split-storage.methods.user", "h2"), StorageType.H2));
map.put(SplitStorageType.GROUP, c.getString("split-storage.methods.group", "h2").toLowerCase()); map.put(SplitStorageType.GROUP, StorageType.parse(c.getString("split-storage.methods.group", "h2"), StorageType.H2));
map.put(SplitStorageType.TRACK, c.getString("split-storage.methods.track", "h2").toLowerCase()); map.put(SplitStorageType.TRACK, StorageType.parse(c.getString("split-storage.methods.track", "h2"), StorageType.H2));
map.put(SplitStorageType.UUID, c.getString("split-storage.methods.uuid", "h2").toLowerCase()); map.put(SplitStorageType.UUID, StorageType.parse(c.getString("split-storage.methods.uuid", "h2"), StorageType.H2));
map.put(SplitStorageType.LOG, c.getString("split-storage.methods.log", "h2").toLowerCase()); map.put(SplitStorageType.LOG, StorageType.parse(c.getString("split-storage.methods.log", "h2"), StorageType.H2));
return ImmutableMap.copyOf(map); return ImmutableMap.copyOf(map);
})); }));

View File

@@ -115,7 +115,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
// now the configuration is loaded, we can create a storage factory and load initial dependencies // now the configuration is loaded, we can create a storage factory and load initial dependencies
StorageFactory storageFactory = new StorageFactory(this); StorageFactory storageFactory = new StorageFactory(this);
Set<StorageType> storageTypes = storageFactory.getRequiredTypes(StorageType.H2); Set<StorageType> storageTypes = storageFactory.getRequiredTypes();
this.dependencyManager.loadStorageDependencies(storageTypes); this.dependencyManager.loadStorageDependencies(storageTypes);
// register listeners // register listeners
@@ -132,7 +132,7 @@ public abstract class AbstractLuckPermsPlugin implements LuckPermsPlugin {
} }
// initialise storage // initialise storage
this.storage = storageFactory.getInstance(StorageType.H2); this.storage = storageFactory.getInstance();
this.messagingService = provideMessagingFactory().getInstance(); this.messagingService = provideMessagingFactory().getInstance();
// setup the update task buffer // setup the update task buffer

View File

@@ -26,7 +26,6 @@
package me.lucko.luckperms.common.storage; package me.lucko.luckperms.common.storage;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
@@ -59,45 +58,20 @@ public class StorageFactory {
this.plugin = plugin; this.plugin = plugin;
} }
public Set<StorageType> getRequiredTypes(StorageType defaultMethod) { public Set<StorageType> getRequiredTypes() {
if (this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) { if (this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) {
return this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream() return ImmutableSet.copyOf(this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).values());
.map(e -> {
StorageType type = StorageType.parse(e.getValue());
if (type == null) {
this.plugin.getLogger().severe("Storage method for " + e.getKey() + " - " + e.getValue() + " not recognised. " +
"Using the default instead.");
type = defaultMethod;
}
return type;
})
.collect(ImmutableCollectors.toEnumSet(StorageType.class));
} else { } else {
String method = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD); return ImmutableSet.of(this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD));
StorageType type = StorageType.parse(method);
if (type == null) {
this.plugin.getLogger().severe("Storage method '" + method + "' not recognised. Using the default instead.");
type = defaultMethod;
}
return ImmutableSet.of(type);
} }
} }
public Storage getInstance(StorageType defaultMethod) { public Storage getInstance() {
Storage storage; Storage storage;
if (this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) { if (this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE)) {
this.plugin.getLogger().info("Loading storage provider... [SPLIT STORAGE]"); this.plugin.getLogger().info("Loading storage provider... [SPLIT STORAGE]");
Map<SplitStorageType, StorageType> mappedTypes = this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS).entrySet().stream() Map<SplitStorageType, StorageType> mappedTypes = this.plugin.getConfiguration().get(ConfigKeys.SPLIT_STORAGE_OPTIONS);
.map(e -> {
StorageType type = StorageType.parse(e.getValue());
if (type == null) {
type = defaultMethod;
}
return Maps.immutableEntry(e.getKey(), type);
})
.collect(ImmutableCollectors.toEnumMap(SplitStorageType.class, Map.Entry::getKey, Map.Entry::getValue));
Map<StorageType, StorageImplementation> backing = mappedTypes.values().stream() Map<StorageType, StorageImplementation> backing = mappedTypes.values().stream()
.distinct() .distinct()
.collect(ImmutableCollectors.toEnumMap(StorageType.class, e -> e, this::createNewImplementation)); .collect(ImmutableCollectors.toEnumMap(StorageType.class, e -> e, this::createNewImplementation));
@@ -106,12 +80,7 @@ public class StorageFactory {
storage = new Storage(this.plugin, new SplitStorage(this.plugin, backing, mappedTypes)); storage = new Storage(this.plugin, new SplitStorage(this.plugin, backing, mappedTypes));
} else { } else {
String method = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD); StorageType type = this.plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
StorageType type = StorageType.parse(method);
if (type == null) {
type = defaultMethod;
}
this.plugin.getLogger().info("Loading storage provider... [" + type.name() + "]"); this.plugin.getLogger().info("Loading storage provider... [" + type.name() + "]");
storage = makeInstance(type); storage = makeInstance(type);
} }

View File

@@ -63,7 +63,7 @@ public enum StorageType {
this.identifiers = ImmutableList.copyOf(identifiers); this.identifiers = ImmutableList.copyOf(identifiers);
} }
public static StorageType parse(String name) { public static StorageType parse(String name, StorageType def) {
for (StorageType t : values()) { for (StorageType t : values()) {
for (String id : t.getIdentifiers()) { for (String id : t.getIdentifiers()) {
if (id.equalsIgnoreCase(name)) { if (id.equalsIgnoreCase(name)) {
@@ -71,7 +71,7 @@ public enum StorageType {
} }
} }
} }
return null; return def;
} }
public String getName() { public String getName() {