first join

This commit is contained in:
mworzala 2023-10-07 09:00:15 -04:00 committed by Matt Worzala
parent 13edf5f863
commit 47042abf12
17 changed files with 100 additions and 52 deletions

View File

@ -453,7 +453,7 @@ public class LivingEntity extends Entity implements EquipmentHandler {
if (this instanceof Player player) { if (this instanceof Player player) {
PlayerConnection playerConnection = player.playerConnection; PlayerConnection playerConnection = player.playerConnection;
// connection null during Player initialization (due to #super call) // connection null during Player initialization (due to #super call)
self = playerConnection != null && playerConnection.getConnectionState() == ConnectionState.PLAY; self = playerConnection != null && playerConnection.getServerState() == ConnectionState.PLAY;
} }
EntityPropertiesPacket propertiesPacket = new EntityPropertiesPacket(getEntityId(), List.of(attributeInstance)); EntityPropertiesPacket propertiesPacket = new EntityPropertiesPacket(getEntityId(), List.of(attributeInstance));
if (self) { if (self) {

View File

@ -267,7 +267,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
final JoinGamePacket joinGamePacket = new JoinGamePacket( final JoinGamePacket joinGamePacket = new JoinGamePacket(
getEntityId(), false, List.of(), 0, getEntityId(), false, List.of(), 0,
MinecraftServer.getChunkViewDistance(), MinecraftServer.getChunkViewDistance(), MinecraftServer.getChunkViewDistance(), MinecraftServer.getChunkViewDistance(),
false, true, dimensionType.toString(), spawnInstance.getDimensionName(), false, true, false, dimensionType.toString(), spawnInstance.getDimensionName(),
0, gameMode, null, false, levelFlat, deathLocation, portalCooldown); 0, gameMode, null, false, levelFlat, deathLocation, portalCooldown);
sendPacket(joinGamePacket); sendPacket(joinGamePacket);
@ -1496,7 +1496,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
* @param component the reason * @param component the reason
*/ */
public void kick(@NotNull Component component) { public void kick(@NotNull Component component) {
final ConnectionState connectionState = playerConnection.getConnectionState(); final ConnectionState connectionState = playerConnection.getServerState();
// Packet type depends on the current player connection state // Packet type depends on the current player connection state
final ServerPacket disconnectPacket; final ServerPacket disconnectPacket;
if (connectionState == ConnectionState.LOGIN) { if (connectionState == ConnectionState.LOGIN) {
@ -1920,7 +1920,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
} }
final PacketListenerManager manager = MinecraftServer.getPacketListenerManager(); final PacketListenerManager manager = MinecraftServer.getPacketListenerManager();
// This method is NOT thread-safe // This method is NOT thread-safe
this.packets.drain(packet -> manager.processClientPacket(playerConnection.getConnectionState(), packet, playerConnection), PACKET_PER_TICK); this.packets.drain(packet -> manager.processClientPacket(playerConnection.getClientState(), packet, playerConnection), PACKET_PER_TICK);
} }
/** /**
@ -1930,7 +1930,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
*/ */
public void refreshLatency(int latency) { public void refreshLatency(int latency) {
this.latency = latency; this.latency = latency;
if (getPlayerConnection().getConnectionState() == ConnectionState.PLAY) { if (getPlayerConnection().getServerState() == ConnectionState.PLAY) {
PacketUtils.broadcastPacket(new PlayerInfoUpdatePacket(PlayerInfoUpdatePacket.Action.UPDATE_LATENCY, infoEntry())); PacketUtils.broadcastPacket(new PlayerInfoUpdatePacket(PlayerInfoUpdatePacket.Action.UPDATE_LATENCY, infoEntry()));
} }
} }
@ -2343,7 +2343,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
this.allowServerListings = allowServerListings; this.allowServerListings = allowServerListings;
// TODO: Use the metadata object here // TODO: Use the metadata object here
boolean isInPlayState = getPlayerConnection().getConnectionState() == ConnectionState.PLAY; boolean isInPlayState = getPlayerConnection().getServerState() == ConnectionState.PLAY;
if (isInPlayState) metadata.setNotifyAboutChanges(false); if (isInPlayState) metadata.setNotifyAboutChanges(false);
metadata.setIndex((byte) 17, Metadata.Byte(displayedSkinParts)); metadata.setIndex((byte) 17, Metadata.Byte(displayedSkinParts));
metadata.setIndex((byte) 18, Metadata.Byte((byte) (this.mainHand == MainHand.RIGHT ? 1 : 0))); metadata.setIndex((byte) 18, Metadata.Byte((byte) (this.mainHand == MainHand.RIGHT ? 1 : 0)));

View File

@ -163,9 +163,9 @@ public class BlockPlacementListener {
// Place the block // Place the block
Block resultBlock = playerBlockPlaceEvent.getBlock(); Block resultBlock = playerBlockPlaceEvent.getBlock();
player.sendPacket(new AcknowledgeBlockChangePacket(packet.sequence()));
instance.placeBlock(new BlockHandler.PlayerPlacement(resultBlock, instance, placementPosition, player, hand, blockFace, instance.placeBlock(new BlockHandler.PlayerPlacement(resultBlock, instance, placementPosition, player, hand, blockFace,
packet.cursorPositionX(), packet.cursorPositionY(), packet.cursorPositionZ()), playerBlockPlaceEvent.shouldDoBlockUpdates()); packet.cursorPositionX(), packet.cursorPositionY(), packet.cursorPositionZ()), playerBlockPlaceEvent.shouldDoBlockUpdates());
player.sendPacket(new AcknowledgeBlockChangePacket(packet.sequence()));
// Block consuming // Block consuming
if (playerBlockPlaceEvent.doesConsumeBlock()) { if (playerBlockPlaceEvent.doesConsumeBlock()) {
// Consume the block in the player's hand // Consume the block in the player's hand

View File

@ -1,13 +1,23 @@
package net.minestom.server.listener.preplay; package net.minestom.server.listener.preplay;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.network.ConnectionManager;
import net.minestom.server.network.ConnectionState;
import net.minestom.server.network.packet.client.configuration.ClientFinishConfigurationPacket; import net.minestom.server.network.packet.client.configuration.ClientFinishConfigurationPacket;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public final class ConfigurationListener { public final class ConfigurationListener {
private static final ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager();
public static void finishListener(@NotNull ClientFinishConfigurationPacket packet, @NotNull Player player) { public static void finishListener(@NotNull ClientFinishConfigurationPacket packet, @NotNull Player player) {
//todo move to play state player.getPlayerConnection().setClientState(ConnectionState.PLAY);
System.out.println("Finished configuration for " + player.getUsername() ); System.out.println("Finished configuration for " + player.getUsername() );
CONNECTION_MANAGER.startPlayState(player);
//todo move to play state
} }
} }

View File

@ -111,10 +111,14 @@ public final class HandshakeListener {
} }
switch (packet.nextState()) { switch (packet.nextState()) {
case 1 -> connection.setConnectionState(ConnectionState.STATUS); case 1 -> {
connection.setClientState(ConnectionState.STATUS);
connection.setServerState(ConnectionState.STATUS);
}
case 2 -> { case 2 -> {
if (packet.protocolVersion() == MinecraftServer.PROTOCOL_VERSION) { if (packet.protocolVersion() == MinecraftServer.PROTOCOL_VERSION) {
connection.setConnectionState(ConnectionState.LOGIN); connection.setClientState(ConnectionState.LOGIN);
connection.setServerState(ConnectionState.LOGIN);
} else { } else {
// Incorrect client version // Incorrect client version
disconnect(connection, INVALID_VERSION_TEXT); disconnect(connection, INVALID_VERSION_TEXT);

View File

@ -18,6 +18,7 @@ import net.minestom.server.network.packet.client.login.ClientEncryptionResponseP
import net.minestom.server.network.packet.client.login.ClientLoginAcknowledgedPacket; import net.minestom.server.network.packet.client.login.ClientLoginAcknowledgedPacket;
import net.minestom.server.network.packet.client.login.ClientLoginPluginResponsePacket; import net.minestom.server.network.packet.client.login.ClientLoginPluginResponsePacket;
import net.minestom.server.network.packet.client.login.ClientLoginStartPacket; import net.minestom.server.network.packet.client.login.ClientLoginStartPacket;
import net.minestom.server.network.packet.server.common.ResourcePackSendPacket;
import net.minestom.server.network.packet.server.common.TagsPacket; import net.minestom.server.network.packet.server.common.TagsPacket;
import net.minestom.server.network.packet.server.configuration.FinishConfigurationPacket; import net.minestom.server.network.packet.server.configuration.FinishConfigurationPacket;
import net.minestom.server.network.packet.server.configuration.RegistryDataPacket; import net.minestom.server.network.packet.server.configuration.RegistryDataPacket;
@ -79,7 +80,6 @@ public final class LoginListener {
return; return;
} }
final PlayerSocketConnection socketConnection = (PlayerSocketConnection) connection; final PlayerSocketConnection socketConnection = (PlayerSocketConnection) connection;
socketConnection.setConnectionState(ConnectionState.LOGIN);
final byte[] publicKey = MojangAuth.getKeyPair().getPublic().getEncoded(); final byte[] publicKey = MojangAuth.getKeyPair().getPublic().getEncoded();
byte[] nonce = new byte[4]; byte[] nonce = new byte[4];
@ -221,7 +221,7 @@ public final class LoginListener {
} }
public static void loginAckListener(@NotNull ClientLoginAcknowledgedPacket packet, @NotNull PlayerConnection connection) { public static void loginAckListener(@NotNull ClientLoginAcknowledgedPacket packet, @NotNull PlayerConnection connection) {
connection.setConnectionState(ConnectionState.CONFIGURATION); connection.setClientState(ConnectionState.CONFIGURATION);
CONNECTION_MANAGER.registerPlayer(connection.getPlayer()); CONNECTION_MANAGER.registerPlayer(connection.getPlayer());
@ -262,6 +262,7 @@ public final class LoginListener {
AsyncUtils.runAsync(() -> { AsyncUtils.runAsync(() -> {
//todo event //todo event
connection.setServerState(ConnectionState.PLAY);
connection.sendPacket(new FinishConfigurationPacket()); connection.sendPacket(new FinishConfigurationPacket());
}); });
} }

View File

@ -220,12 +220,14 @@ public final class ConnectionManager {
// Send login success packet // Send login success packet
LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(player.getUuid(), player.getUsername(), 0); LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(player.getUuid(), player.getUsername(), 0);
playerConnection.sendPacket(loginSuccessPacket); playerConnection.sendPacket(loginSuccessPacket);
// playerConnection.setConnectionState(ConnectionState.CONFIGURATION); playerConnection.setServerState(ConnectionState.CONFIGURATION);
// if (register) registerPlayer(player); // WHEN ENTERING CONFIGURATION THIS SHOULD BE SET
// this.waitingPlayers.relaxedOffer(player);
}); });
} }
public void startPlayState(@NotNull Player player) {
this.waitingPlayers.relaxedOffer(player);
}
/** /**
* Calls the player initialization callbacks and the event {@link AsyncPlayerPreLoginEvent}. * Calls the player initialization callbacks and the event {@link AsyncPlayerPreLoginEvent}.
* <p> * <p>
@ -262,7 +264,7 @@ public final class ConnectionManager {
// Send login success packet // Send login success packet
LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(player.getUuid(), player.getUsername(), 0); LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(player.getUuid(), player.getUsername(), 0);
playerConnection.sendPacket(loginSuccessPacket); playerConnection.sendPacket(loginSuccessPacket);
playerConnection.setConnectionState(ConnectionState.PLAY); // playerConnection.setConnectionState(ConnectionState.PLAY);
if (register) registerPlayer(player); if (register) registerPlayer(player);
this.waitingPlayers.relaxedOffer(player); this.waitingPlayers.relaxedOffer(player);
}); });
@ -300,13 +302,12 @@ public final class ConnectionManager {
EventDispatcher.call(loginEvent); EventDispatcher.call(loginEvent);
final Instance spawningInstance = loginEvent.getSpawningInstance(); final Instance spawningInstance = loginEvent.getSpawningInstance();
Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent"); Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent");
// Spawn the player at Player#getRespawnPoint // Spawn the player at Player#getRespawnPoint
if (DebugUtils.INSIDE_TEST) { CompletableFuture<Void> spawnFuture = waitingPlayer.UNSAFE_init(spawningInstance);
// Required to get the exact moment the player spawns
waitingPlayer.UNSAFE_init(spawningInstance).join(); // Required to get the exact moment the player spawns
} else { if (DebugUtils.INSIDE_TEST) spawnFuture.join();
waitingPlayer.UNSAFE_init(spawningInstance);
}
}); });
} }

View File

@ -5,6 +5,9 @@ import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.network.packet.client.ClientPacket; import net.minestom.server.network.packet.client.ClientPacket;
import net.minestom.server.network.packet.client.ClientPacketsHandler; import net.minestom.server.network.packet.client.ClientPacketsHandler;
import net.minestom.server.network.packet.client.handshake.ClientHandshakePacket; import net.minestom.server.network.packet.client.handshake.ClientHandshakePacket;
import net.minestom.server.network.packet.client.play.ClientPlayerPositionAndRotationPacket;
import net.minestom.server.network.packet.client.play.ClientPlayerPositionPacket;
import net.minestom.server.network.packet.client.play.ClientPlayerRotationPacket;
import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerConnection;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -50,12 +53,14 @@ public class PacketProcessor {
} }
public ClientPacket process(@NotNull PlayerConnection connection, int packetId, ByteBuffer body) { public ClientPacket process(@NotNull PlayerConnection connection, int packetId, ByteBuffer body) {
final ClientPacket packet = create(connection.getConnectionState(), packetId, body); final ClientPacket packet = create(connection.getClientState(), packetId, body);
System.out.println("PROCESS " + connection.getConnectionState() + " " + packet.getClass().getSimpleName()); var packetClass = packet.getClass();
if (packetClass != ClientPlayerPositionPacket.class && packetClass != ClientPlayerPositionAndRotationPacket.class && packetClass != ClientPlayerRotationPacket.class)
System.out.println("PROCESS " + connection.getClientState() + " " + packet.getClass().getSimpleName());
final ConnectionState state = connection.getConnectionState(); final ConnectionState state = connection.getClientState();
switch (state) { switch (state) {
case HANDSHAKE, STATUS, LOGIN, CONFIGURATION -> packetListenerManager.processClientPacket(connection.getConnectionState(), packet, connection); case HANDSHAKE, STATUS, LOGIN, CONFIGURATION -> packetListenerManager.processClientPacket(connection.getClientState(), packet, connection);
case PLAY -> { case PLAY -> {
final Player player = connection.getPlayer(); final Player player = connection.getPlayer();
assert player != null; assert player != null;

View File

@ -118,6 +118,7 @@ public sealed class ClientPacketsHandler permits ClientPacketsHandler.Status, Cl
register(nextId(), ClientVehicleMovePacket::new); register(nextId(), ClientVehicleMovePacket::new);
register(nextId(), ClientSteerBoatPacket::new); register(nextId(), ClientSteerBoatPacket::new);
register(nextId(), ClientPickItemPacket::new); register(nextId(), ClientPickItemPacket::new);
nextId(); // Ping request
register(nextId(), ClientCraftRecipeRequest::new); register(nextId(), ClientCraftRecipeRequest::new);
register(nextId(), ClientPlayerAbilitiesPacket::new); register(nextId(), ClientPlayerAbilitiesPacket::new);
register(nextId(), ClientPlayerDiggingPacket::new); register(nextId(), ClientPlayerDiggingPacket::new);

View File

@ -24,7 +24,6 @@ public final class ServerPacketIdentifier {
public static final int BUNDLE = nextPlayId(); public static final int BUNDLE = nextPlayId();
public static final int SPAWN_ENTITY = nextPlayId(); public static final int SPAWN_ENTITY = nextPlayId();
public static final int SPAWN_EXPERIENCE_ORB = nextPlayId(); public static final int SPAWN_EXPERIENCE_ORB = nextPlayId();
public static final int SPAWN_PLAYER = nextPlayId();
public static final int ENTITY_ANIMATION = nextPlayId(); public static final int ENTITY_ANIMATION = nextPlayId();
public static final int STATISTICS = nextPlayId(); public static final int STATISTICS = nextPlayId();
public static final int ACKNOWLEDGE_BLOCK_CHANGE = nextPlayId(); public static final int ACKNOWLEDGE_BLOCK_CHANGE = nextPlayId();

View File

@ -17,6 +17,7 @@ import static net.minestom.server.network.NetworkBuffer.*;
public record JoinGamePacket( public record JoinGamePacket(
int entityId, boolean isHardcore, List<String> worlds, int maxPlayers, int entityId, boolean isHardcore, List<String> worlds, int maxPlayers,
int viewDistance, int simulationDistance, boolean reducedDebugInfo, boolean enableRespawnScreen, int viewDistance, int simulationDistance, boolean reducedDebugInfo, boolean enableRespawnScreen,
boolean doLimitedCrafting,
String dimensionType, String world, long hashedSeed, GameMode gameMode, GameMode previousGameMode, String dimensionType, String world, long hashedSeed, GameMode gameMode, GameMode previousGameMode,
boolean isDebug, boolean isFlat, DeathLocation deathLocation, int portalCooldown boolean isDebug, boolean isFlat, DeathLocation deathLocation, int portalCooldown
) implements ServerPacket { ) implements ServerPacket {
@ -34,6 +35,7 @@ public record JoinGamePacket(
reader.read(VAR_INT), reader.read(VAR_INT),
reader.read(BOOLEAN), reader.read(BOOLEAN),
reader.read(BOOLEAN), reader.read(BOOLEAN),
reader.read(BOOLEAN),
reader.read(STRING), reader.read(STRING),
reader.read(STRING), reader.read(STRING),
@ -57,6 +59,7 @@ public record JoinGamePacket(
writer.write(VAR_INT, simulationDistance); writer.write(VAR_INT, simulationDistance);
writer.write(BOOLEAN, reducedDebugInfo); writer.write(BOOLEAN, reducedDebugInfo);
writer.write(BOOLEAN, enableRespawnScreen); writer.write(BOOLEAN, enableRespawnScreen);
writer.write(BOOLEAN, doLimitedCrafting);
writer.write(STRING, dimensionType); writer.write(STRING, dimensionType);
writer.write(STRING, world); writer.write(STRING, world);

View File

@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull;
import static net.minestom.server.network.NetworkBuffer.BYTE; import static net.minestom.server.network.NetworkBuffer.BYTE;
import static net.minestom.server.network.NetworkBuffer.FLOAT; import static net.minestom.server.network.NetworkBuffer.FLOAT;
public record PlayerAbilitiesPacket(byte flags, float flyingSpeed, float fieldViewModifier) implements ServerPacket { public record PlayerAbilitiesPacket(byte flags, float flyingSpeed, float walkingSpeed) implements ServerPacket {
public static final byte FLAG_INVULNERABLE = 0x01; public static final byte FLAG_INVULNERABLE = 0x01;
public static final byte FLAG_FLYING = 0x02; public static final byte FLAG_FLYING = 0x02;
public static final byte FLAG_ALLOW_FLYING = 0x04; public static final byte FLAG_ALLOW_FLYING = 0x04;
@ -23,7 +23,7 @@ public record PlayerAbilitiesPacket(byte flags, float flyingSpeed, float fieldVi
public void write(@NotNull NetworkBuffer writer) { public void write(@NotNull NetworkBuffer writer) {
writer.write(BYTE, flags); writer.write(BYTE, flags);
writer.write(FLOAT, flyingSpeed); writer.write(FLOAT, flyingSpeed);
writer.write(FLOAT, fieldViewModifier); writer.write(FLOAT, walkingSpeed);
} }
@Override @Override

View File

@ -32,6 +32,6 @@ public record SpawnPlayerPacket(int entityId, @NotNull UUID playerUuid,
@Override @Override
public int getId(@NotNull ConnectionState state) { public int getId(@NotNull ConnectionState state) {
return ServerPacketIdentifier.SPAWN_PLAYER; throw new UnsupportedOperationException("SpawnPlayerPacket is not supported in " + state);
} }
} }

View File

@ -16,7 +16,7 @@ public class FakePlayerConnection extends PlayerConnection {
@Override @Override
public void sendPacket(@NotNull SendablePacket packet) { public void sendPacket(@NotNull SendablePacket packet) {
FakePlayerController controller = getFakePlayer().getController(); FakePlayerController controller = getFakePlayer().getController();
final ServerPacket serverPacket = SendablePacket.extractServerPacket(getConnectionState(), packet); final ServerPacket serverPacket = SendablePacket.extractServerPacket(getServerState(), packet);
controller.consumePacket(serverPacket); controller.consumePacket(serverPacket);
} }

View File

@ -20,13 +20,15 @@ import java.util.List;
*/ */
public abstract class PlayerConnection { public abstract class PlayerConnection {
private Player player; private Player player;
private volatile ConnectionState connectionState; private volatile ConnectionState clientState;
private volatile ConnectionState serverState;
private PlayerPublicKey playerPublicKey; private PlayerPublicKey playerPublicKey;
volatile boolean online; volatile boolean online;
public PlayerConnection() { public PlayerConnection() {
this.online = true; this.online = true;
this.connectionState = ConnectionState.HANDSHAKE; this.clientState = ConnectionState.HANDSHAKE;
this.serverState = ConnectionState.HANDSHAKE;
} }
/** /**
@ -139,8 +141,14 @@ public abstract class PlayerConnection {
return online; return online;
} }
public void setConnectionState(@NotNull ConnectionState connectionState) { @ApiStatus.Internal
this.connectionState = connectionState; public void setClientState(@NotNull ConnectionState state) {
this.clientState = state;
}
@ApiStatus.Internal
public void setServerState(@NotNull ConnectionState state) {
this.serverState = state;
} }
/** /**
@ -148,8 +156,17 @@ public abstract class PlayerConnection {
* *
* @return the client connection state * @return the client connection state
*/ */
public @NotNull ConnectionState getConnectionState() { public @NotNull ConnectionState getClientState() {
return connectionState; return clientState;
}
/**
* Gets the server connection state.
*
* @return the server connection state
*/
public @NotNull ConnectionState getServerState() {
return clientState;
} }
public PlayerPublicKey playerPublicKey() { public PlayerPublicKey playerPublicKey() {
@ -163,7 +180,8 @@ public abstract class PlayerConnection {
@Override @Override
public String toString() { public String toString() {
return "PlayerConnection{" + return "PlayerConnection{" +
"connectionState=" + connectionState + "clientState=" + clientState +
", serverState=" + serverState +
", identifier=" + getIdentifier() + ", identifier=" + getIdentifier() +
'}'; '}';
} }

View File

@ -13,6 +13,9 @@ import net.minestom.server.network.packet.client.ClientPacket;
import net.minestom.server.network.packet.client.handshake.ClientHandshakePacket; import net.minestom.server.network.packet.client.handshake.ClientHandshakePacket;
import net.minestom.server.network.packet.server.*; import net.minestom.server.network.packet.server.*;
import net.minestom.server.network.packet.server.login.SetCompressionPacket; import net.minestom.server.network.packet.server.login.SetCompressionPacket;
import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.network.packet.server.play.TimeUpdatePacket;
import net.minestom.server.network.packet.server.play.UpdateLightPacket;
import net.minestom.server.network.socket.Worker; import net.minestom.server.network.socket.Worker;
import net.minestom.server.utils.ObjectPool; import net.minestom.server.utils.ObjectPool;
import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.PacketUtils;
@ -112,7 +115,7 @@ public class PlayerSocketConnection extends PlayerConnection {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);
} finally { } finally {
if (payload.position() != payload.limit()) { if (payload.position() != payload.limit()) {
LOGGER.warn("WARNING: Packet ({}) 0x{} not fully read ({}) {}", getConnectionState(), Integer.toHexString(id), payload, packet); LOGGER.warn("WARNING: Packet ({}) 0x{} not fully read ({}) {}", getClientState(), Integer.toHexString(id), payload, packet);
} }
} }
}); });
@ -290,14 +293,14 @@ public class PlayerSocketConnection extends PlayerConnection {
/** /**
* Adds an entry to the plugin request map. * Adds an entry to the plugin request map.
* <p> * <p>
* Only working if {@link #getConnectionState()} is {@link net.minestom.server.network.ConnectionState#LOGIN}. * Only working if {@link #getServerState()} ()} is {@link net.minestom.server.network.ConnectionState#LOGIN}.
* *
* @param messageId the message id * @param messageId the message id
* @param channel the packet channel * @param channel the packet channel
* @throws IllegalStateException if a messageId with the value {@code messageId} already exists for this connection * @throws IllegalStateException if a messageId with the value {@code messageId} already exists for this connection
*/ */
public void addPluginRequestEntry(int messageId, @NotNull String channel) { public void addPluginRequestEntry(int messageId, @NotNull String channel) {
if (!getConnectionState().equals(ConnectionState.LOGIN)) { if (getServerState() != ConnectionState.LOGIN) {
return; return;
} }
Check.stateCondition(pluginRequestMap.containsKey(messageId), "You cannot have two messageId with the same value"); Check.stateCondition(pluginRequestMap.containsKey(messageId), "You cannot have two messageId with the same value");
@ -317,10 +320,10 @@ public class PlayerSocketConnection extends PlayerConnection {
} }
@Override @Override
public void setConnectionState(@NotNull ConnectionState connectionState) { public void setClientState(@NotNull ConnectionState state) {
super.setConnectionState(connectionState); super.setClientState(state);
// Clear the plugin request map (since it is not used anymore) // Clear the plugin request map (since it is not used anymore)
if (connectionState.equals(ConnectionState.PLAY)) { if (state == ConnectionState.PLAY) {
this.pluginRequestMap.clear(); this.pluginRequestMap.clear();
} }
} }
@ -338,7 +341,7 @@ public class PlayerSocketConnection extends PlayerConnection {
final Player player = getPlayer(); final Player player = getPlayer();
// Outgoing event // Outgoing event
if (player != null && outgoing.hasListener()) { if (player != null && outgoing.hasListener()) {
final ServerPacket serverPacket = SendablePacket.extractServerPacket(getConnectionState(), packet); final ServerPacket serverPacket = SendablePacket.extractServerPacket(getServerState(), packet);
PlayerPacketOutEvent event = new PlayerPacketOutEvent(player, serverPacket); PlayerPacketOutEvent event = new PlayerPacketOutEvent(player, serverPacket);
outgoing.call(event); outgoing.call(event);
if (event.isCancelled()) return; if (event.isCancelled()) return;
@ -348,15 +351,18 @@ public class PlayerSocketConnection extends PlayerConnection {
writeServerPacketSync(serverPacket, compressed); writeServerPacketSync(serverPacket, compressed);
} else if (packet instanceof FramedPacket framedPacket) { } else if (packet instanceof FramedPacket framedPacket) {
var buffer = framedPacket.body(); var buffer = framedPacket.body();
System.out.println("SEND " + getConnectionState() + " " + "UNKNOWN FRAMED"); System.out.println("SEND " + getServerState() + " " + "UNKNOWN FRAMED");
writeBufferSync(buffer, 0, buffer.limit()); writeBufferSync(buffer, 0, buffer.limit());
} else if (packet instanceof CachedPacket cachedPacket) { } else if (packet instanceof CachedPacket cachedPacket) {
var buffer = cachedPacket.body(getConnectionState()); var buffer = cachedPacket.body(getServerState());
if (buffer != null) { if (buffer != null) {
System.out.println("SEND " + getConnectionState() + " " + cachedPacket.packet(getConnectionState()).getClass().getSimpleName()); var packetClass = cachedPacket.packet(getServerState()).getClass();
if (packetClass != TimeUpdatePacket.class && packetClass != UpdateLightPacket.class && packetClass != ChunkDataPacket.class) {
System.out.println("SEND " + getServerState() + " " + cachedPacket.packet(getServerState()).getClass().getSimpleName());
}
writeBufferSync(buffer, buffer.position(), buffer.remaining()); writeBufferSync(buffer, buffer.position(), buffer.remaining());
} }
else writeServerPacketSync(cachedPacket.packet(getConnectionState()), compressed); else writeServerPacketSync(cachedPacket.packet(getServerState()), compressed);
} else if (packet instanceof LazyPacket lazyPacket) { } else if (packet instanceof LazyPacket lazyPacket) {
writeServerPacketSync(lazyPacket.packet(), compressed); writeServerPacketSync(lazyPacket.packet(), compressed);
} else { } else {
@ -373,8 +379,8 @@ public class PlayerSocketConnection extends PlayerConnection {
} }
} }
try (var hold = ObjectPool.PACKET_POOL.hold()) { try (var hold = ObjectPool.PACKET_POOL.hold()) {
var buffer = PacketUtils.createFramedPacket(getConnectionState(), hold.get(), serverPacket, compressed); var buffer = PacketUtils.createFramedPacket(getServerState(), hold.get(), serverPacket, compressed);
System.out.println("SEND " + getConnectionState() + " " + serverPacket.getClass().getSimpleName() + " " + buffer.limit()); System.out.println("SEND " + getServerState() + " cls=" + serverPacket.getClass().getSimpleName() + " id=" + serverPacket.getId(getServerState()));
writeBufferSync(buffer, 0, buffer.limit()); writeBufferSync(buffer, 0, buffer.limit());
} }
} }

View File

@ -324,7 +324,7 @@ public final class PacketUtils {
private synchronized void append(Viewable viewable, ServerPacket serverPacket, Player player) { private synchronized void append(Viewable viewable, ServerPacket serverPacket, Player player) {
try (var hold = ObjectPool.PACKET_POOL.hold()) { try (var hold = ObjectPool.PACKET_POOL.hold()) {
final ByteBuffer framedPacket = createFramedPacket(player.getPlayerConnection().getConnectionState(), hold.get(), serverPacket); final ByteBuffer framedPacket = createFramedPacket(player.getPlayerConnection().getServerState(), hold.get(), serverPacket);
final int packetSize = framedPacket.limit(); final int packetSize = framedPacket.limit();
if (packetSize >= buffer.capacity()) { if (packetSize >= buffer.capacity()) {
process(viewable); process(viewable);