From f688eb457451266ecfe2500429b20db795eb7c74 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sun, 18 Sep 2016 22:51:24 +0200 Subject: [PATCH] #937 Fix auto login after register not working in sync mode - Need a small delay to allow the database to store the PlayerAuth object in the registration process --- .../process/login/AsynchronousLogin.java | 12 +++------ .../process/register/AsyncRegister.java | 25 +++++++++++-------- 2 files changed, 18 insertions(+), 19 deletions(-) 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 d66e62fe6..e810b2e38 100644 --- a/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java +++ b/src/main/java/fr/xephi/authme/process/login/AsynchronousLogin.java @@ -89,8 +89,8 @@ public class AsynchronousLogin implements AsynchronousProcess { PlayerAuth pAuth = database.getAuth(name); if (pAuth == null) { service.send(player, MessageKey.USER_NOT_REGISTERED); - - // TODO ljacqu 20160612: Why is the message task being canceled and added again here? + // Recreate the message task to immediately send the message again as response + // and to make sure we send the right register message (password vs. email registration) playerDataTaskManager.registerMessageTask(name, false); return null; } @@ -186,12 +186,8 @@ public class AsynchronousLogin implements AsynchronousProcess { } else if (player.isOnline()) { ConsoleLogger.fine(player.getName() + " used the wrong password"); if (service.getProperty(RestrictionSettings.KICK_ON_WRONG_PASSWORD)) { - bukkitService.scheduleSyncDelayedTask(new Runnable() { - @Override - public void run() { - player.kickPlayer(service.retrieveSingleMessage(MessageKey.WRONG_PASSWORD)); - } - }); + bukkitService.scheduleSyncTaskFromOptionallyAsyncTask( + () -> player.kickPlayer(service.retrieveSingleMessage(MessageKey.WRONG_PASSWORD))); } else if (tempbanManager.shouldTempban(ip)) { tempbanManager.tempbanPlayer(player); } else { diff --git a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java index d97c21962..c7630fc34 100644 --- a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java @@ -15,6 +15,7 @@ import fr.xephi.authme.security.PasswordSecurity; import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.TwoFactor; import fr.xephi.authme.settings.properties.EmailSettings; +import fr.xephi.authme.settings.properties.PluginSettings; import fr.xephi.authme.settings.properties.RegistrationSettings; import fr.xephi.authme.settings.properties.RestrictionSettings; import fr.xephi.authme.settings.properties.SecuritySettings; @@ -36,37 +37,34 @@ import static fr.xephi.authme.permission.PlayerStatePermission.ALLOW_MULTIPLE_AC */ public class AsyncRegister implements AsynchronousProcess { + /** + * Number of ticks to wait before running the login action when it is run synchronously. + * A small delay is necessary or the database won't return the newly saved PlayerAuth object + * and the login process thinks the user is not registered. + */ + private static final int SYNC_LOGIN_DELAY = 5; + @Inject private DataSource database; - @Inject private PlayerCache playerCache; - @Inject private PasswordSecurity passwordSecurity; - @Inject private ProcessService service; - @Inject private SyncProcessManager syncProcessManager; - @Inject private PermissionsManager permissionsManager; - @Inject private ValidationService validationService; - @Inject private SendMailSSL sendMailSsl; - @Inject private AsynchronousLogin asynchronousLogin; - @Inject private BukkitService bukkitService; - AsyncRegister() { } @@ -172,7 +170,12 @@ public class AsyncRegister implements AsynchronousProcess { } if (!service.getProperty(RegistrationSettings.FORCE_LOGIN_AFTER_REGISTER) && autoLogin) { - bukkitService.runTaskOptionallyAsync(() -> asynchronousLogin.login(player, "dontneed", true)); + if (service.getProperty(PluginSettings.USE_ASYNC_TASKS)) { + bukkitService.runTaskAsynchronously(() -> asynchronousLogin.login(player, "dontneed", true)); + } else { + bukkitService.scheduleSyncDelayedTask( + () -> asynchronousLogin.login(player, "dontneed", true), SYNC_LOGIN_DELAY); + } } syncProcessManager.processSyncPasswordRegister(player);