forked from SpigotMC/BungeeCord
-
Notifications
You must be signed in to change notification settings - Fork 51
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
BoomEaro
wants to merge
2
commits into
Leymooo:master
Choose a base branch
from
BoomEaro:feature/rgbcolors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
proxy/src/main/java/ru/leymooo/botfilter/utils/ColorsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one probably too |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this blanked line