Skip to content

Commit 365a696

Browse files
committed
General improvements
1 parent 74fe0aa commit 365a696

File tree

5 files changed

+97
-47
lines changed

5 files changed

+97
-47
lines changed

src/main/java/me/croabeast/prismatic/ClientVersion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* Example usage:
2727
* <pre><code>
2828
* int majorVersion = ClientVersion.getClientVersion(player);
29-
* boolean isLegacy = ClientVersion.isLegacy(player);
29+
* boolean legacy = ClientVersion.legacy(player);
3030
* System.out.println("Player client major version: " + majorVersion);
3131
* </code></pre>
3232
* </p>

src/main/java/me/croabeast/prismatic/PrismaticAPI.java

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,72 @@ private ChatColor getClosestColor(Color color) {
9898
* Retrieves a {@link ChatColor} for the given {@link Color}, taking into account legacy mode.
9999
*
100100
* @param color the AWT color to convert
101-
* @param isLegacy if {@code true}, legacy colors are used (closest match); otherwise, modern RGB support is used
101+
* @param legacy if {@code true}, legacy colors are used (closest match); otherwise, modern RGB support is used
102102
* @return the corresponding {@link ChatColor}
103103
*/
104-
private ChatColor getBukkit(Color color, boolean isLegacy) {
105-
return isLegacy ? getClosestColor(color) : ChatColor.of(color);
104+
private ChatColor getBukkit(Color color, boolean legacy) {
105+
return legacy ? getClosestColor(color) : ChatColor.of(color);
106106
}
107107

108108
/**
109109
* Converts a hexadecimal color string into a {@link ChatColor}.
110110
*
111111
* @param string the hexadecimal color code (without the leading '#' character)
112-
* @param isLegacy if {@code true}, legacy color mode is used
112+
* @param legacy if {@code true}, legacy color mode is used
113113
* @return the resulting {@link ChatColor}
114114
*/
115-
public ChatColor fromString(String string, boolean isLegacy) {
116-
return getBukkit(new Color(Integer.parseInt(string, 16)), isLegacy);
115+
public ChatColor fromString(String string, boolean legacy) {
116+
return getBukkit(new Color(Integer.parseInt(string, 16)), legacy);
117+
}
118+
119+
/**
120+
* Parses a color string into a {@link ChatColor}, using optional player context
121+
* to decide if legacy (pre-1.16) color mode should apply.
122+
* <p>
123+
* The input may include legacy color code markers ('&' or '§').
124+
* </p>
125+
* - If the cleaned string is a single hex-digit or formatting code (0–9, A–F, K–O, R),
126+
* returns {@link ChatColor#getByChar(char)}.
127+
* <p>
128+
* - If it’s a six-digit hex (A–F, 0–9), delegates to
129+
* {@link #fromString(String, boolean)} with legacy determined by server and client versions.
130+
* </p>
131+
* - Otherwise, defaults to {@link ChatColor#WHITE}.
132+
*
133+
* @param player the {@link Player} whose client version may force legacy mode (may be {@code null})
134+
* @param string the color code to parse (may include '&' or '§' markers)
135+
* @return the corresponding {@link ChatColor}, or {@link ChatColor#WHITE} if unrecognized
136+
*/
137+
public ChatColor fromString(Player player, String string) {
138+
if (string.matches("^[&§]x")) string = string.substring(2);
139+
string = string.replaceAll("[&§]", "");
140+
141+
if (string.matches("^(?i)[a-fk-or0-9]$"))
142+
return ChatColor.getByChar(string.toCharArray()[0]);
143+
144+
if (string.matches("^(?i)[a-f0-9]{6}$")) {
145+
boolean legacy = ClientVersion.SERVER_VERSION < 16.0;
146+
if (player != null)
147+
legacy = legacy || ClientVersion.isLegacy(player);
148+
return fromString(string, legacy);
149+
}
150+
151+
return ChatColor.WHITE;
152+
}
153+
154+
/**
155+
* Parses a color string into a {@link ChatColor} based solely on server version,
156+
* without any player-specific legacy checks.
157+
* <p>
158+
* This is equivalent to calling {@link #fromString(Player, String)} with a {@code null}
159+
* player, so legacy mode is determined only by {@link ClientVersion#SERVER_VERSION}.
160+
* </p>
161+
*
162+
* @param string the color code to parse (may include '&' or '§' markers)
163+
* @return the corresponding {@link ChatColor}, or {@link ChatColor#WHITE} if unrecognized
164+
*/
165+
public ChatColor fromString(String string) {
166+
return fromString(null, string);
117167
}
118168

119169
/**
@@ -122,10 +172,10 @@ public ChatColor fromString(String string, boolean isLegacy) {
122172
* @param start the starting color of the gradient
123173
* @param end the ending color of the gradient
124174
* @param step the number of steps (colors) in the gradient
125-
* @param isLegacy if {@code true}, legacy color mode is used
175+
* @param legacy if {@code true}, legacy color mode is used
126176
* @return an array of {@link ChatColor} forming the gradient
127177
*/
128-
private ChatColor[] createGradient(Color start, Color end, int step, boolean isLegacy) {
178+
private ChatColor[] createGradient(Color start, Color end, int step, boolean legacy) {
129179
ChatColor[] colors = new ChatColor[step];
130180
int stepR = Math.abs(start.getRed() - end.getRed()) / (step - 1),
131181
stepG = Math.abs(start.getGreen() - end.getGreen()) / (step - 1),
@@ -141,7 +191,7 @@ private ChatColor[] createGradient(Color start, Color end, int step, boolean isL
141191
start.getGreen() + ((stepG * i) * direction[1]),
142192
start.getBlue() + ((stepB * i) * direction[2])
143193
);
144-
colors[i] = getBukkit(color, isLegacy);
194+
colors[i] = getBukkit(color, legacy);
145195
}
146196
return colors;
147197
}
@@ -151,15 +201,15 @@ private ChatColor[] createGradient(Color start, Color end, int step, boolean isL
151201
*
152202
* @param step the number of colors in the rainbow
153203
* @param sat the saturation level (0.0 to 1.0)
154-
* @param isLegacy if {@code true}, legacy color mode is used
204+
* @param legacy if {@code true}, legacy color mode is used
155205
* @return an array of {@link ChatColor} forming a rainbow
156206
*/
157-
private ChatColor[] createRainbow(int step, float sat, boolean isLegacy) {
207+
private ChatColor[] createRainbow(int step, float sat, boolean legacy) {
158208
ChatColor[] colors = new ChatColor[step];
159209
double colorStep = (1.00 / step);
160210
for (int i = 0; i < step; i++) {
161211
Color color = Color.getHSBColor((float) (colorStep * i), sat, sat);
162-
colors[i] = getBukkit(color, isLegacy);
212+
colors[i] = getBukkit(color, legacy);
163213
}
164214
return colors;
165215
}
@@ -169,11 +219,11 @@ private ChatColor[] createRainbow(int step, float sat, boolean isLegacy) {
169219
*
170220
* @param color the color to apply
171221
* @param string the string to colorize
172-
* @param isLegacy if {@code true}, legacy color mode is used
222+
* @param legacy if {@code true}, legacy color mode is used
173223
* @return the colorized string
174224
*/
175-
public String applyColor(Color color, String string, boolean isLegacy) {
176-
return getBukkit(color, isLegacy) + string;
225+
public String applyColor(Color color, String string, boolean legacy) {
226+
return getBukkit(color, legacy) + string;
177227
}
178228

179229
/**
@@ -212,25 +262,25 @@ private String apply(String source, ChatColor[] colors) {
212262
* @param string the string to apply the gradient to
213263
* @param start the starting color of the gradient
214264
* @param end the ending color of the gradient
215-
* @param isLegacy if {@code true}, legacy color mode is used
265+
* @param legacy if {@code true}, legacy color mode is used
216266
* @return the string with a gradient effect applied
217267
*/
218-
public String applyGradient(String string, Color start, Color end, boolean isLegacy) {
268+
public String applyGradient(String string, Color start, Color end, boolean legacy) {
219269
int i = stripSpecial(string).length();
220-
return i <= 1 ? string : apply(string, createGradient(start, end, i, isLegacy));
270+
return i <= 1 ? string : apply(string, createGradient(start, end, i, legacy));
221271
}
222272

223273
/**
224274
* Applies a rainbow color effect to the given string.
225275
*
226276
* @param string the string to apply the rainbow effect to
227277
* @param saturation the saturation level for the rainbow (0.0 to 1.0)
228-
* @param isLegacy if {@code true}, legacy color mode is used
278+
* @param legacy if {@code true}, legacy color mode is used
229279
* @return the string with a rainbow effect applied
230280
*/
231-
public String applyRainbow(String string, float saturation, boolean isLegacy) {
281+
public String applyRainbow(String string, float saturation, boolean legacy) {
232282
int i = stripSpecial(string).length();
233-
return i == 0 ? string : apply(string, createRainbow(i, saturation, isLegacy));
283+
return i == 0 ? string : apply(string, createRainbow(i, saturation, legacy));
234284
}
235285

236286
/**
@@ -245,11 +295,11 @@ public String applyRainbow(String string, float saturation, boolean isLegacy) {
245295
* @return the colorized string
246296
*/
247297
public String colorize(Player player, String string) {
248-
boolean isLegacy = ClientVersion.SERVER_VERSION < 16.0;
298+
boolean legacy = ClientVersion.SERVER_VERSION < 16.0;
249299
if (player != null)
250-
isLegacy = isLegacy || ClientVersion.isLegacy(player);
300+
legacy = legacy || ClientVersion.isLegacy(player);
251301
for (ColorPattern p : ColorPattern.COLOR_PATTERNS)
252-
string = p.apply(string, isLegacy);
302+
string = p.apply(string, legacy);
253303
return ChatColor.translateAlternateColorCodes('&', string);
254304
}
255305

src/main/java/me/croabeast/prismatic/color/ColorPattern.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ public interface ColorPattern {
4242
* Applies the color pattern to the given string.
4343
* <p>
4444
* This method transforms the input text by inserting color codes according to the pattern's rules.
45-
* The {@code isLegacy} flag specifies whether legacy color formatting should be used.
45+
* The {@code legacy} flag specifies whether legacy color formatting should be used.
4646
* </p>
4747
*
4848
* @param string the input text to which the color pattern will be applied
49-
* @param isLegacy {@code true} if legacy formatting (e.g. 16-color mode) should be used; {@code false} for modern RGB support
49+
* @param legacy {@code true} if legacy formatting (e.g. 16-color mode) should be used; {@code false} for modern RGB support
5050
* @return the transformed, colorized string
5151
*/
52-
@NotNull String apply(String string, boolean isLegacy);
52+
@NotNull String apply(String string, boolean legacy);
5353
}

src/main/java/me/croabeast/prismatic/color/MultiColor.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static Color getColor(String line) {
115115
final Pattern custom = Pattern.compile("<(#([a-f\\d]{6})(:#([a-f\\d]{6}))+)>(.+?)</g(radient)?>");
116116

117117
@Override
118-
public @NotNull String apply(String string, boolean isLegacy) {
118+
public @NotNull String apply(String string, boolean legacy) {
119119
Matcher m = custom.matcher(string);
120120
while (m.find()) {
121121
String[] colors = m.group(1).split(":");
@@ -135,7 +135,7 @@ static Color getColor(String line) {
135135
textPart,
136136
getColor(colors[i]),
137137
getColor(colors[i + 1]),
138-
isLegacy
138+
legacy
139139
);
140140
result.append(i > 0 ? textPart.substring(15) : textPart);
141141
i++;
@@ -166,13 +166,13 @@ static Color getColor(String line) {
166166
* </p>
167167
*
168168
* @param string the input string to process
169-
* @param isLegacy {@code true} to use legacy color formatting; {@code false} for modern formatting
169+
* @param legacy {@code true} to use legacy color formatting; {@code false} for modern formatting
170170
* @return the colorized string after all transformations have been applied
171171
*/
172172
@Override
173-
public @NotNull String apply(String string, boolean isLegacy) {
173+
public @NotNull String apply(String string, boolean legacy) {
174174
for (ColorPattern color : colors)
175-
string = color.apply(string, isLegacy);
175+
string = color.apply(string, legacy);
176176
return string;
177177
}
178178

@@ -210,7 +210,7 @@ private class Gradient implements ColorPattern {
210210
*/
211211
Gradient(String prefix) {
212212
pattern = Pattern.compile("(?i)" + gradientPattern(prefix));
213-
applier = (string, isLegacy) -> {
213+
applier = (string, legacy) -> {
214214
Matcher matcher = Gradient.this.pattern.matcher(string);
215215
while (matcher.find()) {
216216
String x = matcher.group(1), text = matcher.group(2),
@@ -229,7 +229,7 @@ private class Gradient implements ColorPattern {
229229
array[i],
230230
getColor(ids.get(i)),
231231
getColor(ids.get(i + 1)),
232-
isLegacy
232+
legacy
233233
));
234234
i++;
235235
}
@@ -249,8 +249,8 @@ private class Gradient implements ColorPattern {
249249
}
250250

251251
@Override
252-
public @NotNull String apply(String string, boolean isLegacy) {
253-
return applier.apply(string, isLegacy);
252+
public @NotNull String apply(String string, boolean legacy) {
253+
return applier.apply(string, legacy);
254254
}
255255

256256
@Override
@@ -280,12 +280,12 @@ private class Rainbow implements ColorPattern {
280280
*/
281281
Rainbow(String prefix) {
282282
pattern = Pattern.compile("(?i)" + rainbowPattern(prefix));
283-
applier = (string, isLegacy) -> {
283+
applier = (string, legacy) -> {
284284
Matcher matcher = Rainbow.this.pattern.matcher(string);
285285
while (matcher.find()) {
286286
String g = matcher.group(), c = matcher.group(2);
287287
float f = Float.parseFloat(matcher.group(1));
288-
String temp = PrismaticAPI.applyRainbow(c, f, isLegacy);
288+
String temp = PrismaticAPI.applyRainbow(c, f, legacy);
289289
string = string.replace(g, temp);
290290
}
291291
return string;
@@ -300,8 +300,8 @@ private class Rainbow implements ColorPattern {
300300
}
301301

302302
@Override
303-
public @NotNull String apply(String string, boolean isLegacy) {
304-
return applier.apply(string, isLegacy);
303+
public @NotNull String apply(String string, boolean legacy) {
304+
return applier.apply(string, legacy);
305305
}
306306

307307
@Override

src/main/java/me/croabeast/prismatic/color/SingleColor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ class SingleColor implements ColorPattern {
6060
* </p>
6161
*
6262
* @param string the string to be colorized
63-
* @param isLegacy {@code true} to apply legacy formatting; {@code false} for modern RGB colors
63+
* @param legacy {@code true} to apply legacy formatting; {@code false} for modern RGB colors
6464
* @return the colorized string
6565
*/
6666
@Override
67-
public @NotNull String apply(String string, boolean isLegacy) {
67+
public @NotNull String apply(String string, boolean legacy) {
6868
for (ColorPattern color : colors)
69-
string = color.apply(string, isLegacy);
69+
string = color.apply(string, legacy);
7070
return string;
7171
}
7272

@@ -125,14 +125,14 @@ private class Color implements ColorPattern {
125125
* </p>
126126
*
127127
* @param string the input string to transform
128-
* @param isLegacy {@code true} to use legacy color formatting; {@code false} for modern formatting
128+
* @param legacy {@code true} to use legacy color formatting; {@code false} for modern formatting
129129
* @return the string with this color pattern applied
130130
*/
131131
@Override
132-
public @NotNull String apply(String string, boolean isLegacy) {
132+
public @NotNull String apply(String string, boolean legacy) {
133133
Matcher m = pattern.matcher(string);
134134
while (m.find()) {
135-
ChatColor c = PrismaticAPI.fromString(m.group(1), isLegacy);
135+
ChatColor c = PrismaticAPI.fromString(m.group(1), legacy);
136136
string = string.replace(m.group(), c.toString());
137137
}
138138
return string;

0 commit comments

Comments
 (0)