Skip to content

Commit

Permalink
feat(key): add asMinimalString
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Oct 10, 2023
1 parent c4e02a2 commit 67e9e6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions key/src/main/java/net/kyori/adventure/key/Key.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,21 @@ static boolean allowedInValue(final char character) {
*/
@NotNull String asString();

/**
* Returns the string representation of this key in minimal form.
*
* <p>If the {@link #namespace()} of this key is {@link #MINECRAFT_NAMESPACE}, only the {@link #value()} will be returned.</p>
*
* @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<? extends ExaminableProperty> examinableProperties() {
return Stream.of(
Expand Down
7 changes: 7 additions & 0 deletions key/src/test/java/net/kyori/adventure/key/KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 67e9e6f

Please sign in to comment.