make placeholders more compatible with formatting

This commit is contained in:
2023-07-24 09:23:57 +02:00
parent 15817e8dbb
commit ef3161f2ea
2 changed files with 39 additions and 9 deletions
@@ -16,4 +16,8 @@ public class PAPIHook {
return component;
}
public static String setPlaceholders(String string, OfflinePlayer offlinePlayer) {
return PlaceholderAPI.setPlaceholders(offlinePlayer, string);
}
}
@@ -5,6 +5,7 @@ import me.youhavetrouble.notjustnameplates.displays.DisplayContent;
import me.youhavetrouble.notjustnameplates.hooks.PAPIHook;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.GameMode;
@@ -183,20 +184,45 @@ public class Nameplate {
private Component parseText(String text, Player player) {
Component component = MiniMessage.miniMessage().deserialize(text);
if (player == null || !player.isOnline()) return component;
if (player == null || !player.isOnline()) return Component.empty();
if (NotJustNameplates.isPapiHooked()) {
component = PAPIHook.setPlaceholders(component, player);
text = PAPIHook.setPlaceholders(text, player);
}
component = component.replaceText(builder -> {
builder.matchLiteral("%displayname%");
builder.replacement(player.displayName());
});
text = text.replaceAll("%displayname%", LegacyComponentSerializer.legacyAmpersand().serialize(player.displayName()));
return component;
text = makeColorsWork(LegacyComponentSerializer.AMPERSAND_CHAR, text);
text = makeColorsWork(LegacyComponentSerializer.SECTION_CHAR, text);
return MiniMessage.miniMessage().deserialize(text);
}
private String makeColorsWork(Character symbol, String string) {
// Adventure and ChatColor do not like each other, so this is a thing.
string = string.replaceAll(symbol + "0", "<black>");
string = string.replaceAll(symbol + "1", "<dark_blue>");
string = string.replaceAll(symbol + "2", "<dark_green>");
string = string.replaceAll(symbol + "3", "<dark_aqua>");
string = string.replaceAll(symbol + "4", "<dark_red>");
string = string.replaceAll(symbol + "5", "<dark_purple>");
string = string.replaceAll(symbol + "6", "<gold>");
string = string.replaceAll(symbol + "7", "<gray>");
string = string.replaceAll(symbol + "8", "<dark_gray>");
string = string.replaceAll(symbol + "9", "<blue>");
string = string.replaceAll(symbol + "a", "<green>");
string = string.replaceAll(symbol + "b", "<aqua>");
string = string.replaceAll(symbol + "c", "<red>");
string = string.replaceAll(symbol + "d", "<light_purple>");
string = string.replaceAll(symbol + "e", "<yellow>");
string = string.replaceAll(symbol + "f", "<white>");
string = string.replaceAll(symbol + "k", "<obfuscated>");
string = string.replaceAll(symbol + "l", "<bold>");
string = string.replaceAll(symbol + "m", "<strikethrough>");
string = string.replaceAll(symbol + "n", "<underlined>");
string = string.replaceAll(symbol + "o", "<italic>");
string = string.replaceAll(symbol + "r", "<reset>");
return string;
}
}