Use all 1.19.80 Bedrock legacy codes for color downsampling

This will make RGB downsampling a bit more colorful.
This commit is contained in:
Camotoy 2023-04-24 18:44:33 -04:00
parent bc66f81630
commit abba88112a
No known key found for this signature in database
GPG Key ID: 7EEFB66FE798081F
2 changed files with 54 additions and 10 deletions

View File

@ -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<TeamColor, String> 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<CharacterAndFormat> 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';
}

View File

@ -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"