Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
6d9f630
feat: basic component serializing to nbt (without styling)
Codestech1 Jun 8, 2024
d299dd6
feat: serialize component children and types
Codestech1 Jun 8, 2024
302310e
misc: i'm too lazy to name this commit, but it fixes and adds a lot o…
Codestech1 Jun 9, 2024
c0de1c3
chore: remove useless gson serializer
Codestech1 Jun 9, 2024
d3e7142
chore: add support for legacy item tags
Codestech1 Jun 9, 2024
46d98f9
feat: component deserialization
Codestech1 Jun 9, 2024
4fa0add
fix: translatable component arguments aren't deserializing
Codestech1 Jun 9, 2024
c075dbd
fix: legacy hover events aren't serialized properly
Codestech1 Jun 12, 2024
d84df6e
fix: serializing show entity as a legacy hover event throws an except…
Codestech1 Jun 12, 2024
555d838
misc: split the style serialization and deserialization to other class
Codestech1 Jun 13, 2024
1131031
feat: add an emit rgb flag
Codestech1 Jun 13, 2024
fda8f6d
feat: add a builder for the nbt component serializer
Codestech1 Jun 13, 2024
a59ee73
chore: add a non-null requirement to NBTComponentSerializerImpl.Build…
Codestech1 Jun 13, 2024
2219847
feat: nbt component providers
Codestech1 Jun 13, 2024
45dfb49
chore: add remaining serializer options to builder
Codestech1 Jun 15, 2024
3c9cd80
chore: cleanup
Codestech1 Jun 15, 2024
84671f8
chore: add javadocs to and simplify NBTSerializerOptions and add lice…
Codestech1 Jun 15, 2024
be0cc07
chore: add javadocs for public classes
Codestech1 Jun 15, 2024
766be5e
chore: add tests
Codestech1 Jun 15, 2024
3495750
fix: hover events are not serialized properly, feat: add a test for h…
Codestech1 Jun 15, 2024
09b47d0
feat: add a test for click event
Codestech1 Jun 15, 2024
7152ebb
chore: resolve requested changes
Codestech1 Jun 19, 2024
840108a
feat: add a possibility to (de)-serialize a style without a component
Codestech1 Jun 19, 2024
5a661d4
chore: simplify serializing text colors with TextColor#toString
Codestech1 Jun 21, 2024
0686e96
feat: shadow color serializing
Codestech1 Jun 20, 2025
5d8655f
chore: update all `@since` tags to use version 4.24.0
Codestech1 Jun 20, 2025
0b2df75
chore: update ClickEventSerializer to support the latest format
Codestech1 Jun 21, 2025
de47cf4
chore: create ShowItemSerializer for future use in StyleSerializer to…
Codestech1 Jun 21, 2025
fb7acc2
misc: fix double colon in JSON ShowItemSerializer
Codestech1 Jun 21, 2025
1f14b30
chore: update ShowEntitySerializer to support the latest format
Codestech1 Jun 21, 2025
db24ed4
chore: update HoverEventSerializer to support the latest format
Codestech1 Jun 21, 2025
a5add7f
chore: add missing options to the NBTSerializerOptions
Codestech1 Jun 21, 2025
8d8c702
chore: apply new serializing system of click events and hover events …
Codestech1 Jun 21, 2025
0249ee4
chore: update StyleSerializer fields to support the latest format
Codestech1 Jun 21, 2025
05a0ffd
chore: serialize and deserialize text decorations in loops
Codestech1 Jun 22, 2025
244d4ed
chore: ensure order of serializing and use ComponentTreeConstants in …
Codestech1 Jun 22, 2025
d6e6302
chore: do not serialize or deserialize "type" fields of components as…
Codestech1 Jun 22, 2025
7279f3b
chore: remove serialize/deserialize data sources TODO
Codestech1 Jun 22, 2025
3839257
feat: add an option for emitting default nbt component interpret value
Codestech1 Jun 22, 2025
f3f0ef4
chore: remove redundant cast in NBTSerializerUtils#getRequiredTag
Codestech1 Jun 22, 2025
82b05af
chore: fix spacing in NBTSerializerUtils
Codestech1 Jun 22, 2025
58965ed
chore: vanilla-like component children serializing
Codestech1 Jun 22, 2025
4f4b312
chore: vanilla-like translatable component arguments serializing
Codestech1 Jun 25, 2025
8830759
chore: add missing license headers and update to 2025
Codestech1 Jun 25, 2025
2f09462
chore: use TextDecoration#values for style decoration serialization (…
Codestech1 Jun 25, 2025
c570d11
chore: use option schema for NBT serializer options instead
Codestech1 Jun 26, 2025
cd6d689
feat: deserializing components from list binary tags
Codestech1 Jun 26, 2025
5dee5a6
feat: deserialize and serialize legacy additional data of show item h…
Codestech1 Jun 26, 2025
ea06fde
chore: make the code consistent
Codestech1 Jun 28, 2025
fefcdf0
feat: initial versioned option state with options for 23w40a
Codestech1 Jun 28, 2025
cfda0f7
feat: support plain string show-item hover events in deserialization
Codestech1 Jun 28, 2025
7462373
feat: add 24w09a version to the versioned option state
Codestech1 Jun 28, 2025
0521b6e
feat: add 24w44a version to the versioned option state
Codestech1 Jun 28, 2025
34a851d
feat: add 25w02a version to the versioned option state
Codestech1 Jun 28, 2025
00982a2
chore: remove unneeded options
Codestech1 Jun 28, 2025
b30b969
chore: replace ClickEventSerializer#deserializeCamel and ClickEventSe…
Codestech1 Jun 28, 2025
db4c094
chore: add TODO messages about show_text field names in HoverEventSer…
Codestech1 Jun 28, 2025
46776c8
chore: review whether the serialization matches Minecraft 23w40a and …
Codestech1 Jun 28, 2025
1fdd7b6
chore: remove all previous tests
Codestech1 Jun 28, 2025
2a06206
feat: text component serialization test
Codestech1 Jun 28, 2025
c0b02ff
chore: use ComponentTreeConstants instead for field names in TextComp…
Codestech1 Jun 28, 2025
089a79f
feat: translatable component serialization test
Codestech1 Jun 28, 2025
8422ca2
chore: make TextComponentTest package-private
Codestech1 Jun 28, 2025
92abc51
feat: keybind component serialization test
Codestech1 Jun 28, 2025
fe2dc95
feat: score component serialization test
Codestech1 Jun 28, 2025
7039ecc
feat: selector component serialization test
Codestech1 Jun 28, 2025
6e84d01
feat: block NBT component serialization test
Codestech1 Jun 29, 2025
9d4472b
feat: entity NBT component serialization test
Codestech1 Jun 29, 2025
d58e6ea
feat: storage NBT component serialization test
Codestech1 Jun 29, 2025
c1821c6
feat: component style serialization test, chore: use jetbrains not-nu…
Codestech1 Jun 29, 2025
262c91f
feat: add a test for deserializing component from lists, fix: deseria…
Codestech1 Jun 29, 2025
5afac86
feat: show entity serialization test
Codestech1 Jun 29, 2025
249b64f
feat: show item serialization test
Codestech1 Jun 30, 2025
0ceb729
chore: cleanup tests
Codestech1 Jun 30, 2025
ff3360e
feat: legacy hover event deserializing
Codestech1 Jun 30, 2025
1a01102
chore: replace Key serialization with KeySerializer
Codestech1 Jun 30, 2025
bc5a781
chore: resolve TODOs
Codestech1 Jul 1, 2025
c2141bf
chore: cleanup
Codestech1 Jul 1, 2025
658f9b5
chore: fix style of the main module
Codestech1 Jul 1, 2025
cdadca3
chore: fix style of the test module
Codestech1 Jul 1, 2025
2966d88
fix: spotless task is failing
Codestech1 Jul 1, 2025
3a724bb
fix: lists are not serialized as arrays when it is possible (vanilla …
Codestech1 Jul 2, 2025
c50ff9c
fix(nbt): TagStringWriter does not write compound entries in vanilla …
Codestech1 Jul 3, 2025
84c9988
chore: replace NBTSerializerOptions#SHADOW_COLOR_MODE with NBTSeriali…
Codestech1 Jul 6, 2025
1d1def4
fix: optimized lists are emitted on 25w04a and above
Codestech1 Jul 6, 2025
9677185
chore: replace adventure version references in the NBT Serializer fro…
Codestech1 Aug 11, 2025
e33a3a5
fix: adapt to sealed ClickEvent action hierarchy and single-type-para…
MelonHell Apr 18, 2026
42ff0c6
feat: object component serialization
MelonHell Apr 18, 2026
2298158
feat: nbt component plain flag serialization
MelonHell Apr 18, 2026
6076bfe
feat: emit click url https option
MelonHell Apr 18, 2026
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
17 changes: 13 additions & 4 deletions nbt/src/main/java/net/kyori/adventure/nbt/TagStringWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

import java.io.IOException;
import java.io.Writer;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* An emitter for the SNBT format.
Expand Down Expand Up @@ -80,10 +82,17 @@ public TagStringWriter writeTag(final BinaryTag tag) throws IOException {

private TagStringWriter writeCompound(final CompoundBinaryTag tag) throws IOException {
this.beginCompound();
for (final Map.Entry<String, ? extends BinaryTag> entry : tag) {
this.key(entry.getKey());
this.writeTag(entry.getValue());

final List<String> keys = new ArrayList<>(tag.keySet());
Collections.sort(keys);

for (final String key : keys) {
final BinaryTag value = tag.get(key);
if (value == null) continue;
this.key(key);
this.writeTag(value);
}

this.endCompound();
return this;
}
Expand Down
10 changes: 5 additions & 5 deletions nbt/src/test/resources/bigtest.snbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
shortTest: 32767s,
longTest: 9223372036854775807L,
byteTest: 127b,
"byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))": [B; 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B, 0B, 62B, 34B, 16B, 8B, 10B, 22B, 44B, 76B, 18B, 70B, 32B, 4B, 86B, 78B, 80B, 92B, 14B, 46B, 88B, 40B, 2B, 74B, 56B, 48B, 50B, 62B, 84B, 16B, 58B, 10B, 72B, 44B, 26B, 18B, 20B, 32B, 54B, 86B, 28B, 80B, 42B, 14B, 96B, 88B, 90B, 2B, 24B, 56B, 98B, 50B, 12B, 84B, 66B, 58B, 60B, 72B, 94B, 26B, 68B, 20B, 82B, 54B, 36B, 28B, 30B, 42B, 64B, 96B, 38B, 90B, 52B, 24B, 6B, 98B, 0B, 12B, 34B, 66B, 8B, 60B, 22B, 94B, 76B, 68B, 70B, 82B, 4B, 36B, 78B, 30B, 92B, 64B, 46B, 38B, 40B, 52B, 74B, 6B, 48B],
"listTest (long)": [11L, 12L, 13L, 14L, 15L],
floatTest: 0.49823147f,
byteTest: 127b,
doubleTest: 0.4931287132182315d,
floatTest: 0.49823147f,
intTest: 2147483647,
"listTest (compound)": [
{
Expand All @@ -17,6 +14,8 @@
name: "Compound tag #1"
}
],
"listTest (long)": [11L, 12L, 13L, 14L, 15L],
longTest: 9223372036854775807L,
"nested compound test": {
egg: {
name: "Eggbert",
Expand All @@ -27,5 +26,6 @@
value: 0.75f
}
},
shortTest: 32767s,
stringTest: "HELLO WORLD THIS IS A TEST STRING ÅÄÖ!"
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ sequenceOf(
"text-serializer-legacy",
"text-serializer-plain",
"text-serializer-ansi",
"text-serializer-nbt"
).forEach {
include("adventure-$it")
project(":adventure-$it").projectDir = file(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ public final class ComponentTreeConstants {
@ApiStatus.Obsolete
public static final String HOVER_EVENT_VALUE = "value";
@ApiStatus.Obsolete
public static final String SHOW_TEXT_TEXT = "text";
@ApiStatus.Obsolete
public static final String SHOW_ENTITY_TYPE = "type";
public static final String SHOW_ENTITY_ID = "id";
public static final String SHOW_ENTITY_UUID = "uuid";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void write(final JsonWriter out, final HoverEvent.ShowItem value) throws
out.name(SHOW_ITEM_COMPONENTS);
out.beginObject();
for (final Map.Entry<Key, GsonDataComponentValue> entry : value.dataComponentsAs(GsonDataComponentValue.class).entrySet()) {
final JsonElement el = entry.getValue().element();;
final JsonElement el = entry.getValue().element();
if (el instanceof JsonNull) { // removed
out.name(DATA_COMPONENT_REMOVAL_PREFIX + entry.getKey().asString());
out.beginObject().endObject();
Expand Down
14 changes: 14 additions & 0 deletions text-serializer-nbt/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id("adventure.common-conventions")
}

dependencies {
api(libs.option)
api(projects.adventureApi)
api(projects.adventureNbt)
compileOnlyApi(libs.autoService.annotations)
implementation(projects.adventureTextSerializerCommons)
annotationProcessor(libs.autoService)
}

applyJarMetadata("net.kyori.adventure.text.serializer.nbt")
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2025 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.serializer.nbt;

import java.io.IOException;
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.BinaryTagTypes;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.StringBinaryTag;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.event.ClickEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.CLICK_EVENT_ACTION;
import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.CLICK_EVENT_COMMAND;
import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.CLICK_EVENT_ID;
import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.CLICK_EVENT_PAGE;
import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.CLICK_EVENT_PAYLOAD;
import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.CLICK_EVENT_URL;
import static net.kyori.adventure.text.serializer.commons.ComponentTreeConstants.CLICK_EVENT_VALUE;
import static net.kyori.adventure.text.serializer.nbt.NBTSerializerUtils.SNBT_CODEC;
import static net.kyori.adventure.text.serializer.nbt.NBTSerializerUtils.requiredTag;

final class ClickEventSerializer {

private static final String FALLBACK_URL_PROTOCOL = "https://";

private ClickEventSerializer() {
}

static @Nullable ClickEvent<?> deserialize(final @NotNull CompoundBinaryTag compound, final boolean snakeCase) {
final StringBinaryTag actionTag = requiredTag(compound, CLICK_EVENT_ACTION, BinaryTagTypes.STRING);
final ClickEvent.Action<?> action = ClickEvent.Action.NAMES.valueOrThrow(actionTag.value());

if (!action.readable()) {
return null;
}

if (snakeCase) {
return switch (action) {
case ClickEvent.Action.OpenUrl ignored ->
ClickEvent.openUrl(requiredTag(compound, CLICK_EVENT_URL, BinaryTagTypes.STRING).value());
case ClickEvent.Action.RunCommand ignored ->
ClickEvent.runCommand(requiredTag(compound, CLICK_EVENT_COMMAND, BinaryTagTypes.STRING).value());
case ClickEvent.Action.SuggestCommand ignored ->
ClickEvent.suggestCommand(requiredTag(compound, CLICK_EVENT_COMMAND, BinaryTagTypes.STRING).value());
case ClickEvent.Action.ChangePage ignored ->
ClickEvent.changePage(requiredTag(compound, CLICK_EVENT_PAGE, BinaryTagTypes.INT).value());
case ClickEvent.Action.CopyToClipboard ignored ->
ClickEvent.copyToClipboard(requiredTag(compound, CLICK_EVENT_VALUE, BinaryTagTypes.STRING).value());
case ClickEvent.Action.Custom ignored -> {
try {
final StringBinaryTag clickEventIdTag = requiredTag(compound, CLICK_EVENT_ID, BinaryTagTypes.STRING);
final BinaryTag payloadTag = requiredTag(compound, CLICK_EVENT_PAYLOAD);
yield ClickEvent.custom(KeySerializer.deserialize(clickEventIdTag), BinaryTagHolder.encode(payloadTag, SNBT_CODEC));
} catch (final IOException exception) {
throw new RuntimeException("An error occurred while encoding payload tag", exception);
}
}
// Non-readable actions are filtered out above.
case ClickEvent.Action.OpenFile ignored -> null;
case ClickEvent.Action.ShowDialog ignored -> null;
};
} else {
final String value = requiredTag(compound, CLICK_EVENT_VALUE, BinaryTagTypes.STRING).value();
if (action instanceof ClickEvent.Action.TextCarrier) {
@SuppressWarnings("unchecked")
final ClickEvent.Action<ClickEvent.Payload.Text> textAction = (ClickEvent.Action<ClickEvent.Payload.Text>) action;
return ClickEvent.clickEvent(textAction, ClickEvent.Payload.string(value));
} else if (action instanceof ClickEvent.Action.ChangePage) {
return ClickEvent.changePage(Integer.parseInt(value));
}
return null;
}
}

static @Nullable CompoundBinaryTag serialize(final @NotNull ClickEvent<?> event, final boolean snakeCase,
final @NotNull NBTComponentSerializerImpl serializer) {
final ClickEvent.Action<?> action = event.action();
if (!action.readable()) {
return null;
}

final CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder()
.putString(CLICK_EVENT_ACTION, ClickEvent.Action.NAMES.keyOrThrow(action));

final boolean emitHttps = serializer.options().value(NBTSerializerOptions.EMIT_CLICK_URL_HTTPS);

if (snakeCase) {
final ClickEvent.Payload payload = event.payload();
switch (payload) {
case ClickEvent.Payload.Text text -> {
final String payloadFieldName = switch (action) {
case ClickEvent.Action.OpenUrl ignored -> CLICK_EVENT_URL;
case ClickEvent.Action.RunCommand ignored -> CLICK_EVENT_COMMAND;
case ClickEvent.Action.SuggestCommand ignored -> CLICK_EVENT_COMMAND;
case ClickEvent.Action.CopyToClipboard ignored -> CLICK_EVENT_VALUE;
default -> throw new IllegalArgumentException("Unexpected text-payload click event action: " + action);
};
builder.putString(payloadFieldName, textPayloadValue(action, text.value(), emitHttps));
}
case ClickEvent.Payload.Int intPayload -> builder.putInt(CLICK_EVENT_PAGE, intPayload.integer());
case ClickEvent.Payload.Custom customPayload -> {
try {
builder.put(CLICK_EVENT_ID, KeySerializer.serialize(customPayload.key()));
final BinaryTagHolder nbt = customPayload.nbt();
if (nbt != null) {
builder.put(CLICK_EVENT_PAYLOAD, nbt.get(SNBT_CODEC));
}
} catch (final IOException exception) {
throw new RuntimeException("An error occurred while decoding a payload tag", exception);
}
}
case ClickEvent.Payload.Dialog ignored -> {
}
}
} else {
final ClickEvent.Payload payload = event.payload();
final String value = switch (payload) {
case ClickEvent.Payload.Text text -> textPayloadValue(action, text.value(), emitHttps);
case ClickEvent.Payload.Int intPayload -> String.valueOf(intPayload.integer());
case ClickEvent.Payload.Custom ignored -> null;
case ClickEvent.Payload.Dialog ignored -> null;
};
if (value == null) {
return null;
}
builder.putString(CLICK_EVENT_VALUE, value);
}

return builder.build();
}

private static @NotNull String textPayloadValue(final ClickEvent.@NotNull Action<?> action, final @NotNull String value, final boolean emitHttps) {
if (emitHttps && action == ClickEvent.Action.OPEN_URL && !hasUrlScheme(value)) {
return FALLBACK_URL_PROTOCOL + value;
}
return value;
}

@SuppressWarnings("HttpUrlsUsage")
private static boolean hasUrlScheme(final @NotNull String url) {
return url.startsWith("http://") || url.startsWith("https://");
}
}
Loading