mirror of
https://github.com/YouHaveTrouble/NotJustNameplates.git
synced 2026-05-12 06:26:58 +00:00
parse placeholders in a better way
This commit is contained in:
@@ -2,8 +2,10 @@ package me.youhavetrouble.notjustnameplates;
|
||||
|
||||
import me.youhavetrouble.notjustnameplates.commands.MainCommand;
|
||||
import me.youhavetrouble.notjustnameplates.displays.DisplayContent;
|
||||
import me.youhavetrouble.notjustnameplates.nameplates.Nameplate;
|
||||
import me.youhavetrouble.notjustnameplates.nameplates.NameplateManager;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
@@ -23,9 +25,12 @@ public final class NotJustNameplates extends JavaPlugin {
|
||||
|
||||
private static boolean papiHook = false;
|
||||
|
||||
public static final MiniMessage miniMessage = null;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
|
||||
config = new NJNConfig(this);
|
||||
papiHook = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
package me.youhavetrouble.notjustnameplates.hooks;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextReplacementConfig;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
public class PAPIHook {
|
||||
|
||||
public static Component setPlaceholders(Component component, OfflinePlayer offlinePlayer) {
|
||||
component = component.replaceText(TextReplacementConfig.builder()
|
||||
.match(PlaceholderAPI.getPlaceholderPattern())
|
||||
.replacement((matchResult, builder) -> LegacyComponentSerializer.legacyAmpersand().deserialize(PlaceholderAPI.setPlaceholders(offlinePlayer, matchResult.group(0))))
|
||||
.build());
|
||||
return component;
|
||||
}
|
||||
|
||||
public static String setPlaceholders(String string, OfflinePlayer offlinePlayer) {
|
||||
return PlaceholderAPI.setPlaceholders(offlinePlayer, string);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -2,10 +2,11 @@ package me.youhavetrouble.notjustnameplates.nameplates;
|
||||
|
||||
import me.youhavetrouble.notjustnameplates.NotJustNameplates;
|
||||
import me.youhavetrouble.notjustnameplates.displays.DisplayContent;
|
||||
import me.youhavetrouble.notjustnameplates.hooks.PAPIHook;
|
||||
import me.youhavetrouble.notjustnameplates.text.TextParser;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Color;
|
||||
import org.bukkit.GameMode;
|
||||
@@ -186,43 +187,8 @@ public class Nameplate {
|
||||
|
||||
if (player == null || !player.isOnline()) return Component.empty();
|
||||
|
||||
if (NotJustNameplates.isPapiHooked()) {
|
||||
text = PAPIHook.setPlaceholders(text, player);
|
||||
}
|
||||
|
||||
text = text.replaceAll("%displayname%", LegacyComponentSerializer.legacyAmpersand().serialize(player.displayName()));
|
||||
|
||||
text = makeColorsWork(LegacyComponentSerializer.AMPERSAND_CHAR, text);
|
||||
text = makeColorsWork(LegacyComponentSerializer.SECTION_CHAR, text);
|
||||
|
||||
return MiniMessage.miniMessage().deserialize(text);
|
||||
return TextParser.parseWithPlaceholders(text, player);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package me.youhavetrouble.notjustnameplates.text;
|
||||
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import me.youhavetrouble.notjustnameplates.NotJustNameplates;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.Tag;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TextParser {
|
||||
|
||||
public static Component parseWithPlaceholders(String input, Player player) {
|
||||
MiniMessage miniMessage = MiniMessage.builder()
|
||||
.tags(TagResolver.builder()
|
||||
.resolvers(
|
||||
StandardTags.defaults(),
|
||||
placeholderTag(player))
|
||||
.build())
|
||||
.build();
|
||||
return miniMessage.deserialize(input);
|
||||
}
|
||||
|
||||
public static @NotNull TagResolver placeholderTag(final @NotNull Player player) {
|
||||
return TagResolver.resolver("placeholder", (argumentQueue, context) -> {
|
||||
final String placeholder = argumentQueue.popOr("placeholder tag requires an argument").value();
|
||||
switch (placeholder) {
|
||||
case "name" -> {
|
||||
return Tag.selfClosingInserting(player.name()) ;
|
||||
}
|
||||
case "displayname" -> {
|
||||
return Tag.selfClosingInserting(player.displayName());
|
||||
}
|
||||
default -> {
|
||||
if (NotJustNameplates.isPapiHooked()) {
|
||||
final String parsedPlaceholder = PlaceholderAPI.setPlaceholders(player, '%' + placeholder + '%');
|
||||
Component componentPlaceholder = LegacyComponentSerializer.legacySection().deserialize(parsedPlaceholder);
|
||||
return Tag.selfClosingInserting(componentPlaceholder);
|
||||
}
|
||||
return Tag.selfClosingInserting(Component.text(placeholder));
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,7 +6,7 @@ nameplates:
|
||||
billboard: "vertical"
|
||||
frames:
|
||||
1:
|
||||
text: "%displayname%"
|
||||
text: "<placeholder:displayname>"
|
||||
|
||||
messages:
|
||||
config-reloaded: "<aqua>NJN Config reloaded."
|
||||
|
||||
Reference in New Issue
Block a user