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 1 commit
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,7 +28,8 @@
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;
Expand All @@ -47,6 +48,8 @@ private BukkitComponentSerializer() {
}

private static final boolean IS_1_16 = findEnum(Material.class, "NETHERITE_PICKAXE") != null;
private static final boolean IS_1_20_3 = findEnum(Material.class, "CRAFTER") != null;
private static final boolean IS_1_20_5 = findEnum(Material.class, "MACE") != null;

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

static {
FLATTENER = FacetComponentFlattener.get(Bukkit.getServer(), TRANSLATORS);
if (IS_1_16) {
if (IS_1_20_5) {
LEGACY_SERIALIZER = LegacyComponentSerializer.builder()
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.flattener(FLATTENER)
.build();
GSON_SERIALIZER = GsonComponentSerializer.builder()
.legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
.options(JSONOptions.byDataVersion().at(3819))
.build();
} else if (IS_1_20_3) {
LEGACY_SERIALIZER = LegacyComponentSerializer.builder()
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.flattener(FLATTENER)
.build();
GSON_SERIALIZER = GsonComponentSerializer.builder()
.legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
.options(JSONOptions.byDataVersion().at(3679))
.build();
} else if (IS_1_16) {
LEGACY_SERIALIZER = LegacyComponentSerializer.builder()
.hexColors()
.useUnusualXRepeatedCharacterHexFormat()
.flattener(FLATTENER)
.build();
GSON_SERIALIZER = GsonComponentSerializer.builder()
.legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
.options(JSONOptions.byDataVersion().at(2526))
.build();
} else {
LEGACY_SERIALIZER = LegacyComponentSerializer.builder()
Expand All @@ -74,7 +98,7 @@ private BukkitComponentSerializer() {
.build();
GSON_SERIALIZER = GsonComponentSerializer.builder()
.legacyHoverEventSerializer(NBTLegacyHoverEventSerializer.get())
.emitLegacyHoverEvent()
.options(JSONOptions.byDataVersion().at(0))
.downsampleColors()
.build();
}
Expand Down