mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-25 18:17:34 +01:00
Mute the message task when the player has to solve a captcha
This commit is contained in:
parent
f8745876de
commit
1b60b07f60
@ -4,6 +4,7 @@ import fr.xephi.authme.data.CaptchaManager;
|
||||
import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.command.CommandService;
|
||||
import fr.xephi.authme.command.PlayerCommand;
|
||||
import fr.xephi.authme.data.limbo.LimboCache;
|
||||
import fr.xephi.authme.message.MessageKey;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@ -21,6 +22,9 @@ public class CaptchaCommand extends PlayerCommand {
|
||||
@Inject
|
||||
private CommandService commandService;
|
||||
|
||||
@Inject
|
||||
private LimboCache limboCache;
|
||||
|
||||
@Override
|
||||
public void runCommand(Player player, List<String> arguments) {
|
||||
final String playerName = player.getName().toLowerCase();
|
||||
@ -39,6 +43,7 @@ public class CaptchaCommand extends PlayerCommand {
|
||||
if (isCorrectCode) {
|
||||
commandService.send(player, MessageKey.CAPTCHA_SUCCESS);
|
||||
commandService.send(player, MessageKey.LOGIN_MESSAGE);
|
||||
limboCache.getPlayerData(player.getName()).getMessageTask().setMuted(false);
|
||||
} else {
|
||||
String newCode = captchaManager.generateCode(player.getName());
|
||||
commandService.send(player, MessageKey.CAPTCHA_WRONG_ERROR, newCode);
|
||||
|
@ -1,5 +1,6 @@
|
||||
package fr.xephi.authme.data.limbo;
|
||||
|
||||
import fr.xephi.authme.task.MessageTask;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
@ -16,7 +17,7 @@ public class LimboPlayer {
|
||||
private final float walkSpeed;
|
||||
private final float flySpeed;
|
||||
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) {
|
||||
this.loc = loc;
|
||||
@ -94,7 +95,7 @@ public class LimboPlayer {
|
||||
*
|
||||
* @return The task responsible for sending the message regularly
|
||||
*/
|
||||
public BukkitTask getMessageTask() {
|
||||
public MessageTask getMessageTask() {
|
||||
return messageTask;
|
||||
}
|
||||
|
||||
@ -103,7 +104,7 @@ public class LimboPlayer {
|
||||
*
|
||||
* @param messageTask The message task to set
|
||||
*/
|
||||
public void setMessageTask(BukkitTask messageTask) {
|
||||
public void setMessageTask(MessageTask messageTask) {
|
||||
if (this.messageTask != null) {
|
||||
this.messageTask.cancel();
|
||||
}
|
||||
|
@ -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 (captchaManager.isCaptchaRequired(player.getName())) {
|
||||
limboCache.getPlayerData(player.getName()).getMessageTask().setMuted(true);
|
||||
service.send(player, MessageKey.USAGE_CAPTCHA,
|
||||
captchaManager.getCaptchaCodeOrGenerateNew(player.getName()));
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import fr.xephi.authme.settings.properties.RegistrationSettings;
|
||||
import fr.xephi.authme.settings.properties.RestrictionSettings;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -57,8 +58,8 @@ public class LimboPlayerTaskManager {
|
||||
ConsoleLogger.info("PlayerData for '" + name + "' is not available");
|
||||
} else {
|
||||
cancelTask(limboPlayer.getMessageTask());
|
||||
BukkitTask messageTask = bukkitService.runTask(new MessageTask(name, messages.retrieve(key),
|
||||
interval, bukkitService, limboCache, playerCache));
|
||||
MessageTask messageTask = new MessageTask(name, messages.retrieve(key), bukkitService, playerCache);
|
||||
bukkitService.runTaskTimer(messageTask, 2 * TICKS_PER_SECOND, interval * TICKS_PER_SECOND);
|
||||
limboPlayer.setMessageTask(messageTask);
|
||||
}
|
||||
}
|
||||
@ -110,4 +111,15 @@ public class LimboPlayerTaskManager {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import fr.xephi.authme.data.auth.PlayerCache;
|
||||
import fr.xephi.authme.data.limbo.LimboCache;
|
||||
import fr.xephi.authme.service.BukkitService;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
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.
|
||||
*/
|
||||
public class MessageTask implements Runnable {
|
||||
public class MessageTask extends BukkitRunnable {
|
||||
|
||||
private final String name;
|
||||
private final String[] message;
|
||||
private final int interval;
|
||||
private final BukkitService bukkitService;
|
||||
private final LimboCache limboCache;
|
||||
private final PlayerCache playerCache;
|
||||
private boolean isMuted;
|
||||
|
||||
/*
|
||||
* Constructor.
|
||||
*/
|
||||
public MessageTask(String name, String[] lines, int interval, BukkitService bukkitService,
|
||||
LimboCache limboCache, PlayerCache playerCache) {
|
||||
public MessageTask(String name, String[] lines, BukkitService bukkitService, PlayerCache playerCache) {
|
||||
this.name = name;
|
||||
this.message = lines;
|
||||
this.interval = interval;
|
||||
this.bukkitService = bukkitService;
|
||||
this.limboCache = limboCache;
|
||||
this.playerCache = playerCache;
|
||||
isMuted = false;
|
||||
}
|
||||
|
||||
public void setMuted(boolean isMuted) {
|
||||
this.isMuted = isMuted;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
if (playerCache.isAuthenticated(name)) {
|
||||
cancel();
|
||||
}
|
||||
|
||||
if(isMuted) {
|
||||
return;
|
||||
}
|
||||
|
||||
@ -44,11 +50,7 @@ public class MessageTask implements Runnable {
|
||||
for (String ms : message) {
|
||||
player.sendMessage(ms);
|
||||
}
|
||||
BukkitTask nextTask = bukkitService.runTaskLater(this, interval * TICKS_PER_SECOND);
|
||||
if (limboCache.hasPlayerData(name)) {
|
||||
limboCache.getPlayerData(name).setMessageTask(nextTask);
|
||||
}
|
||||
return;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -64,8 +64,6 @@ public class LimboPlayerTaskManagerTest {
|
||||
given(limboCache.getPlayerData(name)).willReturn(limboPlayer);
|
||||
MessageKey key = MessageKey.REGISTER_EMAIL_MESSAGE;
|
||||
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.USE_EMAIL_REGISTRATION)).willReturn(true);
|
||||
|
||||
@ -73,7 +71,8 @@ public class LimboPlayerTaskManagerTest {
|
||||
limboPlayerTaskManager.registerMessageTask(name, false);
|
||||
|
||||
// then
|
||||
verify(limboPlayer).setMessageTask(bukkiTask);
|
||||
MessageTask bukkitTask = mock(MessageTask.class);
|
||||
verify(limboPlayer).setMessageTask(bukkitTask);
|
||||
verify(messages).retrieve(key);
|
||||
}
|
||||
|
||||
@ -114,7 +113,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
public void shouldCancelExistingMessageTask() {
|
||||
// given
|
||||
LimboPlayer limboPlayer = mock(LimboPlayer.class);
|
||||
BukkitTask existingMessageTask = mock(BukkitTask.class);
|
||||
MessageTask existingMessageTask = mock(MessageTask.class);
|
||||
given(limboPlayer.getMessageTask()).willReturn(existingMessageTask);
|
||||
|
||||
String name = "bobby";
|
||||
@ -122,8 +121,7 @@ public class LimboPlayerTaskManagerTest {
|
||||
given(messages.retrieve(MessageKey.REGISTER_EMAIL_MESSAGE))
|
||||
.willReturn(new String[]{"Please register", "Use /register"});
|
||||
|
||||
BukkitTask bukkiTask = mock(BukkitTask.class);
|
||||
given(bukkitService.runTask(any(MessageTask.class))).willReturn(bukkiTask);
|
||||
MessageTask bukkiTask = mock(MessageTask.class);
|
||||
given(settings.getProperty(RegistrationSettings.MESSAGE_INTERVAL)).willReturn(8);
|
||||
given(settings.getProperty(RegistrationSettings.USE_EMAIL_REGISTRATION)).willReturn(true);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user