Skip to content

Instantly share code, notes, and snippets.

@kezz
Created June 14, 2022 15:13
Show Gist options
  • Select an option

  • Save kezz/ff1bcb8c8db4e113e0119c210026d5ad to your computer and use it in GitHub Desktop.

Select an option

Save kezz/ff1bcb8c8db4e113e0119c210026d5ad to your computer and use it in GitHub Desktop.
PlaceholderAPI in MiniMessage
// Example method to create a MiniMessage placeholder that parses PlaceholderAPI placeholders for a player.
// The tag added is of the format "<papi:papi_placeholder>". For example, "<papi:luckperms_prefix>".
// Credit to mbaxter.
/**
* Creates a tag resolver capable of resolving PlaceholderAPI tags for a given player.
*
* @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 = PlaceholderAPI.setPlaceholders(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);
});
}
@mbax
Copy link

mbax commented Jul 7, 2022

omg I'm famous!

Keep in mind this is a simple example and you may need additional complexity.

  1. Self closing tags are great for inserting content like a player's account balance into an existing formatting structure, but sometimes people liked using placeholders for establishing formatting that persists past the placeholder and impacts subsequent content. It could be worth creating a further argument for the tag resolver to cause it to become an inserting tag instead of self-closing inserting tag, like <papi:luckperms_prefix:inserting> or similar.

  2. This example doesn't additionally include relational placeholders, which have been around for five years. You'd either want a second resolver or just have code in the above example that also detects if the placeholder argument starts with rel_. The method in papi for getting that is:

PlaceholderAPI.setRelationalPlaceholders(Player one, Player two, String message);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment