From 0a33132f14722cba0ee4912f5a124242e0dfae66 Mon Sep 17 00:00:00 2001 From: BoomEaro <21033866+BoomEaro@users.noreply.github.com> Date: Wed, 15 Jun 2022 14:10:36 +0300 Subject: [PATCH 1/2] HEX colors support --- .../botfilter/caching/CachedMessage.java | 12 +- .../botfilter/caching/CachedTitle.java | 8 +- .../botfilter/caching/PacketUtils.java | 10 +- .../ru/leymooo/botfilter/config/Settings.java | 8 +- .../leymooo/botfilter/utils/ColorsUtils.java | 123 ++++++++++++++++++ 5 files changed, 139 insertions(+), 22 deletions(-) create mode 100644 proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java diff --git a/proxy/src/main/java/ru/leymooo/botfilter/caching/CachedMessage.java b/proxy/src/main/java/ru/leymooo/botfilter/caching/CachedMessage.java index 89ad85f524..1c18c74eff 100644 --- a/proxy/src/main/java/ru/leymooo/botfilter/caching/CachedMessage.java +++ b/proxy/src/main/java/ru/leymooo/botfilter/caching/CachedMessage.java @@ -2,16 +2,14 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; -import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.ChatMessageType; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.ProtocolConstants; import net.md_5.bungee.protocol.packet.Chat; import net.md_5.bungee.protocol.packet.SystemChat; import ru.leymooo.botfilter.config.Settings; +import ru.leymooo.botfilter.utils.ColorsUtils; /** * @author Leymooo @@ -33,12 +31,8 @@ public CachedMessage(String message) private static DefinedPacket createMessagePacket(String message, boolean is119) { - message = ComponentSerializer.toString( - TextComponent.fromLegacyText( - ChatColor.translateAlternateColorCodes( '&', - message.replace( "%prefix%", Settings.IMP.MESSAGES.PREFIX ).replace( "%nl%", "\n" ) ) ) ); - - + message = ColorsUtils.serializeTextWithColorToJson( + message.replace( "%prefix%", Settings.IMP.MESSAGES.PREFIX ).replace( "%nl%", "\n" ) ); if ( is119 ) { return new SystemChat( message, ChatMessageType.SYSTEM.ordinal() ); diff --git a/proxy/src/main/java/ru/leymooo/botfilter/caching/CachedTitle.java b/proxy/src/main/java/ru/leymooo/botfilter/caching/CachedTitle.java index 49f863c7aa..c568a39ea1 100644 --- a/proxy/src/main/java/ru/leymooo/botfilter/caching/CachedTitle.java +++ b/proxy/src/main/java/ru/leymooo/botfilter/caching/CachedTitle.java @@ -2,15 +2,13 @@ import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.protocol.Protocol; import net.md_5.bungee.protocol.ProtocolConstants; import net.md_5.bungee.protocol.packet.Subtitle; import net.md_5.bungee.protocol.packet.Title; import net.md_5.bungee.protocol.packet.TitleTimes; import ru.leymooo.botfilter.config.Settings; +import ru.leymooo.botfilter.utils.ColorsUtils; /** * @author Leymooo @@ -34,7 +32,7 @@ public CachedTitle(String raw, int in, int stay, int out) this.title = new ByteBuf[PacketUtils.PROTOCOLS_COUNT]; Title titlePacket = new Title(); titlePacket.setAction( Title.Action.TITLE ); - titlePacket.setText( ComponentSerializer.toString( TextComponent.fromLegacyText( ChatColor.translateAlternateColorCodes( '&', title ) ) ) ); + titlePacket.setText( ColorsUtils.serializeTextWithColorToJson( title ) ); PacketUtils.fillArray( this.title, titlePacket, Protocol.GAME ); } if ( subtitle != null && !subtitle.isEmpty() ) @@ -42,7 +40,7 @@ public CachedTitle(String raw, int in, int stay, int out) this.subtitle = new ByteBuf[PacketUtils.PROTOCOLS_COUNT]; Title subTitlePacket = new Title(); subTitlePacket.setAction( Title.Action.SUBTITLE ); - subTitlePacket.setText( ComponentSerializer.toString( TextComponent.fromLegacyText( ChatColor.translateAlternateColorCodes( '&', subtitle ) ) ) ); + subTitlePacket.setText( ColorsUtils.serializeTextWithColorToJson( subtitle ) ); PacketUtils.fillArray( this.subtitle, subTitlePacket, ProtocolConstants.MINECRAFT_1_8, ProtocolConstants.MINECRAFT_1_16_4, Protocol.GAME ); Subtitle subtitlePacket1 = new Subtitle(); subtitlePacket1.setText( subTitlePacket.getText() ); diff --git a/proxy/src/main/java/ru/leymooo/botfilter/caching/PacketUtils.java b/proxy/src/main/java/ru/leymooo/botfilter/caching/PacketUtils.java index d959499ca6..f06ff2d50a 100644 --- a/proxy/src/main/java/ru/leymooo/botfilter/caching/PacketUtils.java +++ b/proxy/src/main/java/ru/leymooo/botfilter/caching/PacketUtils.java @@ -5,9 +5,6 @@ import io.netty.channel.Channel; import java.util.HashMap; import java.util.Random; -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.netty.ChannelWrapper; import net.md_5.bungee.protocol.DefinedPacket; import net.md_5.bungee.protocol.Protocol; @@ -23,6 +20,7 @@ import ru.leymooo.botfilter.packets.SetExp; import ru.leymooo.botfilter.packets.SetSlot; import ru.leymooo.botfilter.packets.TimeUpdate; +import ru.leymooo.botfilter.utils.ColorsUtils; import ru.leymooo.botfilter.utils.Dimension; /** @@ -151,10 +149,8 @@ public static void init() private static DefinedPacket createKickPacket(String message) { - return new Kick( ComponentSerializer.toString( - TextComponent.fromLegacyText( - ChatColor.translateAlternateColorCodes( '&', - message.replace( "%prefix%", Settings.IMP.MESSAGES.PREFIX ).replace( "%nl%", "\n" ) ) ) ) ); + return new Kick( ColorsUtils.serializeTextWithColorToJson( + message.replace( "%prefix%", Settings.IMP.MESSAGES.PREFIX ).replace( "%nl%", "\n" ) ) ); } diff --git a/proxy/src/main/java/ru/leymooo/botfilter/config/Settings.java b/proxy/src/main/java/ru/leymooo/botfilter/config/Settings.java index cade126ff0..2afa5770f5 100644 --- a/proxy/src/main/java/ru/leymooo/botfilter/config/Settings.java +++ b/proxy/src/main/java/ru/leymooo/botfilter/config/Settings.java @@ -63,7 +63,13 @@ public void reload(File file) save( file ); } - @Comment("Не используйте '\\n', используйте %nl%") + @Comment( + { + "Не используйте '\\n', используйте %nl%", + "Перед использованием HEX цветов, используйте символ '&'. Пример: c9dff", + "Внимание! Для клиентов ниже 1.16, раскрашивание текста HEX цветами не даст никакого результата, то есть цвет будет всегда белым." + } + ) public static class MESSAGES { diff --git a/proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java b/proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java new file mode 100644 index 0000000000..5ca94bff94 --- /dev/null +++ b/proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java @@ -0,0 +1,123 @@ +package ru.leymooo.botfilter.utils; + +import lombok.experimental.UtilityClass; +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.chat.ComponentSerializer; + +//Оригинал: https://github.com/filoghost/FCommons/blob/master/src/main/java/me/filoghost/fcommons/Colors.java +@UtilityClass +public class ColorsUtils +{ + private static final char ALT_COLOR_CHAR = '&'; + private static final CharArray ALT_COLOR_CODES = new CharArray( "0123456789AaBbCcDdEeFfKkLlMmNnOoRr" ); + private static final CharArray ALT_HEX_CODES = new CharArray( "0123456789AaBbCcDdEeFf" ); + private static final int ALT_HEX_COLOR_LENGTH = 6; + public static String serializeTextWithColorToJson(String text) + { + return ComponentSerializer.toString( TextComponent.fromLegacyText( colorize( text ) ) ); + } + public static String colorize(String string) + { + if ( isEmpty( string ) || string.indexOf( ALT_COLOR_CHAR ) < 0 ) + { + return string; + } + + StringBuilder result = new StringBuilder( string.length() ); + + int i = 0; + while ( i < string.length() ) + { + char currentChar = string.charAt( i ); + + if ( currentChar == ALT_COLOR_CHAR && i + 1 < string.length() ) + { + char nextChar = string.charAt( i + 1 ); + + if ( nextChar == '#' && isAltHexColor( string, i + 2 ) ) + { + result.append( ChatColor.COLOR_CHAR ); + result.append( 'x' ); + translateAltHexColor( string, i + 2, result ); + + i += 2 + ALT_HEX_COLOR_LENGTH; // Skip prefix and hex string + continue; + } + + if ( ALT_COLOR_CODES.contains( nextChar ) ) + { + result.append( ChatColor.COLOR_CHAR ); + result.append( Character.toLowerCase( nextChar ) ); + + i += 2; // Skip color char and color code + continue; + } + } + + // Normal char + result.append( currentChar ); + i++; + } + + return result.toString(); + } + + private static boolean isEmpty(String string) + { + return string == null || string.isEmpty(); + } + + private static boolean isAltHexColor(String string, int beginIndex) + { + if ( string.length() - beginIndex < ALT_HEX_COLOR_LENGTH ) + { + return false; + } + + for ( int i = 0; i < ALT_HEX_COLOR_LENGTH; i++ ) + { + char hexCode = string.charAt( beginIndex + i ); + if ( !ALT_HEX_CODES.contains( hexCode ) ) + { + return false; + } + } + + return true; + } + + private static void translateAltHexColor(String string, int beginIndex, StringBuilder output) + { + for ( int i = 0; i < ALT_HEX_COLOR_LENGTH; i++ ) + { + char hexCode = string.charAt( beginIndex + i ); + output.append( ChatColor.COLOR_CHAR ); + output.append( Character.toLowerCase( hexCode ) ); + } + } + + private static class CharArray + { + private final char[] chars; + CharArray(String chars) + { + this.chars = chars.toCharArray(); + } + + boolean contains(char c) + { + for ( char element : chars ) + { + if ( c == element ) + { + return true; + } + } + + return false; + } + + } + +} From 5aa08e0eda0860bf434adbab295ef434ed1e67af Mon Sep 17 00:00:00 2001 From: BoomEaro <21033866+BoomEaro@users.noreply.github.com> Date: Mon, 11 Jul 2022 19:16:33 +0300 Subject: [PATCH 2/2] Remove some blanked lines --- .../src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java b/proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java index 5ca94bff94..8ce187581b 100644 --- a/proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java +++ b/proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java @@ -114,10 +114,7 @@ boolean contains(char c) return true; } } - return false; } - } - }