Skip to content

Commit 2b513f9

Browse files
committed
feature: NBT component plain
1 parent dcba87d commit 2b513f9

File tree

15 files changed

+223
-44
lines changed

15 files changed

+223
-44
lines changed

api/src/main/java/net/kyori/adventure/text/AbstractNBTComponentBuilder.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ abstract sealed class AbstractNBTComponentBuilder<C extends NBTComponent<C>, B e
3131
protected @Nullable String nbtPath;
3232
protected boolean interpret = NBTComponent.INTERPRET_DEFAULT;
3333
protected @Nullable Component separator;
34+
protected boolean plain;
3435

3536
AbstractNBTComponentBuilder() {
3637
}
@@ -40,6 +41,7 @@ abstract sealed class AbstractNBTComponentBuilder<C extends NBTComponent<C>, B e
4041
this.nbtPath = component.nbtPath();
4142
this.interpret = component.interpret();
4243
this.separator = component.separator();
44+
this.plain = component.plain();
4345
}
4446

4547
@Override
@@ -52,14 +54,31 @@ public B nbtPath(final String nbtPath) {
5254
@Override
5355
@SuppressWarnings("unchecked")
5456
public B interpret(final boolean interpret) {
57+
if (this.interpret == interpret) return (B) this;
58+
checkInterpretPlainState(interpret, this.plain);
5559
this.interpret = interpret;
5660
return (B) this;
5761
}
5862

63+
@Override
64+
@SuppressWarnings("unchecked")
65+
public B plain(final boolean plain) {
66+
if (this.plain == plain) return (B) this;
67+
checkInterpretPlainState(this.interpret, plain);
68+
this.plain = plain;
69+
return (B) this;
70+
}
71+
5972
@Override
6073
@SuppressWarnings("unchecked")
6174
public B separator(final @Nullable ComponentLike separator) {
6275
this.separator = ComponentLike.unbox(separator);
6376
return (B) this;
6477
}
78+
79+
static void checkInterpretPlainState(final boolean interpret, final boolean plain) {
80+
if (interpret && plain) {
81+
throw new IllegalArgumentException("Cannot have `interpret` and `plain` set to `true` at the same time");
82+
}
83+
}
6584
}

api/src/main/java/net/kyori/adventure/text/BlockNBTComponentImpl.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,67 @@
3030
import org.jspecify.annotations.Nullable;
3131

3232
import static java.util.Objects.requireNonNull;
33+
import static net.kyori.adventure.text.AbstractNBTComponentBuilder.checkInterpretPlainState;
3334

3435
record BlockNBTComponentImpl(
3536
List<Component> children,
3637
Style style,
3738
String nbtPath,
3839
boolean interpret,
3940
@Nullable Component separator,
40-
Pos pos
41+
Pos pos,
42+
boolean plain
4143
) implements BlockNBTComponent {
42-
static BlockNBTComponent create(final List<? extends ComponentLike> children, final Style style, final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final Pos pos) {
44+
static BlockNBTComponent create(final List<? extends ComponentLike> children, final Style style, final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final Pos pos, final boolean plain) {
4345
return new BlockNBTComponentImpl(
4446
ComponentLike.asComponents(children, IS_NOT_EMPTY),
4547
requireNonNull(style, "style"),
4648
requireNonNull(nbtPath, "nbtPath"),
4749
interpret,
4850
ComponentLike.unbox(separator),
49-
requireNonNull(pos, "pos")
51+
requireNonNull(pos, "pos"),
52+
plain
5053
);
5154
}
5255

5356
@Override
5457
public BlockNBTComponent nbtPath(final String nbtPath) {
5558
if (Objects.equals(this.nbtPath, nbtPath)) return this;
56-
return create(this.children, this.style, nbtPath, this.interpret, this.separator, this.pos);
59+
return create(this.children, this.style, nbtPath, this.interpret, this.separator, this.pos, this.plain);
5760
}
5861

5962
@Override
6063
public BlockNBTComponent interpret(final boolean interpret) {
6164
if (this.interpret == interpret) return this;
62-
return create(this.children, this.style, this.nbtPath, interpret, this.separator, this.pos);
65+
checkInterpretPlainState(interpret, this.plain);
66+
return create(this.children, this.style, this.nbtPath, interpret, this.separator, this.pos, this.plain);
67+
}
68+
69+
@Override
70+
public BlockNBTComponent plain(final boolean plain) {
71+
if (this.plain == plain) return this;
72+
checkInterpretPlainState(this.interpret, plain);
73+
return create(this.children, this.style, this.nbtPath, this.interpret, this.separator, this.pos, plain);
6374
}
6475

6576
@Override
6677
public BlockNBTComponent separator(final @Nullable ComponentLike separator) {
67-
return create(this.children, this.style, this.nbtPath, this.interpret, separator, this.pos);
78+
return create(this.children, this.style, this.nbtPath, this.interpret, separator, this.pos, this.plain);
6879
}
6980

7081
@Override
7182
public BlockNBTComponent pos(final Pos pos) {
72-
return create(this.children, this.style, this.nbtPath, this.interpret, this.separator, pos);
83+
return create(this.children, this.style, this.nbtPath, this.interpret, this.separator, pos, this.plain);
7384
}
7485

7586
@Override
7687
public BlockNBTComponent children(final List<? extends ComponentLike> children) {
77-
return create(children, this.style, this.nbtPath, this.interpret, this.separator, this.pos);
88+
return create(children, this.style, this.nbtPath, this.interpret, this.separator, this.pos, this.plain);
7889
}
7990

8091
@Override
8192
public BlockNBTComponent style(final Style style) {
82-
return create(this.children, style, this.nbtPath, this.interpret, this.separator, this.pos);
93+
return create(this.children, style, this.nbtPath, this.interpret, this.separator, this.pos, this.plain);
8394
}
8495

8596
@Override
@@ -108,7 +119,7 @@ public Builder pos(final Pos pos) {
108119
public BlockNBTComponent build() {
109120
if (this.nbtPath == null) throw new IllegalStateException("nbt path must be set");
110121
if (this.pos == null) throw new IllegalStateException("pos must be set");
111-
return create(this.children, this.buildStyle(), this.nbtPath, this.interpret, this.separator, this.pos);
122+
return create(this.children, this.buildStyle(), this.nbtPath, this.interpret, this.separator, this.pos, this.plain);
112123
}
113124
}
114125

api/src/main/java/net/kyori/adventure/text/Component.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ static BlockNBTComponent blockNBT(final String nbtPath, final boolean interpret,
326326
*/
327327
@Contract(value = "_, _, _, _ -> new", pure = true)
328328
static BlockNBTComponent blockNBT(final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final BlockNBTComponent.Pos pos) {
329-
return BlockNBTComponentImpl.create(List.of(), Style.empty(), nbtPath, interpret, separator, pos);
329+
return BlockNBTComponentImpl.create(List.of(), Style.empty(), nbtPath, interpret, separator, pos, NBTComponent.PLAIN_DEFAULT);
330330
}
331331

332332
/*
@@ -753,7 +753,7 @@ static StorageNBTComponent storageNBT(final String nbtPath, final boolean interp
753753
*/
754754
@Contract(value = "_, _, _, _ -> new", pure = true)
755755
static StorageNBTComponent storageNBT(final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final Key storage) {
756-
return StorageNBTComponentImpl.create(List.of(), Style.empty(), nbtPath, interpret, separator, storage);
756+
return StorageNBTComponentImpl.create(List.of(), Style.empty(), nbtPath, interpret, separator, storage, NBTComponent.PLAIN_DEFAULT);
757757
}
758758

759759
/*

api/src/main/java/net/kyori/adventure/text/EntityNBTComponentImpl.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,57 +29,68 @@
2929
import org.jspecify.annotations.Nullable;
3030

3131
import static java.util.Objects.requireNonNull;
32+
import static net.kyori.adventure.text.AbstractNBTComponentBuilder.checkInterpretPlainState;
3233

3334
record EntityNBTComponentImpl(
3435
List<Component> children,
3536
Style style,
3637
String nbtPath,
3738
boolean interpret,
3839
Component separator,
39-
String selector
40+
String selector,
41+
boolean plain
4042
) implements EntityNBTComponent {
41-
static EntityNBTComponent create(final List<? extends ComponentLike> children, final Style style, final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final String selector) {
43+
static EntityNBTComponent create(final List<? extends ComponentLike> children, final Style style, final String nbtPath, final boolean interpret, final @Nullable ComponentLike separator, final String selector, final boolean plain) {
4244
return new EntityNBTComponentImpl(
4345
ComponentLike.asComponents(children, IS_NOT_EMPTY),
4446
requireNonNull(style, "style"),
4547
requireNonNull(nbtPath, "nbtPath"),
4648
interpret,
4749
ComponentLike.unbox(separator),
48-
requireNonNull(selector, "selector")
50+
requireNonNull(selector, "selector"),
51+
plain
4952
);
5053
}
5154

5255
@Override
5356
public EntityNBTComponent nbtPath(final String nbtPath) {
5457
if (Objects.equals(this.nbtPath, nbtPath)) return this;
55-
return create(this.children, this.style, nbtPath, this.interpret, this.separator, this.selector);
58+
return create(this.children, this.style, nbtPath, this.interpret, this.separator, this.selector, this.plain);
5659
}
5760

5861
@Override
5962
public EntityNBTComponent interpret(final boolean interpret) {
6063
if (this.interpret == interpret) return this;
61-
return create(this.children, this.style, this.nbtPath, interpret, this.separator, this.selector);
64+
checkInterpretPlainState(interpret, this.plain);
65+
return create(this.children, this.style, this.nbtPath, interpret, this.separator, this.selector, this.plain);
66+
}
67+
68+
@Override
69+
public EntityNBTComponent plain(final boolean plain) {
70+
if (this.plain == plain) return this;
71+
checkInterpretPlainState(this.interpret, plain);
72+
return create(this.children, this.style, this.nbtPath, this.interpret, this.separator, this.selector, plain);
6273
}
6374

6475
@Override
6576
public EntityNBTComponent separator(final @Nullable ComponentLike separator) {
66-
return create(this.children, this.style, this.nbtPath, this.interpret, separator, this.selector);
77+
return create(this.children, this.style, this.nbtPath, this.interpret, separator, this.selector, this.plain);
6778
}
6879

6980
@Override
7081
public EntityNBTComponent selector(final String selector) {
7182
if (Objects.equals(this.selector, selector)) return this;
72-
return create(this.children, this.style, this.nbtPath, this.interpret, this.separator, selector);
83+
return create(this.children, this.style, this.nbtPath, this.interpret, this.separator, selector, this.plain);
7384
}
7485

7586
@Override
7687
public EntityNBTComponent children(final List<? extends ComponentLike> children) {
77-
return create(children, this.style, this.nbtPath, this.interpret, this.separator, this.selector);
88+
return create(children, this.style, this.nbtPath, this.interpret, this.separator, this.selector, this.plain);
7889
}
7990

8091
@Override
8192
public EntityNBTComponent style(final Style style) {
82-
return create(this.children, style, this.nbtPath, this.interpret, this.separator, this.selector);
93+
return create(this.children, style, this.nbtPath, this.interpret, this.separator, this.selector, this.plain);
8394
}
8495

8596
@Override
@@ -108,7 +119,7 @@ public Builder selector(final String selector) {
108119
public EntityNBTComponent build() {
109120
if (this.nbtPath == null) throw new IllegalStateException("nbt path must be set");
110121
if (this.selector == null) throw new IllegalStateException("selector must be set");
111-
return create(this.children, this.buildStyle(), this.nbtPath, this.interpret, this.separator, this.selector);
122+
return create(this.children, this.buildStyle(), this.nbtPath, this.interpret, this.separator, this.selector, this.plain);
112123
}
113124
}
114125
}

api/src/main/java/net/kyori/adventure/text/NBTComponent.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
* <dd>a path to specify which parts of the nbt you want displayed(<a href="https://minecraft.wiki/w/NBT_path_format#Examples">examples</a>).</dd>
3838
* <dt>interpret</dt>
3939
* <dd>a boolean telling adventure if the fetched NBT value should be parsed as JSON</dd>
40+
* <dt>plain</dt>
41+
* <dd>a boolean telling adventure if the fetched NBT value should be pretty-printed without styling</dd>
4042
* </dl>
4143
*
4244
* <p>This component is rendered serverside and can therefore receive platform-defined
@@ -55,6 +57,13 @@ public sealed interface NBTComponent<C extends NBTComponent<C>> extends Componen
5557
*/
5658
boolean INTERPRET_DEFAULT = false;
5759

60+
/**
61+
* The default value for {@link #plain()}.
62+
*
63+
* @since 5.0.0
64+
*/
65+
boolean PLAIN_DEFAULT = false;
66+
5867
/**
5968
* Gets the NBT path.
6069
*
@@ -76,6 +85,8 @@ public sealed interface NBTComponent<C extends NBTComponent<C>> extends Componen
7685
/**
7786
* Gets if we should be interpreting.
7887
*
88+
* <p>This cannot be {@code true} if {@link #plain()} is also {@code true}.</p>
89+
*
7990
* @return if we should be interpreting
8091
* @since 4.0.0
8192
*/
@@ -84,8 +95,11 @@ public sealed interface NBTComponent<C extends NBTComponent<C>> extends Componen
8495
/**
8596
* Sets if we should be interpreting.
8697
*
98+
* <p>This cannot be {@code true} if {@link #plain()} is also {@code true}.</p>
99+
*
87100
* @param interpret if we should be interpreting.
88101
* @return an NBT component
102+
* @throws IllegalArgumentException if set to {@code true} and {@link #plain()} is also {@code true}
89103
* @since 4.0.0
90104
*/
91105
@Contract(pure = true)
@@ -103,11 +117,35 @@ public sealed interface NBTComponent<C extends NBTComponent<C>> extends Componen
103117
* Sets the separator.
104118
*
105119
* @param separator the separator
106-
* @return the separator
120+
* @return an NBT component
107121
* @since 4.8.0
108122
*/
109123
C separator(final @Nullable ComponentLike separator);
110124

125+
/**
126+
* Gets if styling should be removed from pretty-printed NBT.
127+
*
128+
* <p>This cannot be {@code true} if {@link #interpret()} is also {@code true}.</p>
129+
*
130+
* @return if styling should be removed when pretty-printed
131+
* @since 5.0.0
132+
* @sinceMinecraft 26.1
133+
*/
134+
boolean plain();
135+
136+
/**
137+
* Sets if styling should be removed from pretty-printed NBT.
138+
*
139+
* <p>This cannot be {@code true} if {@link #interpret()} is also {@code true}.</p>
140+
*
141+
* @param plain if styling should be removed when pretty-printed
142+
* @return an NBT component
143+
* @throws IllegalArgumentException if set to {@code true} and {@link #interpret()} is also {@code true}
144+
* @since 5.0.0
145+
* @sinceMinecraft 26.1
146+
*/
147+
C plain(final boolean plain);
148+
111149
@Override
112150
NBTComponentBuilder<C, ? extends NBTComponentBuilder<C, ?>> toBuilder();
113151
}

api/src/main/java/net/kyori/adventure/text/NBTComponentBuilder.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ public sealed interface NBTComponentBuilder<C extends NBTComponent<C>, B extends
5151
/**
5252
* Sets whether to interpret.
5353
*
54+
* <p>This cannot be {@code true} if {@link #plain(boolean)} is also {@code true}.</p>
55+
*
5456
* @param interpret if we should be interpreting
5557
* @return this builder
58+
* @throws IllegalArgumentException if set to {@code true} and {@link #plain(boolean)} is also {@code true}
5659
* @since 4.0.0
5760
*/
5861
@Contract("_ -> this")
@@ -67,4 +70,17 @@ public sealed interface NBTComponentBuilder<C extends NBTComponent<C>, B extends
6770
*/
6871
@Contract("_ -> this")
6972
B separator(final @Nullable ComponentLike separator);
73+
74+
/**
75+
* Sets if styling should be removed from pretty-printed NBT.
76+
*
77+
* <p>This cannot be {@code true} if {@link #interpret(boolean)} is also {@code true}.</p>
78+
*
79+
* @param plain if styling should be removed when pretty-printed
80+
* @return this builder
81+
* @throws IllegalArgumentException if set to {@code true} and {@link #interpret(boolean)} is also {@code true}
82+
* @since 5.0.0
83+
* @sinceMinecraft 26.1
84+
*/
85+
B plain(final boolean plain);
7086
}

0 commit comments

Comments
 (0)