9
0
mirror of https://github.com/HibiscusMC/HibiscusCommons.git synced 2025-12-19 15:09:26 +00:00

feat: add papi tag handler

This commit is contained in:
LoJoSho
2024-03-18 23:34:41 -05:00
parent cea9a60b4c
commit 5ab438a451
2 changed files with 30 additions and 1 deletions

View File

@@ -10,7 +10,7 @@ plugins {
}
group = "me.lojosho"
version = "0.2.8"
version = "0.2.8-DEV"
allprojects {
apply(plugin = "java")

View File

@@ -1,8 +1,14 @@
package me.lojosho.hibiscuscommons.util;
import me.lojosho.hibiscuscommons.hooks.Hooks;
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 AdventureUtils {
@@ -16,4 +22,27 @@ public class AdventureUtils {
).
build();
/**
* Creates a tag resolver capable of resolving PlaceholderAPI tags for a given player.
* Credit to mbaxter, <a href="https://gist.github.com/kezz/ff1bcb8c8db4e113e0119c210026d5ad">...</a>
*
* @param player the player
* @return the tag resolver
*/
public @NotNull TagResolver papiTag(final @NotNull Player player) {
return TagResolver.resolver("papi", (argumentQueue, context) -> {
// Get the string placeholder that they want to use.
final String papiPlaceholder = argumentQueue.popOr("papi tag requires an argument").value();
// Then get PAPI to parse the placeholder for the given player.
final String parsedPlaceholder = Hooks.processPlaceholders(player, '%' + papiPlaceholder + '%');
// We need to turn this ugly legacy string into a nice component.
final Component componentPlaceholder = LegacyComponentSerializer.legacySection().deserialize(parsedPlaceholder);
// Finally, return the tag instance to insert the placeholder!
return Tag.selfClosingInserting(componentPlaceholder);
});
}
}