PlaceholderAPI hook

This commit is contained in:
HaHaWTH 2024-07-14 16:24:04 +08:00
parent e0bf35284c
commit 731c6477a5
8 changed files with 126 additions and 2 deletions

14
pom.xml
View File

@ -623,6 +623,12 @@
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url> <url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</repository> </repository>
<!-- Placeholder API Repo -->
<repository>
<id>placeholderapi-repo</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<!-- Multiverse Repo --> <!-- Multiverse Repo -->
<repository> <repository>
<id>onarandombox-repo-releases</id> <id>onarandombox-repo-releases</id>
@ -1038,6 +1044,14 @@
</exclusions> </exclusions>
</dependency> </dependency>
<!-- Placeholder API -->
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.6</version>
<scope>provided</scope>
</dependency>
<!-- EssentialsX plugin --> <!-- EssentialsX plugin -->
<dependency> <dependency>
<groupId>net.essentialsx</groupId> <groupId>net.essentialsx</groupId>

View File

@ -53,6 +53,9 @@ public class ServerListener implements Listener {
} else if ("ProtocolLib".equalsIgnoreCase(pluginName)) { } else if ("ProtocolLib".equalsIgnoreCase(pluginName)) {
protocolLibService.disable(); protocolLibService.disable();
logger.warning("ProtocolLib has been disabled, unhooking packet adapters!"); 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(); spawnLoader.loadCmiSpawn();
} else if ("ProtocolLib".equalsIgnoreCase(pluginName)) { } else if ("ProtocolLib".equalsIgnoreCase(pluginName)) {
protocolLibService.setup(); protocolLibService.setup();
} else if ("PlaceholderAPI".equalsIgnoreCase(pluginName)) {
pluginHookService.tryHookToPlaceholderApi();
} }
} }
} }

View File

@ -37,7 +37,7 @@ public class GeoIpService {
private volatile boolean downloading; private volatile boolean downloading;
@Inject @Inject
GeoIpService(@DataFolder File dataFolder){ GeoIpService(@DataFolder File dataFolder) {
this.dataFile = dataFolder.toPath().resolve(DATABASE_FILE); this.dataFile = dataFolder.toPath().resolve(DATABASE_FILE);
// Fires download of recent data or the initialization of the look up service // Fires download of recent data or the initialization of the look up service

View File

@ -6,6 +6,8 @@ import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MVWorldManager;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.output.ConsoleLoggerFactory; 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.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -26,6 +28,8 @@ public class PluginHookService {
private Essentials essentials; private Essentials essentials;
private Plugin cmi; private Plugin cmi;
private MultiverseCore multiverse; private MultiverseCore multiverse;
private PlaceholderAPIPlugin placeholderApi;
private AuthMeExpansion authMeExpansion;
/** /**
* Constructor. * Constructor.
@ -38,6 +42,7 @@ public class PluginHookService {
tryHookToEssentials(); tryHookToEssentials();
tryHookToCmi(); tryHookToCmi();
tryHookToMultiverse(); 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. * Attempts to create a hook into CMI.
*/ */
@ -180,6 +199,16 @@ public class PluginHookService {
multiverse = null; multiverse = null;
} }
/**
* Unhooks from PlaceholderAPI.
*/
public void unhookPlaceholderApi() {
if (placeholderApi != null) {
authMeExpansion.unregister();
placeholderApi = null;
}
}
// ------ // ------
// Helpers // Helpers
// ------ // ------

View File

@ -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;
}
}

View File

@ -15,6 +15,10 @@ public final class HooksSettings implements SettingsHolder {
public static final Property<Boolean> MULTIVERSE = public static final Property<Boolean> MULTIVERSE =
newProperty("Hooks.multiverse", true); newProperty("Hooks.multiverse", true);
@Comment("Do we need to hook with PlaceholderAPI for AuthMe placeholders?")
public static final Property<Boolean> PLACEHOLDER_API =
newProperty("Hooks.placeholderapi", true);
@Comment("Do we need to hook with BungeeCord?") @Comment("Do we need to hook with BungeeCord?")
public static final Property<Boolean> BUNGEECORD = public static final Property<Boolean> BUNGEECORD =
newProperty("Hooks.bungeecord", false); newProperty("Hooks.bungeecord", false);

View File

@ -21,7 +21,7 @@ public final class PluginSettings implements SettingsHolder {
@Comment({ @Comment({
"Send i18n messages to player based on their client settings, this option will override `settings.messagesLanguage`", "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." "This will not affect language of AuthMe help command."
}) })
public static final Property<Boolean> I18N_MESSAGES = public static final Property<Boolean> I18N_MESSAGES =

View File

@ -20,6 +20,7 @@ softdepend:
- EssentialsSpawn - EssentialsSpawn
- ProtocolLib - ProtocolLib
- floodgate - floodgate
- PlaceholderAPI
commands: commands:
authme: authme:
description: AuthMe op commands description: AuthMe op commands