From 3abb0b5ac4067021e071a7afb11a04424c4301e1 Mon Sep 17 00:00:00 2001 From: Lucas Jacques Date: Fri, 12 Aug 2016 17:37:15 +0200 Subject: [PATCH] Messages: simplify public interface, split to array as last operation --- .../java/fr/xephi/authme/output/Messages.java | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/main/java/fr/xephi/authme/output/Messages.java b/src/main/java/fr/xephi/authme/output/Messages.java index 71430d5e..5c076f1a 100644 --- a/src/main/java/fr/xephi/authme/output/Messages.java +++ b/src/main/java/fr/xephi/authme/output/Messages.java @@ -3,7 +3,6 @@ package fr.xephi.authme.output; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.initialization.SettingsDependent; import fr.xephi.authme.settings.Settings; -import fr.xephi.authme.util.StringUtils; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; @@ -19,6 +18,9 @@ import java.io.InputStreamReader; */ public class Messages implements SettingsDependent { + // Custom Authme tag replaced to new line + private static final String NEWLINE_TAG = "%nl%"; + private FileConfiguration configuration; private String fileName; private final String defaultFile; @@ -71,18 +73,12 @@ public class Messages implements SettingsDependent { * @return The message split by new lines */ public String[] retrieve(MessageKey key) { - final String code = key.getKey(); - String message = configuration.getString(code); - - if (message == null) { - ConsoleLogger.warning("Error getting message with key '" + code + "'. " - + "Please verify your config file at '" + fileName + "'"); - return formatMessage(getDefault(code)); - } - if(message.isEmpty()) { + String message = retrieveMessage(key); + if (message.isEmpty()) { + // Return empty array instead of array with 1 empty string as entry return new String[0]; } - return formatMessage(message); + return message.split("\n"); } /** @@ -91,8 +87,16 @@ public class Messages implements SettingsDependent { * @param key The message key to retrieve * @return The message from the file */ - public String retrieveSingle(MessageKey key) { - return StringUtils.join("\n", retrieve(key)); + private String retrieveMessage(MessageKey key) { + final String code = key.getKey(); + String message = configuration.getString(code); + + if (message == null) { + ConsoleLogger.warning("Error getting message with key '" + code + "'. " + + "Please verify your config file at '" + fileName + "'"); + return formatMessage(getDefault(code)); + } + return formatMessage(message); } /** @@ -105,7 +109,7 @@ public class Messages implements SettingsDependent { * @return The message from the file with replacements */ public String retrieveSingle(MessageKey key, String... replacements) { - String message = retrieveSingle(key); + String message = retrieveMessage(key); String[] tags = key.getTags(); if (replacements.length == tags.length) { for (int i = 0; i < tags.length; ++i) { @@ -141,12 +145,9 @@ public class Messages implements SettingsDependent { return "Error retrieving message '" + code + "'"; } - private static String[] formatMessage(String message) { - String[] lines = message.split("%nl%"); - for (int i = 0; i < lines.length; ++i) { - lines[i] = ChatColor.translateAlternateColorCodes('&', lines[i]); - } - return lines; + private static String formatMessage(String message) { + return ChatColor.translateAlternateColorCodes('&', message) + .replace(NEWLINE_TAG, "\n"); } }