Mute the message task when the player has to solve a captcha

This commit is contained in:
Gabriele C 2016-10-23 15:14:16 +02:00
parent f8745876de
commit 1b60b07f60
6 changed files with 42 additions and 23 deletions

View File

@ -4,6 +4,7 @@ import fr.xephi.authme.data.CaptchaManager;
import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.command.CommandService; import fr.xephi.authme.command.CommandService;
import fr.xephi.authme.command.PlayerCommand; import fr.xephi.authme.command.PlayerCommand;
import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.MessageKey;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -21,6 +22,9 @@ public class CaptchaCommand extends PlayerCommand {
@Inject @Inject
private CommandService commandService; private CommandService commandService;
@Inject
private LimboCache limboCache;
@Override @Override
public void runCommand(Player player, List<String> arguments) { public void runCommand(Player player, List<String> arguments) {
final String playerName = player.getName().toLowerCase(); final String playerName = player.getName().toLowerCase();
@ -39,6 +43,7 @@ public class CaptchaCommand extends PlayerCommand {
if (isCorrectCode) { if (isCorrectCode) {
commandService.send(player, MessageKey.CAPTCHA_SUCCESS); commandService.send(player, MessageKey.CAPTCHA_SUCCESS);
commandService.send(player, MessageKey.LOGIN_MESSAGE); commandService.send(player, MessageKey.LOGIN_MESSAGE);
limboCache.getPlayerData(player.getName()).getMessageTask().setMuted(false);
} else { } else {
String newCode = captchaManager.generateCode(player.getName()); String newCode = captchaManager.generateCode(player.getName());
commandService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode); commandService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode);

View File

@ -1,5 +1,6 @@
package fr.xephi.authme.data.limbo; package fr.xephi.authme.data.limbo;
import fr.xephi.authme.task.MessageTask;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
@ -16,7 +17,7 @@ public class LimboPlayer {
private final float walkSpeed; private final float walkSpeed;
private final float flySpeed; private final float flySpeed;
private BukkitTask timeoutTask = null; private BukkitTask timeoutTask = null;
private BukkitTask messageTask = null; private MessageTask messageTask = null;
public LimboPlayer(Location loc, boolean operator, String group, boolean fly, float walkSpeed, float flySpeed) { public LimboPlayer(Location loc, boolean operator, String group, boolean fly, float walkSpeed, float flySpeed) {
this.loc = loc; this.loc = loc;
@ -94,7 +95,7 @@ public class LimboPlayer {
* *
* @return The task responsible for sending the message regularly * @return The task responsible for sending the message regularly
*/ */
public BukkitTask getMessageTask() { public MessageTask getMessageTask() {
return messageTask; return messageTask;
} }
@ -103,7 +104,7 @@ public class LimboPlayer {
* *
* @param messageTask The message task to set * @param messageTask The message task to set
*/ */
public void setMessageTask(BukkitTask messageTask) { public void setMessageTask(MessageTask messageTask) {
if (this.messageTask != null) { if (this.messageTask != null) {
this.messageTask.cancel(); this.messageTask.cancel();
} }

View File

@ -195,6 +195,7 @@ public class AsynchronousLogin implements AsynchronousProcess {
// If the authentication fails check if Captcha is required and send a message to the player // If the authentication fails check if Captcha is required and send a message to the player
if (captchaManager.isCaptchaRequired(player.getName())) { if (captchaManager.isCaptchaRequired(player.getName())) {
limboCache.getPlayerData(player.getName()).getMessageTask().setMuted(true);
service.send(player, MessageKey.USAGE_CAPTCHA, service.send(player, MessageKey.USAGE_CAPTCHA,
captchaManager.getCaptchaCodeOrGenerateNew(player.getName())); captchaManager.getCaptchaCodeOrGenerateNew(player.getName()));
} }

View File

@ -11,6 +11,7 @@ import fr.xephi.authme.settings.properties.RegistrationSettings;
import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.RestrictionSettings;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import javax.inject.Inject; import javax.inject.Inject;
@ -57,8 +58,8 @@ public class LimboPlayerTaskManager {
ConsoleLogger.info("PlayerData for '" + name + "' is not available"); ConsoleLogger.info("PlayerData for '" + name + "' is not available");
} else { } else {
cancelTask(limboPlayer.getMessageTask()); cancelTask(limboPlayer.getMessageTask());
BukkitTask messageTask = bukkitService.runTask(new MessageTask(name, messages.retrieve(key), MessageTask messageTask = new MessageTask(name, messages.retrieve(key), bukkitService, playerCache);
interval, bukkitService, limboCache, playerCache)); bukkitService.runTaskTimer(messageTask, 2 * TICKS_PER_SECOND, interval * TICKS_PER_SECOND);
limboPlayer.setMessageTask(messageTask); limboPlayer.setMessageTask(messageTask);
} }
} }
@ -110,4 +111,15 @@ public class LimboPlayerTaskManager {
task.cancel(); task.cancel();
} }
} }
/**
* Null-safe method to cancel a potentially existing task.
*
* @param task the task to cancel (or null)
*/
private static void cancelTask(BukkitRunnable task) {
if (task != null) {
task.cancel();
}
}
} }

View File

@ -4,6 +4,7 @@ import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.data.limbo.LimboCache; import fr.xephi.authme.data.limbo.LimboCache;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask; import org.bukkit.scheduler.BukkitTask;
import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND; import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND;
@ -11,31 +12,36 @@ import static fr.xephi.authme.service.BukkitService.TICKS_PER_SECOND;
/** /**
* Message shown to a player in a regular interval as long as he is not logged in. * Message shown to a player in a regular interval as long as he is not logged in.
*/ */
public class MessageTask implements Runnable { public class MessageTask extends BukkitRunnable {
private final String name; private final String name;
private final String[] message; private final String[] message;
private final int interval;
private final BukkitService bukkitService; private final BukkitService bukkitService;
private final LimboCache limboCache;
private final PlayerCache playerCache; private final PlayerCache playerCache;
private boolean isMuted;
/* /*
* Constructor. * Constructor.
*/ */
public MessageTask(String name, String[] lines, int interval, BukkitService bukkitService, public MessageTask(String name, String[] lines, BukkitService bukkitService, PlayerCache playerCache) {
LimboCache limboCache, PlayerCache playerCache) {
this.name = name; this.name = name;
this.message = lines; this.message = lines;
this.interval = interval;
this.bukkitService = bukkitService; this.bukkitService = bukkitService;
this.limboCache = limboCache;
this.playerCache = playerCache; this.playerCache = playerCache;
isMuted = false;
}
public void setMuted(boolean isMuted) {
this.isMuted = isMuted;
} }
@Override @Override
public void run() { public void run() {
if (playerCache.isAuthenticated(name)) { if (playerCache.isAuthenticated(name)) {
cancel();
}
if(isMuted) {
return; return;
} }
@ -44,11 +50,7 @@ public class MessageTask implements Runnable {
for (String ms : message) { for (String ms : message) {
player.sendMessage(ms); player.sendMessage(ms);
} }
BukkitTask nextTask = bukkitService.runTaskLater(this, interval * TICKS_PER_SECOND); break;
if (limboCache.hasPlayerData(name)) {
limboCache.getPlayerData(name).setMessageTask(nextTask);
}
return;
} }
} }
} }

View File

@ -64,8 +64,6 @@ public class LimboPlayerTaskManagerTest {
given(limboCache.getPlayerData(name)).willReturn(limboPlayer); given(limboCache.getPlayerData(name)).willReturn(limboPlayer);
MessageKey key = MessageKey.REGISTER_EMAIL_MESSAGE; MessageKey key = MessageKey.REGISTER_EMAIL_MESSAGE;
given(messages.retrieve(key)).willReturn(new String[]{"Please register!"}); given(messages.retrieve(key)).willReturn(new String[]{"Please register!"});
BukkitTask bukkiTask = mock(BukkitTask.class);
given(bukkitService.runTask(any(MessageTask.class))).willReturn(bukkiTask);
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(12); given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(12);
given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true); given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
@ -73,7 +71,8 @@ public class LimboPlayerTaskManagerTest {
limboPlayerTaskManager.registerMessageTask(name, false); limboPlayerTaskManager.registerMessageTask(name, false);
// then // then
verify(limboPlayer).setMessageTask(bukkiTask); MessageTask bukkitTask = mock(MessageTask.class);
verify(limboPlayer).setMessageTask(bukkitTask);
verify(messages).retrieve(key); verify(messages).retrieve(key);
} }
@ -114,7 +113,7 @@ public class LimboPlayerTaskManagerTest {
public void shouldCancelExistingMessageTask() { public void shouldCancelExistingMessageTask() {
// given // given
LimboPlayer limboPlayer = mock(LimboPlayer.class); LimboPlayer limboPlayer = mock(LimboPlayer.class);
BukkitTask existingMessageTask = mock(BukkitTask.class); MessageTask existingMessageTask = mock(MessageTask.class);
given(limboPlayer.getMessageTask()).willReturn(existingMessageTask); given(limboPlayer.getMessageTask()).willReturn(existingMessageTask);
String name = "bobby"; String name = "bobby";
@ -122,8 +121,7 @@ public class LimboPlayerTaskManagerTest {
given(messages.retrieve(MessageKey.REGISTER_EMAIL_MESSAGE)) given(messages.retrieve(MessageKey.REGISTER_EMAIL_MESSAGE))
.willReturn(new String[]{"Please register", "Use /register"}); .willReturn(new String[]{"Please register", "Use /register"});
BukkitTask bukkiTask = mock(BukkitTask.class); MessageTask bukkiTask = mock(MessageTask.class);
given(bukkitService.runTask(any(MessageTask.class))).willReturn(bukkiTask);
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(8); given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(8);
given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true); given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);