From fafb78ee16d8b070e7ef10d94c4f36b9a281fdff Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 10 Apr 2024 04:43:25 +0100 Subject: [PATCH] Prune KeyedUtils Removed fromParts() in favour of pluginKey(), as the NamespacedKey parts constructor was de-deprecated. --- .../civmodcore/inventory/items/MoreTags.java | 3 +- .../mc/civmodcore/utilities/KeyedUtils.java | 109 ++++++++++++------ 2 files changed, 72 insertions(+), 40 deletions(-) diff --git a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/inventory/items/MoreTags.java b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/inventory/items/MoreTags.java index ed0fe70cf..09b278a78 100644 --- a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/inventory/items/MoreTags.java +++ b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/inventory/items/MoreTags.java @@ -15,7 +15,6 @@ import org.bukkit.Tag; import org.bukkit.block.data.Ageable; import vg.civcraft.mc.civmodcore.utilities.CivLogger; -import vg.civcraft.mc.civmodcore.utilities.KeyedUtils; /** * Fills in the gaps between {@link Tag} and {@link MaterialTags}. @@ -293,7 +292,7 @@ private static class BetterTag implements Tag { private final Set values; private BetterTag(final String key, final Set values) { - this.key = KeyedUtils.fromParts("civmodcore", key); + this.key = new NamespacedKey("civmodcore", key); this.values = values; } diff --git a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/utilities/KeyedUtils.java b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/utilities/KeyedUtils.java index 7b6f304bf..9fafb946c 100644 --- a/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/utilities/KeyedUtils.java +++ b/plugins/civmodcore-paper/src/main/java/vg/civcraft/mc/civmodcore/utilities/KeyedUtils.java @@ -1,73 +1,106 @@ package vg.civcraft.mc.civmodcore.utilities; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; -import org.bukkit.Keyed; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.key.Keyed; import org.bukkit.NamespacedKey; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * Utility class to make dealing with namespace keys easier. */ public final class KeyedUtils { - /** * Converts a stringified namespaced-key back into a {@link NamespacedKey}. - * - * @param key The stringified namespace-key, which MUST be formatted as {@code namespace:name} - * @return Returns a valid {@link NamespacedKey}, or null. - * @throws IllegalArgumentException Will throw if the stringified key fails a "[a-z0-9._-]+" check, or if the - * total length is longer than 256. */ - @Nullable - public static NamespacedKey fromString(@Nullable final String key) { + public static @Nullable NamespacedKey fromString( + final String key + ) { return key == null ? null : NamespacedKey.fromString(key); } /** - * Converts a namespace and a key into a {@link NamespacedKey}. + * Converts a {@link Key} into a string. * - * @param namespace The namespace name. - * @param key The namespaced key. - * @return Returns a valid {@link NamespacedKey}, or null. - * @throws IllegalArgumentException Will throw if either part fails a "[a-z0-9._-]+" check, or if the total - * combined length is longer than 256. + * @apiNote This will be immediately defunct the moment we move to Kotlin. */ - @SuppressWarnings("deprecation") - @Nonnull - public static NamespacedKey fromParts(@Nonnull final String namespace, @Nonnull final String key) { - return new NamespacedKey(namespace, key); + @Contract("!null -> !null") + public static @Nullable String getString( + final Key key + ) { + return key == null ? null : key.asString(); } /** - * Converts a {@link NamespacedKey} into a string. + * Converts a {@link Keyed} into a string. * - * @param key The {@link NamespacedKey} to convert. - * @return Returns the stringified {@link NamespacedKey}, or null. + * @apiNote This will be immediately defunct the moment we move to Kotlin. */ - @Nullable - public static String getString(@Nullable final NamespacedKey key) { - return key == null ? null : key.toString(); + @Contract("!null -> !null") + public static @Nullable String getString( + final Keyed keyed + ) { + return keyed == null ? null : getString(keyed.key()); } /** - * Retrieves a {@link Keyed}'s {@link NamespacedKey} and converts it to a string. + * Convenience method that lets you construct a key namespaced to a plugin without needing a plugin instance. * - * @param keyed The {@link Keyed} instance. - * @return Returns the stringified {@link Keyed}'s {@link NamespacedKey}, or null. + *

+     * // If you used NamespacedKey as intended
+     * new NamespacedKey(ExamplePlugin.getInstance(), "example");
+     *
+     * // Using KeyedUtils.pluginKey()
+     * KeyedUtils.pluginKey(ExamplePlugin.class, "example");
+     * 
*/ - @Nullable - public static String getString(@Nullable final Keyed keyed) { - return keyed == null ? null : getString(keyed.getKey()); + public static @NotNull NamespacedKey pluginKey( + final @NotNull Class pluginClass, + final @NotNull String key + ) { + return new NamespacedKey(JavaPlugin.getPlugin(pluginClass), key); } /** - * @param key The namespaced-key. - * @return Returns a new {@link NamespacedKey} for testing purposes. + * Creates a new {@link NamespacedKey} for testing purposes. */ - @SuppressWarnings("deprecation") - @Nonnull - public static NamespacedKey testKey(@Nonnull final String key) { + public static @NotNull NamespacedKey testKey( + final @NotNull String key + ) { return new NamespacedKey("test", key); } + /** + * Creates a ditto key, primarily for child + * keys within {@link org.bukkit.persistence.PersistentDataContainer PersistentDataContainers}. + * + *

+     * // If you used PDCs as intended
+     * {
+     *     "CivModCore:players": [
+     *         {
+     *             "CivModCore:name": "Example",
+     *             "CivModCore:uuid": "61629d9c-c2a1-4379-b98e-4b538c6628c5"
+     *         }
+     *     ]
+     * }
+     *
+     * // Using ditto keys
+     * {
+     *     "CivModCore:players": [
+     *         {
+     *             ".:name": "Example",
+     *             ".:uuid": "61629d9c-c2a1-4379-b98e-4b538c6628c5"
+     *         }
+     *     ]
+     * }
+     * 
+ */ + public static @NotNull NamespacedKey dittoKey( + final @NotNull String key + ) { + return new NamespacedKey(".", key); + } }