Skip to content

Commit

Permalink
feat(hooks): add placeholder api boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
darksaid98 committed Jul 19, 2024
1 parent 4a9a938 commit d78b9b7
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ repositories {

maven("https://maven.athyrium.eu/releases")

maven("https://repo.extendedclip.com/content/repositories/placeholderapi/") // PlaceholderAPI
maven("https://repo.dmulloy2.net/repository/public/") // ProtocolLib

maven("https://jitpack.io/") {
content {
includeGroup("com.github.MilkBowl") // VaultAPI
Expand Down Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation("org.bstats:bstats-bukkit:3.0.2")
compileOnly("com.github.MilkBowl:VaultAPI:1.7.1")
compileOnly("com.comphenix.protocol:ProtocolLib:5.1.0")
compileOnly("me.clip:placeholderapi:2.11.6")

// Database Dependencies
implementation("com.zaxxer:HikariCP:5.1.0")
Expand Down Expand Up @@ -179,7 +180,7 @@ bukkit { // Options: https://github.com/Minecrell/plugin-yml#bukkit
// Misc properties
load = net.minecrell.pluginyml.bukkit.BukkitPluginDescription.PluginLoadOrder.POSTWORLD // STARTUP or POSTWORLD
depend = listOf()
softDepend = listOf("Vault", "ProtocolLib")
softDepend = listOf("Vault", "ProtocolLib", "PlaceholderAPI")
}

flyway {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.exampleuser.exampleplugin.config.ConfigHandler;
import io.github.exampleuser.exampleplugin.db.DatabaseHandler;
import io.github.exampleuser.exampleplugin.hooks.BStatsHook;
import io.github.exampleuser.exampleplugin.hooks.PAPIHook;
import io.github.exampleuser.exampleplugin.hooks.ProtocolLibHook;
import io.github.exampleuser.exampleplugin.hooks.VaultHook;
import io.github.exampleuser.exampleplugin.listener.ListenerHandler;
Expand All @@ -28,6 +29,7 @@ public class ExamplePlugin extends JavaPlugin {
private static BStatsHook bStatsHook;
private static VaultHook vaultHook;
private static ProtocolLibHook protocolLibHook;
private static PAPIHook papiHook;

/**
* Gets plugin instance.
Expand All @@ -49,6 +51,7 @@ public void onLoad() {
bStatsHook = new BStatsHook(instance);
vaultHook = new VaultHook(instance);
protocolLibHook = new ProtocolLibHook(instance);
papiHook = new PAPIHook(instance);

configHandler.onLoad();
databaseHandler.onLoad();
Expand All @@ -58,6 +61,7 @@ public void onLoad() {
bStatsHook.onLoad();
vaultHook.onLoad();
protocolLibHook.onLoad();
papiHook.onLoad();
}

@Override
Expand All @@ -70,6 +74,7 @@ public void onEnable() {
bStatsHook.onEnable();
vaultHook.onEnable();
protocolLibHook.onEnable();
papiHook.onEnable();

if (vaultHook.isVaultLoaded()) {
Logger.get().info(ColorParser.of("<green>Vault has been found on this server. Vault support enabled.").build());
Expand All @@ -94,6 +99,7 @@ public void onDisable() {
bStatsHook.onDisable();
vaultHook.onDisable();
protocolLibHook.onDisable();
papiHook.onDisable();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.exampleuser.exampleplugin.hooks;

import io.github.exampleuser.exampleplugin.ExamplePlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A PlaceholderAPI expansion. Read the docs at <a href="https://wiki.placeholderapi.com/developers/creating-a-placeholderexpansion/">here</a> on how to register your custom placeholders.
*/
public class PAPIExpansion extends PlaceholderExpansion {
private final ExamplePlugin plugin;

public PAPIExpansion(ExamplePlugin plugin) {
this.plugin = plugin;
}

@Override
@SuppressWarnings("UnstableApiUsage")
public @NotNull String getIdentifier() {
return plugin.getPluginMeta().getName().replace(' ', '_').toLowerCase();
}

@Override
@SuppressWarnings("UnstableApiUsage")
public @NotNull String getAuthor() {
return String.join(", ", plugin.getPluginMeta().getAuthors());
}

@Override
@SuppressWarnings("UnstableApiUsage")
public @NotNull String getVersion() {
return plugin.getPluginMeta().getVersion();
}

@Override
public boolean persist() {
return true; // This needs to be true, or PlaceholderAPI will unregister the expansion during a plugin reload.
}

@Override
public @Nullable String onRequest(OfflinePlayer p, @NotNull String params) {
return switch (params) {
case "example" -> "placeholder text";
case "example2" -> "placeholder text2";
default -> null;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.github.exampleuser.exampleplugin.hooks;

import io.github.exampleuser.exampleplugin.ExamplePlugin;
import io.github.exampleuser.exampleplugin.Reloadable;
import org.bukkit.Bukkit;

/**
* A hook to interface with <a href="https://wiki.placeholderapi.com/">PlaceholderAPI</a>.
*/
public class PAPIHook implements Reloadable {
private final ExamplePlugin plugin;
private final static String pluginName = "PlaceholderAPI";
private PAPIExpansion PAPIExpansion;

/**
* Instantiates a new PlaceholderAPI hook.
*
* @param plugin the plugin instance
*/
public PAPIHook(ExamplePlugin plugin) {
this.plugin = plugin;
}

@Override
public void onLoad() {
}

@Override
public void onEnable() {
if (!Bukkit.getPluginManager().isPluginEnabled(pluginName))
return;

PAPIExpansion = new PAPIExpansion(plugin);
}

@Override
public void onDisable() {
PAPIExpansion.unregister();
PAPIExpansion = null;
}
}

0 comments on commit d78b9b7

Please sign in to comment.