Skip to content

Commit ad8d730

Browse files
committed
chore: use option schema for NBT serializer options instead
1 parent 1a948f2 commit ad8d730

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,16 @@ final class NBTComponentSerializerImpl implements NBTComponentSerializer {
9898
static final class Instances {
9999
static final NBTComponentSerializer INSTANCE = SERVICE
100100
.map(Provider::nbt)
101-
.orElseGet(() -> new NBTComponentSerializerImpl(OptionState.emptyOptionState()));
101+
.orElseGet(() -> new NBTComponentSerializerImpl(NBTSerializerOptions.schema().emptyState()));
102102
}
103103

104104
private final OptionState flags;
105105

106106
NBTComponentSerializerImpl(@NotNull OptionState flags) {
107-
this.flags = flags;
107+
this.flags = requireNonNull(flags, "flags");
108+
if (flags.schema() != NBTSerializerOptions.schema()) {
109+
throw new IllegalArgumentException("The specified option state does not use the NBT serializer option schema");
110+
}
108111
}
109112

110113
@Override
@@ -325,7 +328,7 @@ static final class Instances {
325328

326329
static final class BuilderImpl implements NBTComponentSerializer.Builder {
327330

328-
private OptionState flags = OptionState.emptyOptionState();
331+
private OptionState flags = NBTSerializerOptions.schema().emptyState();
329332

330333
BuilderImpl() {
331334
BUILDER.accept(this); // let service provider touch the builder before anybody else touches it
@@ -339,7 +342,7 @@ static final class BuilderImpl implements NBTComponentSerializer.Builder {
339342

340343
@Override
341344
public @NotNull Builder editOptions(@NotNull Consumer<OptionState.Builder> optionEditor) {
342-
final OptionState.Builder builder = OptionState.optionState().values(this.flags);
345+
final OptionState.Builder builder = NBTSerializerOptions.schema().stateBuilder().values(this.flags);
343346
requireNonNull(optionEditor, "optionEditor").accept(builder);
344347
this.flags = builder.build();
345348
return this;

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

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

2626
import net.kyori.option.Option;
27+
import net.kyori.option.OptionSchema;
28+
import org.jetbrains.annotations.NotNull;
2729

2830
/**
2931
* Options that can apply to {@linkplain NBTComponentSerializer NBT serializers}.
@@ -40,28 +42,28 @@ public final class NBTSerializerOptions {
4042
* @since 4.24.0
4143
* @sinceMinecraft 1.20.3
4244
*/
43-
public static final Option<Boolean> EMIT_COMPACT_TEXT_COMPONENT = Option.booleanOption(key("emit/compact_text_component"), true);
45+
public static final Option<Boolean> EMIT_COMPACT_TEXT_COMPONENT;
4446

4547
/**
4648
* How to emit shadow colour data.
4749
*
4850
* @since 4.24.0
4951
*/
50-
public static final Option<ShadowColorEmitMode> SHADOW_COLOR_MODE = Option.enumOption(key("emit/shadow_color"), ShadowColorEmitMode.class, ShadowColorEmitMode.EMIT_INTEGER);
52+
public static final Option<ShadowColorEmitMode> SHADOW_COLOR_MODE;
5153

5254
/**
5355
* Control how hover event values should be emitted.
5456
*
5557
* @since 4.24.0
5658
*/
57-
public static final Option<HoverEventValueMode> EMIT_HOVER_EVENT_TYPE = Option.enumOption(key("emit/hover_value_mode"), HoverEventValueMode.class, HoverEventValueMode.SNAKE_CASE);
59+
public static final Option<HoverEventValueMode> EMIT_HOVER_EVENT_TYPE;
5860

5961
/**
6062
* Control how click event values should be emitted.
6163
*
6264
* @since 4.24.0
6365
*/
64-
public static final Option<ClickEventValueMode> EMIT_CLICK_EVENT_TYPE = Option.enumOption(key("emit/click_value_mode"), ClickEventValueMode.class, ClickEventValueMode.SNAKE_CASE);
66+
public static final Option<ClickEventValueMode> EMIT_CLICK_EVENT_TYPE;
6567

6668
/**
6769
* Whether to emit the default hover event item stack quantity of {@code 1}.
@@ -70,21 +72,41 @@ public final class NBTSerializerOptions {
7072
*
7173
* @since 4.24.0
7274
*/
73-
public static final Option<Boolean> EMIT_DEFAULT_ITEM_HOVER_QUANTITY = Option.booleanOption(key("emit/default_item_hover_quantity"), true);
75+
public static final Option<Boolean> EMIT_DEFAULT_ITEM_HOVER_QUANTITY;
7476

7577
/**
7678
* Whether to emit the default interpret value ({@code false}) of NBT components.
7779
*
7880
* @since 4.24.0
7981
*/
80-
public static final Option<Boolean> EMIT_DEFAULT_NBT_INTERPRET_VALUE = Option.booleanOption(key("emit/default_nbt_interpret_value"), true);
82+
public static final Option<Boolean> EMIT_DEFAULT_NBT_INTERPRET_VALUE;
8183

8284
/**
8385
* Control how entity ids of show entity hover events should be emitted.
8486
*
8587
* @since 4.24.0
8688
*/
87-
public static final Option<ShowEntityUUIDEmitMode> EMIT_SHOW_ENTITY_UUID_TYPE = Option.enumOption(key("emit/show_entity_uuid"), ShowEntityUUIDEmitMode.class, ShowEntityUUIDEmitMode.EMIT_INT_ARRAY);
89+
public static final Option<ShowEntityUUIDEmitMode> EMIT_SHOW_ENTITY_UUID_TYPE;
90+
91+
private static final OptionSchema SCHEMA;
92+
93+
// TODO: Add show item hover data mode
94+
// TODO: Add component type field emitting mode
95+
// TODO: Add a way to serialize components as lists
96+
// TODO: Add source field emitting mode
97+
// TODO: Add show item hover data mode
98+
99+
static {
100+
OptionSchema.Mutable schema = OptionSchema.emptySchema();
101+
EMIT_COMPACT_TEXT_COMPONENT = schema.booleanOption(key("emit/compact_text_component"), true);
102+
SHADOW_COLOR_MODE = schema.enumOption(key("emit/shadow_color"), ShadowColorEmitMode.class, ShadowColorEmitMode.EMIT_INTEGER);
103+
EMIT_HOVER_EVENT_TYPE = schema.enumOption(key("emit/hover_value_mode"), HoverEventValueMode.class, HoverEventValueMode.SNAKE_CASE);
104+
EMIT_CLICK_EVENT_TYPE = schema.enumOption(key("emit/click_value_mode"), ClickEventValueMode.class, ClickEventValueMode.SNAKE_CASE);
105+
EMIT_DEFAULT_ITEM_HOVER_QUANTITY = schema.booleanOption(key("emit/default_item_hover_quantity"), true);
106+
EMIT_DEFAULT_NBT_INTERPRET_VALUE = schema.booleanOption(key("emit/default_nbt_interpret_value"), true);
107+
EMIT_SHOW_ENTITY_UUID_TYPE = schema.enumOption(key("emit/show_entity_uuid"), ShowEntityUUIDEmitMode.class, ShowEntityUUIDEmitMode.EMIT_INT_ARRAY);
108+
SCHEMA = schema.frozenView();
109+
}
88110

89111
private NBTSerializerOptions() {
90112
}
@@ -93,6 +115,16 @@ private static String key(final String value) {
93115
return "adventure:nbt/" + value;
94116
}
95117

118+
/**
119+
* A schema of available options.
120+
*
121+
* @return the schema of known NBT serializer options
122+
* @since 4.20.0
123+
*/
124+
public static @NotNull OptionSchema schema() {
125+
return SCHEMA;
126+
}
127+
96128
/**
97129
* Configure how to emit hover event values.
98130
*

0 commit comments

Comments
 (0)