Adding placeholders¶
Creation of new placeholders is simple. You just need to import eu.pb4.placeholders.api.Placeholders and call static registerServer or registerCommon method. Main difference of these two is which type of PlaceholderContext object you get (and if it can be used outside of server context). You only need to provide 2 arguments: - Identifier with your mod id as namespace and path as argument name (with one additional limitation being not allowed to use / in it). - A function (in form of lambda for example) that takes PlaceholderContext and nullable string argument, returns PlaceholderResult,
You can additionally provide an argument parser, allowing you to handler argument values early.
Example of common parser
Using the context¶
ServerPlaceholderContext object passed to placeholder contains allows retrieving the server, the ServerCommandSource, the source world (if exist), the source ServerPlayer (if exist) and everything PlaceholderContext does. It extends the PlaceholderContext, which works on both server and client and provides the source Entity (if exists), the source Player (if exist), and the source's GameProfile (if exists).
It also includes few methods for checking if they are present, such as hasLevel(), hasPlayer(), hasGameProfile(), and hasEntity().
Here is example for a placeholder, which requires a player:
Arguments¶
You can also add an argument to your placeholder, which removes requirement of mostly repeated placeholders and allows degree of customisation. Argument itself is a string, so you can parse it in any way.
Returning correct value¶
Placeholders need to return instance of PlaceholderResult. It can be created by usage of provided static methods on this class.
If it was successful:
PlaceholderResult.value(Component text)- Creates a value with text
If it was invalid (for example, no player or argument):
PlaceholderResult.invalid()- Creates simple invalid resultPlaceholderResult.invalid(String reason)-- Creates invalid result with a reason. The reason is returned as a response, but may be used further by other parsers.