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

feat(key): add asMinimalString #983

Merged
merged 1 commit into from
Oct 10, 2023
Merged
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
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