Creation of new placeholders is simple. You just need to import
eu.pb4.placeholders.api.Placeholders and call static
register method. 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
- A function (in form of lambda for example) that takes PlaceholderContext and nullable string argument, returns PlaceholderResult,
Using the context¶
PlaceholderContext object passed to placeholder contains allows retrieving the server, the
ServerCommandSource, the source world (if exist), the source
ServerPlayerEntity (if exist), the source
Entity (if exists), and the source's
GameProfile (if exists).
It also includes few methods for checking if they are present, such as
Here is example for a placeholder, which requires a player:
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(Text text)- Creates a value with text
PlaceholderResult.value(String text)- Creates a value from string, by parsing it with TextParser
If it was invalid (for example, no player or argument):
PlaceholderResult.invalid()- Creates simple invalid result
PlaceholderResult.invalid(String reason)-- Creates invalid result with a reason. The reason is returned as a response, but may be used further by other parsers.