-
Notifications
You must be signed in to change notification settings - Fork 209
Hook into PlaceholderAPI
*The GitHub release may be different from the spigot release
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!
Before you can actually make use of PlaceholderAPI, you first have to import it into your project.
To import PlaceholderAPI, simply add the following code to your pom.xml
Replace {VERSION}
with the version listed at the top of this page.
<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>
<version>{VERSION}</version>
<scope>provided</scope>
</dependency>
</dependencies>
Here is how you can import PlaceholderAPI through gradle.
Put this into your Gradle.build.
Replace {VERSION}
with the version listed at the top of this page.
repositories {
maven {
url = 'https://repo.extendedclip.com/content/repositories/placeholderapi/'
}
}
dependencies {
compileOnly 'me.clip:placeholderapi:{VERSION}'
}
Do the following steps, if you can't import PlaceholderAPI through Maven or Gradle.
Rightclick your project and choose "Properties -> Java Build Path".
Click on "Add external jar" and add the PlaceholderAPI.jar (Pluginpage) to your project.
- Click on File -> Project Structure (Ctrl + Alt + Shift + S on windows)
- Go to the tab Modules
- Click the + on the right side and choose Library... -> Java
- Select the PlaceholderAPI.jar
Next step is to go to your plugin.yml and add PlaceholderAPI as a depend or softdepend, depending (no pun intended) on if it is optional or not.
Example Softdepend:
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.here
softdepend: [PlaceholderAPI] # This is used, if your plugin works without PlaceholderAPI.
Example Depend:
name: ExamplePlugin
version: 1.0
author: author
main: your.main.path.here
depend: [PlaceholderAPI] # If your plugin requires PlaceholderAPI, to work, use this.
You can add your placeholders to PlaceholderAPI plugin by making an external expansion or by implementing that into your plugin itself if you're adding placeholders from a plugin.
Here you can see how to make an external expansion that doesn't require a plugin (Placeholders without a plugin).
You can add your plugin's placeholders to PlaceholderAPI with two ways:
- By making an external expansion (Separate jar file).
- By creating a class inside your plugin (Inner class).
To use placeholders from other plugins in our own plugin, we simply have to use the setPlaceholders
method.
Let's assume we want to send an own join message that shows the group a player has.
To achieve that, we can do the following:
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().getPlugin("PlaceholderAPI") != null) {
/*
* We register the EventListeneres here, when PlaceholderAPI is installed.
* Since all events are in the main class (this class), we simply use "this"
*/
Bukkit.getPluginManager().registerEvents(this, this);
} else {
throw new RuntimeException("Could not find PlaceholderAPI!! Plugin can not work without it!");
}
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onJoin(PlayerJoinEvent event) {
String joinText = "%player_name% &ajoined the server! He/she is rank &f%vault_rank%";
// We parse the placeholders using "setPlaceholders"
joinText = PlaceholderAPI.setPlaceholders(event.getPlayer(), joinText);
event.setJoinMessage(withPlaceholdersSet);
}
}
© 2015 - 2024 PlaceholderAPI Team
Thanks for using PlaceholderAPI.Plugin-page | Placeholders | Plugins using PlaceholderAPI | Hook into PlaceholderAPI