diff --git a/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java b/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java index 696773537..be07e33ae 100644 --- a/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/captcha/CaptchaCommand.java @@ -58,9 +58,7 @@ public class CaptchaCommand extends ExecutableCommand { plugin.cap.remove(playerNameLowerCase); String randStr = new RandomString(Settings.captchaLength).nextString(); plugin.cap.put(playerNameLowerCase, randStr); - for (String s : m.retrieve(MessageKey.CAPTCHA_WRONG_ERROR)) { - player.sendMessage(s.replace("THE_CAPTCHA", plugin.cap.get(playerNameLowerCase))); - } + m.send(player, MessageKey.CAPTCHA_WRONG_ERROR, plugin.cap.get(playerNameLowerCase)); return true; } diff --git a/src/main/java/fr/xephi/authme/output/Messages.java b/src/main/java/fr/xephi/authme/output/Messages.java index ad586fd12..e27e90618 100644 --- a/src/main/java/fr/xephi/authme/output/Messages.java +++ b/src/main/java/fr/xephi/authme/output/Messages.java @@ -47,6 +47,31 @@ public class Messages { } } + /** + * Send the given message code to the player with the given tag replacements. Note that this method + * issues an exception if the number of supplied replacements doesn't correspond to the number of tags + * the message key contains. + * + * @param sender The entity to send the message to + * @param key The key of the message to send + * @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) { + throw new RuntimeException("Given replacement size does not match the tags in message key '" + key + "'"); + } + + for (int i = 0; i < tags.length; ++i) { + message = message.replace(tags[i], replacements[i]); + } + + for (String line : message.split("\n")) { + sender.sendMessage(line); + } + } + /** * Retrieve the message from the text file and return it split by new line as an array. * diff --git a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java index 025defb21..b588361c8 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -57,20 +57,10 @@ public class AsynchronousLogin { this.database = data; } - /** - * Method getIP. - * - * @return String - */ protected String getIP() { return plugin.getIP(player); } - /** - * Method needsCaptcha. - * - * @return boolean - */ protected boolean needsCaptcha() { if (Settings.useCaptcha) { if (!plugin.captcha.containsKey(name)) { @@ -82,9 +72,7 @@ public class AsynchronousLogin { } if (plugin.captcha.containsKey(name) && plugin.captcha.get(name) > Settings.maxLoginTry) { plugin.cap.putIfAbsent(name, rdm.nextString()); - for (String s : m.retrieve(MessageKey.USAGE_CAPTCHA)) { - player.sendMessage(s.replace("THE_CAPTCHA", plugin.cap.get(name)).replace("", plugin.cap.get(name))); - } + m.send(player, MessageKey.USAGE_CAPTCHA, plugin.cap.get(name)); return true; } } @@ -230,12 +218,6 @@ public class AsynchronousLogin { } } - /** - * Method displayOtherAccounts. - * - * @param auth PlayerAuth - * @param p Player - */ public void displayOtherAccounts(PlayerAuth auth, Player p) { if (!Settings.displayOtherAccounts) { return; diff --git a/src/main/resources/messages/messages_cz.yml b/src/main/resources/messages/messages_cz.yml index 591e4e757..4108ea5a6 100644 --- a/src/main/resources/messages/messages_cz.yml +++ b/src/main/resources/messages/messages_cz.yml @@ -38,7 +38,7 @@ regex: '&cTvuj nick obsahuje nepovolene znaky. Pripustne znaky jsou: REG_EX' add_email: '&cPridej prosim svuj email pomoci : /email add TvujEmail TvujEmail' recovery_email: '&cZapomel jsi heslo? Zadej: /email recovery ' usage_captcha: '&cPouzij: /captcha ' -wrong_captcha: '&cSpatne opsana Captcha, pouzij prosim: /captcha CAPTCHA_TEXT' +wrong_captcha: '&cSpatne opsana Captcha, pouzij prosim: /captcha THE_CAPTCHA' valid_captcha: '&cZadana captcha je v poradku!' kick_forvip: '&cVIP Hrac se pripojil na plny server!' kick_fullserver: '&cServer je plne obsazen, zkus to pozdeji prosim!' diff --git a/src/main/resources/messages/messages_es.yml b/src/main/resources/messages/messages_es.yml index 850d750a6..a70663309 100644 --- a/src/main/resources/messages/messages_es.yml +++ b/src/main/resources/messages/messages_es.yml @@ -39,7 +39,7 @@ regex: '&cTu usuario tiene carácteres no admitidos, los cuales son: REG_EX' add_email: '&cPor favor agrega tu e-mail con: /email add tuEmail confirmarEmail' recovery_email: '&c¿Olvidaste tu contraseña? Por favor usa /email recovery ' usage_captcha: '&cUso: /captcha ' -wrong_captcha: '&cCaptcha incorrecto, please use : /captcha EL_CAPTCHA' +wrong_captcha: '&cCaptcha incorrecto, please use : /captcha THE_CAPTCHA' valid_captcha: '&c¡ Captcha ingresado correctamente !' kick_forvip: '&cUn jugador VIP ha ingresado al servidor lleno!' kick_fullserver: '&cEl servidor está lleno, lo sentimos!' diff --git a/src/test/java/fr/xephi/authme/output/MessagesIntegrationTest.java b/src/test/java/fr/xephi/authme/output/MessagesIntegrationTest.java index aa7367e2b..343a56871 100644 --- a/src/test/java/fr/xephi/authme/output/MessagesIntegrationTest.java +++ b/src/test/java/fr/xephi/authme/output/MessagesIntegrationTest.java @@ -2,9 +2,11 @@ package fr.xephi.authme.output; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.util.WrapperMock; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mockito; import java.io.File; @@ -14,6 +16,8 @@ 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.mock; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; /** @@ -137,4 +141,58 @@ public class MessagesIntegrationTest { verify(player).sendMessage(line); } } + + @Test + public void shouldSendMessageToPlayerWithTagReplacement() { + // given + MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR; + CommandSender sender = Mockito.mock(CommandSender.class); + + // when + messages.send(sender, key, "1234"); + + // then + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(sender, times(1)).sendMessage(captor.capture()); + String message = captor.getValue(); + assertThat(message, equalTo("Use /captcha 1234 to solve the captcha")); + } + + @Test + public void shouldNotThrowForKeyWithNoTagReplacements() { + // given + MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR; + CommandSender sender = mock(CommandSender.class); + + // when + messages.send(sender, key); + + // then + ArgumentCaptor captor = ArgumentCaptor.forClass(String.class); + verify(sender, times(1)).sendMessage(captor.capture()); + String message = captor.getValue(); + assertThat(message, equalTo("Use /captcha THE_CAPTCHA to solve the captcha")); + } + + @Test(expected = RuntimeException.class) + public void shouldThrowForInvalidReplacementCount() { + // given + MessageKey key = MessageKey.CAPTCHA_WRONG_ERROR; + + // when + messages.send(mock(CommandSender.class), key, "rep", "rep2"); + + // then - expect exception + } + + @Test(expected = RuntimeException.class) + public void shouldThrowForReplacementsOnKeyWithNoTags() { + // given + MessageKey key = MessageKey.UNKNOWN_USER; + + // when + messages.send(mock(CommandSender.class), key, "Replacement"); + + // then - expect exception + } } diff --git a/src/test/resources/messages_test.yml b/src/test/resources/messages_test.yml index 23dd12bdb..73aea93e2 100644 --- a/src/test/resources/messages_test.yml +++ b/src/test/resources/messages_test.yml @@ -4,3 +4,4 @@ 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 "' usage_log: '&cUsage: /login ' wrong_pwd: '&cWrong password!' +wrong_captcha: 'Use /captcha THE_CAPTCHA to solve the captcha'