diff --git a/api/src/main/java/net/luckperms/api/metastacking/MetaStackDefinition.java b/api/src/main/java/net/luckperms/api/metastacking/MetaStackDefinition.java index 31d1ce5ef..062f61bbc 100644 --- a/api/src/main/java/net/luckperms/api/metastacking/MetaStackDefinition.java +++ b/api/src/main/java/net/luckperms/api/metastacking/MetaStackDefinition.java @@ -45,12 +45,12 @@ public interface MetaStackDefinition { /** * The {@link OptionKey} for the prefix {@link MetaStackDefinition}. */ - OptionKey PREFIX_STACK_KEY = new OptionKey(){}; + OptionKey PREFIX_STACK_KEY = OptionKey.of("prefixstack", MetaStackDefinition.class); /** * The {@link OptionKey} for the suffix {@link MetaStackDefinition}. */ - OptionKey SUFFIX_STACK_KEY = new OptionKey(){}; + OptionKey SUFFIX_STACK_KEY = OptionKey.of("suffixstack", MetaStackDefinition.class); /** * Gets an immutable list of the elements in this stack definition diff --git a/api/src/main/java/net/luckperms/api/node/metadata/NodeMetadataKey.java b/api/src/main/java/net/luckperms/api/node/metadata/NodeMetadataKey.java index daedb3309..b87dcb9f1 100644 --- a/api/src/main/java/net/luckperms/api/node/metadata/NodeMetadataKey.java +++ b/api/src/main/java/net/luckperms/api/node/metadata/NodeMetadataKey.java @@ -32,13 +32,10 @@ import java.util.Objects; /** * Represents a key for a specific type of node metadata. * - *

Metadata keys are compared using reference equality, the - * {@link #equals(Object)} method should not be implemented.

- * *

It is intended that {@link NodeMetadataKey}s are obtained and stored as * follows.

*

- *     public static final NodeMetadataKey SPECIAL_METADATA_KEY = NodeMetadataKey.of("SpecialMetadata", SpecialMetadata.class);
+ *     public static final NodeMetadataKey SPECIAL_METADATA_KEY = NodeMetadataKey.of("specialmetadata", SpecialMetadata.class);
  * 

* * @param the metadata type diff --git a/api/src/main/java/net/luckperms/api/node/metadata/SimpleNodeMetadataKey.java b/api/src/main/java/net/luckperms/api/node/metadata/SimpleNodeMetadataKey.java index b34eba49a..ff1cbcace 100644 --- a/api/src/main/java/net/luckperms/api/node/metadata/SimpleNodeMetadataKey.java +++ b/api/src/main/java/net/luckperms/api/node/metadata/SimpleNodeMetadataKey.java @@ -27,12 +27,14 @@ package net.luckperms.api.node.metadata; import org.checkerframework.checker.nullness.qual.NonNull; +import java.util.Objects; + final class SimpleNodeMetadataKey implements NodeMetadataKey { private final String name; private final Class type; SimpleNodeMetadataKey(String name, Class type) { - this.name = name; + this.name = name.toLowerCase(); this.type = type; } @@ -50,4 +52,18 @@ final class SimpleNodeMetadataKey implements NodeMetadataKey { public String toString() { return "NodeMetadataKey(name=" + this.name + ", type=" + this.type.getName() + ")"; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SimpleNodeMetadataKey that = (SimpleNodeMetadataKey) o; + return this.name.equals(that.name) && + this.type.equals(that.type); + } + + @Override + public int hashCode() { + return Objects.hash(this.name, this.type); + } } diff --git a/api/src/main/java/net/luckperms/api/node/metadata/types/InheritanceOriginMetadata.java b/api/src/main/java/net/luckperms/api/node/metadata/types/InheritanceOriginMetadata.java index 3084ae15f..5c1d7014c 100644 --- a/api/src/main/java/net/luckperms/api/node/metadata/types/InheritanceOriginMetadata.java +++ b/api/src/main/java/net/luckperms/api/node/metadata/types/InheritanceOriginMetadata.java @@ -39,7 +39,7 @@ public interface InheritanceOriginMetadata { /** * The {@link NodeMetadataKey} for {@link InheritanceOriginMetadata}. */ - NodeMetadataKey KEY = NodeMetadataKey.of("InheritanceOrigin", InheritanceOriginMetadata.class); + NodeMetadataKey KEY = NodeMetadataKey.of("inheritanceorigin", InheritanceOriginMetadata.class); /** * Gets the location where the {@link Node} is inherited from. diff --git a/api/src/main/java/net/luckperms/api/query/OptionKey.java b/api/src/main/java/net/luckperms/api/query/OptionKey.java index 89830a18f..918275fcd 100644 --- a/api/src/main/java/net/luckperms/api/query/OptionKey.java +++ b/api/src/main/java/net/luckperms/api/query/OptionKey.java @@ -25,19 +25,52 @@ package net.luckperms.api.query; +import net.luckperms.api.node.metadata.NodeMetadataKey; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.Objects; + /** * Represents a key for a custom option defined in {@link QueryOptions}. * - *

Option keys are compared using reference equality, the - * {@link #equals(Object)} method should not be implemented.

- * *

It is intended that {@link OptionKey}s are created and defined as follows.

*

- *     public static final OptionKey SPECIAL_OPTION = new OptionKey(){};
+ *     public static final OptionKey SPECIAL_OPTION = OptionKey.of("special", String.class);
  * 

* - * @param the option type + * @param the option type */ -public interface OptionKey { +public interface OptionKey { + + /** + * Creates a new {@link NodeMetadataKey} for the given name and type. + * + *

Note that the returned key implements object reference equality.

+ * + * @param name the name + * @param type the type + * @param the type parameter + * @return the key + */ + static @NonNull OptionKey of(@NonNull String name, @NonNull Class type) { + Objects.requireNonNull(name, "name"); + Objects.requireNonNull(type, "type"); + return new SimpleOptionKey<>(name, type); + } + + /** + * Gets a name describing the key type. + * + * @return the key name + */ + @NonNull String name(); + + /** + * Gets the type of the key + * + * @return the type + */ + @NonNull Class getType(); } diff --git a/api/src/main/java/net/luckperms/api/query/SimpleOptionKey.java b/api/src/main/java/net/luckperms/api/query/SimpleOptionKey.java new file mode 100644 index 000000000..d65ede9fb --- /dev/null +++ b/api/src/main/java/net/luckperms/api/query/SimpleOptionKey.java @@ -0,0 +1,69 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.luckperms.api.query; + +import org.checkerframework.checker.nullness.qual.NonNull; + +import java.util.Objects; + +final class SimpleOptionKey implements OptionKey { + private final String name; + private final Class type; + + SimpleOptionKey(String name, Class type) { + this.name = name.toLowerCase(); + this.type = type; + } + + @Override + public @NonNull String name() { + return this.name; + } + + @Override + public @NonNull Class getType() { + return this.type; + } + + @Override + public String toString() { + return "OptionKey(name=" + this.name + ", type=" + this.type.getName() + ")"; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SimpleOptionKey that = (SimpleOptionKey) o; + return this.name.equals(that.name) && + this.type.equals(that.type); + } + + @Override + public int hashCode() { + return Objects.hash(this.name, this.type); + } +} diff --git a/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrderFunction.java b/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrderFunction.java index 96a01f144..ee13c2689 100644 --- a/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrderFunction.java +++ b/api/src/main/java/net/luckperms/api/query/dataorder/DataQueryOrderFunction.java @@ -42,7 +42,7 @@ public interface DataQueryOrderFunction { /** * The {@link OptionKey} for {@link DataQueryOrderFunction}. */ - OptionKey KEY = new OptionKey(){}; + OptionKey KEY = OptionKey.of("dataqueryorderfunction", DataQueryOrderFunction.class); /** * Gets the {@link DataQueryOrder} comparator for the given diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/context/BukkitContextManager.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/context/BukkitContextManager.java index a0ec415e0..5cdc4c912 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/context/BukkitContextManager.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/context/BukkitContextManager.java @@ -45,7 +45,7 @@ import java.util.concurrent.TimeUnit; public class BukkitContextManager extends ContextManager { - public static final OptionKey OP_OPTION = new OptionKey(){}; + public static final OptionKey OP_OPTION = OptionKey.of("op", Boolean.class); // cache the creation of ContextsCache instances for online players with no expiry private final LoadingMap> onlineSubjectCaches = LoadingMap.of(key -> new QueryOptionsCache<>(key, this)); diff --git a/bukkit/src/main/java/me/lucko/luckperms/bukkit/inject/permissible/LPPermissionAttachment.java b/bukkit/src/main/java/me/lucko/luckperms/bukkit/inject/permissible/LPPermissionAttachment.java index bd974b203..8d1051b09 100644 --- a/bukkit/src/main/java/me/lucko/luckperms/bukkit/inject/permissible/LPPermissionAttachment.java +++ b/bukkit/src/main/java/me/lucko/luckperms/bukkit/inject/permissible/LPPermissionAttachment.java @@ -56,7 +56,7 @@ import java.util.Set; */ public class LPPermissionAttachment extends PermissionAttachment { - public static final NodeMetadataKey TRANSIENT_SOURCE_KEY = NodeMetadataKey.of("TransientSource", LPPermissionAttachment.class); + public static final NodeMetadataKey TRANSIENT_SOURCE_KEY = NodeMetadataKey.of("transientsource", LPPermissionAttachment.class); /** * The field in PermissionAttachment where the attachments applied permissions diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ContextsFile.java b/common/src/main/java/me/lucko/luckperms/common/config/ContextsFile.java index 2e8c89556..208c71a13 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ContextsFile.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ContextsFile.java @@ -56,29 +56,14 @@ public class ContextsFile { public void load() { Path file = this.configuration.getPlugin().getBootstrap().getDataDirectory().resolve("contexts.json"); - Path oldFile = this.configuration.getPlugin().getBootstrap().getDataDirectory().resolve("static-contexts.json"); - if (Files.exists(oldFile)) { - try { - Files.move(oldFile, file); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (!Files.exists(file)) { save(); return; } - boolean save = false; try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { JsonObject data = GsonProvider.normal().fromJson(reader, JsonObject.class); - if (data.has("context")) { - this.staticContexts = ContextSetJsonSerializer.deserializeContextSet(data.get("context").getAsJsonObject()).immutableCopy(); - save = true; - } - if (data.has("static-contexts")) { this.staticContexts = ContextSetJsonSerializer.deserializeContextSet(data.get("static-contexts").getAsJsonObject()).immutableCopy(); } @@ -90,10 +75,6 @@ public class ContextsFile { } catch (IOException e) { e.printStackTrace(); } - - if (save) { - save(); - } } public void save() { diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/context/NukkitContextManager.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/context/NukkitContextManager.java index 338d19a3d..909cf99eb 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/context/NukkitContextManager.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/context/NukkitContextManager.java @@ -45,7 +45,7 @@ import java.util.concurrent.TimeUnit; public class NukkitContextManager extends ContextManager { - public static final OptionKey OP_OPTION = new OptionKey(){}; + public static final OptionKey OP_OPTION = OptionKey.of("op", Boolean.class); // cache the creation of ContextsCache instances for online players with no expiry private final LoadingMap> onlineSubjectCaches = LoadingMap.of(key -> new QueryOptionsCache<>(key, this)); diff --git a/nukkit/src/main/java/me/lucko/luckperms/nukkit/inject/permissible/LPPermissionAttachment.java b/nukkit/src/main/java/me/lucko/luckperms/nukkit/inject/permissible/LPPermissionAttachment.java index 3e40f013b..1e5af83fe 100644 --- a/nukkit/src/main/java/me/lucko/luckperms/nukkit/inject/permissible/LPPermissionAttachment.java +++ b/nukkit/src/main/java/me/lucko/luckperms/nukkit/inject/permissible/LPPermissionAttachment.java @@ -58,7 +58,7 @@ import java.util.Set; */ public class LPPermissionAttachment extends PermissionAttachment { - public static final NodeMetadataKey TRANSIENT_SOURCE_KEY = NodeMetadataKey.of("TransientSource", LPPermissionAttachment.class); + public static final NodeMetadataKey TRANSIENT_SOURCE_KEY = NodeMetadataKey.of("transientsource", LPPermissionAttachment.class); /** * The field in PermissionAttachment where the attachments applied permissions