|
5 | 5 | import net.kyori.adventure.text.format.TextColor;
|
6 | 6 | import net.kyori.adventure.text.format.TextDecoration;
|
7 | 7 | import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
| 8 | +import net.matrixcreations.libraries.interpolaters.ColorInterpolater; |
8 | 9 |
|
9 | 10 | import java.util.regex.Matcher;
|
10 | 11 | import java.util.regex.Pattern;
|
@@ -59,53 +60,56 @@ private static String processLegacyCodes(String text) {
|
59 | 60 | }
|
60 | 61 |
|
61 | 62 | private static String applyGradient(String text, String startColor, String endColor) {
|
| 63 | + // Use the hex color strings without the '#' prefix directly |
62 | 64 | TextColor start = TextColor.fromHexString("#" + startColor);
|
63 | 65 | TextColor end = TextColor.fromHexString("#" + endColor);
|
64 | 66 |
|
65 | 67 | StringBuilder result = new StringBuilder();
|
| 68 | + int length = text.length(); |
66 | 69 |
|
67 |
| - // Initialize variables to track formatting changes |
68 |
| - boolean bold = false; |
69 |
| - boolean italic = false; |
70 |
| - boolean underlined = false; |
71 |
| - boolean strikethrough = false; |
72 |
| - boolean obfuscated = false; |
| 70 | + boolean isBold = false; |
| 71 | + boolean isItalic = false; |
| 72 | + boolean isUnderlined = false; |
| 73 | + boolean isStrikethrough = false; |
| 74 | + boolean isObfuscated = false; |
73 | 75 |
|
74 |
| - int length = text.length(); |
75 | 76 | for (int i = 0; i < length; i++) {
|
76 |
| - char currentChar = text.charAt(i); |
| 77 | + // Calculate the gradient color based on the position in the text |
| 78 | + float ratio = (float) i / (length - 1); |
| 79 | + TextColor color = ColorInterpolater.interpolateColor(start, end, ratio); |
77 | 80 |
|
78 |
| - // Handle formatting codes |
| 81 | + char currentChar = text.charAt(i); |
79 | 82 | if (currentChar == '§') {
|
80 |
| - i++; // Move to next char which holds the format code |
81 |
| - char formatCode = text.charAt(i); |
82 |
| - switch (formatCode) { |
83 |
| - case 'l': bold = true; break; |
84 |
| - case 'o': italic = true; break; |
85 |
| - case 'n': underlined = true; break; |
86 |
| - case 'm': strikethrough = true; break; |
87 |
| - case 'k': obfuscated = true; break; |
88 |
| - case 'r': // Reset all formatting |
89 |
| - bold = italic = underlined = strikethrough = obfuscated = false; |
90 |
| - break; |
| 83 | + if (i + 1 < length) { |
| 84 | + char formatCode = text.charAt(i + 1); |
| 85 | + switch (formatCode) { |
| 86 | + case 'l': isBold = true; break; |
| 87 | + case 'o': isItalic = true; break; |
| 88 | + case 'n': isUnderlined = true; break; |
| 89 | + case 'm': isStrikethrough = true; break; |
| 90 | + case 'k': isObfuscated = true; break; |
| 91 | + case 'r': // Reset formatting |
| 92 | + isBold = false; |
| 93 | + isItalic = false; |
| 94 | + isUnderlined = false; |
| 95 | + isStrikethrough = false; |
| 96 | + isObfuscated = false; |
| 97 | + break; |
| 98 | + } |
| 99 | + i++; |
| 100 | + continue; |
91 | 101 | }
|
92 |
| - continue; // Skip processing this char |
93 | 102 | }
|
94 | 103 |
|
95 |
| - // Calculate gradient color interpolation |
96 |
| - float ratio = (float) i / length; |
97 |
| - TextColor color = interpolateColor(start, end, ratio); |
98 |
| - |
99 |
| - // Build text component with proper formatting |
100 | 104 | TextComponent.Builder component = Component.text()
|
101 | 105 | .content(String.valueOf(currentChar))
|
102 | 106 | .color(color);
|
103 | 107 |
|
104 |
| - if (bold) component.decorate(TextDecoration.BOLD); |
105 |
| - if (italic) component.decorate(TextDecoration.ITALIC); |
106 |
| - if (underlined) component.decorate(TextDecoration.UNDERLINED); |
107 |
| - if (strikethrough) component.decorate(TextDecoration.STRIKETHROUGH); |
108 |
| - if (obfuscated) component.decorate(TextDecoration.OBFUSCATED); |
| 108 | + if (isBold) component.decorate(TextDecoration.BOLD); |
| 109 | + if (isItalic) component.decorate(TextDecoration.ITALIC); |
| 110 | + if (isUnderlined) component.decorate(TextDecoration.UNDERLINED); |
| 111 | + if (isStrikethrough) component.decorate(TextDecoration.STRIKETHROUGH); |
| 112 | + if (isObfuscated) component.decorate(TextDecoration.OBFUSCATED); |
109 | 113 |
|
110 | 114 | result.append(LegacyComponentSerializer.legacySection().serialize(component.build()));
|
111 | 115 | }
|
|
0 commit comments