Deprecate Messages#send(String) in favor of the MessageKey enum

This commit is contained in:
ljacqu 2015-11-24 23:11:14 +01:00
parent cf4e47488b
commit cb11ae9610
2 changed files with 74 additions and 35 deletions

View File

@ -37,27 +37,36 @@ public class Messages extends CustomConfiguration {
}
/**
* Sends the given message code to the player.
* Send the given message code to the player.
*
* @param sender The entity to send the message to
* @param msg The message code to send
* @param key The key of the message to send
*/
public void send(CommandSender sender, String msg) {
if (!Settings.messagesLanguage.equalsIgnoreCase(singleton.language)) {
singleton.reloadMessages();
}
String loc = (String) singleton.get(msg);
if (loc == null) {
loc = "Error with Translation files, please contact the admin for verify or update translation";
ConsoleLogger.showError("Error with the " + msg + " translation, verify in your " + getConfigFile() + " !");
}
for (String l : loc.split("&n")) {
sender.sendMessage(l.replace("&", SECTION_SIGN));
public void send(CommandSender sender, MessageKey key) {
String[] lines = retrieve(key);
for (String line : lines) {
sender.sendMessage(line);
}
}
public String[] send(MessageKey key) {
return send(key.getKey());
/**
* Send the given message code to the player.
*
* @param sender The entity to send the message to
* @param msg The message code to send
*
* @deprecated Use {@link Messages#send(CommandSender, MessageKey)} instead
*/
@Deprecated
public void send(CommandSender sender, String msg) {
String[] lines = retrieve(msg);
for (String line : lines) {
sender.sendMessage(line);
}
}
public String[] retrieve(MessageKey key) {
return retrieve(key.getKey());
}
/**
@ -66,19 +75,11 @@ public class Messages extends CustomConfiguration {
* @param msg The message code to retrieve
*
* @return The message split by new lines
* @deprecated Use {@link Messages#retrieve(MessageKey)} instead.
*/
@Deprecated
public String[] send(String msg) {
String s = retrieveMessage(msg);
int i = s.split("&n").length;
String[] loc = new String[i];
int a;
for (a = 0; a < i; a++) {
loc[a] = s.split("&n")[a].replace("&", SECTION_SIGN);
}
if (loc.length == 0) {
loc[0] = "Error with " + msg + " translation; Please contact the admin for verify or update translation files";
}
return loc;
return retrieve(msg);
}
/**
@ -88,26 +89,32 @@ public class Messages extends CustomConfiguration {
*
* @return The message
*/
private static String retrieveMessage(String key) {
private static String[] retrieve(String key) {
if (!Settings.messagesLanguage.equalsIgnoreCase(singleton.language)) {
singleton.reloadMessages();
}
String message = (String) singleton.get(key);
if (message != null) {
return message;
return formatMessage(message);
}
// Message is null: log key not being found and send error back as message
String retrievalError = "Error getting message with key '" + key + "'. ";
ConsoleLogger.showError(retrievalError + "Please verify your config file at '"
+ singleton.getConfigFile().getName() + "'");
return retrievalError + "Please contact the admin to verify or update the AuthMe messages file.";
return new String[]{
retrievalError + "Please contact the admin to verify or update the AuthMe messages file."};
}
private static String formatChatCodes(String message) {
private static String[] formatMessage(String message) {
// TODO: Check that the codes actually exist, i.e. replace &c but not &y
// TODO: Allow '&' to be retained with the code '&&'
return message.replace("&", SECTION_SIGN);
String[] lines = message.split("&n");
for (int i = 0; i < lines.length; ++i) {
// We don't initialize a StringBuilder here because mostly we will only have one entry
lines[i] = lines[i].replace("&", SECTION_SIGN);
}
return lines;
}
public void reloadMessages() {

View File

@ -3,8 +3,10 @@ package fr.xephi.authme.settings;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.AuthMeMockUtil;
import fr.xephi.authme.ConsoleLogger;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import java.io.File;
import java.net.URL;
@ -13,6 +15,7 @@ import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
/**
* Test for {@link Messages}.
@ -48,7 +51,7 @@ public class MessagesTest {
MessageKey key = MessageKey.UNKNOWN_USER;
// when
String[] send = messages.send(key);
String[] send = messages.retrieve(key);
// then
String[] lines = new String[]{"This test message", "includes", "some new lines"};
@ -61,7 +64,7 @@ public class MessagesTest {
MessageKey key = MessageKey.UNSAFE_QUIT_LOCATION;
// when
String[] message = messages.send(key);
String[] message = messages.retrieve(key);
// then
assertThat(message, arrayWithSize(1));
@ -74,7 +77,7 @@ public class MessagesTest {
MessageKey key = MessageKey.NOT_LOGGED_IN;
// when
String[] message = messages.send(key);
String[] message = messages.retrieve(key);
// then
assertThat(message, arrayWithSize(1));
@ -88,10 +91,39 @@ public class MessagesTest {
MessageKey key = MessageKey.UNREGISTERED_SUCCESS;
// when
String[] message = messages.send(key);
String[] message = messages.retrieve(key);
// then
assertThat(message, arrayWithSize(1));
assertThat(message[0], startsWith("Error getting message with key '"));
}
@Test
public void shouldSendMessageToPlayer() {
// given
MessageKey key = MessageKey.UNSAFE_QUIT_LOCATION;
Player player = Mockito.mock(Player.class);
// when
messages.send(player, key);
// then
verify(player).sendMessage("§cHere we have§bdefined some colors §dand some other §lthings");
}
@Test
public void shouldSendMultiLineMessageToPlayer() {
// given
MessageKey key = MessageKey.UNKNOWN_USER;
Player player = Mockito.mock(Player.class);
// when
messages.send(player, key);
// then
String[] lines = new String[]{"This test message", "includes", "some new lines"};
for (String line : lines) {
verify(player).sendMessage(line);
}
}
}