mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-02 14:38:26 +01:00
Simplify login process
This commit is contained in:
parent
aaa71f4e9a
commit
2cc92e5e3e
@ -10,7 +10,6 @@ import net.minestom.server.event.player.PlayerSpawnEvent;
|
|||||||
import net.minestom.server.instance.*;
|
import net.minestom.server.instance.*;
|
||||||
import net.minestom.server.instance.batch.ChunkBatch;
|
import net.minestom.server.instance.batch.ChunkBatch;
|
||||||
import net.minestom.server.instance.block.Block;
|
import net.minestom.server.instance.block.Block;
|
||||||
import net.minestom.server.network.ConnectionManager;
|
|
||||||
import net.minestom.server.world.biomes.Biome;
|
import net.minestom.server.world.biomes.Biome;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package net.minestom.server.event.player;
|
package net.minestom.server.event.player;
|
||||||
|
|
||||||
import net.minestom.server.entity.Player;
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.event.Event;
|
|
||||||
import net.minestom.server.event.trait.PlayerEvent;
|
import net.minestom.server.event.trait.PlayerEvent;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -17,10 +16,10 @@ public class AsyncPlayerPreLoginEvent implements PlayerEvent {
|
|||||||
private String username;
|
private String username;
|
||||||
private UUID playerUuid;
|
private UUID playerUuid;
|
||||||
|
|
||||||
public AsyncPlayerPreLoginEvent(@NotNull Player player, @NotNull String username, @NotNull UUID playerUuid) {
|
public AsyncPlayerPreLoginEvent(@NotNull Player player) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.username = username;
|
this.username = player.getUsername();
|
||||||
this.playerUuid = playerUuid;
|
this.playerUuid = player.getUuid();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -161,24 +161,6 @@ public final class ConnectionManager {
|
|||||||
this.broadcastMessage(jsonMessage, null);
|
this.broadcastMessage(jsonMessage, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Collection<Player> getRecipients(@Nullable PlayerValidator condition) {
|
|
||||||
Collection<Player> recipients;
|
|
||||||
|
|
||||||
// Get the recipients
|
|
||||||
if (condition == null) {
|
|
||||||
recipients = getOnlinePlayers();
|
|
||||||
} else {
|
|
||||||
recipients = new ArrayList<>();
|
|
||||||
getOnlinePlayers().forEach(player -> {
|
|
||||||
final boolean result = condition.isValid(player);
|
|
||||||
if (result)
|
|
||||||
recipients.add(player);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return recipients;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all the listeners which are called for each packet received.
|
* Gets all the listeners which are called for each packet received.
|
||||||
*
|
*
|
||||||
@ -346,7 +328,6 @@ public final class ConnectionManager {
|
|||||||
final Player player = this.connectionPlayerMap.get(connection);
|
final Player player = this.connectionPlayerMap.get(connection);
|
||||||
if (player == null)
|
if (player == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.players.remove(player);
|
this.players.remove(player);
|
||||||
this.connectionPlayerMap.remove(connection);
|
this.connectionPlayerMap.remove(connection);
|
||||||
}
|
}
|
||||||
@ -362,58 +343,39 @@ public final class ConnectionManager {
|
|||||||
*/
|
*/
|
||||||
public void startPlayState(@NotNull Player player, boolean register) {
|
public void startPlayState(@NotNull Player player, boolean register) {
|
||||||
AsyncUtils.runAsync(() -> {
|
AsyncUtils.runAsync(() -> {
|
||||||
String username = player.getUsername();
|
final PlayerConnection playerConnection = player.getPlayerConnection();
|
||||||
UUID uuid = player.getUuid();
|
|
||||||
|
|
||||||
// Call pre login event
|
// Call pre login event
|
||||||
AsyncPlayerPreLoginEvent asyncPlayerPreLoginEvent = new AsyncPlayerPreLoginEvent(player, username, uuid);
|
AsyncPlayerPreLoginEvent asyncPlayerPreLoginEvent = new AsyncPlayerPreLoginEvent(player);
|
||||||
EventDispatcher.call(asyncPlayerPreLoginEvent);
|
EventDispatcher.call(asyncPlayerPreLoginEvent);
|
||||||
|
|
||||||
// Close the player channel if he has been disconnected (kick)
|
// Close the player channel if he has been disconnected (kick)
|
||||||
final boolean online = player.isOnline();
|
if (!player.isOnline()) {
|
||||||
if (!online) {
|
|
||||||
final PlayerConnection playerConnection = player.getPlayerConnection();
|
|
||||||
if (playerConnection instanceof NettyPlayerConnection) {
|
if (playerConnection instanceof NettyPlayerConnection) {
|
||||||
((NettyPlayerConnection) playerConnection).getChannel().flush();
|
((NettyPlayerConnection) playerConnection).getChannel().flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
//playerConnection.disconnect();
|
//playerConnection.disconnect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change UUID/Username based on the event
|
// Change UUID/Username based on the event
|
||||||
{
|
{
|
||||||
final String eventUsername = asyncPlayerPreLoginEvent.getUsername();
|
final String eventUsername = asyncPlayerPreLoginEvent.getUsername();
|
||||||
final UUID eventUuid = asyncPlayerPreLoginEvent.getPlayerUuid();
|
final UUID eventUuid = asyncPlayerPreLoginEvent.getPlayerUuid();
|
||||||
|
|
||||||
if (!player.getUsername().equals(eventUsername)) {
|
if (!player.getUsername().equals(eventUsername)) {
|
||||||
player.setUsernameField(eventUsername);
|
player.setUsernameField(eventUsername);
|
||||||
username = eventUsername;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!player.getUuid().equals(eventUuid)) {
|
if (!player.getUuid().equals(eventUuid)) {
|
||||||
player.setUuid(eventUuid);
|
player.setUuid(eventUuid);
|
||||||
uuid = eventUuid;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send login success packet
|
// Send login success packet
|
||||||
{
|
LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(player.getUuid(), player.getUsername());
|
||||||
final PlayerConnection connection = player.getPlayerConnection();
|
if (playerConnection instanceof NettyPlayerConnection) {
|
||||||
|
((NettyPlayerConnection) playerConnection).writeAndFlush(loginSuccessPacket);
|
||||||
LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(uuid, username);
|
} else {
|
||||||
if (connection instanceof NettyPlayerConnection) {
|
playerConnection.sendPacket(loginSuccessPacket);
|
||||||
((NettyPlayerConnection) connection).writeAndFlush(loginSuccessPacket);
|
|
||||||
} else {
|
|
||||||
connection.sendPacket(loginSuccessPacket);
|
|
||||||
}
|
|
||||||
|
|
||||||
connection.setConnectionState(ConnectionState.PLAY);
|
|
||||||
}
|
}
|
||||||
|
playerConnection.setConnectionState(ConnectionState.PLAY);
|
||||||
// Add the player to the waiting list
|
// Add the player to the waiting list
|
||||||
addWaitingPlayer(player);
|
this.waitingPlayers.add(player);
|
||||||
|
|
||||||
if (register) {
|
if (register) {
|
||||||
registerPlayer(player);
|
registerPlayer(player);
|
||||||
}
|
}
|
||||||
@ -457,7 +419,19 @@ public final class ConnectionManager {
|
|||||||
* Connects waiting players.
|
* Connects waiting players.
|
||||||
*/
|
*/
|
||||||
public void updateWaitingPlayers() {
|
public void updateWaitingPlayers() {
|
||||||
waitingPlayersTick();
|
Player waitingPlayer;
|
||||||
|
while ((waitingPlayer = waitingPlayers.poll()) != null) {
|
||||||
|
PlayerLoginEvent loginEvent = new PlayerLoginEvent(waitingPlayer);
|
||||||
|
EventDispatcher.call(loginEvent);
|
||||||
|
final Instance spawningInstance = loginEvent.getSpawningInstance();
|
||||||
|
|
||||||
|
Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent");
|
||||||
|
|
||||||
|
waitingPlayer.UNSAFE_init(spawningInstance);
|
||||||
|
|
||||||
|
// Spawn the player at Player#getRespawnPoint during the next instance tick
|
||||||
|
spawningInstance.scheduleNextTick(waitingPlayer::setInstance);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -478,32 +452,4 @@ public final class ConnectionManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds connected clients after the handshake (used to free the networking threads).
|
|
||||||
*/
|
|
||||||
private void waitingPlayersTick() {
|
|
||||||
Player waitingPlayer;
|
|
||||||
while ((waitingPlayer = waitingPlayers.poll()) != null) {
|
|
||||||
PlayerLoginEvent loginEvent = new PlayerLoginEvent(waitingPlayer);
|
|
||||||
EventDispatcher.call(loginEvent);
|
|
||||||
final Instance spawningInstance = loginEvent.getSpawningInstance();
|
|
||||||
|
|
||||||
Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent");
|
|
||||||
|
|
||||||
waitingPlayer.UNSAFE_init(spawningInstance);
|
|
||||||
|
|
||||||
// Spawn the player at Player#getRespawnPoint during the next instance tick
|
|
||||||
spawningInstance.scheduleNextTick(waitingPlayer::setInstance);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Adds a player into the waiting list, to be handled during the next server tick.
|
|
||||||
*
|
|
||||||
* @param player the {@link Player player} to add into the waiting list
|
|
||||||
*/
|
|
||||||
public void addWaitingPlayer(@NotNull Player player) {
|
|
||||||
this.waitingPlayers.add(player);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -245,6 +245,7 @@ public class NettyPlayerConnection extends PlayerConnection {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void disconnect() {
|
public void disconnect() {
|
||||||
|
refreshOnline(false);
|
||||||
this.channel.close();
|
this.channel.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,7 +150,6 @@ public abstract class PlayerConnection {
|
|||||||
return MinecraftServer.getNettyServer().getPort();
|
return MinecraftServer.getNettyServer().getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Forcing the player to disconnect.
|
* Forcing the player to disconnect.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user