Pre login event is now async

This commit is contained in:
themode 2020-12-27 22:16:19 +01:00
parent a3ae2f39a5
commit 28c5e39f8b
4 changed files with 26 additions and 19 deletions

View File

@ -4,8 +4,8 @@ import com.google.common.collect.Queues;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.chat.ChatColor; import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText; 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.PlayerLoginEvent;
import net.minestom.server.event.player.PlayerPreLoginEvent;
import net.minestom.server.instance.Instance; import net.minestom.server.instance.Instance;
import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.packet.server.play.KeepAlivePacket; 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.Queue;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer; import java.util.function.Consumer;
public final class EntityManager { 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. * If the {@link Player} hasn't been kicked, add him to the waiting list.
* <p> * <p>
* Can be considered as a pre-init thing, * Can be considered as a pre-init thing,
@ -89,22 +90,27 @@ public final class EntityManager {
playerInitialization.accept(player); playerInitialization.accept(player);
} }
CompletableFuture.runAsync(() -> {
// Call pre login event // Call pre login event
PlayerPreLoginEvent playerPreLoginEvent = new PlayerPreLoginEvent(player, player.getUsername(), player.getUuid()); AsyncPlayerPreLoginEvent asyncPlayerPreLoginEvent = new AsyncPlayerPreLoginEvent(player, player.getUsername(), player.getUuid());
player.callEvent(PlayerPreLoginEvent.class, playerPreLoginEvent); player.callEvent(AsyncPlayerPreLoginEvent.class, asyncPlayerPreLoginEvent);
// Ignore the player if he has been disconnected (kick) // Ignore the player if he has been disconnected (kick)
final boolean online = player.isOnline(); final boolean online = player.isOnline();
if (!online) if (!online)
return; return;
// Add him to the list and change his username/uuid if changed // Change UUID/Username based on the event
this.waitingPlayers.add(player); {
final String username = asyncPlayerPreLoginEvent.getUsername();
final String username = playerPreLoginEvent.getUsername(); final UUID uuid = asyncPlayerPreLoginEvent.getPlayerUuid();
final UUID uuid = playerPreLoginEvent.getPlayerUuid();
player.setUsername(username); player.setUsername(username);
player.setUuid(uuid); player.setUuid(uuid);
} }
// Add the player to the waiting list
this.waitingPlayers.add(player);
});
}
} }

View File

@ -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. * mostly unsafe outside of it.
* *
* @param username the new player name * @param username the new player name

View File

@ -11,12 +11,12 @@ import java.util.UUID;
* Called before the player initialization, it can be used to kick the player before any connection * Called before the player initialization, it can be used to kick the player before any connection
* or to change his final username/uuid. * or to change his final username/uuid.
*/ */
public class PlayerPreLoginEvent extends PlayerEvent { public class AsyncPlayerPreLoginEvent extends PlayerEvent {
private String username; private String username;
private UUID playerUuid; 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); super(player);
this.username = username; this.username = username;
this.playerUuid = playerUuid; this.playerUuid = playerUuid;

View File

@ -7,6 +7,7 @@ import net.minestom.server.chat.ColoredText;
import net.minestom.server.chat.JsonMessage; import net.minestom.server.chat.JsonMessage;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.entity.fakeplayer.FakePlayer; 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.ClientPacketConsumer;
import net.minestom.server.listener.manager.ServerPacketConsumer; import net.minestom.server.listener.manager.ServerPacketConsumer;
import net.minestom.server.network.packet.client.login.LoginStartPacket; 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 * 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. * 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} * 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 * @param playerInitialization the {@link Player} initialization consumer
*/ */