Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prune KeyedUtils #400

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand Down Expand Up @@ -293,7 +292,7 @@ private static class BetterTag<T extends Keyed> implements Tag<T> {
private final Set<T> values;

private BetterTag(final String key, final Set<T> values) {
this.key = KeyedUtils.fromParts("civmodcore", key);
this.key = new NamespacedKey("civmodcore", key);
this.values = values;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <pre><code>
* // If you used NamespacedKey as intended
* new NamespacedKey(ExamplePlugin.getInstance(), "example");
*
* // Using KeyedUtils.pluginKey()
* KeyedUtils.pluginKey(ExamplePlugin.class, "example");
* </code></pre>
*/
@Nullable
public static String getString(@Nullable final Keyed keyed) {
return keyed == null ? null : getString(keyed.getKey());
public static <T extends JavaPlugin> @NotNull NamespacedKey pluginKey(
final @NotNull Class<T> 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 <a href="https://dictionary.cambridge.org/dictionary/english/ditto">ditto</a> key, primarily for child
* keys within {@link org.bukkit.persistence.PersistentDataContainer PersistentDataContainers}.
*
* <pre><code>
* // 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"
* }
* ]
* }
* </code></pre>
*/
public static @NotNull NamespacedKey dittoKey(
final @NotNull String key
) {
return new NamespacedKey(".", key);
}
}
Loading