papi support

This commit is contained in:
2023-07-22 16:07:12 +02:00
parent 7675ef3fc3
commit 7f9f7ff53a
5 changed files with 37 additions and 0 deletions
@@ -20,10 +20,13 @@ public final class NotJustNameplates extends JavaPlugin {
private final TeamManager teamManager = new TeamManager();
private NameplateManager nameplateManager = null;
private static boolean papiHook = false;
@Override
public void onEnable() {
instance = this;
config = new NJNConfig(this);
papiHook = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
DefaultPermissions.registerPermission("notjustnameplates.seeown", "Allows a player to see their own nameplate", PermissionDefault.FALSE);
DefaultPermissions.registerPermission("notjustnameplates.command", "Allows a player to use the /njn command", PermissionDefault.TRUE);
@@ -52,6 +55,7 @@ public final class NotJustNameplates extends JavaPlugin {
public void reloadPluginConfig() {
config = new NJNConfig(this);
papiHook = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null && Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI");
nameplateManager.reloadNameplates();
updateNameplatesBasedOnPermission();
}
@@ -92,4 +96,8 @@ public final class NotJustNameplates extends JavaPlugin {
return config;
}
public static boolean isPapiHooked() {
return papiHook;
}
}
@@ -0,0 +1,19 @@
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;
}
}
@@ -2,6 +2,7 @@ package me.youhavetrouble.notjustnameplates.nameplates;
import me.youhavetrouble.notjustnameplates.NotJustNameplates;
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 org.bukkit.Bukkit;
@@ -167,6 +168,10 @@ public class Nameplate {
if (player == null || !player.isOnline()) return component;
if (NotJustNameplates.isPapiHooked()) {
component = PAPIHook.setPlaceholders(component, player);
}
component = component.replaceText(builder -> {
builder.matchLiteral("%displayname%");
builder.replacement(player.displayName());