Skip to content

NBT Component Serializer #1084

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

Open
wants to merge 87 commits into
base: main/4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
29c997a
feat: basic component serializing to nbt (without styling)
Codestech1 Jun 8, 2024
34a381a
feat: serialize component children and types
Codestech1 Jun 8, 2024
5bb4c5a
misc: i'm too lazy to name this commit, but it fixes and adds a lot o…
Codestech1 Jun 9, 2024
97809cf
chore: remove useless gson serializer
Codestech1 Jun 9, 2024
1c750c3
chore: add support for legacy item tags
Codestech1 Jun 9, 2024
92ea881
feat: component deserialization
Codestech1 Jun 9, 2024
9858a9f
fix: translatable component arguments aren't deserializing
Codestech1 Jun 9, 2024
4c7b00c
fix: legacy hover events aren't serialized properly
Codestech1 Jun 12, 2024
0a52c01
fix: serializing show entity as a legacy hover event throws an except…
Codestech1 Jun 12, 2024
a020c0a
misc: split the style serialization and deserialization to other class
Codestech1 Jun 13, 2024
a39a101
feat: add an emit rgb flag
Codestech1 Jun 13, 2024
8e74088
feat: add a builder for the nbt component serializer
Codestech1 Jun 13, 2024
f96ca38
chore: add a non-null requirement to NBTComponentSerializerImpl.Build…
Codestech1 Jun 13, 2024
bba80fb
feat: nbt component providers
Codestech1 Jun 13, 2024
0ec4d53
chore: add remaining serializer options to builder
Codestech1 Jun 15, 2024
e8b5efd
chore: cleanup
Codestech1 Jun 15, 2024
f8427a0
chore: add javadocs to and simplify NBTSerializerOptions and add lice…
Codestech1 Jun 15, 2024
8c43bac
chore: add javadocs for public classes
Codestech1 Jun 15, 2024
2f4c78c
chore: add tests
Codestech1 Jun 15, 2024
2021707
fix: hover events are not serialized properly, feat: add a test for h…
Codestech1 Jun 15, 2024
9021233
feat: add a test for click event
Codestech1 Jun 15, 2024
c7f406a
chore: resolve requested changes
Codestech1 Jun 19, 2024
cc298cb
feat: add a possibility to (de)-serialize a style without a component
Codestech1 Jun 19, 2024
8996ab0
chore: simplify serializing text colors with TextColor#toString
Codestech1 Jun 21, 2024
43c34aa
Merge branch 'KyoriPowered:main/4' into feat/nbt-text-serializer
Codestech1 May 9, 2025
da4737c
Merge branch 'KyoriPowered:main/4' into feat/nbt-text-serializer
Codestech1 Jun 20, 2025
c5e3525
feat: shadow color serializing
Codestech1 Jun 20, 2025
6e77a33
chore: update all `@since` tags to use version 4.24.0
Codestech1 Jun 20, 2025
95bd146
chore: update ClickEventSerializer to support the latest format
Codestech1 Jun 21, 2025
847b477
chore: create ShowItemSerializer for future use in StyleSerializer to…
Codestech1 Jun 21, 2025
8cc7c47
misc: fix double colon in JSON ShowItemSerializer
Codestech1 Jun 21, 2025
5591b42
chore: update ShowEntitySerializer to support the latest format
Codestech1 Jun 21, 2025
f955ef6
chore: update HoverEventSerializer to support the latest format
Codestech1 Jun 21, 2025
473e199
chore: add missing options to the NBTSerializerOptions
Codestech1 Jun 21, 2025
7d90b2f
chore: apply new serializing system of click events and hover events …
Codestech1 Jun 21, 2025
1500a14
chore: update StyleSerializer fields to support the latest format
Codestech1 Jun 21, 2025
5214303
chore: serialize and deserialize text decorations in loops
Codestech1 Jun 22, 2025
bf2092f
chore: ensure order of serializing and use ComponentTreeConstants in …
Codestech1 Jun 22, 2025
b2e6cd1
chore: do not serialize or deserialize "type" fields of components as…
Codestech1 Jun 22, 2025
2c10acd
chore: remove serialize/deserialize data sources TODO
Codestech1 Jun 22, 2025
7dc38e1
feat: add an option for emitting default nbt component interpret value
Codestech1 Jun 22, 2025
69e2a3f
chore: remove redundant cast in NBTSerializerUtils#getRequiredTag
Codestech1 Jun 22, 2025
a45a90c
chore: fix spacing in NBTSerializerUtils
Codestech1 Jun 22, 2025
7f7ccd5
chore: vanilla-like component children serializing
Codestech1 Jun 22, 2025
3cb553e
chore: vanilla-like translatable component arguments serializing
Codestech1 Jun 25, 2025
a85d088
chore: add missing license headers and update to 2025
Codestech1 Jun 25, 2025
1a948f2
chore: use TextDecoration#values for style decoration serialization (…
Codestech1 Jun 25, 2025
ad8d730
chore: use option schema for NBT serializer options instead
Codestech1 Jun 26, 2025
619a62a
feat: deserializing components from list binary tags
Codestech1 Jun 26, 2025
923905c
feat: deserialize and serialize legacy additional data of show item h…
Codestech1 Jun 26, 2025
0a354ce
chore: make the code consistent
Codestech1 Jun 28, 2025
15c63b6
feat: initial versioned option state with options for 23w40a
Codestech1 Jun 28, 2025
cd4a440
feat: support plain string show-item hover events in deserialization
Codestech1 Jun 28, 2025
6674ca9
feat: add 24w09a version to the versioned option state
Codestech1 Jun 28, 2025
3eb33f2
feat: add 24w44a version to the versioned option state
Codestech1 Jun 28, 2025
7d7b6b4
feat: add 25w02a version to the versioned option state
Codestech1 Jun 28, 2025
4a36ed3
chore: remove unneeded options
Codestech1 Jun 28, 2025
f6b30be
chore: replace ClickEventSerializer#deserializeCamel and ClickEventSe…
Codestech1 Jun 28, 2025
67e5711
chore: add TODO messages about show_text field names in HoverEventSer…
Codestech1 Jun 28, 2025
310d841
chore: review whether the serialization matches Minecraft 23w40a and …
Codestech1 Jun 28, 2025
8b0c247
chore: remove all previous tests
Codestech1 Jun 28, 2025
fee8bd0
feat: text component serialization test
Codestech1 Jun 28, 2025
bac65e1
chore: use ComponentTreeConstants instead for field names in TextComp…
Codestech1 Jun 28, 2025
0ab8662
feat: translatable component serialization test
Codestech1 Jun 28, 2025
514f23f
chore: make TextComponentTest package-private
Codestech1 Jun 28, 2025
bf1d2ab
feat: keybind component serialization test
Codestech1 Jun 28, 2025
ab6ae46
feat: score component serialization test
Codestech1 Jun 28, 2025
0b1d3ad
feat: selector component serialization test
Codestech1 Jun 28, 2025
dd6fcd1
feat: block NBT component serialization test
Codestech1 Jun 29, 2025
44c7953
feat: entity NBT component serialization test
Codestech1 Jun 29, 2025
904748a
feat: storage NBT component serialization test
Codestech1 Jun 29, 2025
0d2d83c
feat: component style serialization test, chore: use jetbrains not-nu…
Codestech1 Jun 29, 2025
bcafca4
feat: add a test for deserializing component from lists, fix: deseria…
Codestech1 Jun 29, 2025
7a6d5ce
feat: show entity serialization test
Codestech1 Jun 29, 2025
5e22119
feat: show item serialization test
Codestech1 Jun 30, 2025
fac131f
chore: cleanup tests
Codestech1 Jun 30, 2025
f61a594
feat: legacy hover event deserializing
Codestech1 Jun 30, 2025
736b5d0
chore: replace Key serialization with KeySerializer
Codestech1 Jun 30, 2025
1b335f2
chore: resolve TODOs
Codestech1 Jul 1, 2025
e4cee93
chore: cleanup
Codestech1 Jul 1, 2025
1ef87f0
chore: fix style of the main module
Codestech1 Jul 1, 2025
f64f2f5
chore: fix style of the test module
Codestech1 Jul 1, 2025
d82991a
fix: spotless task is failing
Codestech1 Jul 1, 2025
b6e307c
fix: lists are not serialized as arrays when it is possible (vanilla …
Codestech1 Jul 2, 2025
7a6f3b9
fix(nbt): TagStringWriter does not write compound entries in vanilla …
Codestech1 Jul 3, 2025
a87c34a
chore: replace NBTSerializerOptions#SHADOW_COLOR_MODE with NBTSeriali…
Codestech1 Jul 6, 2025
96146cc
fix: optimized lists are emitted on 25w04a and above
Codestech1 Jul 6, 2025
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
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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
11 changes: 11 additions & 0 deletions text-serializer-nbt/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id("adventure.common-conventions")
}

dependencies {
api(libs.option)
api(projects.adventureApi)
api(projects.adventureNbt)
}

applyJarMetadata("net.kyori.adventure.text.serializer.nbt")
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2024 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 net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.text.event.ClickEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

final class ClickEventSerializer {

private static final String ACTION = "action";
private static final String VALUE = "value";

private static final String CLICK_EVENT_OPEN_URL = "open_url";
private static final String CLICK_EVENT_OPEN_FILE = "open_file";
private static final String CLICK_EVENT_RUN_COMMAND = "run_command";
private static final String CLICK_EVENT_SUGGEST_COMMAND = "suggest_command";
private static final String CLICK_EVENT_CHANGE_PAGE = "change_page";
private static final String CLICK_EVENT_COPY_TO_CLIPBOARD = "copy_to_clipboard";

private ClickEventSerializer() {
}

static @Nullable ClickEvent deserialize(@NotNull CompoundBinaryTag tag) {
ClickEvent.Action action;
String actionString = tag.getString(ACTION);

if (actionString.isEmpty()) {
return null;
}

switch (actionString) {
case CLICK_EVENT_OPEN_URL:
action = ClickEvent.Action.OPEN_URL;
break;
case CLICK_EVENT_OPEN_FILE:
action = ClickEvent.Action.OPEN_FILE;
break;
case CLICK_EVENT_RUN_COMMAND:
action = ClickEvent.Action.RUN_COMMAND;
break;
case CLICK_EVENT_SUGGEST_COMMAND:
action = ClickEvent.Action.SUGGEST_COMMAND;
break;
case CLICK_EVENT_CHANGE_PAGE:
action = ClickEvent.Action.CHANGE_PAGE;
break;
case CLICK_EVENT_COPY_TO_CLIPBOARD:
action = ClickEvent.Action.COPY_TO_CLIPBOARD;
break;
default:
return null;
}

return ClickEvent.clickEvent(action, tag.getString(VALUE));
}

static @NotNull CompoundBinaryTag serialize(@NotNull ClickEvent event) {
ClickEvent.Action action = event.action();
String actionString;

switch (action) {
case OPEN_URL:
actionString = CLICK_EVENT_OPEN_URL;
break;
case OPEN_FILE:
actionString = CLICK_EVENT_OPEN_FILE;
break;
case RUN_COMMAND:
actionString = CLICK_EVENT_RUN_COMMAND;
break;
case SUGGEST_COMMAND:
actionString = CLICK_EVENT_SUGGEST_COMMAND;
break;
case CHANGE_PAGE:
actionString = CLICK_EVENT_CHANGE_PAGE;
break;
case COPY_TO_CLIPBOARD:
actionString = CLICK_EVENT_COPY_TO_CLIPBOARD;
break;
default:
// Never called, but needed for proper compilation
throw new IllegalStateException("Unknown click event action: " + action);
}

return CompoundBinaryTag.builder()
.putString(ACTION, actionString)
.putString(VALUE, event.value())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2024 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 net.kyori.adventure.key.Key;
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.StringBinaryTag;
import net.kyori.adventure.nbt.api.BinaryTagHolder;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.DataComponentValue;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

final class HoverEventSerializer {

private static final String HOVER_EVENT_ACTION = "action";
private static final String HOVER_EVENT_CONTENTS = "contents";
@Deprecated
private static final String HOVER_EVENT_VALUE = "value";

private static final String HOVER_EVENT_SHOW_TEXT = "show_text";
private static final String HOVER_EVENT_SHOW_ITEM = "show_item";
private static final String HOVER_EVENT_SHOW_ENTITY = "show_entity";
@Deprecated
private static final String HOVER_EVENT_SHOW_ACHIEVEMENT = "show_achievement";

private static final String SHOW_ITEM_ID = "id";
private static final String SHOW_ITEM_COUNT = "count";
private static final String SHOW_ITEM_COMPONENTS = "components";
@Deprecated
private static final String SHOW_ITEM_TAG = "tag";

private static final String SHOW_ENTITY_TYPE = "type";
private static final String SHOW_ENTITY_ID = "id";
private static final String SHOW_ENTITY_NAME = "name";

private HoverEventSerializer() {
}

static @NotNull HoverEvent<?> deserialize(@NotNull CompoundBinaryTag compound, @NotNull NBTComponentSerializerImpl serializer) {
String actionString = compound.getString(HOVER_EVENT_ACTION);
HoverEvent.Action<?> action = HoverEvent.Action.NAMES.valueOrThrow(actionString);
Class<?> actionType = action.type();

BinaryTag contents = compound.get(HOVER_EVENT_CONTENTS);
if (contents == null) {
contents = compound.get(HOVER_EVENT_VALUE);
if (contents == null) {
throw new IllegalArgumentException("The hover event doesn't contain any contents");
}
}

if (Component.class.isAssignableFrom(actionType)) {
return HoverEvent.showText(serializer.deserialize(contents));
} else if (HoverEvent.ShowItem.class.isAssignableFrom(actionType)) {
CompoundBinaryTag showItemContents = (CompoundBinaryTag) contents;

Key itemId = Key.key(showItemContents.getString(SHOW_ITEM_ID));
int itemCount = showItemContents.getInt(SHOW_ITEM_COUNT);

BinaryTag components = showItemContents.get(SHOW_ITEM_COMPONENTS);
BinaryTag tag = showItemContents.get(SHOW_ITEM_TAG);

if (components != null) {
CompoundBinaryTag componentsCompound = (CompoundBinaryTag) components;
Map<Key, DataComponentValue> componentValues = new HashMap<>();

for (String string : componentsCompound.keySet()) {
BinaryTag value = componentsCompound.get(string);
if (value == null) continue;
componentValues.put(Key.key(string), NBTDataComponentValue.nbtDataComponentValue(value));
}

return HoverEvent.showItem(itemId, itemCount, componentValues);
} else if (tag != null) {
BinaryTagHolder holder = BinaryTagHolder.binaryTagHolder(((StringBinaryTag) tag).value());
return HoverEvent.showItem(itemId, itemCount, holder);
} else {
return HoverEvent.showItem(itemId, itemCount);
}
} else if (HoverEvent.ShowEntity.class.isAssignableFrom(actionType)) {
CompoundBinaryTag showEntityContents = (CompoundBinaryTag) contents;

Key entityType = Key.key(showEntityContents.getString(SHOW_ENTITY_TYPE));
UUID entityId = UUID.fromString(showEntityContents.getString(SHOW_ENTITY_ID));

BinaryTag entityName = showEntityContents.get(SHOW_ENTITY_NAME);

if (entityName != null) {
return HoverEvent.showEntity(entityType, entityId, serializer.deserialize(entityName));
} else {
return HoverEvent.showEntity(entityType, entityId);
}
} else if (String.class.isAssignableFrom(actionType)) {
return HoverEvent.showAchievement(((StringBinaryTag) contents).value());
} else {
// Never called, but needed for proper compilation
throw new IllegalArgumentException("Unknown hover event");
}
}

static <V> @Nullable CompoundBinaryTag serialize(@NotNull HoverEvent<V> event, @NotNull NBTComponentSerializerImpl serializer) {
OptionState flags = serializer.flags();
HoverEvent.Action<V> action = event.action();

NBTSerializerOptions.HoverEventValueMode hoverMode = flags.value(NBTSerializerOptions.EMIT_HOVER_EVENT_TYPE);

if (action == HoverEvent.Action.SHOW_ACHIEVEMENT) {
if (hoverMode == NBTSerializerOptions.HoverEventValueMode.MODERN_ONLY) {
return null;
}

return CompoundBinaryTag.builder()
.putString(HOVER_EVENT_ACTION, HOVER_EVENT_SHOW_ACHIEVEMENT)
.putString(HOVER_EVENT_VALUE, (String) event.value())
.build();
}

BinaryTag contents;
String actionString;

if (action == HoverEvent.Action.SHOW_TEXT) {
contents = serializer.serialize((Component) event.value());
actionString = HOVER_EVENT_SHOW_TEXT;
} else if (action == HoverEvent.Action.SHOW_ITEM) {
HoverEvent.ShowItem item = (HoverEvent.ShowItem) event.value();

CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder()
.putString(SHOW_ITEM_ID, item.item().asString())
.putInt(SHOW_ITEM_COUNT, item.count());

Map<Key, NBTDataComponentValue> components = item.dataComponentsAs(NBTDataComponentValue.class);
NBTSerializerOptions.ShowItemHoverDataMode dataMode = flags.value(NBTSerializerOptions.SHOW_ITEM_HOVER_DATA_MODE);

if (!components.isEmpty() && dataMode != NBTSerializerOptions.ShowItemHoverDataMode.EMIT_LEGACY_NBT) {
CompoundBinaryTag.Builder dataComponentsBuilder = CompoundBinaryTag.builder();

for (Map.Entry<Key, NBTDataComponentValue> entry : components.entrySet()) {
dataComponentsBuilder.put(entry.getKey().asString(), entry.getValue().binaryTag());
}

builder.put(SHOW_ITEM_COMPONENTS, dataComponentsBuilder.build());
} else if (dataMode != NBTSerializerOptions.ShowItemHoverDataMode.EMIT_DATA_COMPONENTS) {
BinaryTagHolder holder = item.nbt();
if (holder != null) {
builder.putString(SHOW_ITEM_TAG, holder.string());
}
}

contents = builder.build();
actionString = HOVER_EVENT_SHOW_ITEM;
} else if (action == HoverEvent.Action.SHOW_ENTITY) {
if (hoverMode == NBTSerializerOptions.HoverEventValueMode.LEGACY_ONLY) {
return null;
}

HoverEvent.ShowEntity item = (HoverEvent.ShowEntity) event.value();

CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder()
.putString(SHOW_ENTITY_TYPE, item.type().asString())
.putString(SHOW_ENTITY_ID, item.id().toString());

Component customName = item.name();
if (customName != null) {
builder.put(SHOW_ENTITY_NAME, serializer.serialize(customName));
}

contents = builder.build();
actionString = HOVER_EVENT_SHOW_ENTITY;
} else {
throw new IllegalArgumentException("Don't know how to serialize " + event + " as a HoverEvent");
}

return CompoundBinaryTag.builder()
.putString(HOVER_EVENT_ACTION, actionString)
.put(hoverMode == NBTSerializerOptions.HoverEventValueMode.LEGACY_ONLY ? HOVER_EVENT_VALUE : HOVER_EVENT_CONTENTS, contents)
.build();
}
}
Loading