11package be .betterplugins .core .messaging .messenger ;
22
33import 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 ;
47import org .apache .commons .lang .StringUtils ;
58import org .bukkit .Bukkit ;
69import 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 );
0 commit comments