Start refactoring for Messages class
This commit is contained in:
parent
2e5b7ecf29
commit
cf4e47488b
@ -7,10 +7,14 @@ import java.io.File;
|
||||
|
||||
/**
|
||||
*/
|
||||
// TODO ljacqu 20151124: This class is a weird mix between singleton and POJO
|
||||
public class Messages extends CustomConfiguration {
|
||||
|
||||
/** The section symbol, used in Minecraft for formatting codes. */
|
||||
private static final String SECTION_SIGN = "\u00a7";
|
||||
private static Messages singleton = null;
|
||||
private String lang = "en";
|
||||
private String language = "en";
|
||||
|
||||
|
||||
/**
|
||||
* Constructor for Messages.
|
||||
@ -22,7 +26,7 @@ public class Messages extends CustomConfiguration {
|
||||
super(file);
|
||||
load();
|
||||
singleton = this;
|
||||
this.lang = lang;
|
||||
this.language = lang;
|
||||
}
|
||||
|
||||
public static Messages getInstance() {
|
||||
@ -32,8 +36,14 @@ public class Messages extends CustomConfiguration {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the given message code to the player.
|
||||
*
|
||||
* @param sender The entity to send the message to
|
||||
* @param msg The message code to send
|
||||
*/
|
||||
public void send(CommandSender sender, String msg) {
|
||||
if (!Settings.messagesLanguage.equalsIgnoreCase(singleton.lang)) {
|
||||
if (!Settings.messagesLanguage.equalsIgnoreCase(singleton.language)) {
|
||||
singleton.reloadMessages();
|
||||
}
|
||||
String loc = (String) singleton.get(msg);
|
||||
@ -42,26 +52,28 @@ public class Messages extends CustomConfiguration {
|
||||
ConsoleLogger.showError("Error with the " + msg + " translation, verify in your " + getConfigFile() + " !");
|
||||
}
|
||||
for (String l : loc.split("&n")) {
|
||||
sender.sendMessage(l.replace("&", "\u00a7"));
|
||||
sender.sendMessage(l.replace("&", SECTION_SIGN));
|
||||
}
|
||||
}
|
||||
|
||||
public String[] send(MessageKey key) {
|
||||
return send(key.getKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the message from the text file and returns it split by new line as an array.
|
||||
*
|
||||
* @param msg The message code to retrieve
|
||||
*
|
||||
* @return The message split by new lines
|
||||
*/
|
||||
public String[] send(String msg) {
|
||||
if (!Settings.messagesLanguage.equalsIgnoreCase(singleton.lang)) {
|
||||
singleton.reloadMessages();
|
||||
}
|
||||
String s = (String) singleton.get(msg);
|
||||
if (s == null) {
|
||||
ConsoleLogger.showError("Error with the " + msg + " translation, verify in your " + getConfigFile() + " !");
|
||||
String[] loc = new String[1];
|
||||
loc[0] = "Error with " + msg + " translation; Please contact the admin for verify or update translation files";
|
||||
return (loc);
|
||||
}
|
||||
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] = ((String) this.get(msg)).split("&n")[a].replace("&", "\u00a7");
|
||||
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";
|
||||
@ -69,6 +81,35 @@ public class Messages extends CustomConfiguration {
|
||||
return loc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the message from the configuration file.
|
||||
*
|
||||
* @param key The key to retrieve
|
||||
*
|
||||
* @return The message
|
||||
*/
|
||||
private static String retrieveMessage(String key) {
|
||||
if (!Settings.messagesLanguage.equalsIgnoreCase(singleton.language)) {
|
||||
singleton.reloadMessages();
|
||||
}
|
||||
String message = (String) singleton.get(key);
|
||||
if (message != null) {
|
||||
return 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.";
|
||||
}
|
||||
|
||||
private static String formatChatCodes(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);
|
||||
}
|
||||
|
||||
public void reloadMessages() {
|
||||
singleton = new Messages(Settings.messageFile, Settings.messagesLanguage);
|
||||
}
|
||||
|
||||
@ -9,7 +9,10 @@ import org.junit.Test;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.hamcrest.Matchers.arrayWithSize;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link Messages}.
|
||||
@ -19,6 +22,11 @@ public class MessagesTest {
|
||||
private static final String YML_TEST_FILE = "messages_test.yml";
|
||||
private Messages messages;
|
||||
|
||||
/**
|
||||
* Loads the messages in the file {@code messages_test.yml} in the test resources folder.
|
||||
* The file does not contain all messages defined in {@link MessageKey} and its contents
|
||||
* reflect various test cases -- not what the keys stand for.
|
||||
*/
|
||||
@Before
|
||||
public void setUpMessages() {
|
||||
AuthMe authMe = AuthMeMockUtil.mockAuthMeInstance();
|
||||
@ -35,10 +43,55 @@ public class MessagesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldLoadMessages() {
|
||||
public void shouldLoadMessageAndSplitAtNewLines() {
|
||||
// given
|
||||
MessageKey key = MessageKey.UNKNOWN_USER;
|
||||
|
||||
// when
|
||||
String[] send = messages.send(MessageKey.CAPTCHA_WRONG_ERROR.getKey());
|
||||
System.out.println(asList(send));
|
||||
String[] send = messages.send(key);
|
||||
|
||||
// then
|
||||
String[] lines = new String[]{"This test message", "includes", "some new lines"};
|
||||
assertThat(send, equalTo(lines));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFormatColorCodes() {
|
||||
// given
|
||||
MessageKey key = MessageKey.UNSAFE_QUIT_LOCATION;
|
||||
|
||||
// when
|
||||
String[] message = messages.send(key);
|
||||
|
||||
// then
|
||||
assertThat(message, arrayWithSize(1));
|
||||
assertThat(message[0], equalTo("§cHere we have§bdefined some colors §dand some other §lthings"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldRetainApostrophes() {
|
||||
// given
|
||||
MessageKey key = MessageKey.NOT_LOGGED_IN;
|
||||
|
||||
// when
|
||||
String[] message = messages.send(key);
|
||||
|
||||
// then
|
||||
assertThat(message, arrayWithSize(1));
|
||||
assertThat(message[0], equalTo("Apostrophes ' should be loaded correctly, don't you think?"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnErrorForUnknownCode() {
|
||||
// given
|
||||
// The following is a key that is not defined in the test file
|
||||
MessageKey key = MessageKey.UNREGISTERED_SUCCESS;
|
||||
|
||||
// when
|
||||
String[] message = messages.send(key);
|
||||
|
||||
// then
|
||||
assertThat(message, arrayWithSize(1));
|
||||
assertThat(message[0], startsWith("Error getting message with key '"));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,58 +1,6 @@
|
||||
unknown_user: '&cCan''t find the requested user in the database!'
|
||||
unsafe_spawn: '&cYour quit location was unsafe, you have been teleported to the world''s spawnpoint.'
|
||||
not_logged_in: '&cYou''re not logged in!'
|
||||
unknown_user: 'This test message&nincludes&nsome new lines'
|
||||
unsafe_spawn: '&cHere we have&bdefined some colors &dand some other <hings'
|
||||
not_logged_in: 'Apostrophes '' should be loaded correctly, don''t you think?'
|
||||
reg_voluntarily: 'You can register yourself to the server with the command "/register <password> <ConfirmPassword>"'
|
||||
usage_log: '&cUsage: /login <password>'
|
||||
wrong_pwd: '&cWrong password!'
|
||||
unregistered: '&cSuccessfully unregistered!'
|
||||
reg_disabled: '&cIn-game registration is disabled!'
|
||||
valid_session: '&2Logged-in due to Session Reconnection.'
|
||||
login: '&2Successful login!'
|
||||
vb_nonActiv: '&cYour account isn''t activated yet, please check your emails!'
|
||||
user_regged: '&cYou already have registered this username!'
|
||||
usage_reg: '&cUsage: /register <password> <ConfirmPassword>'
|
||||
max_reg: '&cYou have exceeded the maximum number of registrations for your connection!'
|
||||
no_perm: '&4You don''t have the permission to perform this action!'
|
||||
error: '&4An unexpected error occurred, please contact an Administrator!'
|
||||
login_msg: '&cPlease, login with the command "/login <password>"'
|
||||
reg_msg: '&3Please, register to the server with the command "/register <password> <ConfirmPassword>"'
|
||||
reg_email_msg: '&3Please, register to the server with the command "/register <email> <confirmEmail>"'
|
||||
usage_unreg: '&cUsage: /unregister <password>'
|
||||
pwd_changed: '&2Password changed successfully!'
|
||||
user_unknown: '&cThis user isn''t registered!'
|
||||
password_error: '&cPasswords didn''t match, check them again!'
|
||||
password_error_nick: '&cYou can''t use your name as password, please choose another one...'
|
||||
password_error_unsafe: '&cThe chosen password isn''t safe, please choose another one...'
|
||||
invalid_session: '&cYour IP has been changed and your session data has expired!'
|
||||
reg_only: '&4Only registered users can join the server! Please visit http://example.com to register yourself!'
|
||||
logged_in: '&cYou''re already logged in!'
|
||||
logout: '&2Logged-out successfully!'
|
||||
same_nick: '&4The same username is already playing on the server!'
|
||||
registered: '&2Successfully registered!'
|
||||
pass_len: '&cYour password is too short or too long! Please try with another one!'
|
||||
reload: '&2Configuration and database have been reloaded correctly!'
|
||||
timeout: '&4Login timeout exceeded, you have been kicked from the server, please try again!'
|
||||
usage_changepassword: '&cUsage: /changepassword <oldPassword> <newPassword>'
|
||||
name_len: '&4Your username is either too short or too long!'
|
||||
regex: '&4Your username contains illegal characters. Allowed chars: REG_EX'
|
||||
add_email: '&3Please add your email to your account with the command "/email add <yourEmail> <confirmEmail>"'
|
||||
recovery_email: '&3Forgot your password? Please use the command "/email recovery <yourEmail>"'
|
||||
usage_captcha: '&3To login you have to solve a captcha code, please use the command "/captcha <theCaptcha>"'
|
||||
wrong_captcha: '&cWrong Captcha, please type "/captcha THE_CAPTCHA" into the chat!'
|
||||
valid_captcha: '&2Captcha code solved correctly!'
|
||||
kick_forvip: '&3A VIP Player has joined the server when it was full!'
|
||||
kick_fullserver: '&4The server is full, try again later!'
|
||||
usage_email_add: '&cUsage: /email add <email> <confirmEmail>'
|
||||
usage_email_change: '&cUsage: /email change <oldEmail> <newEmail>'
|
||||
usage_email_recovery: '&cUsage: /email recovery <Email>'
|
||||
new_email_invalid: '&cInvalid New Email, try again!'
|
||||
old_email_invalid: '&cInvalid Old Email, try again!'
|
||||
email_invalid: '&cInvalid Email address, try again!'
|
||||
email_added: '&2Email address successfully added to your account!'
|
||||
email_confirm: '&cPlease confirm your email address!'
|
||||
email_changed: '&2Email address changed correctly!'
|
||||
email_send: '&2Recovery email sent correctly! Check your email inbox!'
|
||||
email_exists: '&cA recovery email was already sent! You can discart it and send a new one using the command below:'
|
||||
country_banned: '&4Your country is banned from this server!'
|
||||
antibot_auto_enabled: '&4[AntiBotService] AntiBot enabled due to the huge number of connections!'
|
||||
antibot_auto_disabled: '&2[AntiBotService] AntiBot disabled disabled after %m minutes!'
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user