Skip to content
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

Fix the Bukkit component serializer by setting the correct JSONOptions according to the server version #197

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

// Adventure version
ext.adventure = "4.13.1"
ext.adventure = "4.17.0"

group 'net.kyori'
version '4.3.5-SNAPSHOT'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
import net.kyori.adventure.platform.facet.FacetComponentFlattener;
import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.gson.legacyimpl.NBTLegacyHoverEventSerializer;
import net.kyori.adventure.text.serializer.json.JSONOptions;
import net.kyori.adventure.text.serializer.json.legacyimpl.NBTLegacyHoverEventSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.jetbrains.annotations.NotNull;

import static net.kyori.adventure.platform.bukkit.MinecraftReflection.findEnum;
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.findClass;
import static net.kyori.adventure.platform.bukkit.MinecraftReflection.hasMethod;

/**
* A pair of component serializers for {@link org.bukkit.Bukkit}.
Expand All @@ -46,7 +47,8 @@ public final class BukkitComponentSerializer {
private BukkitComponentSerializer() {
}

private static final boolean IS_1_16 = findEnum(Material.class, "NETHERITE_PICKAXE") != null;
private static final Class<?> UNSAFE_VALUES_CLASS = findClass("org.bukkit.UnsafeValues");
private static final int DATA_VERSION = hasMethod(UNSAFE_VALUES_CLASS, "getDataVersion") ? Bukkit.getUnsafe().getDataVersion() : 0;

private static final Collection<FacetComponentFlattener.Translator<Server>> TRANSLATORS = Facet.of(
SpigotFacet.Translator::new,
Expand All @@ -58,26 +60,22 @@ private BukkitComponentSerializer() {

static {
FLATTENER = FacetComponentFlattener.get(Bukkit.getServer(), TRANSLATORS);
if (IS_1_16) {
if (DATA_VERSION >= 2526) {
LEGACY_SERIALIZER = LegacyComponentSerializer.builder()
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.flattener(FLATTENER)
.build();
GSON_SERIALIZER = GsonComponentSerializer.builder()
.legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
.build();
} else {
LEGACY_SERIALIZER = LegacyComponentSerializer.builder()
.character(LegacyComponentSerializer.SECTION_CHAR)
.flattener(FLATTENER)
.build();
GSON_SERIALIZER = GsonComponentSerializer.builder()
.legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
.emitLegacyHoverEvent()
.downsampleColors()
.build();
}
GSON_SERIALIZER = GsonComponentSerializer.builder()
.legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
.options(JSONOptions.byDataVersion().at(DATA_VERSION))
.build();
}

/**
Expand Down