Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Полноценная поддержка HEX цветов #118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,15 +32,15 @@ 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() )
{
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() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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" ) ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,13 @@ public void reload(File file)
save( file );
}

@Comment("Не используйте '\\n', используйте %nl%")
@Comment(
{
"Не используйте '\\n', используйте %nl%",
"Перед использованием HEX цветов, используйте символ '&'. Пример: &#9c9dff",
"Внимание! Для клиентов ниже 1.16, раскрашивание текста HEX цветами не даст никакого результата, то есть цвет будет всегда белым."
}
)
public static class MESSAGES
{

Expand Down
123 changes: 123 additions & 0 deletions proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this blanked line

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one probably too

}