11package com .eternalcode .formatter .legacy ;
22
33import com .google .common .collect .ImmutableMap ;
4+ import java .util .Set ;
45import java .util .function .Predicate ;
5- import net .kyori .adventure .text .serializer .legacy .LegacyComponentSerializer ;
66
77import java .util .Map ;
88import java .util .regex .Pattern ;
9+ import org .jetbrains .annotations .VisibleForTesting ;
910
1011public final class Legacy {
1112
1213 private static final Pattern COLOR_LEGACY_PATTERN = Pattern .compile ("(?i)&([0-9A-FK-ORX#])" );
1314 private static final Pattern HEX_LEGACY_PATTERN = Pattern .compile ("(?i)&#([0-9A-F]{6})" );
1415 private static final Pattern HEX_LEGACY_VANILLA_PATTERN = Pattern .compile ("(?i)&x(&[0-9A-F]){6}" );
1516
17+ private static final Set <Character > COLORS = Set .of (
18+ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,
19+ '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f'
20+ );
21+
22+ private static final Set <Character > DECORATIONS = Set .of (
23+ 'k' , 'l' , 'm' , 'n' , 'o'
24+ );
25+
1626 private static final Map <Character , String > codeTranslations = new ImmutableMap .Builder <Character , String >()
1727 .put ('0' , "<black>" )
1828 .put ('1' , "<dark_blue>" )
@@ -66,7 +76,8 @@ public final class Legacy {
6676 private Legacy () {
6777 }
6878
69- public static String clearSection (String text ) {
79+ @ VisibleForTesting
80+ static String clearSection (String text ) {
7081 return text .replace ('§' , '&' );
7182 }
7283
@@ -75,7 +86,8 @@ public static String legacyToAdventure(String input) {
7586 }
7687
7788 public static String legacyToAdventure (String input , Predicate <String > hasPermission ) {
78- String result = HEX_LEGACY_VANILLA_PATTERN .matcher (input ).replaceAll (matchResult -> {
89+ String result = clearSection (input );
90+ result = HEX_LEGACY_VANILLA_PATTERN .matcher (result ).replaceAll (matchResult -> {
7991 String hexColor = matchResult .group ().replace ("&x" , "" ).replace ("&" , "" );
8092 return "<#" + hexColor + ">" ;
8193 });
@@ -98,16 +110,17 @@ public static String legacyToAdventure(String input, Predicate<String> hasPermis
98110 }
99111
100112 private static boolean hasPermissionForLegacyCode (Predicate <String > hasPermission , char code ) {
101- if (hasWildcardPermission (hasPermission )) {
113+ if (hasPermission .test ("chatformatter.*" )) {
114+ return true ;
115+ }
116+ if (COLORS .contains (code ) && hasPermission .test ("chatformatter.color.*" )) {
117+ return true ;
118+ }
119+ if (DECORATIONS .contains (code ) && hasPermission .test ("chatformatter.decorations.*" )) {
102120 return true ;
103121 }
104122 String permission = legacyCodeToPermission .get (code );
105123 return permission != null && hasPermission .test (permission );
106124 }
107125
108- private static boolean hasWildcardPermission (Predicate <String > hasPermission ) {
109- return hasPermission .test ("chatformatter.*" )
110- || hasPermission .test ("chatformatter.color.*" )
111- || hasPermission .test ("chatformatter.decorations.*" );
112- }
113126}
0 commit comments