Skip to content

Commit

Permalink
PlaceholderAPI hook
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Jul 14, 2024
1 parent e0bf352 commit 731c647
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 2 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,12 @@
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</repository>

<!-- Placeholder API Repo -->
<repository>
<id>placeholderapi-repo</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>

<!-- Multiverse Repo -->
<repository>
<id>onarandombox-repo-releases</id>
Expand Down Expand Up @@ -1038,6 +1044,14 @@
</exclusions>
</dependency>

<!-- Placeholder API -->
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.6</version>
<scope>provided</scope>
</dependency>

<!-- EssentialsX plugin -->
<dependency>
<groupId>net.essentialsx</groupId>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/fr/xephi/authme/listener/ServerListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public void onPluginDisable(PluginDisableEvent event) {
} 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");
}
}

Expand All @@ -74,6 +77,8 @@ public void onPluginEnable(PluginEnableEvent event) {
spawnLoader.loadCmiSpawn();
} else if ("ProtocolLib".equalsIgnoreCase(pluginName)) {
protocolLibService.setup();
} else if ("PlaceholderAPI".equalsIgnoreCase(pluginName)) {
pluginHookService.tryHookToPlaceholderApi();
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/fr/xephi/authme/service/GeoIpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/fr/xephi/authme/service/PluginHookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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;
Expand All @@ -26,6 +28,8 @@ public class PluginHookService {
private Essentials essentials;
private Plugin cmi;
private MultiverseCore multiverse;
private PlaceholderAPIPlugin placeholderApi;
private AuthMeExpansion authMeExpansion;

/**
* Constructor.
Expand All @@ -38,6 +42,7 @@ public PluginHookService(PluginManager pluginManager) {
tryHookToEssentials();
tryHookToCmi();
tryHookToMultiverse();
tryHookToPlaceholderApi();
}

/**
Expand Down Expand Up @@ -133,6 +138,20 @@ public void tryHookToEssentials() {
}
}

/**
* 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.
*/
Expand Down Expand Up @@ -180,6 +199,16 @@ public void unhookMultiverse() {
multiverse = null;
}

/**
* Unhooks from PlaceholderAPI.
*/
public void unhookPlaceholderApi() {
if (placeholderApi != null) {
authMeExpansion.unregister();
placeholderApi = null;
}
}

// ------
// Helpers
// ------
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public final class HooksSettings implements SettingsHolder {
public static final Property<Boolean> MULTIVERSE =
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?")
public static final Property<Boolean> BUNGEECORD =
newProperty("Hooks.bungeecord", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> I18N_MESSAGES =
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ softdepend:
- EssentialsSpawn
- ProtocolLib
- floodgate
- PlaceholderAPI
commands:
authme:
description: AuthMe op commands
Expand Down

0 comments on commit 731c647

Please sign in to comment.