Skip to content

Commit 63fe59b

Browse files
author
Dieter Nuytemans
committed
Add PAPI support
1 parent e22957d commit 63fe59b

File tree

5 files changed

+115
-27
lines changed

5 files changed

+115
-27
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@
1616

1717

1818
<repositories>
19+
<!-- Spigot -->
1920
<repository>
2021
<id>spigot-repo</id>
2122
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
2223
</repository>
24+
<!-- PAPI -->
25+
<repository>
26+
<id>placeholderapi</id>
27+
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
28+
</repository>
2329
</repositories>
2430

2531
<dependencies>
@@ -30,6 +36,13 @@
3036
<version>1.17-R0.1-SNAPSHOT</version>
3137
<scope>provided</scope>
3238
</dependency>
39+
<!-- PAPI -->
40+
<dependency>
41+
<groupId>me.clip</groupId>
42+
<artifactId>placeholderapi</artifactId>
43+
<version>2.10.9</version>
44+
<scope>provided</scope>
45+
</dependency>
3346
<!-- Mockito -->
3447
<dependency>
3548
<groupId>org.mockito</groupId>

src/main/java/be/betterplugins/core/messaging/messenger/Messenger.java

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package be.betterplugins.core.messaging.messenger;
22

33
import be.betterplugins.core.messaging.logging.BPLogger;
4+
import be.betterplugins.core.messaging.messenger.placeholderapi.IPapiUtil;
5+
import be.betterplugins.core.messaging.messenger.placeholderapi.NoPapiUtil;
6+
import be.betterplugins.core.messaging.messenger.placeholderapi.PapiUtil;
47
import org.apache.commons.lang.StringUtils;
58
import org.bukkit.Bukkit;
69
import org.bukkit.ChatColor;
@@ -19,36 +22,44 @@ public class Messenger
1922
private final String prefix;
2023
private final BPLogger logger;
2124

25+
private final IPapiUtil papiUtil;
26+
2227
/**
23-
* Creates a messenger for player output
24-
* @param messages the messages from lang.yml, mapping path to message
25-
* @param prefix the prefix for all messages
28+
* Creates a messenger for player output.
29+
*
30+
* @param messages the messages from lang.yml, mapping path to message.
31+
* @param prefix the prefix for all messages.
2632
*/
2733
public Messenger(Map<String, String> messages, BPLogger logger, String prefix)
2834
{
2935
this.messages = messages;
3036
this.logger = logger;
3137
this.prefix = prefix;
38+
39+
boolean hasPapi = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
40+
this.papiUtil = hasPapi ? new PapiUtil() : new NoPapiUtil();
3241
}
3342

3443

3544
/**
36-
* Compose a ready-to-be-sent BetterPlugin message
37-
* @param messageID the ID of the message, or a custom message
38-
* @param replacements the tag replacements for this message
39-
* @return the message ready to be sent
45+
* Compose a ready-to-be-sent BetterPlugin message.
46+
*
47+
* @param messageID the ID of the message, or a custom message.
48+
* @param replacements the tag replacements for this message.
49+
* @return the message ready to be sent.
4050
*/
4151
public String composeMessage(String messageID, MsgEntry... replacements)
4252
{
4353
return this.composeMessage(messageID, true, replacements);
4454
}
4555

4656
/**
47-
* Compose a ready-to-be-sent BetterPlugin message
48-
* @param messageID the ID of the message, or a custom message
49-
* @param includePrefix whether or not a prefix should be put in front of this message
50-
* @param replacements the tag replacements for this message
51-
* @return the message ready to be sent
57+
* Compose a ready-to-be-sent BetterPlugin message.
58+
*
59+
* @param messageID the ID of the message, or a custom message.
60+
* @param includePrefix whether or not a prefix should be put in front of this message.
61+
* @param replacements the tag replacements for this message.
62+
* @return the message ready to be sent.
5263
*/
5364
public String composeMessage(String messageID, boolean includePrefix, MsgEntry... replacements)
5465
{
@@ -118,13 +129,16 @@ else if (options.length >= 1)
118129

119130

120131
/**
121-
* Send a message from lang.yml to a CommandSender
122-
* If the message does not exist, it will be sent to the player in its raw form
123-
* As optional parameter, a list or several MsgEntries can be given as parameter
124-
* @param receiver the receiver
125-
* @param messageID the id of the message
126-
* @param replacements The strings that are to be replaced to allow using variables in messages
127-
* @return False if this message is disabled (set to "" or "ignored"), true otherwise
132+
* Send a message from lang.yml to a CommandSender.
133+
* If the message does not exist, it will be sent to the player in its raw form.
134+
* As optional parameter, a list or several MsgEntries can be given as parameter.
135+
* Will automatically replace PAPI placeholders relative to the provided <player>-tag or each receiver if no <player>-tag is provided.
136+
* To not replace placeholders, provide a MsgEntry that replaces <player> by null.
137+
*
138+
* @param receiver the receiver.
139+
* @param messageID the id of the message.
140+
* @param replacements The strings that are to be replaced to allow using variables in messages.
141+
* @return False if this message is disabled (set to "" or "ignored"), true otherwise.
128142
*/
129143
public boolean sendMessage(CommandSender receiver, String messageID, MsgEntry... replacements)
130144
{
@@ -134,13 +148,16 @@ public boolean sendMessage(CommandSender receiver, String messageID, MsgEntry...
134148

135149

136150
/**
137-
* Send a message from lang.yml to a list of players
138-
* If the message does not exist, it will be sent to the player in its raw form
139-
* As optional parameter, a list or several MsgEntries can be given as parameter
140-
* @param receivers the list of players
141-
* @param messageID the id of the message
142-
* @param replacements The strings that are to be replaced to allow using variables in messages
143-
* @return False if this message is disabled (set to "" or "ignored"), true otherwise
151+
* Send a message from lang.yml to a list of players.
152+
* If the message does not exist, it will be sent to the player in its raw form.
153+
* As optional parameter, a list or several MsgEntries can be given as parameter.
154+
* Will automatically replace PAPI placeholders relative to the provided <player>-tag or each receiver if no <player>-tag is provided.
155+
* To not replace placeholders, provide a MsgEntry that replaces <player> by null.
156+
*
157+
* @param receivers the list of players.
158+
* @param messageID the id of the message.
159+
* @param replacements The strings that are to be replaced to allow using variables in messages.
160+
* @return False if this message is disabled (set to "" or "ignored"), true otherwise.
144161
*/
145162
public boolean sendMessage(List<? extends CommandSender> receivers, String messageID, MsgEntry... replacements)
146163
{
@@ -151,23 +168,46 @@ public boolean sendMessage(List<? extends CommandSender> receivers, String messa
151168

152169
// Get the player if there is a player who did an action
153170
Player placeholderPlayer = null;
171+
boolean hasPlayerTag = false;
154172
for (MsgEntry entry : replacements)
173+
{
155174
if (entry.getTag().equals("<player>"))
175+
{
176+
hasPlayerTag = true;
156177
placeholderPlayer = Bukkit.getPlayer(entry.getReplacement());
178+
}
179+
}
157180

181+
// Replace PAPI placeholders with respect to the player that did an action
182+
if (placeholderPlayer != null)
183+
{
184+
message = papiUtil.setPlaceholders( placeholderPlayer, message );
185+
}
158186

159187
// Send everyone a message
160188
for (CommandSender receiver : receivers)
161189
{
162-
// Get the senders name
190+
// Get the senders name and replace <user> tags
163191
String name = receiver.getName();
164192
String finalMessage = message.replace("<user>", ChatColor.stripColor( name ));
193+
194+
// Use PAPI to replace messages. ONLY When no <player> tag was provided
195+
if (!hasPlayerTag && receiver instanceof Player)
196+
{
197+
Player player = (Player) receiver;
198+
finalMessage = papiUtil.setPlaceholders( player, finalMessage );
199+
}
200+
201+
// Send the message
165202
sendMessage(receiver, finalMessage);
166203
}
167204

168205
return true;
169206
}
170207

208+
/**
209+
* Used so the method of sending can be overridden
210+
*/
171211
protected void sendMessage(CommandSender receiver, String message)
172212
{
173213
receiver.sendMessage( message );
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package be.betterplugins.core.messaging.messenger.placeholderapi;
2+
3+
import org.bukkit.entity.Player;
4+
5+
public interface IPapiUtil
6+
{
7+
8+
String setPlaceholders(Player player, String message);
9+
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package be.betterplugins.core.messaging.messenger.placeholderapi;
2+
3+
import org.bukkit.entity.Player;
4+
5+
public class NoPapiUtil implements IPapiUtil
6+
{
7+
@Override
8+
public String setPlaceholders(Player player, String message)
9+
{
10+
return message;
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package be.betterplugins.core.messaging.messenger.placeholderapi;
2+
3+
import me.clip.placeholderapi.PlaceholderAPI;
4+
import org.bukkit.entity.Player;
5+
6+
public class PapiUtil implements IPapiUtil
7+
{
8+
@Override
9+
public String setPlaceholders(Player player, String message)
10+
{
11+
return PlaceholderAPI.setPlaceholders( player, message );
12+
}
13+
}

0 commit comments

Comments
 (0)