Registering PlaceholderAPI Placeholders¶
Warning
The Placeholder manager is an optional manager. This manager should only be accessed if the PlaceholderAPI plugin is present on the server when the PySpigot plugin is enabled.
PySpigot includes a manager that interfaces with PlaceholderAPI if you would like to register placeholder expansions in your script.
For instructions on importing the placeholder manager into your script, visit the General Information page.
Placeholder Manager Usage¶
All placeholders created by scripts will follow this general format: %script:<scriptname>_<placeholder>%, where <scriptname> is the name of your script (without .py), and <placeholder> is the specific placeholder, which you will handle yourself in a placeholder "replacer" function. See the code example below for details.
Relational placeholders can also be registered with the placeholder manager, and they follow the general format %rel_script:<scriptname>_<placeholder>%, where <scriptname> is the name of your script (without .py), and <placeholder> is the specific placeholder, which you will handle yourself in the relational placeholder "replacer" function. See the code example below for details.
There are several functions available from the placeholder manager for registering/unregistering placeholders:
registerPlaceholder(placeholder_function): Registers a new placeholder expansion with default author ("Script Author") and version ("1.0.0").- When the placeholder is used,
placeholder_functionis called. Should return the text that should replace the placeholder. - Returns a
ScriptPlaceholder, which represents the placeholder that was registered.
- When the placeholder is used,
registerPlaceholder(placeholder_function, relational_placeholder_function): Registers a new placeholder expansion with default author ("Script Author") and version ("1.0.0").- When the placeholder is used,
placeholder_functionis called. Should return the text that should replace the placeholder. This argument can beNone. - When a relational placeholder is used,
relational_placeholder_functionis used. Should return the text that should replace the placeholder. This argument can beNone. - Returns a
ScriptPlaceholder, which represents the placeholder that was registered.
- When the placeholder is used,
registerPlaceholder(placeholder_function, author, version): Registers a new placeholder expansion with a custom author and version.- When the placeholder is used,
placeholder_functionis called. Should return the text that should replace the placeholder. - Returns a
ScriptPlaceholder, which represents the placeholder that was registered.
- When the placeholder is used,
registerPlaceholder(placeholder_function, relational_placeholder_function, author, version): Registers a new placeholder expansion with a custom author and version.- When the placeholder is used,
placeholder_functionis called. Should return the text that should replace the placeholder. - When a relational placeholder is used,
relational_placeholder_functionis used. Should return the text that should replace the placeholder. This argument can beNone. - Returns a
ScriptPlaceholder, which represents the placeholder that was registered.
- When the placeholder is used,
unregisterPlaceholder(placeholder): Unregisters a previously registered placeholder. Takes a ScriptPlaceholder that was previously returned by theregisterPlaceholderfunction.
Tip
You do not need to unregister your placeholders when your script is stopped/unloaded. PySpigot will handle this for you.
Note
Scripts can only have one placeholder expansion registered at a time. To see how to define multiple placeholders for a script, see the code example below.
Code Examples¶
Regular Placeholder¶
Let's look at the following code that defines and registers a placeholder expansion and replaces two placeholders:
-
Here, we import PySpigot as
psto utilize the placeholder manager (placeholder). -
Here, we define
replace, a function that will be called when the placeholder is used. This function takes two parameters.offline_playeris a Bukkit API OfflinePlayer that represents the player associated with the placeholder.placeholderis the text of the specific placeholder. -
Here, we check if
placeholderis equal to the specific placeholderplaceholder1, a placeholder we define. Ifplaceholderis equal toplaceholder1, then we return the "replaced" text. -
Here, we use
elifto check ifplaceholderis equal to another specific placeholderplaceholder2, another placeholder we define. Ifplaceholderis equal toplaceholder2, then we return the "replaced" text. -
Here, we register our placeholder expansion with the
registerPlaceholderfunction, passing the replacer function we defined earlier. We also assign the returned value ofregisterPlaceholdertoplaceholder. This is aScriptPlaceholderobject, which represents the placeholder expansion that was registered. This can be used to unregister the placeholder expansion if you would like to do so later. We also passNonefor the second argument of this function, as the second argument accepts a function to replace relational placeholders, which we are not doing in this example.
Note that the replacer function for the placeholder expansion takes two arguments:
- The
offline_playerargument is the player associated with the placeholder. For example, if the placeholder is used in the context of a command, thenoffline_playerwould be the player that typed/executed the command. If no player was associated with the placeholder when it was used, then this argument will beNone. - The
placeholderargument is the name of the placeholder that was used.
All placeholder expansion functions should follow this syntax.
Warning
In the above example, and with all placeholders, offline_player could be None if there is no player associated with the placeholder.
If the name of the script is test.py, the placeholders in the above example would be %script:test_placeholder1% and %script:test_placeholder2%.
Relational Placeholder¶
Let's look at the following code that defines and registers a placeholder expansion for a relational placeholder.
-
Here, we import PySpigot as
psto utilize the placeholder manager (placeholder). -
Here, we define
replace_relational, a function that will be called when the relational placeholder is used. This function takes three parameters.player_oneis the first player andplayer_twois the second player associated with the relational placeholder.placeholderis the text of the specific relational placeholder. -
Here, we check if
placeholderis equal to the specific relational placeholder,player_distancein this case. -
Here, we return the distance from player one to player two for the text to be replaced.
-
Here, we register our placeholder expansion with the
registerPlaceholderfunction, passing the replacer function we defined earlier. We also assign the returned value ofregisterPlaceholdertoplaceholder. This is aScriptPlaceholderobject, which represents the placeholder expansion that was registered. This can be used to unregister the placeholder expansion if you would like to do so later. We also passNonefor the first argument of this function, as the first argument accepts a function to replace regular placeholders, which we are not doing in this example.
If the name of the script is test.py, the placeholder in the above example would be %rel_script:test_player_distance%.
Tip
Multiple relational placeholders can be registered at the same time (using the same replacer function) by following the structure of the example in the previous section.
Unregistering a Placeholder¶
Continuing the above code example:
- Here, we unregister the placeholder expansion by passing the
ScriptPlaceholderobject we assigned earlier when registering the placeholder.
Multiple Placeholders¶
As you can see in the above example, you need not register a new placeholder expansion for each specific placeholder you want to define. Instead, all you need to do is check if the placeholder parameter of your replacer function is equal to the placeholder you want to define using if and elif.
Summary¶
- Placeholders defined by scripts follow the format
%script:<scriptname>_<placeholder>%. - Relational placeholders follow the format
%rel_script:<scriptname>_<placeholder>%. - Regular placeholder replacer functions should take two parameters,
offline_playerandplaceholder. You can name them whatever you like. It will be called when the placeholder is used.offline_playeris the player associated with the placeholder, if there is one.placeholderis the specific placeholder that was used. - Relational placeholder replacer functions should take three parameters,
player_one,player_two, andplaceholder. You can name them whatever you like. It will be called when the placeholder is used.player_oneandplayer_twoare the two players associated with the relational placeholder.placeholderis the specific placeholder that was used. - Register your placeholder with PySpigot's placeholder manager using
registerPlaceholder(placeholder_function, relational_placeholder_function)orregisterPlaceholder(placeholder_function, relational_placeholder_function, author, version). - Each script can only have one placeholder expansion registered at a time. Check the
placeholderparameter of your replacer function (usingifandelif) to handle multiple placeholders. - When registering a placeholder expansion, the register functions all return a
ScriptPlaceholder, which can be used to unregister the placeholder expansion at a later time.