Skip to content

Commit 731c647

Browse files
committed
PlaceholderAPI hook
1 parent e0bf352 commit 731c647

File tree

8 files changed

+126
-2
lines changed

8 files changed

+126
-2
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,12 @@
623623
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
624624
</repository>
625625

626+
<!-- Placeholder API Repo -->
627+
<repository>
628+
<id>placeholderapi-repo</id>
629+
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
630+
</repository>
631+
626632
<!-- Multiverse Repo -->
627633
<repository>
628634
<id>onarandombox-repo-releases</id>
@@ -1038,6 +1044,14 @@
10381044
</exclusions>
10391045
</dependency>
10401046

1047+
<!-- Placeholder API -->
1048+
<dependency>
1049+
<groupId>me.clip</groupId>
1050+
<artifactId>placeholderapi</artifactId>
1051+
<version>2.11.6</version>
1052+
<scope>provided</scope>
1053+
</dependency>
1054+
10411055
<!-- EssentialsX plugin -->
10421056
<dependency>
10431057
<groupId>net.essentialsx</groupId>

src/main/java/fr/xephi/authme/listener/ServerListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ public void onPluginDisable(PluginDisableEvent event) {
5353
} else if ("ProtocolLib".equalsIgnoreCase(pluginName)) {
5454
protocolLibService.disable();
5555
logger.warning("ProtocolLib has been disabled, unhooking packet adapters!");
56+
} else if ("PlaceholderAPI".equalsIgnoreCase(pluginName)) {
57+
pluginHookService.unhookPlaceholderApi();
58+
logger.info("PlaceholderAPI has been disabled: unhooking placeholders");
5659
}
5760
}
5861

@@ -74,6 +77,8 @@ public void onPluginEnable(PluginEnableEvent event) {
7477
spawnLoader.loadCmiSpawn();
7578
} else if ("ProtocolLib".equalsIgnoreCase(pluginName)) {
7679
protocolLibService.setup();
80+
} else if ("PlaceholderAPI".equalsIgnoreCase(pluginName)) {
81+
pluginHookService.tryHookToPlaceholderApi();
7782
}
7883
}
7984
}

src/main/java/fr/xephi/authme/service/GeoIpService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GeoIpService {
3737
private volatile boolean downloading;
3838

3939
@Inject
40-
GeoIpService(@DataFolder File dataFolder){
40+
GeoIpService(@DataFolder File dataFolder) {
4141
this.dataFile = dataFolder.toPath().resolve(DATABASE_FILE);
4242

4343
// Fires download of recent data or the initialization of the look up service

src/main/java/fr/xephi/authme/service/PluginHookService.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.onarandombox.MultiverseCore.api.MVWorldManager;
77
import fr.xephi.authme.ConsoleLogger;
88
import fr.xephi.authme.output.ConsoleLoggerFactory;
9+
import fr.xephi.authme.service.hook.papi.AuthMeExpansion;
10+
import me.clip.placeholderapi.PlaceholderAPIPlugin;
911
import org.bukkit.Location;
1012
import org.bukkit.World;
1113
import org.bukkit.entity.Player;
@@ -26,6 +28,8 @@ public class PluginHookService {
2628
private Essentials essentials;
2729
private Plugin cmi;
2830
private MultiverseCore multiverse;
31+
private PlaceholderAPIPlugin placeholderApi;
32+
private AuthMeExpansion authMeExpansion;
2933

3034
/**
3135
* Constructor.
@@ -38,6 +42,7 @@ public PluginHookService(PluginManager pluginManager) {
3842
tryHookToEssentials();
3943
tryHookToCmi();
4044
tryHookToMultiverse();
45+
tryHookToPlaceholderApi();
4146
}
4247

4348
/**
@@ -133,6 +138,20 @@ public void tryHookToEssentials() {
133138
}
134139
}
135140

141+
/**
142+
* Attempts to create a hook into PlaceholderAPI.
143+
*/
144+
public void tryHookToPlaceholderApi() {
145+
try {
146+
placeholderApi = getPlugin(pluginManager, "PlaceholderAPI", PlaceholderAPIPlugin.class);
147+
authMeExpansion = new AuthMeExpansion();
148+
authMeExpansion.register();
149+
} catch (Exception | NoClassDefFoundError ignored) {
150+
placeholderApi = null;
151+
authMeExpansion = null;
152+
}
153+
}
154+
136155
/**
137156
* Attempts to create a hook into CMI.
138157
*/
@@ -180,6 +199,16 @@ public void unhookMultiverse() {
180199
multiverse = null;
181200
}
182201

202+
/**
203+
* Unhooks from PlaceholderAPI.
204+
*/
205+
public void unhookPlaceholderApi() {
206+
if (placeholderApi != null) {
207+
authMeExpansion.unregister();
208+
placeholderApi = null;
209+
}
210+
}
211+
183212
// ------
184213
// Helpers
185214
// ------
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package fr.xephi.authme.service.hook.papi;
2+
3+
import fr.xephi.authme.AuthMe;
4+
import fr.xephi.authme.api.v3.AuthMeApi;
5+
import fr.xephi.authme.settings.Settings;
6+
import fr.xephi.authme.settings.properties.HooksSettings;
7+
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
8+
import org.bukkit.OfflinePlayer;
9+
import org.bukkit.entity.Player;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
/**
13+
* AuthMe PlaceholderAPI expansion class.
14+
* @author Kobe 8
15+
*/
16+
public class AuthMeExpansion extends PlaceholderExpansion {
17+
private final Settings settings = AuthMe.settings;
18+
private final AuthMeApi authMeApi = AuthMeApi.getInstance();
19+
@Override
20+
public @NotNull String getIdentifier() {
21+
return "authme";
22+
}
23+
24+
@Override
25+
public @NotNull String getAuthor() {
26+
return "HaHaWTH";
27+
}
28+
29+
@Override
30+
public @NotNull String getVersion() {
31+
return AuthMe.getPluginVersion();
32+
}
33+
34+
@Override
35+
public boolean persist() {
36+
return true;
37+
}
38+
39+
@Override
40+
public String onRequest(OfflinePlayer player, @NotNull String params) {
41+
if (!settings.getProperty(HooksSettings.PLACEHOLDER_API)) return null;
42+
if (params.equalsIgnoreCase("version")) {
43+
return getVersion();
44+
}
45+
if (params.equalsIgnoreCase("is_registered")) {
46+
if (player != null) {
47+
Player onlinePlayer = player.getPlayer();
48+
if (onlinePlayer != null) {
49+
return String.valueOf(authMeApi.isRegistered(onlinePlayer.getName()));
50+
}
51+
}
52+
}
53+
if (params.equalsIgnoreCase("is_authenticated")) {
54+
if (player != null) {
55+
Player onlinePlayer = player.getPlayer();
56+
if (onlinePlayer != null) {
57+
return String.valueOf(authMeApi.isAuthenticated(onlinePlayer));
58+
}
59+
}
60+
}
61+
if (params.equalsIgnoreCase("last_login_time")) {
62+
if (player != null) {
63+
Player onlinePlayer = player.getPlayer();
64+
if (onlinePlayer != null) {
65+
return authMeApi.getLastLoginTime(onlinePlayer.getName()).toString();
66+
}
67+
}
68+
}
69+
return null;
70+
}
71+
}

src/main/java/fr/xephi/authme/settings/properties/HooksSettings.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public final class HooksSettings implements SettingsHolder {
1515
public static final Property<Boolean> MULTIVERSE =
1616
newProperty("Hooks.multiverse", true);
1717

18+
@Comment("Do we need to hook with PlaceholderAPI for AuthMe placeholders?")
19+
public static final Property<Boolean> PLACEHOLDER_API =
20+
newProperty("Hooks.placeholderapi", true);
21+
1822
@Comment("Do we need to hook with BungeeCord?")
1923
public static final Property<Boolean> BUNGEECORD =
2024
newProperty("Hooks.bungeecord", false);

src/main/java/fr/xephi/authme/settings/properties/PluginSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public final class PluginSettings implements SettingsHolder {
2121

2222
@Comment({
2323
"Send i18n messages to player based on their client settings, this option will override `settings.messagesLanguage`",
24-
"(Requires Protocollib or Packetevents)",
24+
"(Requires ProtocolLib)",
2525
"This will not affect language of AuthMe help command."
2626
})
2727
public static final Property<Boolean> I18N_MESSAGES =

src/main/resources/plugin.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ softdepend:
2020
- EssentialsSpawn
2121
- ProtocolLib
2222
- floodgate
23+
- PlaceholderAPI
2324
commands:
2425
authme:
2526
description: AuthMe op commands

0 commit comments

Comments
 (0)