From ffa5059c671cec518a0abfec475c285e3d292b2d Mon Sep 17 00:00:00 2001 From: games647 Date: Tue, 3 May 2016 16:14:38 +0200 Subject: [PATCH] Use intends to run BungeeCord tasks on a event in background --- .../bungee/PlayerConnectionListener.java | 52 +++++++++++-------- .../bungee/hooks/BungeeAuthHook.java | 41 ++++++--------- 2 files changed, 45 insertions(+), 48 deletions(-) diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/PlayerConnectionListener.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/PlayerConnectionListener.java index 4cb85c71..95fd2a3d 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/PlayerConnectionListener.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/PlayerConnectionListener.java @@ -33,35 +33,43 @@ public class PlayerConnectionListener implements Listener { } @EventHandler - public void onPreLogin(PreLoginEvent preLoginEvent) { + public void onPreLogin(final PreLoginEvent preLoginEvent) { if (preLoginEvent.isCancelled()) { return; } - PendingConnection connection = preLoginEvent.getConnection(); - String username = connection.getName(); - //just enable it for activated users - - PlayerProfile playerProfile = plugin.getStorage().getProfile(username, true); - if (playerProfile != null) { - if (playerProfile.isPremium()) { - if (playerProfile.getUserId() != -1) { - connection.setOnlineMode(true); - } - } else if (playerProfile.getUserId() == -1) { - //user not exists in the db - BungeeAuthPlugin authPlugin = plugin.getBungeeAuthPlugin(); - if (plugin.getConfiguration().getBoolean("autoRegister") - && (authPlugin == null || !authPlugin.isRegistered(username))) { - UUID premiumUUID = plugin.getMojangApiConnector().getPremiumUUID(username); - if (premiumUUID != null) { - plugin.getLogger().log(Level.FINER, "Player {0} uses a premium username", username); - connection.setOnlineMode(true); - plugin.getPendingAutoRegister().put(connection, new Object()); + preLoginEvent.registerIntent(plugin); + ProxyServer.getInstance().getScheduler().runAsync(plugin, new Runnable() { + @Override + public void run() { + PendingConnection connection = preLoginEvent.getConnection(); + String username = connection.getName(); + try { + PlayerProfile playerProfile = plugin.getStorage().getProfile(username, true); + if (playerProfile != null) { + if (playerProfile.isPremium()) { + if (playerProfile.getUserId() != -1) { + connection.setOnlineMode(true); + } + } else if (playerProfile.getUserId() == -1) { + //user not exists in the db + BungeeAuthPlugin authPlugin = plugin.getBungeeAuthPlugin(); + if (plugin.getConfiguration().getBoolean("autoRegister") + && (authPlugin == null || !authPlugin.isRegistered(username))) { + UUID premiumUUID = plugin.getMojangApiConnector().getPremiumUUID(username); + if (premiumUUID != null) { + plugin.getLogger().log(Level.FINER, "Player {0} uses a premium username", username); + connection.setOnlineMode(true); + plugin.getPendingAutoRegister().put(connection, new Object()); + } + } + } } + } finally { + preLoginEvent.completeIntent(plugin); } } - } + }); } @EventHandler diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/hooks/BungeeAuthHook.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/hooks/BungeeAuthHook.java index 947af7b4..3009bff3 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/hooks/BungeeAuthHook.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/hooks/BungeeAuthHook.java @@ -9,7 +9,6 @@ import me.vik1395.BungeeAuth.ListenerClass; import me.vik1395.BungeeAuth.Main; import me.vik1395.BungeeAuth.Password.PasswordHandler; import me.vik1395.BungeeAuth.Tables; -import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -35,20 +34,15 @@ public class BungeeAuthHook implements BungeeAuthPlugin { final Class[] parameterTypes = new Class[]{String.class, String.class}; final Object[] arguments = new Object[]{player.getName(), "online"}; - ProxyServer.getInstance().getScheduler().runAsync(Main.plugin, new Runnable() { - @Override - public void run() { - try { - callProtected("setStatus", parameterTypes, arguments); - ListenerClass.movePlayer(player, false); + try { + callProtected("setStatus", parameterTypes, arguments); + ListenerClass.movePlayer(player, false); - //proparly not thread-safe - ListenerClass.prelogin.get(player.getName()).cancel(); - } catch (Exception ex) { - Main.plugin.getLogger().severe("[BungeeAuth] Error force loging in player"); - } - } - }); + //proparly not thread-safe + ListenerClass.prelogin.get(player.getName()).cancel(); + } catch (Exception ex) { + Main.plugin.getLogger().severe("[BungeeAuth] Error force loging in player"); + } } @Override @@ -84,18 +78,13 @@ public class BungeeAuthHook implements BungeeAuthPlugin { , String.class, String.class, String.class, String.class}; final Object[] arguments = new Object[] {player.getName(), hash, pType, "", lastip, regdate, lastip, lastseen}; - ProxyServer.getInstance().getScheduler().runAsync(Main.plugin, new Runnable() { - @Override - public void run() { - try { - callProtected("newPlayerEntry", parameterTypes, arguments); - //proparly not thread-safe - forceLogin(player); - } catch (Exception ex) { - Main.plugin.getLogger().severe("[BungeeAuth] Error when creating a new player in the Database"); - } - } - }); + try { + callProtected("newPlayerEntry", parameterTypes, arguments); + //proparly not thread-safe + forceLogin(player); + } catch (Exception ex) { + Main.plugin.getLogger().severe("[BungeeAuth] Error when creating a new player in the Database"); + } } //pail ;(