@@ -36,37 +36,49 @@ private static String applyGradient(String text, String startColor, String endCo
36
36
TextColor end = TextColor .fromHexString ("#" + endColor );
37
37
38
38
StringBuilder result = new StringBuilder ();
39
- boolean isBold = text .contains ("§l" ) || text .contains ("&l" );
40
- boolean isItalic = text .contains ("§o" ) || text .contains ("&o" );
41
- boolean isUnderlined = text .contains ("§n" ) || text .contains ("&n" );
42
- boolean isStrikethrough = text .contains ("§m" ) || text .contains ("&m" );
43
- boolean isObfuscated = text .contains ("§k" ) || text .contains ("&k" );
44
-
45
- // Remove formatting markers from the actual text before processing
46
- text = text .replace ("§l" , "" ).replace ("&l" , "" );
47
- text = text .replace ("§o" , "" ).replace ("&o" , "" );
48
- text = text .replace ("§n" , "" ).replace ("&n" , "" );
49
- text = text .replace ("§m" , "" ).replace ("&m" , "" );
50
- text = text .replace ("§k" , "" ).replace ("&k" , "" );
39
+
40
+ boolean bold = false ;
41
+ boolean italic = false ;
42
+ boolean underlined = false ;
43
+ boolean strikethrough = false ;
44
+ boolean obfuscated = false ;
51
45
52
46
int length = text .length ();
53
47
for (int i = 0 ; i < length ; i ++) {
54
- float ratio = (float ) i / length ; // Changed ratio calculation
48
+ char currentChar = text .charAt (i );
49
+
50
+ if (currentChar == '§' ) {
51
+ i ++;
52
+ char formatCode = text .charAt (i );
53
+ switch (formatCode ) {
54
+ case 'l' : bold = true ; break ;
55
+ case 'o' : italic = true ; break ;
56
+ case 'n' : underlined = true ; break ;
57
+ case 'm' : strikethrough = true ; break ;
58
+ case 'k' : obfuscated = true ; break ;
59
+ case 'r' :
60
+ bold = italic = underlined = strikethrough = obfuscated = false ;
61
+ break ;
62
+ }
63
+ continue ;
64
+ }
65
+
66
+ float ratio = (float ) i / length ;
55
67
TextColor color = ColorInterpolater .interpolateColor (start , end , ratio );
56
68
57
69
TextComponent .Builder component = Component .text ()
58
- .content (String .valueOf (text . charAt ( i ) ))
70
+ .content (String .valueOf (currentChar ))
59
71
.color (color );
60
72
61
- if (isBold ) component .decorate (TextDecoration .BOLD );
62
- if (isItalic ) component .decorate (TextDecoration .ITALIC );
63
- if (isUnderlined ) component .decorate (TextDecoration .UNDERLINED );
64
- if (isStrikethrough ) component .decorate (TextDecoration .STRIKETHROUGH );
65
- if (isObfuscated ) component .decorate (TextDecoration .OBFUSCATED );
73
+ if (bold ) component .decorate (TextDecoration .BOLD );
74
+ if (italic ) component .decorate (TextDecoration .ITALIC );
75
+ if (underlined ) component .decorate (TextDecoration .UNDERLINED );
76
+ if (strikethrough ) component .decorate (TextDecoration .STRIKETHROUGH );
77
+ if (obfuscated ) component .decorate (TextDecoration .OBFUSCATED );
66
78
67
79
result .append (LegacyComponentSerializer .legacySection ().serialize (component .build ()));
68
80
}
69
81
70
82
return result .toString ();
71
83
}
72
- }
84
+ }
0 commit comments