From 28c5e39f8b407fbfcdac7ffcd0102e9d62890c56 Mon Sep 17 00:00:00 2001 From: themode Date: Sun, 27 Dec 2020 22:16:19 +0100 Subject: [PATCH] Pre login event is now async --- .../minestom/server/entity/EntityManager.java | 36 +++++++++++-------- .../net/minestom/server/entity/Player.java | 2 +- ...ent.java => AsyncPlayerPreLoginEvent.java} | 4 +-- .../server/network/ConnectionManager.java | 3 +- 4 files changed, 26 insertions(+), 19 deletions(-) rename src/main/java/net/minestom/server/event/player/{PlayerPreLoginEvent.java => AsyncPlayerPreLoginEvent.java} (89%) diff --git a/src/main/java/net/minestom/server/entity/EntityManager.java b/src/main/java/net/minestom/server/entity/EntityManager.java index 3a77d5218..ecfeb2f7e 100644 --- a/src/main/java/net/minestom/server/entity/EntityManager.java +++ b/src/main/java/net/minestom/server/entity/EntityManager.java @@ -4,8 +4,8 @@ import com.google.common.collect.Queues; import net.minestom.server.MinecraftServer; import net.minestom.server.chat.ChatColor; import net.minestom.server.chat.ColoredText; +import net.minestom.server.event.player.AsyncPlayerPreLoginEvent; import net.minestom.server.event.player.PlayerLoginEvent; -import net.minestom.server.event.player.PlayerPreLoginEvent; import net.minestom.server.instance.Instance; import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.packet.server.play.KeepAlivePacket; @@ -15,6 +15,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Queue; import java.util.UUID; +import java.util.concurrent.CompletableFuture; import java.util.function.Consumer; public final class EntityManager { @@ -74,7 +75,7 @@ public final class EntityManager { } /** - * Calls the player initialization callbacks and the event {@link PlayerPreLoginEvent}. + * Calls the player initialization callbacks and the event {@link AsyncPlayerPreLoginEvent}. * If the {@link Player} hasn't been kicked, add him to the waiting list. *

* Can be considered as a pre-init thing, @@ -89,22 +90,27 @@ public final class EntityManager { playerInitialization.accept(player); } - // Call pre login event - PlayerPreLoginEvent playerPreLoginEvent = new PlayerPreLoginEvent(player, player.getUsername(), player.getUuid()); - player.callEvent(PlayerPreLoginEvent.class, playerPreLoginEvent); + CompletableFuture.runAsync(() -> { + // Call pre login event + AsyncPlayerPreLoginEvent asyncPlayerPreLoginEvent = new AsyncPlayerPreLoginEvent(player, player.getUsername(), player.getUuid()); + player.callEvent(AsyncPlayerPreLoginEvent.class, asyncPlayerPreLoginEvent); - // Ignore the player if he has been disconnected (kick) - final boolean online = player.isOnline(); - if (!online) - return; + // Ignore the player if he has been disconnected (kick) + final boolean online = player.isOnline(); + if (!online) + return; - // Add him to the list and change his username/uuid if changed - this.waitingPlayers.add(player); + // Change UUID/Username based on the event + { + final String username = asyncPlayerPreLoginEvent.getUsername(); + final UUID uuid = asyncPlayerPreLoginEvent.getPlayerUuid(); - final String username = playerPreLoginEvent.getUsername(); - final UUID uuid = playerPreLoginEvent.getPlayerUuid(); + player.setUsername(username); + player.setUuid(uuid); + } - player.setUsername(username); - player.setUuid(uuid); + // Add the player to the waiting list + this.waitingPlayers.add(player); + }); } } diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 09761157a..160476f67 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -1286,7 +1286,7 @@ public class Player extends LivingEntity implements CommandSender { } /** - * Changes the internal player name, used for the {@link PlayerPreLoginEvent} + * Changes the internal player name, used for the {@link AsyncPlayerPreLoginEvent} * mostly unsafe outside of it. * * @param username the new player name diff --git a/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java b/src/main/java/net/minestom/server/event/player/AsyncPlayerPreLoginEvent.java similarity index 89% rename from src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java rename to src/main/java/net/minestom/server/event/player/AsyncPlayerPreLoginEvent.java index dfcb4d03f..a38586d16 100644 --- a/src/main/java/net/minestom/server/event/player/PlayerPreLoginEvent.java +++ b/src/main/java/net/minestom/server/event/player/AsyncPlayerPreLoginEvent.java @@ -11,12 +11,12 @@ import java.util.UUID; * Called before the player initialization, it can be used to kick the player before any connection * or to change his final username/uuid. */ -public class PlayerPreLoginEvent extends PlayerEvent { +public class AsyncPlayerPreLoginEvent extends PlayerEvent { private String username; private UUID playerUuid; - public PlayerPreLoginEvent(@NotNull Player player, @NotNull String username, @NotNull UUID playerUuid) { + public AsyncPlayerPreLoginEvent(@NotNull Player player, @NotNull String username, @NotNull UUID playerUuid) { super(player); this.username = username; this.playerUuid = playerUuid; diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index 9bb9d715c..c49e7c7f1 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -7,6 +7,7 @@ import net.minestom.server.chat.ColoredText; import net.minestom.server.chat.JsonMessage; import net.minestom.server.entity.Player; import net.minestom.server.entity.fakeplayer.FakePlayer; +import net.minestom.server.event.player.AsyncPlayerPreLoginEvent; import net.minestom.server.listener.manager.ClientPacketConsumer; import net.minestom.server.listener.manager.ServerPacketConsumer; import net.minestom.server.network.packet.client.login.LoginStartPacket; @@ -255,7 +256,7 @@ public final class ConnectionManager { * This callback should be exclusively used to add event listeners since it is called directly after a * player join (before any chunk is sent) and the client behavior can therefore be unpredictable. * You can add your "init" code in {@link net.minestom.server.event.player.PlayerLoginEvent} - * or even {@link net.minestom.server.event.player.PlayerPreLoginEvent}. + * or even {@link AsyncPlayerPreLoginEvent}. * * @param playerInitialization the {@link Player} initialization consumer */