diff --git a/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java b/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java index 0ec176f07..069c7abcb 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/AbstractConfiguration.java @@ -69,11 +69,11 @@ public class AbstractConfiguration implements LuckPermsConfiguration { // if values are null, must be loading for the first time if (this.values == null) { - this.values = new Object[ConfigKeys.size()]; + this.values = new Object[ConfigKeys.getKeys().size()]; reload = false; } - for (ConfigKey key : ConfigKeys.getKeys().values()) { + for (ConfigKey key : ConfigKeys.getKeys()) { // don't reload enduring keys. if (reload && key instanceof ConfigKeyTypes.EnduringKey) { continue; diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index fbb342f24..e5b639e44 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -46,12 +46,11 @@ import net.luckperms.api.query.Flag; import net.luckperms.api.query.QueryMode; import net.luckperms.api.query.QueryOptions; -import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Arrays; import java.util.EnumMap; import java.util.EnumSet; -import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; @@ -539,60 +538,35 @@ public final class ConfigKeys { */ public static final ConfigKey TREE_VIEWER_URL_PATTERN = stringKey("tree-viewer-url", "https://luckperms.net/treeview/#"); - private static final Map> KEYS; - private static final int SIZE; + private static final List> KEYS; static { - Map> keys = new LinkedHashMap<>(); - Field[] values = ConfigKeys.class.getFields(); - int i = 0; + // get a list of all keys + KEYS = Arrays.stream(ConfigKeys.class.getFields()) + .filter(f -> Modifier.isStatic(f.getModifiers())) + .filter(f -> ConfigKey.class.equals(f.getType())) + .map(f -> { + try { + return (ConfigKeyTypes.BaseConfigKey) f.get(null); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + }) + .collect(ImmutableCollectors.toList()); - for (Field f : values) { - // ignore non-static fields - if (!Modifier.isStatic(f.getModifiers())) { - continue; - } - - // ignore fields that aren't configkeys - if (!ConfigKey.class.equals(f.getType())) { - continue; - } - - try { - // get the key instance - ConfigKeyTypes.BaseConfigKey key = (ConfigKeyTypes.BaseConfigKey) f.get(null); - // set the ordinal value of the key. - key.ordinal = i++; - // add the key to the return map - keys.put(f.getName(), key); - } catch (Exception e) { - throw new RuntimeException("Exception processing field: " + f, e); - } + // set ordinal values + for (int i = 0; i < KEYS.size(); i++) { + KEYS.get(i).ordinal = i; } - - KEYS = ImmutableMap.copyOf(keys); - SIZE = i; } /** - * Gets a map of the keys defined in this class. - * - *

The string key in the map is the {@link Field#getName() field name} - * corresponding to each key.

+ * Gets a list of the keys defined in this class. * * @return the defined keys */ - public static Map> getKeys() { + public static List> getKeys() { return KEYS; } - /** - * Gets the number of defined keys. - * - * @return how many keys are defined in this class - */ - public static int size() { - return SIZE; - } - }