From fc226e101064a0374322863226773f5d910cc364 Mon Sep 17 00:00:00 2001 From: juanmuscaria Date: Wed, 15 Sep 2021 19:55:00 -0300 Subject: [PATCH] Moved LoginEvent logic to GameProfileRequestEvent. Allow overwriting online mode uuid and support for forwardSkin. Added one second delay for login command. --- .../velocity/listener/ConnectListener.java | 70 +++++++------------ .../velocity/task/ForceLoginTask.java | 1 - 2 files changed, 24 insertions(+), 47 deletions(-) diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java index e18a1498..7b5c55e5 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/listener/ConnectListener.java @@ -25,6 +25,7 @@ */ package com.github.games647.fastlogin.velocity.listener; +import com.github.games647.craftapi.UUIDAdapter; import com.github.games647.fastlogin.core.RateLimiter; import com.github.games647.fastlogin.core.StoredProfile; import com.github.games647.fastlogin.core.shared.LoginSession; @@ -33,17 +34,18 @@ import com.github.games647.fastlogin.velocity.VelocityLoginSession; import com.github.games647.fastlogin.velocity.task.AsyncPremiumCheck; import com.github.games647.fastlogin.velocity.task.ForceLoginTask; import com.velocitypowered.api.event.Continuation; -import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.DisconnectEvent; -import com.velocitypowered.api.event.connection.LoginEvent; import com.velocitypowered.api.event.connection.PreLoginEvent; +import com.velocitypowered.api.event.player.GameProfileRequestEvent; import com.velocitypowered.api.event.player.ServerConnectedEvent; import com.velocitypowered.api.proxy.InboundConnection; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.server.RegisteredServer; +import java.util.ArrayList; import java.util.UUID; +import java.util.concurrent.TimeUnit; public class ConnectListener { @@ -74,59 +76,32 @@ public class ConnectListener { plugin.getScheduler().runAsync(asyncPremiumCheck); } - @Subscribe(order = PostOrder.LATE) - public void onLogin(LoginEvent loginEvent) { - //use the login event instead of the post login event in order to send the login success packet to the client - //with the offline uuid this makes it possible to set the skin then - Player connection = loginEvent.getPlayer(); - if (connection.isOnlineMode()) { - LoginSession session = plugin.getSession().get(connection.getRemoteAddress()); + @Subscribe + public void onGameprofileRequest(GameProfileRequestEvent event) { + if (event.isOnlineMode()) { + LoginSession session = plugin.getSession().get(event.getConnection().getRemoteAddress()); - UUID verifiedUUID = connection.getUniqueId(); - String verifiedUsername = connection.getUsername(); + UUID verifiedUUID = event.getGameProfile().getId(); + String verifiedUsername = event.getUsername(); session.setUuid(verifiedUUID); session.setVerifiedUsername(verifiedUsername); StoredProfile playerProfile = session.getProfile(); playerProfile.setId(verifiedUUID); + if (!plugin.getCore().getConfig().get("premiumUuid", true)) { + UUID offlineUUID = UUIDAdapter.generateOfflineId(playerProfile.getName()); + event.setGameProfile(event.getGameProfile().withId(offlineUUID)); + plugin.getLog().info("Overridden UUID from {} to {} (based of {}) on {}", + verifiedUUID, offlineUUID, verifiedUsername, event.getConnection()); + } - // bungeecord will do this automatically so override it on disabled option -// if (uniqueIdSetter != null) { -// InitialHandler initialHandler = (InitialHandler) connection; -// -// if (!plugin.getCore().getConfig().get("premiumUuid", true)) { -// setOfflineId(initialHandler, verifiedUsername); -// } -// -// if (!plugin.getCore().getConfig().get("forwardSkin", true)) { -// // this is null on offline mode -// LoginResult loginProfile = initialHandler.getLoginProfile(); -// loginProfile.setProperties(emptyProperties); -// } -// } + if (!plugin.getCore().getConfig().get("forwardSkin", true)) { + //FIXME: Do I need to remove *all* properties or only the skin related ones? + event.setGameProfile(event.getGameProfile().withProperties(new ArrayList<>())); + } } } -// private void setOfflineId(InitialHandler connection, String username) { -// try { -// final UUID oldPremiumId = connection.getUniqueId(); -// final UUID offlineUUID = UUIDAdapter.generateOfflineId(username); -// -// // BungeeCord only allows setting the UUID in PreLogin events and before requesting online mode -// // However if online mode is requested, it will override previous values -// // So we have to do it with reflection -// uniqueIdSetter.invokeExact(connection, offlineUUID); -// -// String format = "Overridden UUID from {} to {} (based of {}) on {}"; -// plugin.getLog().info(format, oldPremiumId, offlineUUID, username, connection); -// } catch (Exception ex) { -// plugin.getLog().error("Failed to set offline uuid of {}", username, ex); -// } catch (Throwable throwable) { -// // throw remaining exceptions like outofmemory that we shouldn't handle ourself -// Throwables.throwIfUnchecked(throwable); -// } -// } - @Subscribe public void onServerConnected(ServerConnectedEvent serverConnectedEvent) { Player player = serverConnectedEvent.getPlayer(); @@ -141,7 +116,10 @@ public class ConnectListener { // In this case it means that the force command (plugin message) is already received and processed while // player is still in the login phase and reported to be offline. Runnable loginTask = new ForceLoginTask(plugin.getCore(), player, server, session); - plugin.getScheduler().runAsync(loginTask); + plugin.getProxy().getScheduler() + .buildTask(plugin, () -> plugin.getScheduler().runAsync(loginTask)) + .delay(1L, TimeUnit.SECONDS) // Delay at least one second, otherwise the login command can be missed + .schedule(); } @Subscribe diff --git a/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/ForceLoginTask.java b/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/ForceLoginTask.java index 4c75bf8d..c2ed6451 100644 --- a/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/ForceLoginTask.java +++ b/velocity/src/main/java/com/github/games647/fastlogin/velocity/task/ForceLoginTask.java @@ -40,7 +40,6 @@ import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.server.RegisteredServer; -import java.nio.charset.StandardCharsets; import java.util.UUID; import java.util.concurrent.ExecutionException;