Merge branches '642-kick-on-fast-commands' and 'master' of https://github.com/AuthMe/AuthMeReloaded into 642-kick-on-fast-commands
This commit is contained in:
@@ -5,6 +5,7 @@ import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.util.expiring.Duration;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Map;
|
||||
@@ -18,7 +19,9 @@ public class Messages {
|
||||
// Custom Authme tag replaced to new line
|
||||
private static final String NEWLINE_TAG = "%nl%";
|
||||
|
||||
private static final String PLAYER_TAG = "%username%";
|
||||
// Global tag replacements
|
||||
private static final String USERNAME_TAG = "%username%";
|
||||
private static final String DISPLAYNAME_TAG = "%displayname%";
|
||||
|
||||
/** Contains the keys of the singular messages for time units. */
|
||||
private static final Map<TimeUnit, MessageKey> TIME_UNIT_SINGULARS = ImmutableMap.<TimeUnit, MessageKey>builder()
|
||||
@@ -51,9 +54,9 @@ public class Messages {
|
||||
* @param key The key of the message to send
|
||||
*/
|
||||
public void send(CommandSender sender, MessageKey key) {
|
||||
String[] lines = retrieve(key);
|
||||
String[] lines = retrieve(key, sender);
|
||||
for (String line : lines) {
|
||||
sender.sendMessage(line.replaceAll(PLAYER_TAG, sender.getName()));
|
||||
sender.sendMessage(line);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +70,7 @@ public class Messages {
|
||||
* @param replacements The replacements to apply for the tags
|
||||
*/
|
||||
public void send(CommandSender sender, MessageKey key, String... replacements) {
|
||||
String message = retrieveSingle(key, replacements).replaceAll(PLAYER_TAG, sender.getName());
|
||||
String message = retrieveSingle(sender, key, replacements);
|
||||
for (String line : message.split("\n")) {
|
||||
sender.sendMessage(line);
|
||||
}
|
||||
@@ -77,10 +80,11 @@ public class Messages {
|
||||
* Retrieve the message from the text file and return it split by new line as an array.
|
||||
*
|
||||
* @param key The message key to retrieve
|
||||
* @param sender The entity to send the message to
|
||||
* @return The message split by new lines
|
||||
*/
|
||||
public String[] retrieve(MessageKey key) {
|
||||
String message = retrieveMessage(key);
|
||||
public String[] retrieve(MessageKey key, CommandSender sender) {
|
||||
String message = retrieveMessage(key, sender);
|
||||
if (message.isEmpty()) {
|
||||
// Return empty array instead of array with 1 empty string as entry
|
||||
return new String[0];
|
||||
@@ -101,18 +105,43 @@ public class Messages {
|
||||
? TIME_UNIT_SINGULARS.get(duration.getTimeUnit())
|
||||
: TIME_UNIT_PLURALS.get(duration.getTimeUnit());
|
||||
|
||||
return value + " " + retrieveMessage(timeUnitKey);
|
||||
return value + " " + retrieveMessage(timeUnitKey, "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the message from the text file.
|
||||
*
|
||||
* @param key The message key to retrieve
|
||||
* @param sender The entity to send the message to
|
||||
* @return The message from the file
|
||||
*/
|
||||
private String retrieveMessage(MessageKey key) {
|
||||
return formatMessage(
|
||||
messagesFileHandler.getMessage(key.getKey()));
|
||||
private String retrieveMessage(MessageKey key, CommandSender sender) {
|
||||
String message = messagesFileHandler.getMessage(key.getKey());
|
||||
String displayName = sender.getName();
|
||||
if (sender instanceof Player) {
|
||||
displayName = ((Player) sender).getDisplayName();
|
||||
}
|
||||
|
||||
return ChatColor.translateAlternateColorCodes('&', message)
|
||||
.replace(NEWLINE_TAG, "\n")
|
||||
.replace(USERNAME_TAG, sender.getName())
|
||||
.replace(DISPLAYNAME_TAG, displayName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the message from the text file.
|
||||
*
|
||||
* @param key The message key to retrieve
|
||||
* @param name The name of the entity to send the message to
|
||||
* @return The message from the file
|
||||
*/
|
||||
private String retrieveMessage(MessageKey key, String name) {
|
||||
String message = messagesFileHandler.getMessage(key.getKey());
|
||||
|
||||
return ChatColor.translateAlternateColorCodes('&', message)
|
||||
.replace(NEWLINE_TAG, "\n")
|
||||
.replace(USERNAME_TAG, name)
|
||||
.replace(DISPLAYNAME_TAG, name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -120,12 +149,13 @@ public class Messages {
|
||||
* logs an error if the number of supplied replacements doesn't correspond to the number of tags
|
||||
* the message key contains.
|
||||
*
|
||||
* @param sender The entity to send the message to
|
||||
* @param key The key of the message to send
|
||||
* @param replacements The replacements to apply for the tags
|
||||
* @return The message from the file with replacements
|
||||
*/
|
||||
public String retrieveSingle(MessageKey key, String... replacements) {
|
||||
String message = retrieveMessage(key);
|
||||
public String retrieveSingle(CommandSender sender, MessageKey key, String... replacements) {
|
||||
String message = retrieveMessage(key, sender);
|
||||
String[] tags = key.getTags();
|
||||
if (replacements.length == tags.length) {
|
||||
for (int i = 0; i < tags.length; ++i) {
|
||||
@@ -137,9 +167,26 @@ public class Messages {
|
||||
return message;
|
||||
}
|
||||
|
||||
private static String formatMessage(String message) {
|
||||
return ChatColor.translateAlternateColorCodes('&', message)
|
||||
.replace(NEWLINE_TAG, "\n");
|
||||
/**
|
||||
* Retrieve the given message code with the given tag replacements. Note that this method
|
||||
* logs an error if the number of supplied replacements doesn't correspond to the number of tags
|
||||
* the message key contains.
|
||||
*
|
||||
* @param name The name of the entity to send the message to
|
||||
* @param key The key of the message to send
|
||||
* @param replacements The replacements to apply for the tags
|
||||
* @return The message from the file with replacements
|
||||
*/
|
||||
public String retrieveSingle(String name, MessageKey key, String... replacements) {
|
||||
String message = retrieveMessage(key, name);
|
||||
String[] tags = key.getTags();
|
||||
if (replacements.length == tags.length) {
|
||||
for (int i = 0; i < tags.length; ++i) {
|
||||
message = message.replace(tags[i], replacements[i]);
|
||||
}
|
||||
} else {
|
||||
ConsoleLogger.warning("Invalid number of replacements for message key '" + key + "'");
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user