Skip to content

Commit

Permalink
[1.5.1] - Improve color parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Luncaaa committed Jul 27, 2024
1 parent ae932d8 commit 20115bf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
14 changes: 11 additions & 3 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,26 @@

<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-minimessage</artifactId>
<artifactId>adventure-api</artifactId>
<version>4.17.0</version>
</dependency>

<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-json</artifactId>
<artifactId>adventure-text-minimessage</artifactId>
<version>4.17.0</version>
</dependency>

<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-gson</artifactId>
<artifactId>adventure-text-serializer-legacy</artifactId>
<version>4.17.0</version>
</dependency>

<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-platform-bungeecord</artifactId>
<version>4.3.3</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.json.JSONComponentSerializer;
import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.Bukkit;
Expand All @@ -16,26 +17,29 @@ public static String getColoredText(String text) {
}

public static String getColoredTextWithPlaceholders(Player player, String text) {
String transformedText = text.replace("%player%", player.getName());

if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
transformedText = PlaceholderAPI.setPlaceholders(player, transformedText);
}
return ComponentSerializer.toString(parseMessage(player, null, text, false));
}

Component c = MiniMessage.miniMessage().deserialize(transformedText);
return ChatColor.translateAlternateColorCodes('&', JSONComponentSerializer.json().serialize(c)).replace("\\n", "\n");
public static BaseComponent[] getTextComponent(String message, Player clickedPlayer, Player globalPlayer, boolean useGlobalPlaceholders) {
return parseMessage(clickedPlayer, globalPlayer, message, useGlobalPlaceholders);
}

public static BaseComponent[] getTextComponent(String message, Player clickedPlayer, Player actionPlayer, boolean useGlobalPlaceholders) {
String transformedText = message.replace("%player%", clickedPlayer.getName()).replace("%global_player%", actionPlayer.getName());
private static BaseComponent[] parseMessage(Player player, Player globalPlayer, String message, boolean useGlobalPlaceholders) {
message = message.replace("%player%", player.getName());
if (globalPlayer != null) message = message.replace("%global_player%", globalPlayer.getName());

Player placeholderPlayer = (useGlobalPlaceholders) ? actionPlayer : clickedPlayer;
Player placeholderPlayer = (useGlobalPlaceholders) ? globalPlayer : player;
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
transformedText = PlaceholderAPI.setPlaceholders(placeholderPlayer, transformedText);
message = PlaceholderAPI.setPlaceholders(placeholderPlayer, message);
}

Component c = MiniMessage.miniMessage().deserialize(transformedText);
String json = ChatColor.translateAlternateColorCodes('&', JSONComponentSerializer.json().serialize(c)).replace("\\n", "\n");
return ComponentSerializer.parse(json);
// From legacy and minimessage format to a component
Component legacy = LegacyComponentSerializer.legacyAmpersand().deserialize(message);
// From component to Minimessage String. Replacing the "\" with nothing makes the minimessage formats work.
String minimessage = MiniMessage.miniMessage().serialize(legacy).replace("\\", "");
// From Minimessage String to Minimessage component
Component component = MiniMessage.miniMessage().deserialize(minimessage);
// From Minimessage component to legacy string.
return BungeeComponentSerializer.get().serialize(component);
}
}

0 comments on commit 20115bf

Please sign in to comment.