Skip to content

Commit a87c34a

Browse files
committed
chore: replace NBTSerializerOptions#SHADOW_COLOR_MODE with NBTSerializerOptions#EMIT_SHADOW_COLOR, fix: versioned option state of NBTSerializerOptions serializes shadow color as a list by default
1 parent 7a6f3b9 commit a87c34a

File tree

3 files changed

+23
-69
lines changed

3 files changed

+23
-69
lines changed

text-serializer-nbt/src/main/java/net/kyori/adventure/text/serializer/nbt/NBTSerializerOptions.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
public final class NBTSerializerOptions {
4040

4141
/**
42-
* How to emit shadow colour data.
42+
* Whether to emit shadow colour data.
4343
*
4444
* @since 4.24.0
45+
* @sinceMinecraft 1.21.4
4546
*/
46-
public static final Option<ShadowColorEmitMode> SHADOW_COLOR_MODE;
47+
public static final Option<Boolean> EMIT_SHADOW_COLOR;
4748

4849
/**
4950
* Control how hover event values should be emitted.
@@ -95,7 +96,7 @@ public final class NBTSerializerOptions {
9596

9697
static {
9798
final OptionSchema.Mutable schema = OptionSchema.emptySchema();
98-
SHADOW_COLOR_MODE = schema.enumOption(key("emit/shadow_color"), ShadowColorEmitMode.class, ShadowColorEmitMode.EMIT_INTEGER);
99+
EMIT_SHADOW_COLOR = schema.booleanOption(key("emit/shadow_color"), true);
99100
EMIT_HOVER_EVENT_TYPE = schema.enumOption(key("emit/hover_value_mode"), HoverEventValueMode.class, HoverEventValueMode.SNAKE_CASE);
100101
EMIT_CLICK_EVENT_TYPE = schema.enumOption(key("emit/click_value_mode"), ClickEventValueMode.class, ClickEventValueMode.SNAKE_CASE);
101102
EMIT_DEFAULT_ITEM_HOVER_QUANTITY = schema.booleanOption(key("emit/default_item_hover_quantity"), true);
@@ -106,7 +107,7 @@ public final class NBTSerializerOptions {
106107
BY_DATA_VERSION = SCHEMA.versionedStateBuilder()
107108
.version(
108109
VERSION_23W40A,
109-
builder -> builder.value(SHADOW_COLOR_MODE, ShadowColorEmitMode.NONE)
110+
builder -> builder.value(EMIT_SHADOW_COLOR, false)
110111
.value(EMIT_HOVER_EVENT_TYPE, HoverEventValueMode.CAMEL_CASE)
111112
.value(EMIT_CLICK_EVENT_TYPE, ClickEventValueMode.CAMEL_CASE)
112113
.value(EMIT_DEFAULT_ITEM_HOVER_QUANTITY, false)
@@ -123,7 +124,7 @@ public final class NBTSerializerOptions {
123124
)
124125
.version(
125126
VERSION_24W44A,
126-
builder -> builder.value(SHADOW_COLOR_MODE, ShadowColorEmitMode.EMIT_ARRAY)
127+
builder -> builder.value(EMIT_SHADOW_COLOR, true)
127128
)
128129
.version(
129130
VERSION_25W02A,
@@ -217,31 +218,6 @@ public enum ClickEventValueMode {
217218
BOTH,
218219
}
219220

220-
/**
221-
* How text shadow colors should be emitted.
222-
*
223-
* @since 4.24.0
224-
* @sinceMinecraft 1.21.4
225-
*/
226-
public enum ShadowColorEmitMode {
227-
/**
228-
* Do not emit shadow colours.
229-
*/
230-
NONE,
231-
/**
232-
* Emit as a single packed integer value containing, in order, ARGB bytes.
233-
*
234-
* @since 4.24.0
235-
*/
236-
EMIT_INTEGER,
237-
/**
238-
* Emit a colour as 4-element float array of the RGBA components of the colour.
239-
*
240-
* @since 4.24.0
241-
*/
242-
EMIT_ARRAY
243-
}
244-
245221
/**
246222
* Configure how to emit show item hovers in {@code hoverEvent} (camelCase) fields.
247223
*

text-serializer-nbt/src/main/java/net/kyori/adventure/text/serializer/nbt/ShadowColorSerializer.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
package net.kyori.adventure.text.serializer.nbt;
2525

2626
import net.kyori.adventure.nbt.BinaryTag;
27-
import net.kyori.adventure.nbt.BinaryTagTypes;
28-
import net.kyori.adventure.nbt.FloatBinaryTag;
2927
import net.kyori.adventure.nbt.IntBinaryTag;
3028
import net.kyori.adventure.nbt.ListBinaryTag;
3129
import net.kyori.adventure.text.format.ShadowColor;
@@ -55,30 +53,10 @@ private ShadowColorSerializer() {
5553
}
5654

5755
static @Nullable BinaryTag serialize(final @NotNull ShadowColor color, final @NotNull NBTComponentSerializerImpl serializer) {
58-
final NBTSerializerOptions.ShadowColorEmitMode emitMode = serializer.options().value(NBTSerializerOptions.SHADOW_COLOR_MODE);
59-
switch (emitMode) {
60-
case NONE:
61-
return null;
62-
case EMIT_INTEGER:
63-
return IntBinaryTag.intBinaryTag(color.value());
64-
case EMIT_ARRAY:
65-
final ListBinaryTag.Builder<FloatBinaryTag> builder = ListBinaryTag.builder(BinaryTagTypes.FLOAT);
66-
addShadowColorComponent(builder, color.red());
67-
addShadowColorComponent(builder, color.green());
68-
addShadowColorComponent(builder, color.blue());
69-
addShadowColorComponent(builder, color.alpha());
70-
return builder.build();
71-
default:
72-
// Never called, but needed for proper compilation
73-
throw new IllegalArgumentException("Unknown shadow color emit mode: " + emitMode);
74-
}
56+
return serializer.options().value(NBTSerializerOptions.EMIT_SHADOW_COLOR) ? IntBinaryTag.intBinaryTag(color.value()) : null;
7557
}
7658

7759
private static int shadowColorComponent(final @NotNull ListBinaryTag tag, final int index) {
7860
return (int) (tag.getFloat(index) * 0xff);
7961
}
80-
81-
private static void addShadowColorComponent(final ListBinaryTag.@NotNull Builder<FloatBinaryTag> builder, final int element) {
82-
builder.add(FloatBinaryTag.floatBinaryTag((float) element / 0xff));
83-
}
8462
}

text-serializer-nbt/src/test/java/net/kyori/adventure/text/serializer/nbt/StyleTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import static net.kyori.adventure.text.serializer.nbt.SerializerTests.deserializeStyle;
4747
import static net.kyori.adventure.text.serializer.nbt.SerializerTests.name;
4848
import static net.kyori.adventure.text.serializer.nbt.SerializerTests.testStyle;
49+
import static org.junit.jupiter.api.Assertions.assertEquals;
4950
import static org.junit.jupiter.api.Assertions.assertThrows;
5051

5152
final class StyleTest {
@@ -117,23 +118,22 @@ void testShadowColorInt() {
117118
}
118119

119120
@Test
120-
void testShadowColorFloats() {
121-
testStyle(
122-
NBTComponentSerializer.builder()
123-
.editOptions(builder -> builder.value(NBTSerializerOptions.SHADOW_COLOR_MODE, NBTSerializerOptions.ShadowColorEmitMode.EMIT_ARRAY))
124-
.build(),
121+
void testShadowColorFloatList() {
122+
assertEquals(
125123
Style.style(ShadowColor.shadowColor(0x80, 0x40, 0xcc, 0xff)),
126-
CompoundBinaryTag.builder()
127-
.put(
128-
ComponentTreeConstants.SHADOW_COLOR,
129-
ListBinaryTag.builder(BinaryTagTypes.FLOAT)
130-
.add(FloatBinaryTag.floatBinaryTag(0.5019608f))
131-
.add(FloatBinaryTag.floatBinaryTag(0.2509804f))
132-
.add(FloatBinaryTag.floatBinaryTag(0.8f))
133-
.add(FloatBinaryTag.floatBinaryTag(1f))
134-
.build()
135-
)
136-
.build()
124+
deserializeStyle(
125+
CompoundBinaryTag.builder()
126+
.put(
127+
ComponentTreeConstants.SHADOW_COLOR,
128+
ListBinaryTag.builder(BinaryTagTypes.FLOAT)
129+
.add(FloatBinaryTag.floatBinaryTag(0.5019608f))
130+
.add(FloatBinaryTag.floatBinaryTag(0.2509804f))
131+
.add(FloatBinaryTag.floatBinaryTag(0.8f))
132+
.add(FloatBinaryTag.floatBinaryTag(1f))
133+
.build()
134+
)
135+
.build()
136+
)
137137
);
138138
}
139139

0 commit comments

Comments
 (0)