Follow remarks

#1509
This commit is contained in:
Maxetto
2018-03-05 19:38:24 +01:00
parent 914c4adc0b
commit 00de15016a
18 changed files with 132 additions and 56 deletions
@@ -18,7 +18,7 @@ public class Messages {
// Custom Authme tag replaced to new line
private static final String NEWLINE_TAG = "%nl%";
private static final String PLAYER_TAG = "%username%";
private static final String USERNAME_TAG = "%username%";
/** 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 +51,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 +67,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(key, sender, replacements);
for (String line : message.split("\n")) {
sender.sendMessage(line);
}
@@ -77,10 +77,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 +102,37 @@ 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());
return ChatColor.translateAlternateColorCodes('&', message)
.replace(NEWLINE_TAG, "\n")
.replace(USERNAME_TAG, sender.getName());
}
/**
* 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);
}
/**
@@ -121,11 +141,12 @@ public class Messages {
* the message key contains.
*
* @param key The key of the message to send
* @param sender The entity to send the message to
* @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(MessageKey key, CommandSender sender, 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 +158,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 key The key of the message to send
* @param name The name of the entity to send the message to
* @param replacements The replacements to apply for the tags
* @return The message from the file with replacements
*/
public String retrieveSingle(MessageKey key, String name, 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;
}
}