diff --git a/build.gradle.kts b/build.gradle.kts index 154499c..34dcb3f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ plugins { } group = "me.lojosho" -version = "0.2.8" +version = "0.2.8-DEV" allprojects { apply(plugin = "java") diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/util/AdventureUtils.java b/common/src/main/java/me/lojosho/hibiscuscommons/util/AdventureUtils.java index 69ce6ec..852cff8 100644 --- a/common/src/main/java/me/lojosho/hibiscuscommons/util/AdventureUtils.java +++ b/common/src/main/java/me/lojosho/hibiscuscommons/util/AdventureUtils.java @@ -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, ... + * + * @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); + }); + } + }