diff --git a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java index 16e15003a..d563f33c0 100644 --- a/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java +++ b/src/main/java/fr/xephi/authme/listener/AuthMePlayerListener.java @@ -17,7 +17,6 @@ import fr.xephi.authme.settings.Settings; import fr.xephi.authme.util.GeoLiteAPI; import fr.xephi.authme.util.Utils; import org.bukkit.Bukkit; -import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -243,8 +242,7 @@ public class AuthMePlayerListener implements Listener { String realName = auth.getRealName(); if (!realName.isEmpty() && !realName.equals("Player") && !realName.equals(event.getName())) { event.setLoginResult(AsyncPlayerPreLoginEvent.Result.KICK_OTHER); - event.setKickMessage( - plugin.getMessages().retrieveSingle(MessageKey.INVALID_NAME_CASE, realName, event.getName())); + event.setKickMessage(m.retrieveSingle(MessageKey.INVALID_NAME_CASE, realName, event.getName())); return; } if (realName.isEmpty() || realName.equals("Player")) { diff --git a/src/main/java/fr/xephi/authme/output/Messages.java b/src/main/java/fr/xephi/authme/output/Messages.java index 5afba4726..9ea87f448 100644 --- a/src/main/java/fr/xephi/authme/output/Messages.java +++ b/src/main/java/fr/xephi/authme/output/Messages.java @@ -55,17 +55,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); - 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.showError("Invalid number of replacements for message key '" + key + "'"); - send(sender, key); - } - + String message = retrieveSingle(key, replacements); for (String line : message.split("\n")) { sender.sendMessage(line); } @@ -99,6 +89,27 @@ public class Messages { return StringUtils.join("\n", retrieve(key)); } + /** + * 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 replacements The replacements to apply for the tags + */ + public String retrieveSingle(MessageKey key, String... replacements) { + String message = retrieveSingle(key); + 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.showError("Invalid number of replacements for message key '" + key + "'"); + } + return message; + } + /** * Reload the messages manager. * diff --git a/src/test/java/fr/xephi/authme/output/MessagesIntegrationTest.java b/src/test/java/fr/xephi/authme/output/MessagesIntegrationTest.java index 5ff79b9dd..399553657 100644 --- a/src/test/java/fr/xephi/authme/output/MessagesIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/output/MessagesIntegrationTest.java @@ -266,4 +266,16 @@ public class MessagesIntegrationTest { assertThat(messages.retrieveSingle(MessageKey.MUST_REGISTER_MESSAGE), equalTo("Message from default file")); } + + @Test + public void shouldRetrieveMessageWithReplacements() { + // given + MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR; + + // when + String result = messages.retrieveSingle(key, "24680"); + + // then + assertThat(result, equalTo("Use /captcha 24680 to solve the captcha")); + } }