Temporary workaround to fix null issue with components

This commit is contained in:
Camotoy 2021-10-20 18:42:02 -04:00
parent 6cbbf8a8dd
commit 0e06a79e5f
No known key found for this signature in database
GPG Key ID: 7EEFB66FE798081F
4 changed files with 100 additions and 2 deletions

View File

@ -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<GsonBuilder> 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();
}
}

View File

@ -45,7 +45,7 @@ public class MessageTranslator {
private static final TranslatableComponentRenderer<String> 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<TeamColor, String> 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);
}
/**

View File

@ -38,7 +38,7 @@ public class JavaSetTitleTextTranslator extends PacketTranslator<ServerSetTitleT
@Override
public void translate(GeyserSession session, ServerSetTitleTextPacket packet) {
String text;
if (packet.getText() == null) { //TODO 1.17 can this happen?
if (packet.getText() == null) { // This can happen, see https://github.com/KyoriPowered/adventure/issues/447
text = " ";
} else {
text = MessageTranslator.convertMessage(packet.getText(), session.getLocale());

View File

@ -25,6 +25,7 @@
package org.geysermc.connector.network.translators.chat;
import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
@ -60,6 +61,8 @@ public class MessageTranslatorTest {
//// Test translation with the ' character (which MessageFormat requires special handling for)
//messages.put("{\"translate\":\"commands.give.success.single\",\"with\":[{\"text\":\"1\"},{\"color\":\"yellow\",\"hoverEvent\":{\"action\":\"show_item\",\"contents\":{\"id\":\"minecraft:player_head\",\"tag\":\"{SkullOwner:\\\"Camotoy\\\"}\"}},\"translate\":\"chat.square_brackets\",\"with\":[{\"extra\":[{\"translate\":\"block.minecraft.player_head.named\",\"with\":[{\"text\":\"Camotoy\"}]}],\"text\":\"\"}]},{\"insertion\":\"DoctorMad9952\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/tell DoctorMad9952 \"},\"hoverEvent\":{\"action\":\"show_entity\",\"contents\":{\"type\":\"minecraft:player\",\"id\":\"8d712993-d208-3dac-b4d8-f2ce7e7d2b75\",\"name\":{\"text\":\"DoctorMad9952\"}}},\"extra\":[{\"text\":\"DoctorMad9952\"}],\"text\":\"\"}]}",
// "Gave 1 §r§e[Camotoy's Head]§r to DoctorMad9952");
MessageTranslator.init();
}
@Test
@ -77,4 +80,9 @@ public class MessageTranslatorTest {
Assert.assertEquals("Reset before message is not handled properly", "§r§eGame Selector", MessageTranslator.convertMessageLenient("§r§eGame Selector"));
Assert.assertEquals("Unimplemented formatting chars not stripped", "Bold Underline", MessageTranslator.convertMessageLenient("§m§nBold Underline"));
}
@Test
public void testNullTextPacket() {
DefaultComponentSerializer.get().deserialize("null");
}
}