Skip to content

Commit 61d517e

Browse files
committed
Add placeholderapi support for AFK
1 parent 67c595c commit 61d517e

7 files changed

Lines changed: 119 additions & 10 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = "com.dumbdogdiner"
11-
version = "3.1.3"
11+
version = "3.1.4"
1212

1313
repositories {
1414
mavenCentral()

src/main/java/com/dumbdogdiner/stickycommands/StickyCommands.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,15 @@
1818
import com.dumbdogdiner.stickycommands.utils.Item;
1919
import com.dumbdogdiner.stickyapi.StickyAPI;
2020
import com.dumbdogdiner.stickyapi.bukkit.util.CommandUtil;
21-
import com.dumbdogdiner.stickyapi.bukkit.util.ServerUtil;
2221
import com.dumbdogdiner.stickyapi.bukkit.util.StartupUtil;
2322
import com.dumbdogdiner.stickyapi.common.translation.LocaleProvider;
24-
import com.dumbdogdiner.stickyapi.common.util.ReflectionUtil;
2523
import com.dumbdogdiner.stickyapi.common.util.TimeUtil;
2624

25+
import me.clip.placeholderapi.PlaceholderAPI;
2726
import net.luckperms.api.LuckPerms;
2827
import org.bukkit.Bukkit;
2928
import org.bukkit.Server;
3029
import org.bukkit.command.Command;
31-
import org.bukkit.command.CommandMap;
3230
import org.bukkit.entity.Player;
3331
import org.bukkit.plugin.RegisteredServiceProvider;
3432
import org.bukkit.plugin.java.JavaPlugin;
@@ -108,17 +106,25 @@ public void onLoad() {
108106
@Override
109107
public void onEnable() {
110108
if (!StartupUtil.setupConfig(this))
111-
return;
109+
return;
112110

113111
this.localeProvider = StartupUtil.setupLocale(this, this.localeProvider);
114112
if (this.localeProvider == null)
115-
return;
113+
return;
114+
// Register PAPI support if present
115+
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
116+
Bukkit.getLogger().info("Registering PlaceholderAPI placeholders");
117+
118+
StickyCommandsPlaceholder.getInstance().register();
119+
}
120+
116121

117122
if (!setupEconomy())
118123
getLogger().severe("Disabled economy commands due to no Vault dependency found!");
119124

120125
if (!setupLuckperms())
121-
getLogger().severe("Disabled group listing/luckperms dependant features due to no Luckperms dependancy found!");
126+
getLogger().severe("Disabled group listing/luckperms dependant features due to no Luckperms dependency found!");
127+
122128

123129
this.database = new Database();
124130
database.createMissingTables();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.dumbdogdiner.stickycommands;
2+
3+
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
4+
import org.bukkit.entity.Player;
5+
6+
public class StickyCommandsPlaceholder extends PlaceholderExpansion {
7+
private static StickyCommandsPlaceholder INSTANCE;
8+
private StickyCommandsPlaceholder(){
9+
10+
}
11+
12+
13+
/**
14+
* Because this is an internal class,
15+
* you must override this method to let PlaceholderAPI know to not unregister your expansion class when
16+
* PlaceholderAPI is reloaded
17+
*
18+
* @return true to persist through reloads
19+
*/
20+
@Override
21+
public boolean persist(){
22+
return true;
23+
}
24+
25+
/**
26+
* The name of the person who created this expansion should go here.
27+
* <br>For convienience do we return the author from the plugin.yml
28+
*
29+
* @return The name of the author as a String.
30+
*/
31+
@Override
32+
public String getAuthor(){
33+
return StickyCommands.getInstance().getDescription().getAuthors().toString();
34+
}
35+
36+
/**
37+
* The placeholder identifier should go here.
38+
* <br>This is what tells PlaceholderAPI to call our onRequest
39+
* method to obtain a value if a placeholder starts with our
40+
* identifier.
41+
* <br>The identifier has to be lowercase and can't contain _ or %
42+
*
43+
* @return The identifier in {@code %<identifier>_<value>%} as String.
44+
*/
45+
@Override
46+
public String getIdentifier(){
47+
return StickyCommands.getInstance().getName().toLowerCase();
48+
}
49+
50+
/**
51+
* This is the version of the expansion.
52+
* <br>You don't have to use numbers, since it is set as a String.
53+
*
54+
* For convienience do we return the version from the plugin.yml
55+
*
56+
* @return The version as a String.
57+
*/
58+
@Override
59+
public String getVersion(){
60+
return StickyCommands.getInstance().getDescription().getVersion();
61+
}
62+
63+
public static StickyCommandsPlaceholder getInstance(){
64+
if(INSTANCE == null)
65+
INSTANCE = new StickyCommandsPlaceholder();
66+
return INSTANCE;
67+
}
68+
69+
/**
70+
* This is the method called when a placeholder with our identifier
71+
* is found and needs a value.
72+
* <br>We specify the value identifier in this method.
73+
* <br>Since version 2.9.1 can you use OfflinePlayers in your requests.
74+
*
75+
* @param player
76+
* A {@link org.bukkit.entity.Player Player}.
77+
* @param identifier
78+
* A String containing the identifier/value.
79+
*
80+
* @return possibly-null String of the requested identifier.
81+
*/
82+
@Override
83+
public String onPlaceholderRequest(Player player, String identifier){
84+
// For now: ASSUME player is the player we want to know if is AFK;
85+
86+
// %stickycommands_afk%
87+
if(identifier.equals("afk")){
88+
return StickyCommands.getInstance().getOnlineUser(player.getUniqueId()).isAfk() ? "&8[AFK]" : "";
89+
}
90+
91+
92+
// We return null if an invalid placeholder was provided
93+
return null;
94+
}
95+
}

src/main/java/com/dumbdogdiner/stickycommands/User.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bukkit.entity.Player;
1111
import org.bukkit.metadata.FixedMetadataValue;
1212
import org.jetbrains.annotations.NotNull;
13+
import me.clip.placeholderapi.PlaceholderAPI;
1314

1415
import lombok.Getter;
1516
import lombok.Setter;
@@ -36,7 +37,7 @@ public class User implements Cacheable {
3637
*/
3738
@Getter
3839
private boolean afk = false;
39-
40+
4041
@Getter
4142
private Integer afkTime = 0;
4243

@@ -45,7 +46,7 @@ public boolean setAfk(boolean AFKState){
4546
afkTime = 0;
4647
afk = AFKState;
4748
if(afk){
48-
getPlayer().setMetadata("AFK", new FixedMetadataValue(StickyCommands.getInstance(), "AFK"));
49+
getPlayer().setMetadata("AFK", new FixedMetadataValue(StickyCommands.getInstance(), "&8[AFK]"));
4950
} else {
5051
getPlayer().removeMetadata("AFK", StickyCommands.getInstance());
5152
}
@@ -55,6 +56,10 @@ public boolean setAfk(boolean AFKState){
5556
public int incAfkTime(){
5657
return ++afkTime;
5758
}
59+
60+
public void resetAfkTime(){
61+
afkTime = 0;
62+
}
5863

5964
// I spent an hour trying to come up with a good solution to this weird problem where if you are being pushed by water, and on the corner water block, your from block is considered air and not water...
6065
// So, we need to keep a buffer of the last 3 blocks the player stood in, and if it contains water, we'll consider it as the water pushing them, since there's no event for

src/main/java/com/dumbdogdiner/stickycommands/commands/Afk.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public ExitCode executeCommand(CommandSender sender, String commandLabel, String
3636

3737
if (user.isAfk()) {
3838
user.setAfk(false);
39+
user.resetAfkTime();
3940
for (Player p : Bukkit.getOnlinePlayers()) {
4041
p.sendMessage(StickyCommands.getInstance().getLocaleProvider().translate("afk.not-afk", variables));
4142
}

src/main/java/com/dumbdogdiner/stickycommands/listeners/AfkEventListener.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ private void checkAfk(Player player, PlayerEvent event) {
8989
StickyCommands.getInstance().getOnlineUserCache().put(player.getUniqueId(), User.fromPlayer(player));
9090

9191
assert user != null;
92+
user.resetAfkTime();
9293
if (user.isAfk()) {
9394
variables.put("player", player.getName());
9495
// Reset their AFK time

src/main/resources/plugin.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name: StickyCommands
2-
version: 3.1.3
2+
version: 3.1.5
33
main: com.dumbdogdiner.stickycommands.StickyCommands
44
load: STARTUP
55
authors:
66
- NotZachery
77
- aakatz3
88
softdepend:
9+
- PlaceholderAPI
910
- Vault
1011
- LuckPerms
1112
api-version: 1.16

0 commit comments

Comments
 (0)