Skip to content

Commit 7013bc3

Browse files
committed
Added new formats.
1 parent 7133743 commit 7013bc3

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package net.matrixcreations.libraries.utils;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.TextComponent;
5+
import net.kyori.adventure.text.format.TextColor;
6+
import net.kyori.adventure.text.format.TextDecoration;
7+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
8+
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
11+
12+
public class WithoutHexUtils {
13+
14+
// Patterns for handling colors without #
15+
private static final Pattern GRADIENT_PATTERN = Pattern.compile("<GRADIENT:([A-Fa-f0-9]{6})>(.*?)</GRADIENT:([A-Fa-f0-9]{6})>");
16+
private static final Pattern SOLID_PATTERN = Pattern.compile("<SOLID:([A-Fa-f0-9]{6})>(.*?)(?:</SOLID>|$)");
17+
18+
public static String process(String text) {
19+
text = processGradients(text);
20+
text = processSolidColors(text);
21+
return processLegacyCodes(text);
22+
}
23+
24+
private static String processGradients(String text) {
25+
Matcher matcher = GRADIENT_PATTERN.matcher(text);
26+
StringBuffer buffer = new StringBuffer();
27+
28+
while (matcher.find()) {
29+
String startColor = matcher.group(1);
30+
String content = matcher.group(2);
31+
String endColor = matcher.group(3);
32+
33+
String gradientText = applyGradient(content, startColor, endColor);
34+
matcher.appendReplacement(buffer, Matcher.quoteReplacement(gradientText));
35+
}
36+
37+
matcher.appendTail(buffer);
38+
return buffer.toString();
39+
}
40+
41+
private static String processSolidColors(String text) {
42+
Matcher matcher = SOLID_PATTERN.matcher(text);
43+
StringBuffer buffer = new StringBuffer();
44+
45+
while (matcher.find()) {
46+
String hexColor = matcher.group(1);
47+
String content = matcher.group(2);
48+
49+
String solidColorText = applySolidColor(content, hexColor);
50+
matcher.appendReplacement(buffer, Matcher.quoteReplacement(solidColorText));
51+
}
52+
53+
matcher.appendTail(buffer);
54+
return buffer.toString();
55+
}
56+
57+
private static String processLegacyCodes(String text) {
58+
return text.replace('&', '§');
59+
}
60+
61+
private static String applyGradient(String text, String startColor, String endColor) {
62+
TextColor start = TextColor.fromHexString("#" + startColor);
63+
TextColor end = TextColor.fromHexString("#" + endColor);
64+
65+
StringBuilder result = new StringBuilder();
66+
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;
73+
74+
int length = text.length();
75+
for (int i = 0; i < length; i++) {
76+
char currentChar = text.charAt(i);
77+
78+
// Handle formatting codes
79+
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;
91+
}
92+
continue; // Skip processing this char
93+
}
94+
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+
TextComponent.Builder component = Component.text()
101+
.content(String.valueOf(currentChar))
102+
.color(color);
103+
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);
109+
110+
result.append(LegacyComponentSerializer.legacySection().serialize(component.build()));
111+
}
112+
113+
return result.toString();
114+
}
115+
116+
private static String applySolidColor(String text, String hexColor) {
117+
TextColor color = TextColor.fromHexString("#" + hexColor);
118+
119+
StringBuilder result = new StringBuilder();
120+
121+
// Initialize variables to track formatting changes
122+
boolean bold = false;
123+
boolean italic = false;
124+
boolean underlined = false;
125+
boolean strikethrough = false;
126+
boolean obfuscated = false;
127+
128+
int length = text.length();
129+
for (int i = 0; i < length; i++) {
130+
char currentChar = text.charAt(i);
131+
132+
// Handle formatting codes
133+
if (currentChar == '§') {
134+
i++; // Move to next char which holds the format code
135+
char formatCode = text.charAt(i);
136+
switch (formatCode) {
137+
case 'l': bold = true; break;
138+
case 'o': italic = true; break;
139+
case 'n': underlined = true; break;
140+
case 'm': strikethrough = true; break;
141+
case 'k': obfuscated = true; break;
142+
case 'r': // Reset all formatting
143+
bold = italic = underlined = strikethrough = obfuscated = false;
144+
break;
145+
}
146+
continue; // Skip processing this char
147+
}
148+
149+
// Build text component with solid color and proper formatting
150+
TextComponent.Builder component = Component.text()
151+
.content(String.valueOf(currentChar))
152+
.color(color);
153+
154+
if (bold) component.decorate(TextDecoration.BOLD);
155+
if (italic) component.decorate(TextDecoration.ITALIC);
156+
if (underlined) component.decorate(TextDecoration.UNDERLINED);
157+
if (strikethrough) component.decorate(TextDecoration.STRIKETHROUGH);
158+
if (obfuscated) component.decorate(TextDecoration.OBFUSCATED);
159+
160+
result.append(LegacyComponentSerializer.legacySection().serialize(component.build()));
161+
}
162+
163+
return result.toString();
164+
}
165+
166+
private static TextColor interpolateColor(TextColor start, TextColor end, float ratio) {
167+
int red = (int) (start.red() + (end.red() - start.red()) * ratio);
168+
int green = (int) (start.green() + (end.green() - start.green()) * ratio);
169+
int blue = (int) (start.blue() + (end.blue() - start.blue()) * ratio);
170+
171+
return TextColor.color(red, green, blue);
172+
}
173+
}

0 commit comments

Comments
 (0)