Rework PlaceholderExpansion page (#654)

* Rework PlaceholderExpansion page

* typo

* Implement suggestions of FunnyCube

* Apply suggestions from code review

Co-authored-by: Star <iamstarmism@gmail.com>

* Update wiki/PlaceholderExpansion.md

Co-authored-by: Star <iamstarmism@gmail.com>

* Rename "With a Plugin (Internal Jar)" to "With a Plugin (Internal Class)"

Co-authored-by: Star <iamstarmism@gmail.com>
This commit is contained in:
Andre_601 2021-07-22 01:29:30 +02:00 committed by GitHub
parent ec7943fe0a
commit afdf80bf00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,397 +1,290 @@
[placeholderexpansion]: https://github.com/PlaceholderAPI/PlaceholderAPI/blob/master/src/main/java/me/clip/placeholderapi/expansion/PlaceholderExpansion.java
[playerexpansion]: https://github.com/PlaceholderAPI/Player-Expansion
[serverexpansion]: https://github.com/PlaceholderAPI/Server-Expansion
[mathexpansion]: https://github.com/Andre601/Math-expansion
[relational]: https://github.com/PlaceholderAPI/PlaceholderAPI/blob/master/src/main/java/me/clip/placeholderapi/expansion/Relational.java
## Overview ## Overview
This page covers how you can use the `PlaceholderExpansion` to add own placeholders to PlaceholderAPI, which then can be used by other plugins. This page will cover how you can create your own [`PlaceholderExpansion`][placeholderexpansion] which you can either [[Upload to the eCloud|Expansion cloud]] or integrate into your own plugin.
PlaceholderAPI is using Expansions for its placeholders, with PlaceholderAPI providing the core. Users can download Expansions from the cloud server using commands in-game or by going [here](https://api.extendedclip.com/home/) if they want to use the placeholder. It's worth noting that PlaceholderAPI relies on expansions being installed. PlaceholderAPI only acts as the core replacing utility while the expansions allow other plugins to use any installed placeholder in their own messages.
You can download Expansions either directly from the eCloud yourself, or download them through the [[download command of PlaceholderAPI|Commands#papi-ecloud-download]].
## Note ## Table of Contents
You can either make a separate jar file, to upload it to the expansion-cloud (recommended) or have it as a local class inside your plugin.
## Examples - [Getting started](#getting-started)
There are multiple methods and ways you can use the PlaceholderExpansion. - [Common Parts](#common-parts)
Those depend on what you want to display through the placeholders in the end. - [Without a Plugin](#without-a-plugin)
- [With a Plugin (External Jar)](#with-a-plugin-external-jar)
- [With a Plugin (Internal Class)](#with-a-plugin-internal-class)
- [Register the Expansion](#register-the-expansion)
- [Relational Placeholders](#relational-placeholders)
- [Notes about Relational Placeholders](#notes-about-relational-placeholders)
* [Without external plugin](#without-external-plugin) ## Getting started
* [With external plugin](#with-external-plugin) For starters, you need to decide what type of [`PlaceholderExpansion`][placeholderexpansion] you want to create. There are various ways to create an expansion. This page will cover the most common ones.
* [Separate jar](#separate-jar)
* [Internal class](#internal-class)
### Without an external plugin ### Common Parts
This part here covers how you create an expansion that doesn't require any external/additional plugins to function. All shown examples will share the same common parts that belong to the [`PlaceholderExpansion`][placeholderexpansion] class.
Examples of such expansions are: In order to not repeat the same basic info for each method throughout this page, and to greatly reduce its overall length, we will cover the most basic/necessary ones here.
- [Player expansion](/PlaceholderAPI/Player-Expansion) ``
- [Math expansion](https://github.com/Andre601/Math-Expansion)
- [Statistics expansion](/PlaceholderAPI/Statistics-Expansion)
Since it would be weird (and also make no real sense) to have this inside your plugin, we assume you make a separate jar-file as an expansion. #### Basic PlaceholderExpansion Structure
To begin, first make the class extend the `PlaceholderExpansion` and add the required methods:
```java ```java
package at.helpch.placeholderapi.example.expansions; package at.helpch.placeholderapi.example.expansions;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
/**
* This class will automatically register as a placeholder expansion
* when a jar including this class is added to the directory
* {@code /plugins/PlaceholderAPI/expansions} on your server.
* <br>
* <br>If you create such a class inside your own plugin, you have to
* register it manually in your plugins {@code onEnable()} by using
* {@code new YourExpansionClass().register();}
*/
public class SomeExpansion extends PlaceholderExpansion { public class SomeExpansion extends PlaceholderExpansion {
/**
* This method should always return true unless we
* have a dependency we need to make sure is on the server
* for our placeholders to work!
*
* @return always true since we do not have any dependencies.
*/
@Override
public boolean canRegister(){
return true;
}
/**
* The name of the person who created this expansion should go here.
*
* @return The name of the author as a String.
*/
@Override @Override
public String getAuthor(){ public String getAuthor(){
return "someauthor"; return "someauthor";
} }
/**
* The placeholder identifier should go here.
* <br>This is what tells PlaceholderAPI to call our onRequest
* method to obtain a value if a placeholder starts with our
* identifier.
* <br>The identifier has to be lowercase and can't contain _ or %
*
* @return The identifier in {@code %<identifier>_<value>%} as String.
*/
@Override @Override
public String getIdentifier(){ public String getIdentifier(){
return "example"; return "example";
} }
/**
* This is the version of this expansion.
* <br>You don't have to use numbers, since it is set as a String.
*
* @return The version as a String.
*/
@Override @Override
public String getVersion(){ public String getVersion(){
return "1.0.0"; return "1.0.0";
} }
/**
* This is the method called when a placeholder with our identifier
* is found and needs a value.
* <br>We specify the value identifier in this method.
* <br>Since version 2.9.1 can you use OfflinePlayers in your requests.
*
* @param player
* A {@link org.bukkit.OfflinePlayer OfflinePlayer}.
* @param identifier
* A String containing the identifier/value.
*
* @return Possibly-null String of the requested identifier.
*/
@Override
public String onRequest(OfflinePlayer player, String identifier){
// %example_placeholder1%
if(identifier.equals("placeholder1")){
return "placeholder1 works";
}
// %example_placeholder2%
if(identifier.equals("placeholder2")){
return "placeholder2 works";
}
// We return null if an invalid placeholder (f.e. %example_placeholder3%)
// was provided
return null;
}
} }
``` ```
Let's quickly break down the different methods you have to implement.
- #### getAuthor
This method allows you to set the name of the expansion's author.
- #### getIdentifier
The name that should be used to identify the placeholders for this expansion.
The identifier is the string after the starting `%` and before the first `_` (`%identifier_values%`) and, therefore, cannot contain any `_`s.
If you want to use `_` in your expansion's name, you can override the optional `getName()` method.
- #### getVersion
This is a string, which means it can contain more than just a number. This is used to determine if a new update is available or not when the expansion is shared on the eCloud.
For expansions that are part of a plugin, this does not really matter.
Those are all the neccessary parts for your PlaceholderExpansion.
Any other methods that are part of the [`PlaceholderExpansion`][placeholderexpansion] class are optional and will usually not be used, or will default to a specific value. Please read the Javadoc comments of those methods for more information.
You must choose between one of these two methods for handling the actual parsing of placeholders:
- #### onRequest(OfflinePlayer, String)
If not explicitly set, this will automatically call [`onPlaceholderRequest(Player, String)`](#onplaceholderrequestplayer-string).
This method is recommended as it allows the usage of `null` and can therefore be used in placeholders that don't require a valid player to be used.
- #### onPlaceholderRequest(Player, String)
If not set, this method will return `null` which PlaceholderAPI sees as an invalid placeholder.
---- ----
## Without a Plugin
An expansion does not always need a plugin to rely on. If the placeholders it provides can return values from just the server itself or some other source (i.e. Java itself), then it can work independently.
### With external plugin Common examples of such Expansions are:
Those examples here applies to people who want to provide information from their own plugin through placeholders from PlaceholderAPI.
There exists a repository showcasing an [example-expansion](/PlaceholderAPI/Example-Expansion) with what you can/should do.
In our examples do we have the plugin `SomePlugin` and want to show certain placeholders with it. - [Player Expansion][playerexpansion]
- [Server Expansion][serverexpansion]
- [Math Expansion][mathexpansion]
There are two ways to actually get information from your plugin and they only are different in if you have the expansion as a separate jar-file or as an internal class. These kinds of expansions don't require any additional plugins to function.
When creating such an expansion is it recommended to use [`onRequest(OfflinePlayer, String)`](#onrequestofflineplayer-string).
#### Separate jar #### Full Example
In our separate jar do we have to make some checks, to be sure, that the required plugin is installed and running. Please see the [Common parts](#common-parts) section for info on the other methods.
The below code shows how the class can look like in case the plugin doesn't have a public and easy to access API.
```java ```java
package at.helpch.placeholderapi.example.expansions; package at.helpch.placeholderapi.example.expansions;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import at.helpch.placeholderapi.example.SomePlugin;
/**
* This class will automatically register as a placeholder expansion
* when a jar including this class is added to the directory
* {@code /plugins/PlaceholderAPI/expansions} on your server.
* <br>
* <br>If you create such a class inside your own plugin, you have to
* register it manually in your plugins {@code onEnable()} by using
* {@code new YourExpansionClass().register();}
*/
public class SomeExpansion extends PlaceholderExpansion { public class SomeExpansion extends PlaceholderExpansion {
// We get an instance of the plugin later.
private SomePlugin plugin;
/**
* Since this expansion requires api access to the plugin "SomePlugin"
* we must check if said plugin is on the server or not.
*
* @return true or false depending on if the required plugin is installed.
*/
@Override
public boolean canRegister(){
return (plugin = (SomePlugin) Bukkit.getPluginManager().getPlugin(getRequiredPlugin())) != null;
}
/**
* The name of the person who created this expansion should go here.
*
* @return The name of the author as a String.
*/
@Override @Override
public String getAuthor(){ public String getAuthor(){
return "someauthor"; return "someauthor";
} }
/**
* The placeholder identifier should go here.
* <br>This is what tells PlaceholderAPI to call our onRequest
* method to obtain a value if a placeholder starts with our
* identifier.
* <br>The identifier has to be lowercase and can't contain _ or %
*
* @return The identifier in {@code %<identifier>_<value>%} as String.
*/
@Override @Override
public String getIdentifier(){ public String getIdentifier(){
return "someplugin"; return "example";
} }
/**
* if the expansion requires another plugin as a dependency, the
* proper name of the dependency should go here.
* <br>Set this to {@code null} if your placeholders do not require
* another plugin to be installed on the server for them to work.
* <br>
* <br>This is extremely important to set your plugin here, since if
* you don't do it, your expansion will throw errors.
*
* @return The name of our dependency.
*/
@Override
public String getRequiredPlugin(){
return "SomePlugin";
}
/**
* This is the version of this expansion.
* <br>You don't have to use numbers, since it is set as a String.
*
* @return The version as a String.
*/
@Override @Override
public String getVersion(){ public String getVersion(){
return "1.0.0"; return "1.0.0";
} }
/**
* This is the method called when a placeholder with our identifier
* is found and needs a value.
* <br>We specify the value identifier in this method.
* <br>Since version 2.9.1 can you use OfflinePlayers in your requests.
*
* @param player
* A {@link org.bukkit.Player Player}.
* @param identifier
* A String containing the identifier/value.
*
* @return possibly-null String of the requested identifier.
*/
@Override @Override
public String onPlaceholderRequest(Player player, String identifier){ public String onRequest(OfflinePlayer player, String params){
if(params.equalsIgnoreCase("name")){
return player == null ? null : player.getName(); // "name" requires the player to be valid
if(p == null){ if(params.equalsIgnoreCase("placeholder1")){
return ""; return "Placeholder Text 1";
}
// %someplugin_placeholder1% if(params.equalsIgnoreCase("placeholder2")){
if(identifier.equals("placeholder1")){ return "Placeholder Text 2";
return plugin.getConfig().getString("placeholder1", "value doesnt exist");
}
// %someplugin_placeholder2% return null; // Placeholder is unknown by the Expansion
if(identifier.equals("placeholder2")){
return plugin.getConfig().getString("placeholder2", "value doesnt exist");
}
// We return null if an invalid placeholder (f.e. %someplugin_placeholder3%)
// was provided
return null;
} }
} }
``` ```
---- ----
#### Internal class ## With a Plugin (External Jar)
You can have the class inside your plugin. If your expansion relies on a plugin to provide its placeholder values, you will need to override a few more methods to make sure everything will work correctly.
This has some advantages but can also have some disadvantages.
If you include a PlaceholderExpansion class in your plugin, you MUST add an override the persist() method to return true Your expansion will need to override the `getRequiredPlugin()` method to return the name of the plugin your expansion depends on.
otherwise, if PlaceholderAPI is reloaded, your expansion will be unregistered and lost forever. PlaceholderAPI automatically checks if this method will either return null, or if the name defined results in a non-null plugin.
It is worth noting that it is a bit more difficult to make a separate jar file that depends on a plugin, as it will require the plugin to have some sort of accessible API in order to get the required values.
One way to bypass this is to override the `canRegister()` method with the following code:
```java
SomePlugin plugin; // This would be the plugin your expansion depends on
@Override
public boolean canregister(){
// This sets plugin to the SomePlugin instance you get through the PluginManager
return (plugin = (SomePlugin) Bukkit.getPluginManager().getPlugin(getRequiredPlugin())) != null;
}
```
Using this code-snippet, you can get a direct instance of the plugin and access things such as config values.
With that said, it is recommended instead to use an API if one is available, as this kind of plugin access is a relatively poor approach.
#### Full Example
Please see the [Common parts](#common-parts) section for info on the other methods.
```java
package at.helpch.placeholderapi.example.expansions;
import at.helpch.placeholderapi.example.SomePlugin;
import org.bukkit.OfflinePlayer;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
public class SomeExpansion extends PlaceholderExpansion {
SomePlugin plugin; // This instance is assigned in canRegister()
@Override
public String getAuthor(){
return "someauthor";
}
@Override
public String getIdentifier(){
return "example";
}
@Override
public String getVersion(){
return "1.0.0";
}
@Override
public String getRequiredPlugin(){
return "SomePlugin";
}
@Override
public boolean canRegister(){
return (plugin = (SomePlugin) Bukkit.getPluginManager().getPlugin(getRequiredPlugin())) != null;
}
@Override
public String onRequest(OfflinePlayer player, String params){
if(params.equalsIgnoreCase("placeholder1")){
return plugin.getConfig().getString("placeholders.placeholder1", "default1");
if(params.equalsIgnoreCase("placeholder2")){
return plugin.getConfig().getString("placeholders.placeholder2", "default2");
return null; // Placeholder is unknown by the expansion
}
}
```
----
## With a Plugin (Internal Class)
The way expansions are handled when they are part of the plugin itself is fairly similar to when you [make an expansion without a plugin dependency](#without-a-plugin).
In fact, you don't even have to override the `getRequiredPlugin()` and `canRegister()` methods as it is always guaranteed that the plugin is available.
Something worth noting, however, is that you need to override the `persist()` method and make it return true. This ensures that the expansion won't be unregistered by PlaceholderAPI whenever it is reloaded.
Finally, you can also use dependency injection as an easier way to access a plugin's methods.
Here is a small code example of how dependency injection may look:
```java
public class SomeExpansion extends PlaceholderExpansion {
final SomePlugin plugin; // The instance is created in the constructor and won't be modified, so it can be final
public SomeExpansion(SomePlugin plugin){
this.plugin = plugin;
}
}
```
#### Full Example
Please see the [Common parts](#common-parts) section for info on the other methods.
```java ```java
package at.helpch.placeholderapi.example.expansions; package at.helpch.placeholderapi.example.expansions;
import at.helpch.placeholderapi.example.SomePlugin;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import at.helpch.placeholderapi.example.SomePlugin;
/**
* This class will be registered through the register-method in the
* plugins onEnable-method.
*/
public class SomeExpansion extends PlaceholderExpansion { public class SomeExpansion extends PlaceholderExpansion {
private SomePlugin plugin; private SomePlugin plugin;
/**
* Since we register the expansion inside our own plugin, we
* can simply use this method here to get an instance of our
* plugin.
*
* @param plugin
* The instance of our plugin.
*/
public SomeExpansion(SomePlugin plugin){ public SomeExpansion(SomePlugin plugin){
this.plugin = plugin; this.plugin = plugin;
} }
/**
* Because this is an internal class,
* you must override this method to let PlaceholderAPI know to not unregister your expansion class when
* PlaceholderAPI is reloaded
*
* @return true to persist through reloads
*/
@Override
public boolean persist(){
return true;
}
/**
* Because this is a internal class, this check is not needed
* and we can simply return {@code true}
*
* @return Always true since it's an internal class.
*/
@Override
public boolean canRegister(){
return true;
}
/**
* The name of the person who created this expansion should go here.
* <br>For convienience do we return the author from the plugin.yml
*
* @return The name of the author as a String.
*/
@Override @Override
public String getAuthor(){ public String getAuthor(){
return plugin.getDescription().getAuthors().toString(); return "someauthor";
} }
/**
* The placeholder identifier should go here.
* <br>This is what tells PlaceholderAPI to call our onRequest
* method to obtain a value if a placeholder starts with our
* identifier.
* <br>The identifier has to be lowercase and can't contain _ or %
*
* @return The identifier in {@code %<identifier>_<value>%} as String.
*/
@Override @Override
public String getIdentifier(){ public String getIdentifier(){
return "someplugin"; return "example";
} }
/**
* This is the version of the expansion.
* <br>You don't have to use numbers, since it is set as a String.
*
* For convienience do we return the version from the plugin.yml
*
* @return The version as a String.
*/
@Override @Override
public String getVersion(){ public String getVersion(){
return plugin.getDescription().getVersion(); return "1.0.0";
} }
/**
* This is the method called when a placeholder with our identifier
* is found and needs a value.
* <br>We specify the value identifier in this method.
* <br>Since version 2.9.1 can you use OfflinePlayers in your requests.
*
* @param player
* A {@link org.bukkit.Player Player}.
* @param identifier
* A String containing the identifier/value.
*
* @return possibly-null String of the requested identifier.
*/
@Override @Override
public String onPlaceholderRequest(Player player, String identifier){ public boolean persist(){
return true; // This is required or else PlaceholderAPI will unregister the Expansion on reload
if(player == null){
return "";
} }
// %someplugin_placeholder1% @Override
if(identifier.equals("placeholder1")){ public String onRequest(OfflinePlayer player, String params){
return plugin.getConfig().getString("placeholder1", "value doesnt exist"); if(params.equalsIgnoreCase("placeholder1")){
} return plugin.getConfig().getString("placeholders.placeholder1", "default1");
// %someplugin_placeholder2% if(params.equalsIgnoreCase("placeholder2")){
if(identifier.equals("placeholder2")){ return plugin.getConfig().getString("placeholders.placeholder2", "default2");
return plugin.getConfig().getString("placeholder2", "value doesnt exist");
}
// We return null if an invalid placeholder (f.e. %someplugin_placeholder3%) return null; // Placeholder is unknown by the Expansion
// was provided
return null;
} }
} }
``` ```
As you can see is this method pretty similar to the one without any external plugins, since we can get an instance of our plugin much easier and also have a 100% guarantee that the plugin is installed and running. ### Register the Expansion
To register the expansion, you will need to call the `register()` method yourself.
This should be done in your plugin's `onEnable()` method after you make sure that PlaceholderAPI is installed and enabled.
Our final step now is to register the class and its placeholders. The plugin doesn't do this on its own.
To achieve this, add the following to your `onEnable()` section (Use your expansion name of course):
```java ```java
package at.helpch.placeholderapi.example package at.helpch.placeholderapi.example
@ -409,3 +302,73 @@ public class SomePlugin extends JavaPlugin{
} }
} }
``` ```
----
## Relational Placeholders
Relational Placeholders are a bit more specific compared to the previous examples.
While they do use the same [common parts](#common-parts) that the other examples do, they have a different method to return placeholders.
In order to use the relational placeholders feature, you will need to implement the [`Relational`][relational] interface, which in return adds the `onPlaceholderRequest(Player, Player, String)` method to use.
#### Full Example
Please see the [Common parts](#common-parts) section for info on the other methods.
In this example, we use the [Internal class setup](#with-a-plugin-internal-jar) and `SomePlugin` has an `areFriends(Player, Player)` method that returns true or false based on if the given players are friends.
```java
package at.helpch.placeholderapi.example.expansions;
import at.helpch.placeholderapi.example.SomePlugin;
import org.bukkit.ChatColor;
import org.bukkit.Player;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import me.clip.placeholderapi.expansion.Relational;
public class SomeExpansion extends PlaceholderExpansion implements Relational {
SomePlugin plugin;
public SomeExpansion(SomePlugin plugin){
this.plugin = plugin;
}
@Override
public String getAuthor(){
return "someauthor";
}
@Override
public String getIdentifier(){
return "example";
}
@Override
public String getVersion(){
return "1.0.0";
}
@Override
public boolean persist(){
return true; // This is required or else PlaceholderAPI will unregister the Expansion on reload
}
@Override
public String onPlaceholderRequest(Player one, Player two, String identifier){
if(one == null || two == null)
return null; // We require both Players to be online
if(params.equalsIgnoreCase("friend")){
if(plugin.areFriends(one, two))
return ChatColor.GREEN + one.getName() + " and " + two.getName() + " are friends!";
else
return ChatColor.GREEN + one.getName() + " and " + two.getName() + " are not friends!";
}
return null; // Placeholder is unknown by the Expansion
}
}
```
### Notes about Relational Placeholders
Relational placeholders will always start with `%rel_` to properly identify them.
So in the above example, the full placeholder will look like `%rel_example_friend%`.