From abba88112af8f8b91a9591639759936bbb54204a Mon Sep 17 00:00:00 2001 From: Camotoy <20743703+Camotoy@users.noreply.github.com> Date: Mon, 24 Apr 2023 18:44:33 -0400 Subject: [PATCH] Use all 1.19.80 Bedrock legacy codes for color downsampling This will make RGB downsampling a bit more colorful. --- .../translator/text/MessageTranslator.java | 62 ++++++++++++++++--- gradle/libs.versions.toml | 2 +- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/org/geysermc/geyser/translator/text/MessageTranslator.java b/core/src/main/java/org/geysermc/geyser/translator/text/MessageTranslator.java index 8fe842e1a..4718ed470 100644 --- a/core/src/main/java/org/geysermc/geyser/translator/text/MessageTranslator.java +++ b/core/src/main/java/org/geysermc/geyser/translator/text/MessageTranslator.java @@ -29,8 +29,10 @@ import com.github.steveice10.mc.protocol.data.DefaultComponentSerializer; import com.github.steveice10.mc.protocol.data.game.scoreboard.TeamColor; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TranslatableComponent; +import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.renderer.TranslatableComponentRenderer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.CharacterAndFormat; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.cloudburstmc.protocol.bedrock.packet.TextPacket; @@ -48,6 +50,9 @@ public class MessageTranslator { // 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; + private static final LegacyComponentSerializer LEGACY_SERIALIZER; + private static final String ALL_COLORS; + // Store team colors for player names private static final Map TEAM_COLORS = new EnumMap<>(TeamColor.class); @@ -93,6 +98,48 @@ public class MessageTranslator { GSON_SERIALIZER = new GsonComponentSerializerWrapper(source); // Tell MCProtocolLib to use this serializer, too. DefaultComponentSerializer.set(GSON_SERIALIZER); + + LegacyComponentSerializer legacySerializer; + String allColors; + try { + Class.forName("net.kyori.adventure.text.serializer.legacy.CharacterAndFormat"); + + List formats = new ArrayList<>(CharacterAndFormat.defaults()); + // The following two do not yet exist on Bedrock - https://bugs.mojang.com/browse/MCPE-41729 + formats.remove(CharacterAndFormat.STRIKETHROUGH); + formats.remove(CharacterAndFormat.UNDERLINED); + + formats.add(CharacterAndFormat.characterAndFormat('g', TextColor.color(221, 214, 5))); // Minecoin Gold + // Add the new characters implemented in 1.19.80 + formats.add(CharacterAndFormat.characterAndFormat('h', TextColor.color(227, 212, 209))); // Quartz + formats.add(CharacterAndFormat.characterAndFormat('i', TextColor.color(206, 212, 202))); // Iron + formats.add(CharacterAndFormat.characterAndFormat('j', TextColor.color(68, 58, 59))); // Netherite + formats.add(CharacterAndFormat.characterAndFormat('m', TextColor.color(151, 22, 7))); // Redstone + formats.add(CharacterAndFormat.characterAndFormat('n', TextColor.color(180, 104, 77))); // Copper + formats.add(CharacterAndFormat.characterAndFormat('p', TextColor.color(222, 177, 45))); // Gold + formats.add(CharacterAndFormat.characterAndFormat('q', TextColor.color(17, 160, 54))); // Emerald + formats.add(CharacterAndFormat.characterAndFormat('s', TextColor.color(44, 186, 168))); // Diamond + formats.add(CharacterAndFormat.characterAndFormat('t', TextColor.color(33, 73, 123))); // Lapis + formats.add(CharacterAndFormat.characterAndFormat('u', TextColor.color(154, 92, 198))); // Amethyst + + legacySerializer = LegacyComponentSerializer.legacySection().toBuilder() + .formats(formats) + .build(); + + StringBuilder colorBuilder = new StringBuilder(); + for (CharacterAndFormat format : formats) { + if (format.format() instanceof TextColor) { + colorBuilder.append(format.character()); + } + } + allColors = colorBuilder.toString(); + } catch (ClassNotFoundException ignored) { + // Velocity doesn't have this yet. + legacySerializer = LegacyComponentSerializer.legacySection(); + allColors = "0123456789abcdef"; + } + LEGACY_SERIALIZER = legacySerializer; + ALL_COLORS = allColors; } /** @@ -107,7 +154,7 @@ public class MessageTranslator { // Translate any components that require it message = RENDERER.render(message, locale); - String legacy = LegacyComponentSerializer.legacySection().serialize(message); + String legacy = LEGACY_SERIALIZER.serialize(message); StringBuilder finalLegacy = new StringBuilder(); char[] legacyChars = legacy.toCharArray(); @@ -123,16 +170,13 @@ public class MessageTranslator { } char next = legacyChars[++i]; - if (next != 'm' && next != 'n') { - // Strikethrough and underline do not exist on Bedrock - if ((next >= '0' && next <= '9') || (next >= 'a' && next <= 'f')) { - // Append this color code, as well as a necessary reset code - if (!lastFormatReset) { - finalLegacy.append(RESET); - } + if (ALL_COLORS.indexOf(next) != -1) { + // Append this color code, as well as a necessary reset code + if (!lastFormatReset) { + finalLegacy.append(RESET); } - finalLegacy.append(BASE).append(next); } + finalLegacy.append(BASE).append(next); lastFormatReset = next == 'r'; } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index a2223abf7..4777f442a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,7 +13,7 @@ protocol = "3.0.0.Beta1-SNAPSHOT" raknet = "0.0.1.Final-SNAPSHOT" mcauthlib = "d9d773e" mcprotocollib = "1.19.4-math2-SNAPSHOT" -adventure = "4.14.0-20230418.205641-5" +adventure = "4.14.0-20230424.215040-7" adventure-platform = "4.1.2" junit = "5.9.2" checkerframework = "3.19.0"