From 731c6477a5f4d82a528c3b9b0d42d302f2c6f4ce Mon Sep 17 00:00:00 2001 From: HaHaWTH <102713261+HaHaWTH@users.noreply.github.com> Date: Sun, 14 Jul 2024 16:24:04 +0800 Subject: [PATCH] PlaceholderAPI hook --- pom.xml | 14 ++++ .../xephi/authme/listener/ServerListener.java | 5 ++ .../fr/xephi/authme/service/GeoIpService.java | 2 +- .../authme/service/PluginHookService.java | 29 ++++++++ .../service/hook/papi/AuthMeExpansion.java | 71 +++++++++++++++++++ .../settings/properties/HooksSettings.java | 4 ++ .../settings/properties/PluginSettings.java | 2 +- src/main/resources/plugin.yml | 1 + 8 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/main/java/fr/xephi/authme/service/hook/papi/AuthMeExpansion.java diff --git a/pom.xml b/pom.xml index 7fed3926..5e78cf86 100644 --- a/pom.xml +++ b/pom.xml @@ -623,6 +623,12 @@ https://s01.oss.sonatype.org/content/repositories/snapshots/ + + + placeholderapi-repo + https://repo.extendedclip.com/content/repositories/placeholderapi/ + + onarandombox-repo-releases @@ -1038,6 +1044,14 @@ + + + me.clip + placeholderapi + 2.11.6 + provided + + net.essentialsx diff --git a/src/main/java/fr/xephi/authme/listener/ServerListener.java b/src/main/java/fr/xephi/authme/listener/ServerListener.java index 97cefa04..f5d96124 100644 --- a/src/main/java/fr/xephi/authme/listener/ServerListener.java +++ b/src/main/java/fr/xephi/authme/listener/ServerListener.java @@ -53,6 +53,9 @@ public class ServerListener implements Listener { } else if ("ProtocolLib".equalsIgnoreCase(pluginName)) { protocolLibService.disable(); logger.warning("ProtocolLib has been disabled, unhooking packet adapters!"); + } else if ("PlaceholderAPI".equalsIgnoreCase(pluginName)) { + pluginHookService.unhookPlaceholderApi(); + logger.info("PlaceholderAPI has been disabled: unhooking placeholders"); } } @@ -74,6 +77,8 @@ public class ServerListener implements Listener { spawnLoader.loadCmiSpawn(); } else if ("ProtocolLib".equalsIgnoreCase(pluginName)) { protocolLibService.setup(); + } else if ("PlaceholderAPI".equalsIgnoreCase(pluginName)) { + pluginHookService.tryHookToPlaceholderApi(); } } } diff --git a/src/main/java/fr/xephi/authme/service/GeoIpService.java b/src/main/java/fr/xephi/authme/service/GeoIpService.java index bf7252ff..f18a85fa 100644 --- a/src/main/java/fr/xephi/authme/service/GeoIpService.java +++ b/src/main/java/fr/xephi/authme/service/GeoIpService.java @@ -37,7 +37,7 @@ public class GeoIpService { private volatile boolean downloading; @Inject - GeoIpService(@DataFolder File dataFolder){ + GeoIpService(@DataFolder File dataFolder) { this.dataFile = dataFolder.toPath().resolve(DATABASE_FILE); // Fires download of recent data or the initialization of the look up service diff --git a/src/main/java/fr/xephi/authme/service/PluginHookService.java b/src/main/java/fr/xephi/authme/service/PluginHookService.java index 3615aa73..df588ee9 100644 --- a/src/main/java/fr/xephi/authme/service/PluginHookService.java +++ b/src/main/java/fr/xephi/authme/service/PluginHookService.java @@ -6,6 +6,8 @@ import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.output.ConsoleLoggerFactory; +import fr.xephi.authme.service.hook.papi.AuthMeExpansion; +import me.clip.placeholderapi.PlaceholderAPIPlugin; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; @@ -26,6 +28,8 @@ public class PluginHookService { private Essentials essentials; private Plugin cmi; private MultiverseCore multiverse; + private PlaceholderAPIPlugin placeholderApi; + private AuthMeExpansion authMeExpansion; /** * Constructor. @@ -38,6 +42,7 @@ public class PluginHookService { tryHookToEssentials(); tryHookToCmi(); tryHookToMultiverse(); + tryHookToPlaceholderApi(); } /** @@ -133,6 +138,20 @@ public class PluginHookService { } } + /** + * Attempts to create a hook into PlaceholderAPI. + */ + public void tryHookToPlaceholderApi() { + try { + placeholderApi = getPlugin(pluginManager, "PlaceholderAPI", PlaceholderAPIPlugin.class); + authMeExpansion = new AuthMeExpansion(); + authMeExpansion.register(); + } catch (Exception | NoClassDefFoundError ignored) { + placeholderApi = null; + authMeExpansion = null; + } + } + /** * Attempts to create a hook into CMI. */ @@ -180,6 +199,16 @@ public class PluginHookService { multiverse = null; } + /** + * Unhooks from PlaceholderAPI. + */ + public void unhookPlaceholderApi() { + if (placeholderApi != null) { + authMeExpansion.unregister(); + placeholderApi = null; + } + } + // ------ // Helpers // ------ diff --git a/src/main/java/fr/xephi/authme/service/hook/papi/AuthMeExpansion.java b/src/main/java/fr/xephi/authme/service/hook/papi/AuthMeExpansion.java new file mode 100644 index 00000000..b4856f16 --- /dev/null +++ b/src/main/java/fr/xephi/authme/service/hook/papi/AuthMeExpansion.java @@ -0,0 +1,71 @@ +package fr.xephi.authme.service.hook.papi; + +import fr.xephi.authme.AuthMe; +import fr.xephi.authme.api.v3.AuthMeApi; +import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.properties.HooksSettings; +import me.clip.placeholderapi.expansion.PlaceholderExpansion; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +/** + * AuthMe PlaceholderAPI expansion class. + * @author Kobe 8 + */ +public class AuthMeExpansion extends PlaceholderExpansion { + private final Settings settings = AuthMe.settings; + private final AuthMeApi authMeApi = AuthMeApi.getInstance(); + @Override + public @NotNull String getIdentifier() { + return "authme"; + } + + @Override + public @NotNull String getAuthor() { + return "HaHaWTH"; + } + + @Override + public @NotNull String getVersion() { + return AuthMe.getPluginVersion(); + } + + @Override + public boolean persist() { + return true; + } + + @Override + public String onRequest(OfflinePlayer player, @NotNull String params) { + if (!settings.getProperty(HooksSettings.PLACEHOLDER_API)) return null; + if (params.equalsIgnoreCase("version")) { + return getVersion(); + } + if (params.equalsIgnoreCase("is_registered")) { + if (player != null) { + Player onlinePlayer = player.getPlayer(); + if (onlinePlayer != null) { + return String.valueOf(authMeApi.isRegistered(onlinePlayer.getName())); + } + } + } + if (params.equalsIgnoreCase("is_authenticated")) { + if (player != null) { + Player onlinePlayer = player.getPlayer(); + if (onlinePlayer != null) { + return String.valueOf(authMeApi.isAuthenticated(onlinePlayer)); + } + } + } + if (params.equalsIgnoreCase("last_login_time")) { + if (player != null) { + Player onlinePlayer = player.getPlayer(); + if (onlinePlayer != null) { + return authMeApi.getLastLoginTime(onlinePlayer.getName()).toString(); + } + } + } + return null; + } +} diff --git a/src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java b/src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java index 08c91c6a..b2949af0 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java @@ -15,6 +15,10 @@ public final class HooksSettings implements SettingsHolder { public static final Property MULTIVERSE = newProperty("Hooks.multiverse", true); + @Comment("Do we need to hook with PlaceholderAPI for AuthMe placeholders?") + public static final Property PLACEHOLDER_API = + newProperty("Hooks.placeholderapi", true); + @Comment("Do we need to hook with BungeeCord?") public static final Property BUNGEECORD = newProperty("Hooks.bungeecord", false); diff --git a/src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java b/src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java index 221496b3..03164af4 100644 --- a/src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java +++ b/src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java @@ -21,7 +21,7 @@ public final class PluginSettings implements SettingsHolder { @Comment({ "Send i18n messages to player based on their client settings, this option will override `settings.messagesLanguage`", - "(Requires Protocollib or Packetevents)", + "(Requires ProtocolLib)", "This will not affect language of AuthMe help command." }) public static final Property I18N_MESSAGES = diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 72e1ea9b..8dd7967b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -20,6 +20,7 @@ softdepend: - EssentialsSpawn - ProtocolLib - floodgate + - PlaceholderAPI commands: authme: description: AuthMe op commands