Using PlaceholderAPI¶
This page is about using PlaceholderAPI in your own plugin, to either let other plugins use your plugin, or just use placeholders from other plugins in your own.
Please note, that the examples in this page are only available for PlaceholderAPI 2.10.0 or higher!
First steps¶
Before you can actually make use of PlaceholderAPI, you first have to import it into your project.
Use the below code example matching your dependency manager.
<repositories>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/releases/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>{version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
repositories {
maven {
url = 'https://repo.extendedclip.com/releases/'
}
}
dependencies {
compileOnly 'me.clip:placeholderapi:{version}'
}
What is {version}
?
Using Javascript, {version}
is replaced with the latest available API version of PlaceholderAPI.
Should you see the placeholder as-is does it mean that you either block Javascript, or that the version couldn't be obtained in time during page load.
You can always find the latest version matching the API version on the releases tab of the GitHub Repository.
Set PlaceholderAPI as (soft)depend¶
Next step is to go to your plugin.yml or paper-plugin.yml and add PlaceholderAPI as a depend or softdepend, depending (no pun intended) on if it is optional or not.
Tab the icons in the code block below for additional information.
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.Here
softdepend: ["PlaceholderAPI"] #
Tab the icons in the code block below for additional information.
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.Here
depend: ["PlaceholderAPI"] #
Tab the icons in the code block below for additional information.
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.Here
dependencies:
server:
PlaceholderAPI:
load: BEFORE #
required: false
Tab the icons in the code block below for additional information.
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.Here
dependencies:
server:
PlaceholderAPI:
load: BEFORE #
required: true
Adding placeholders to PlaceholderAPI¶
A full guide on how to create expansions can be found on the Creating a PlaceholderExpansion page.
Setting placeholders in your plugin¶
PlaceholderAPI offers the ability, to automatically parse placeholders from other plugins within your own plugin, giving the ability for your plugin to support thousands of other placeholders without depending on each plugin individually.
To use placeholders from other plugins in our own plugin, we simply have to (soft)depend on PlaceholderAPI and use the setPlaceholders
method.
It is also important to point out, that any required plugin/dependency for an expansion has to be on the server and enabled, or the setPlaceholders
method will just return the placeholder itself (do nothing).
Example
Let's assume we want to send a custom join message that shows the primary group a player has.
To achieve this, we can do the following:
The below example assumes a soft dependency on PlaceholderAPI to handle PlaceholderAPI not being present more decently.
Tab the icons in the code block below for additional information.
package at.helpch.placeholderapi;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import me.clip.placeholderapi.PlaceholderAPI;
public class JoinExample extends JavaPlugin implements Listener {
@Override
public void onEnable() {
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
Bukkit.getPluginManager().registerEvents(this, this); //
} else {
getLogger().warn("Could not find PlaceholderAPI! This plugin is required."); //
Bukkit.getPluginManager().disablePlugin(this);
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onJoin(PlayerJoinEvent event) {
String joinText = "%player_name% joined the server! They are rank %vault_rank%";
joinText = PlaceholderAPI.setPlaceholders(event.getPlayer(), joinText); //
event.setJoinMessage(joinText);
}
}