Abstract Messages into two layers; move to new 'output' package

- Abstract Messages into two layers: the top layer 'Messages' is how Messages can be retrieved and sent as before. In the background, package-private, MessagesManager actually does the file read and worries about I/O while Messages takes care of higher-level things (such as joining two lines or checking the current language).
This commit is contained in:
ljacqu
2015-12-01 21:45:02 +01:00
parent 2550e04112
commit 690a8d67a3
41 changed files with 213 additions and 190 deletions
@@ -1,134 +0,0 @@
package fr.xephi.authme.settings;
/**
* Keys for translatable messages managed by {@link Messages}.
*/
public enum MessageKey {
KICK_ANTIBOT("kick_antibot"),
UNKNOWN_USER("unknown_user"),
UNSAFE_QUIT_LOCATION("unsafe_spawn"),
NOT_LOGGED_IN("not_logged_in"),
REGISTER_VOLUNTARILY("reg_voluntarily"),
USAGE_LOGIN("usage_log"),
WRONG_PASSWORD("wrong_pwd"),
UNREGISTERED_SUCCESS("unregistered"),
REGISTRATION_DISABLED("reg_disabled"),
SESSION_RECONNECTION("valid_session"),
LOGIN_SUCCESS("login"),
ACCOUNT_NOT_ACTIVATED("vb_nonActiv"),
NAME_ALREADY_REGISTERED("user_regged"),
NO_PERMISSION("no_perm"),
ERROR("error"),
LOGIN_MESSAGE("login_msg"),
REGISTER_MESSAGE("reg_msg"),
REGISTER_EMAIL_MESSAGE("reg_email_msg"),
MAX_REGISTER_EXCEEDED("max_reg"),
USAGE_REGISTER("usage_reg"),
USAGE_UNREGISTER("usage_unreg"),
PASSWORD_CHANGED_SUCCESS("pwd_changed"),
USER_NOT_REGISTERED("user_unknown"),
PASSWORD_MATCH_ERROR("password_error"),
PASSWORD_IS_USERNAME_ERROR("password_error_nick"),
PASSWORD_UNSAFE_ERROR("password_error_unsafe"),
SESSION_EXPIRED("invalid_session"),
MUST_REGISTER_MESSAGE("reg_only"),
ALREADY_LOGGED_IN_ERROR("logged_in"),
LOGOUT_SUCCESS("logout"),
USERNAME_ALREADY_ONLINE_ERROR("same_nick"),
REGISTER_SUCCESS("registered"),
INVALID_PASSWORD_LENGTH("pass_len"),
CONFIG_RELOAD_SUCCESS("reload"),
LOGIN_TIMEOUT_ERROR("timeout"),
USAGE_CHANGE_PASSWORD("usage_changepassword"),
INVALID_NAME_LENGTH("name_len"),
INVALID_NAME_CHARACTERS("regex"),
ADD_EMAIL_MESSAGE("add_email"),
FORGOT_PASSWORD_MESSAGE("recovery_email"),
USAGE_CAPTCHA("usage_captcha"),
CAPTCHA_WRONG_ERROR("wrong_captcha"),
CAPTCHA_SUCCESS("valid_captcha"),
KICK_FOR_VIP("kick_forvip"),
KICK_FULL_SERVER("kick_fullserver"),
USAGE_ADD_EMAIL("usage_email_add"),
USAGE_RECOVER_EMAIL("usage_email_recovery"),
INVALID_NEW_EMAIL("new_email_invalid"),
INVALID_OLD_EMAIL("old_email_invalid"),
INVALID_EMAIL("email_invalid"),
EMAIL_ADDED_SUCCESS("email_added"),
CONFIRM_EMAIL_MESSAGE("email_confirm"),
EMAIL_CHANGED_SUCCESS("email_changed"),
RECOVERY_EMAIL_SENT_MESSAGE("email_send"),
RECOVERY_EMAIL_ALREADY_SENT_MESSAGE("email_exists"),
COUNTRY_BANNED_ERROR("country_banned"),
ANTIBOT_AUTO_ENABLED_MESSAGE("antibot_auto_enabled"),
ANTIBOT_AUTO_DISABLED_MESSAGE("antibot_auto_disabled");
private String key;
MessageKey(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
@@ -1,115 +0,0 @@
package fr.xephi.authme.settings;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.command.CommandSender;
import java.io.File;
/**
* Class for retrieving and sending translatable messages to players.
*/
// TODO ljacqu 20151124: This class is a weird mix between singleton and POJO
// TODO: change it into 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;
private String language;
/**
* Constructor for Messages.
*
* @param file the configuration file
* @param lang the code of the language to use
*/
public Messages(File file, String lang) {
super(file);
load();
this.language = lang;
}
public static Messages getInstance() {
if (singleton == null) {
singleton = new Messages(Settings.messageFile, Settings.messagesLanguage);
}
return singleton;
}
/**
* Send the given message code to the player.
*
* @param sender The entity to send the message to
* @param key The key of the message to send
*/
public void send(CommandSender sender, MessageKey key) {
String[] lines = retrieve(key);
for (String line : lines) {
sender.sendMessage(line);
}
}
/**
* Retrieve the message from the text file and return it split by new line as an array.
*
* @param key The message key to retrieve
*
* @return The message split by new lines
*/
public String[] retrieve(MessageKey key) {
return retrieve(key.getKey());
}
/**
* Retrieve the message from the text file.
*
* @param key The message key to retrieve
*
* @return The message from the file
*/
public String retrieveSingle(MessageKey key) {
return StringUtils.join("\n", retrieve(key.getKey()));
}
/**
* Retrieve the message from the configuration file.
*
* @param key The key to retrieve
*
* @return The message
*/
private String[] retrieve(String key) {
if (!Settings.messagesLanguage.equalsIgnoreCase(language)) {
reloadMessages();
}
String message = (String) get(key);
if (message != null) {
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 '"
+ getConfigFile().getName() + "'");
return new String[]{
retrievalError + "Please contact the admin to verify or update the AuthMe messages file."};
}
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 '&&'
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() {
singleton = new Messages(Settings.messageFile, Settings.messagesLanguage);
}
}