Skip to content

Commit e7ee180

Browse files
committed
1.1 - Added MiniMessage methods
1 parent 365a696 commit e7ee180

File tree

2 files changed

+64
-29
lines changed

2 files changed

+64
-29
lines changed

pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>me.croabeast</groupId>
88
<artifactId>PrismaticAPI</artifactId>
9-
<version>1.0</version>
9+
<version>1.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>PrismaticAPI</name>
@@ -140,6 +140,20 @@
140140
<scope>provided</scope>
141141
</dependency>
142142

143+
<dependency>
144+
<groupId>net.kyori</groupId>
145+
<artifactId>adventure-text-minimessage</artifactId>
146+
<version>4.21.0</version>
147+
<scope>provided</scope>
148+
</dependency>
149+
150+
<dependency>
151+
<groupId>net.kyori</groupId>
152+
<artifactId>adventure-text-serializer-legacy</artifactId>
153+
<version>4.21.0</version>
154+
<scope>provided</scope>
155+
</dependency>
156+
143157
<dependency>
144158
<groupId>com.viaversion</groupId>
145159
<artifactId>viaversion-api</artifactId>

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

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.google.common.collect.ImmutableMap;
44
import lombok.experimental.UtilityClass;
55
import me.croabeast.prismatic.color.ColorPattern;
6+
import net.kyori.adventure.text.Component;
7+
import net.kyori.adventure.text.TextComponent;
8+
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
69
import net.md_5.bungee.api.ChatColor;
710
import org.apache.commons.lang.StringUtils;
811
import org.bukkit.entity.Player;
@@ -117,7 +120,7 @@ public ChatColor fromString(String string, boolean legacy) {
117120
}
118121

119122
/**
120-
* Parses a color string into a {@link ChatColor}, using optional player context
123+
* Parses a color string into a {@link ChatColor}, using server context
121124
* to decide if legacy (pre-1.16) color mode should apply.
122125
* <p>
123126
* The input may include legacy color code markers ('&' or '§').
@@ -126,44 +129,29 @@ public ChatColor fromString(String string, boolean legacy) {
126129
* returns {@link ChatColor#getByChar(char)}.
127130
* <p>
128131
* - 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.
132+
* {@link #fromString(String, boolean)} with legacy determined by server version.
130133
* </p>
131134
* - Otherwise, defaults to {@link ChatColor#WHITE}.
132135
*
133-
* @param player the {@link Player} whose client version may force legacy mode (may be {@code null})
134136
* @param string the color code to parse (may include '&' or '§' markers)
135137
* @return the corresponding {@link ChatColor}, or {@link ChatColor#WHITE} if unrecognized
136138
*/
137-
public ChatColor fromString(Player player, String string) {
139+
public ChatColor fromString(String string) {
140+
ChatColor color = ChatColor.WHITE;
141+
138142
if (string.matches("^[&§]x")) string = string.substring(2);
139143
string = string.replaceAll("[&§]", "");
140144

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-
}
145+
if (string.length() == 1 &&
146+
((color = ChatColor.getByChar(string.toCharArray()[0])) != null))
147+
return color;
150148

151-
return ChatColor.WHITE;
152-
}
149+
if (string.length() == 6)
150+
try {
151+
color = ChatColor.of('#' + string);
152+
} catch (Exception ignored) {}
153153

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);
154+
return color;
167155
}
168156

169157
/**
@@ -226,6 +214,17 @@ public String applyColor(Color color, String string, boolean legacy) {
226214
return getBukkit(color, legacy) + string;
227215
}
228216

217+
/**
218+
* Prepends a given {@link TextComponent} with the {@link ChatColor} corresponding to the provided color.
219+
*
220+
* @param color the color to apply
221+
* @param string the string to colorize
222+
* @return the colorized component
223+
*/
224+
public TextComponent applyColor(Color color, String string) {
225+
return LegacyComponentSerializer.legacySection().deserialize(getBukkit(color, ClientVersion.SERVER_VERSION < 16.0) + string);
226+
}
227+
229228
/**
230229
* Applies an array of {@link ChatColor} objects sequentially to each character of the source string.
231230
* <p>
@@ -270,6 +269,13 @@ public String applyGradient(String string, Color start, Color end, boolean legac
270269
return i <= 1 ? string : apply(string, createGradient(start, end, i, legacy));
271270
}
272271

272+
public TextComponent applyGradient(Color start, Color end, String string) {
273+
int i = stripSpecial(string).length();
274+
return LegacyComponentSerializer.legacySection().deserialize(i > 1 ?
275+
apply(string, createGradient(start, end, i, ClientVersion.SERVER_VERSION < 16.0)) :
276+
string);
277+
}
278+
273279
/**
274280
* Applies a rainbow color effect to the given string.
275281
*
@@ -283,6 +289,13 @@ public String applyRainbow(String string, float saturation, boolean legacy) {
283289
return i == 0 ? string : apply(string, createRainbow(i, saturation, legacy));
284290
}
285291

292+
public TextComponent applyRainbow(float saturation, String string) {
293+
int i = stripSpecial(string).length();
294+
return LegacyComponentSerializer.legacySection().deserialize(i != 0 ?
295+
apply(string, createRainbow(i, saturation, ClientVersion.SERVER_VERSION < 16.0)) :
296+
string);
297+
}
298+
286299
/**
287300
* Colorizes the given string by applying all registered {@link ColorPattern} effects.
288301
* <p>
@@ -313,6 +326,14 @@ public String colorize(String string) {
313326
return colorize(null, string);
314327
}
315328

329+
public TextComponent colorizeAsComponent(Player player, String string) {
330+
return LegacyComponentSerializer.legacySection().deserialize(colorize(player, string));
331+
}
332+
333+
public TextComponent colorizeAsComponent(String string) {
334+
return LegacyComponentSerializer.legacySection().deserialize(colorize(string));
335+
}
336+
316337
/**
317338
* Strips Bukkit color codes (e.g. {@code &a}, {@code §b}) from the provided string.
318339
*

0 commit comments

Comments
 (0)