From 0e06a79e5f56698ace9a003e80265a200b27d161 Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Wed, 20 Oct 2021 18:42:02 -0400 Subject: [PATCH] Temporary workaround to fix null issue with components --- .../chat/GsonComponentSerializerWrapper.java | 84 +++++++++++++++++++ .../translators/chat/MessageTranslator.java | 8 +- .../title/JavaSetTitleTextTranslator.java | 2 +- .../chat/MessageTranslatorTest.java | 8 ++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/network/translators/chat/GsonComponentSerializerWrapper.java diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/chat/GsonComponentSerializerWrapper.java b/connector/src/main/java/org/geysermc/connector/network/translators/chat/GsonComponentSerializerWrapper.java new file mode 100644 index 000000000..c5888c06a --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/network/translators/chat/GsonComponentSerializerWrapper.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019-2021 GeyserMC. http://geysermc.org + * + * 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. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.connector.network.translators.chat; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.UnaryOperator; + +/** + * A wrapper around a normal GsonComponentSerializer to accept null components. + */ +public final class GsonComponentSerializerWrapper implements GsonComponentSerializer { + private final GsonComponentSerializer source; + + public GsonComponentSerializerWrapper(GsonComponentSerializer source) { + this.source = source; + } + + @Override + public @NotNull Gson serializer() { + return this.source.serializer(); + } + + @Override + public @NotNull UnaryOperator populator() { + return this.source.populator(); + } + + @Override + public @NotNull Component deserializeFromTree(@NotNull JsonElement input) { + // This has yet to be an issue, so it won't be overridden unless we have to + return this.source.deserializeFromTree(input); + } + + @Override + public @NotNull JsonElement serializeToTree(@NotNull Component component) { + return this.source.serializeToTree(component); + } + + @Override + public @Nullable Component deserialize(@NotNull String input) { + // See https://github.com/KyoriPowered/adventure/issues/447 + return this.serializer().fromJson(input, Component.class); + } + + @Override + public @NotNull String serialize(@NotNull Component component) { + return this.source.serialize(component); + } + + @Override + public @NotNull Builder toBuilder() { + return this.source.toBuilder(); + } +} diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/chat/MessageTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/chat/MessageTranslator.java index 1f0380f2f..520e27455 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/chat/MessageTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/chat/MessageTranslator.java @@ -45,7 +45,7 @@ public class MessageTranslator { private static final TranslatableComponentRenderer RENDERER = new MinecraftTranslationRegistry(); // Possible TODO: replace the legacy hover event serializer with an empty one since we have no use for hover events - private static final GsonComponentSerializer GSON_SERIALIZER = DefaultComponentSerializer.get(); + private static final GsonComponentSerializer GSON_SERIALIZER; // Store team colors for player names private static final Map TEAM_COLORS = new EnumMap<>(TeamColor.class); @@ -86,6 +86,12 @@ public class MessageTranslator { TEAM_COLORS.put(TeamColor.BOLD, BASE + "l"); TEAM_COLORS.put(TeamColor.STRIKETHROUGH, BASE + "m"); TEAM_COLORS.put(TeamColor.ITALIC, BASE + "o"); + + // Temporary fix for https://github.com/KyoriPowered/adventure/issues/447 + GsonComponentSerializer source = DefaultComponentSerializer.get(); + GSON_SERIALIZER = new GsonComponentSerializerWrapper(source); + // Tell MCProtocolLib to use this serializer, too. + DefaultComponentSerializer.set(GSON_SERIALIZER); } /** diff --git a/connector/src/main/java/org/geysermc/connector/network/translators/java/title/JavaSetTitleTextTranslator.java b/connector/src/main/java/org/geysermc/connector/network/translators/java/title/JavaSetTitleTextTranslator.java index 199d05e3b..ddaa5b677 100644 --- a/connector/src/main/java/org/geysermc/connector/network/translators/java/title/JavaSetTitleTextTranslator.java +++ b/connector/src/main/java/org/geysermc/connector/network/translators/java/title/JavaSetTitleTextTranslator.java @@ -38,7 +38,7 @@ public class JavaSetTitleTextTranslator extends PacketTranslator