From 67e9e6f63e64fd0431b773418e9aaaa0df59b2da Mon Sep 17 00:00:00 2001 From: Riley Park Date: Tue, 10 Oct 2023 02:08:49 -0700 Subject: [PATCH] feat(key): add asMinimalString --- .../main/java/net/kyori/adventure/key/Key.java | 15 +++++++++++++++ .../java/net/kyori/adventure/key/KeyTest.java | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/key/src/main/java/net/kyori/adventure/key/Key.java b/key/src/main/java/net/kyori/adventure/key/Key.java index 5ff563be6..24ecc81c4 100644 --- a/key/src/main/java/net/kyori/adventure/key/Key.java +++ b/key/src/main/java/net/kyori/adventure/key/Key.java @@ -267,6 +267,21 @@ static boolean allowedInValue(final char character) { */ @NotNull String asString(); + /** + * Returns the string representation of this key in minimal form. + * + *

If the {@link #namespace()} of this key is {@link #MINECRAFT_NAMESPACE}, only the {@link #value()} will be returned.

+ * + * @return the string representation + * @since 4.15.0 + */ + default @NotNull String asMinimalString() { + if (this.namespace().equals(MINECRAFT_NAMESPACE)) { + return this.value(); + } + return this.asString(); + } + @Override default @NotNull Stream examinableProperties() { return Stream.of( diff --git a/key/src/test/java/net/kyori/adventure/key/KeyTest.java b/key/src/test/java/net/kyori/adventure/key/KeyTest.java index bd771f5fb..596bdf85d 100644 --- a/key/src/test/java/net/kyori/adventure/key/KeyTest.java +++ b/key/src/test/java/net/kyori/adventure/key/KeyTest.java @@ -67,6 +67,13 @@ void testStringRepresentation() { assertEquals("minecraft:empty", Key.key("empty").toString()); } + @Test + void testAsMinimalString() { + assertEquals("empty", Key.key("empty").asMinimalString()); + assertEquals("empty", Key.key(Key.MINECRAFT_NAMESPACE, "empty").asMinimalString()); + assertEquals("adventure:empty", Key.key("adventure", "empty").asMinimalString()); + } + @Test void testEquality() { new EqualsTester()