Skip to content

MiniMessage support has been added, it is now available. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@
<version>0.4.3</version>
<scope>compile</scope>
</dependency>
<!-- MiniMessage Dependencies -->
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
<version>4.14.0</version>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId>
<version>4.14.0</version>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-platform-bukkit</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>

<!-- Repos -->
Expand Down
45 changes: 36 additions & 9 deletions src/main/java/xyz/geik/glib/chat/ChatUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package xyz.geik.glib.chat;


import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.ChatColor;
import org.apache.commons.lang3.StringEscapeUtils;
import org.bukkit.OfflinePlayer;
Expand Down Expand Up @@ -39,6 +41,16 @@ public class ChatUtils {
*/
private final static Pattern HEX_PATTERN = Pattern.compile("#[a-fA-F0-9]{6}");

/**
* MiniMessage instance for parsing MiniMessage tags
*/
private static final MiniMessage miniMessage = MiniMessage.miniMessage();

/**
* Legacy serializer for converting & color codes
*/
private static final LegacyComponentSerializer legacySerializer = LegacyComponentSerializer.legacy('&');

/**
* Applies chat color formats to message
* @param message to convert
Expand All @@ -47,7 +59,11 @@ public class ChatUtils {
public static String color(String message) {
message = StringEscapeUtils.unescapeHtml4(message);

Matcher matcher = HEX_PATTERN.matcher(message);
Component component = miniMessage.deserialize(message);

String legacyMessage = legacySerializer.serialize(component);

Matcher matcher = HEX_PATTERN.matcher(legacyMessage);
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(buffer, ChatColor.of(matcher.group()).toString());
Expand All @@ -56,17 +72,28 @@ public static String color(String message) {
return ChatColor.translateAlternateColorCodes('&', matcher.appendTail(buffer).toString());
}

/**
* Applies chat color formats to message with PlaceholderAPI support
* @param player OfflinePlayer for PlaceholderAPI
* @param message to convert
* @return String of converted message
*/
public static String color(OfflinePlayer player, String message) {
message = StringEscapeUtils.unescapeHtml4(message);

Matcher matcher = HEX_PATTERN.matcher(message);
message = PlaceholderAPI.setPlaceholders(player, message);

Component component = miniMessage.deserialize(message);

String legacyMessage = legacySerializer.serialize(component);

Matcher matcher = HEX_PATTERN.matcher(legacyMessage);
StringBuffer buffer = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(buffer, ChatColor.of(matcher.group()).toString());
}

return ChatColor.translateAlternateColorCodes('&',
PlaceholderAPI.setPlaceholders(player, matcher.appendTail(buffer).toString()));
return ChatColor.translateAlternateColorCodes('&', matcher.appendTail(buffer).toString());
}

/**
Expand All @@ -79,9 +106,9 @@ public static List<String> color(List<String> list) {
}

/**
* Applies chat color formats to list
* Applies chat color formats to list with PlaceholderAPI support
* @param list to convert
* @param player who will be replaced for placeholders
* @param player OfflinePlayer for PlaceholderAPI
* @return List of converted message
*/
public static List<String> color(List<String> list, OfflinePlayer player) {
Expand Down Expand Up @@ -134,6 +161,6 @@ public static String replacePlaceholders(String string, Placeholder... placehold
* @return converted list value
*/
public static List<String> replacePlaceholders(List<String> list, Placeholder... placeholders) {
return list.stream().map(s-> replacePlaceholders(s, placeholders)).collect(Collectors.toList());
return list.stream().map(s -> replacePlaceholders(s, placeholders)).collect(Collectors.toList());
}
}
}