From d78b9b7bff6cc3e4e00167fc5b4a65e3c99bf9c4 Mon Sep 17 00:00:00 2001 From: darksaid98 Date: Fri, 19 Jul 2024 02:48:09 +0200 Subject: [PATCH] feat(hooks): add placeholder api boilerplate --- build.gradle.kts | 5 +- .../exampleplugin/ExamplePlugin.java | 6 +++ .../exampleplugin/hooks/PAPIExpansion.java | 50 +++++++++++++++++++ .../exampleplugin/hooks/PAPIHook.java | 41 +++++++++++++++ 4 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/exampleuser/exampleplugin/hooks/PAPIExpansion.java create mode 100644 src/main/java/io/github/exampleuser/exampleplugin/hooks/PAPIHook.java diff --git a/build.gradle.kts b/build.gradle.kts index 1bba6e6..25b3f06 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 @@ -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") @@ -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 { diff --git a/src/main/java/io/github/exampleuser/exampleplugin/ExamplePlugin.java b/src/main/java/io/github/exampleuser/exampleplugin/ExamplePlugin.java index 83e019e..d472fe0 100644 --- a/src/main/java/io/github/exampleuser/exampleplugin/ExamplePlugin.java +++ b/src/main/java/io/github/exampleuser/exampleplugin/ExamplePlugin.java @@ -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; @@ -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. @@ -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(); @@ -58,6 +61,7 @@ public void onLoad() { bStatsHook.onLoad(); vaultHook.onLoad(); protocolLibHook.onLoad(); + papiHook.onLoad(); } @Override @@ -70,6 +74,7 @@ public void onEnable() { bStatsHook.onEnable(); vaultHook.onEnable(); protocolLibHook.onEnable(); + papiHook.onEnable(); if (vaultHook.isVaultLoaded()) { Logger.get().info(ColorParser.of("Vault has been found on this server. Vault support enabled.").build()); @@ -94,6 +99,7 @@ public void onDisable() { bStatsHook.onDisable(); vaultHook.onDisable(); protocolLibHook.onDisable(); + papiHook.onDisable(); } /** diff --git a/src/main/java/io/github/exampleuser/exampleplugin/hooks/PAPIExpansion.java b/src/main/java/io/github/exampleuser/exampleplugin/hooks/PAPIExpansion.java new file mode 100644 index 0000000..3f0a02d --- /dev/null +++ b/src/main/java/io/github/exampleuser/exampleplugin/hooks/PAPIExpansion.java @@ -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 here 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; + }; + } +} diff --git a/src/main/java/io/github/exampleuser/exampleplugin/hooks/PAPIHook.java b/src/main/java/io/github/exampleuser/exampleplugin/hooks/PAPIHook.java new file mode 100644 index 0000000..78ba6d8 --- /dev/null +++ b/src/main/java/io/github/exampleuser/exampleplugin/hooks/PAPIHook.java @@ -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 PlaceholderAPI. + */ +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; + } +}