2024-01-13 16:37:26 +01:00
---
description: Guide on how to use PlaceholderAPI in your own plugin.
---
2024-01-13 15:59:50 +01:00
# Using PlaceholderAPI
2022-06-30 21:44:30 +02:00
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
2024-01-13 15:59:50 +01:00
2024-01-26 14:20:49 +01:00
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.
2022-06-30 21:44:30 +02:00
2024-01-13 15:59:50 +01:00
/// tab | :simple-apachemaven: Maven
2024-01-26 14:22:13 +01:00
```{ .xml title="pom.xml" data-md-component="api-version" }
2022-06-30 21:44:30 +02:00
< repositories >
< repository >
< id > placeholderapi< / id >
< url > https://repo.extendedclip.com/content/repositories/placeholderapi/< / url >
< / repository >
< / repositories >
< dependencies >
< dependency >
< groupId > me.clip< / groupId >
< artifactId > placeholderapi< / artifactId >
2024-01-13 15:59:50 +01:00
< version > {version}< / version >
2022-06-30 21:44:30 +02:00
< scope > provided< / scope >
< / dependency >
< / dependencies >
```
2024-01-13 15:59:50 +01:00
///
2022-06-30 21:44:30 +02:00
2024-01-13 15:59:50 +01:00
/// tab | :simple-gradle: Gradle
```{ .groovy title="build.gradle" data-md-component="api-version" }
2022-06-30 21:44:30 +02:00
repositories {
maven {
url = 'https://repo.extendedclip.com/content/repositories/placeholderapi/'
}
}
dependencies {
2024-01-13 15:59:50 +01:00
compileOnly 'me.clip:placeholderapi:{version}'
2022-06-30 21:44:30 +02:00
}
```
2024-01-13 15:59:50 +01:00
///
2022-06-30 21:44:30 +02:00
2024-01-26 14:20:49 +01:00
/// details | What is `{version}` ?
type: question
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 ](https://github.com/PlaceholderAPI/PlaceholderAPI/releases ) of the GitHub Repository.
///
2022-06-30 21:44:30 +02:00
### Set PlaceholderAPI as (soft)depend
2024-01-13 15:59:50 +01:00
2023-08-28 01:17:42 +02:00
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.
2022-06-30 21:44:30 +02:00
2024-01-13 15:59:50 +01:00
/// tab | :simple-spigotmc: plugin.yml
//// tab | Optional dependency
2024-09-26 02:14:06 +02:00
///// note |
Tab the :material-plus-circle: icons in the code block below for additional information.
/////
```{ .yaml .annotate title="plugin.yml" }
2024-01-13 15:59:50 +01:00
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.Here
2024-09-26 02:14:06 +02:00
softdepend: ["PlaceholderAPI"] # (1)
2024-01-13 15:59:50 +01:00
```
2024-09-26 02:14:06 +02:00
1. This sets PlaceholderAPI as an optional dependency for your plugin.
2024-01-13 15:59:50 +01:00
////
//// tab | Required dependency
2024-09-26 02:14:06 +02:00
///// note |
Tab the :material-plus-circle: icons in the code block below for additional information.
/////
```{ .yaml .annotate title="plugin.yml" }
2024-01-13 15:59:50 +01:00
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.Here
2024-09-26 02:14:06 +02:00
depend: ["PlaceholderAPI"] # (1)
2024-01-13 15:59:50 +01:00
```
2024-09-26 02:14:06 +02:00
1. This sets PlaceholderAPI as a required dependency for your plugin.
2024-01-13 15:59:50 +01:00
////
///
/// tab | :fontawesome-regular-paper-plane: paper-plugin.yml
//// tab | Optional dependency
2024-09-26 02:14:06 +02:00
///// note |
Tab the :material-plus-circle: icons in the code block below for additional information.
/////
```{ .yaml .annotate title="paper-plugin.yml" }
2024-01-13 15:59:50 +01:00
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.Here
dependencies:
server:
PlaceholderAPI:
2024-09-26 02:14:06 +02:00
load: BEFORE # (1)
2024-01-13 15:59:50 +01:00
required: false
```
2024-09-26 02:14:06 +02:00
1. Load order is relative to the Dependency.
This means that in this example, PlaceholderAPI is loaded **before** your plugin.
2024-01-13 15:59:50 +01:00
////
//// tab | Required dependency
2024-09-26 02:14:06 +02:00
///// note |
Tab the :material-plus-circle: icons in the code block below for additional information.
/////
```{ .yaml .annotate title="paper-plugin.yml" }
2024-01-13 15:59:50 +01:00
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.Here
dependencies:
server:
PlaceholderAPI:
2024-09-26 02:14:06 +02:00
load: BEFORE # (1)
2024-01-13 15:59:50 +01:00
required: true
```
2024-09-26 02:14:06 +02:00
1. Load order is relative to the Dependency.
This means that in this example, PlaceholderAPI is loaded **before** your plugin.
2024-01-13 15:59:50 +01:00
////
///
2022-06-30 21:44:30 +02:00
## Adding placeholders to PlaceholderAPI
2024-01-13 15:59:50 +01:00
A full guide on how to create expansions can be found on the [Creating a PlaceholderExpansion ](creating-a-placeholderexpansion.md ) page.
2022-06-30 21:44:30 +02:00
## Setting placeholders in your plugin
2024-01-13 15:59:50 +01:00
2022-06-30 21:44:30 +02:00
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 ](#set-placeholderapi-as-softdepend ) 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).
2024-09-26 02:14:06 +02:00
/// details | Example
type: example
2024-01-13 15:59:50 +01:00
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:
2024-09-26 02:14:06 +02:00
//// note |
The below example assumes a **soft dependency** on PlaceholderAPI to handle PlaceholderAPI not being present more decently.
Tab the :material-plus-circle: icons in the code block below for additional information.
////
```{ .java .annotate title="JoinExample.java" }
2022-06-30 21:44:30 +02:00
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() {
2024-09-26 02:14:06 +02:00
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
Bukkit.getPluginManager().registerEvents(this, this); // (1)
2022-06-30 21:44:30 +02:00
} else {
2024-09-26 02:14:06 +02:00
getLogger().warn("Could not find PlaceholderAPI! This plugin is required."); // (2)
2022-06-30 21:44:30 +02:00
Bukkit.getPluginManager().disablePlugin(this);
}
}
@EventHandler (priority = EventPriority.HIGHEST)
public void onJoin(PlayerJoinEvent event) {
2024-05-23 22:49:36 +02:00
String joinText = "%player_name% joined the server! They are rank %vault_rank%";
2022-06-30 21:44:30 +02:00
2024-09-26 02:14:06 +02:00
joinText = PlaceholderAPI.setPlaceholders(event.getPlayer(), joinText); // (3)
2022-06-30 21:44:30 +02:00
event.setJoinMessage(joinText);
}
}
```
2024-09-26 02:14:06 +02:00
1. We check that PlaceholderAPI is present and enabled to then register events to handle (See below).
2. In case PlaceholderAPI is not present are we reporting this issue and disable the plugin.
3. Using `PlaceholderAPI.setPlaceholders(Player, String)` we can parse `%placeholder%` text in the provided String, should they have a matching expansion and said expansion return a non-null String.
In our example are we providing a text containing `%player_name%` and `%vault_rank%` to be parsed, which require the Player and Vault expansion respectively.
Example output: `Notch joined the server! They are rank Admin`
2024-05-23 22:49:36 +02:00
///