Skip to content

Parsing placeholders

Wundero edited this page Sep 5, 2017 · 7 revisions

In order to provide numerous different functions for placeholders, PlaceholderAPI provides a multitude of ways to parse placeholders.

The PlaceholderService class provides a lot of methods, however most of them boil down to a few key methods that can be used for specific purposes.

1: fillPlaceholders(template, source, observer)

2: parse(placeholder, source, observer)

3: replacePlaceholders(template, source, observer)

Fill

This is the first way to parse placeholders. This method takes in a template, source and observer (and only template needs to be present) and outputs a map of placeholder keys to values. This map can be applied to the text template in order to fill its arguments. If an argument is not optional but PlaceholderAPI cannot fill it with a placeholder, it will instead be filled with "{key}", using the key of the argument and the open and close strings of the template.

Parse

This is the second way to parse placeholders, and contains several submethods. Three of these submethods involve using one placeholder, the other parses a list of placeholders. This method provides a way to directly obtain the value that a placeholder returns, rather than filling a template or parsing a long string into a text. This is beneficial because you can, for instance, use this in a script. You could parse %statistic_deaths% and it would return the numerical value instead of a string.

Replace

This is the third and final way to parse placeholders. This method contains numerous submethods which are helpful in reducing argument count. This method takes in a String, Text or TextTemplate and replaces the arguments in it with the parsed placeholder values, then returns it as a Text object. Using this is identical to calling fill and then building it into a text using the returned map, however this can also take in a String or Text, which is useful for parsing chat messages, config files or commands as well.