diff --git a/src/main/java/net/minestom/server/entity/LivingEntity.java b/src/main/java/net/minestom/server/entity/LivingEntity.java index 9d558c289..8b2fe5d6a 100644 --- a/src/main/java/net/minestom/server/entity/LivingEntity.java +++ b/src/main/java/net/minestom/server/entity/LivingEntity.java @@ -453,7 +453,7 @@ public class LivingEntity extends Entity implements EquipmentHandler { if (this instanceof Player player) { PlayerConnection playerConnection = player.playerConnection; // 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)); if (self) { diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index ed1afe091..c15fffa8e 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -267,7 +267,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, final JoinGamePacket joinGamePacket = new JoinGamePacket( getEntityId(), false, List.of(), 0, MinecraftServer.getChunkViewDistance(), MinecraftServer.getChunkViewDistance(), - false, true, dimensionType.toString(), spawnInstance.getDimensionName(), + false, true, false, dimensionType.toString(), spawnInstance.getDimensionName(), 0, gameMode, null, false, levelFlat, deathLocation, portalCooldown); sendPacket(joinGamePacket); @@ -1496,7 +1496,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, * @param component the reason */ 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 final ServerPacket disconnectPacket; if (connectionState == ConnectionState.LOGIN) { @@ -1920,7 +1920,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, } final PacketListenerManager manager = MinecraftServer.getPacketListenerManager(); // 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) { this.latency = latency; - if (getPlayerConnection().getConnectionState() == ConnectionState.PLAY) { + if (getPlayerConnection().getServerState() == ConnectionState.PLAY) { PacketUtils.broadcastPacket(new PlayerInfoUpdatePacket(PlayerInfoUpdatePacket.Action.UPDATE_LATENCY, infoEntry())); } } @@ -2343,7 +2343,7 @@ public class Player extends LivingEntity implements CommandSender, Localizable, this.allowServerListings = allowServerListings; // TODO: Use the metadata object here - boolean isInPlayState = getPlayerConnection().getConnectionState() == ConnectionState.PLAY; + boolean isInPlayState = getPlayerConnection().getServerState() == ConnectionState.PLAY; if (isInPlayState) metadata.setNotifyAboutChanges(false); metadata.setIndex((byte) 17, Metadata.Byte(displayedSkinParts)); metadata.setIndex((byte) 18, Metadata.Byte((byte) (this.mainHand == MainHand.RIGHT ? 1 : 0))); diff --git a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java index bc34bb020..5aaaa10cf 100644 --- a/src/main/java/net/minestom/server/listener/BlockPlacementListener.java +++ b/src/main/java/net/minestom/server/listener/BlockPlacementListener.java @@ -163,9 +163,9 @@ public class BlockPlacementListener { // Place the block Block resultBlock = playerBlockPlaceEvent.getBlock(); - player.sendPacket(new AcknowledgeBlockChangePacket(packet.sequence())); instance.placeBlock(new BlockHandler.PlayerPlacement(resultBlock, instance, placementPosition, player, hand, blockFace, packet.cursorPositionX(), packet.cursorPositionY(), packet.cursorPositionZ()), playerBlockPlaceEvent.shouldDoBlockUpdates()); + player.sendPacket(new AcknowledgeBlockChangePacket(packet.sequence())); // Block consuming if (playerBlockPlaceEvent.doesConsumeBlock()) { // Consume the block in the player's hand diff --git a/src/main/java/net/minestom/server/listener/preplay/ConfigurationListener.java b/src/main/java/net/minestom/server/listener/preplay/ConfigurationListener.java index f663e5b2f..636073c0a 100644 --- a/src/main/java/net/minestom/server/listener/preplay/ConfigurationListener.java +++ b/src/main/java/net/minestom/server/listener/preplay/ConfigurationListener.java @@ -1,13 +1,23 @@ package net.minestom.server.listener.preplay; +import net.minestom.server.MinecraftServer; 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 org.jetbrains.annotations.NotNull; public final class ConfigurationListener { + private static final ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager(); + 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() ); + CONNECTION_MANAGER.startPlayState(player); + + //todo move to play state + } } diff --git a/src/main/java/net/minestom/server/listener/preplay/HandshakeListener.java b/src/main/java/net/minestom/server/listener/preplay/HandshakeListener.java index e57ace10f..5733ac327 100644 --- a/src/main/java/net/minestom/server/listener/preplay/HandshakeListener.java +++ b/src/main/java/net/minestom/server/listener/preplay/HandshakeListener.java @@ -111,10 +111,14 @@ public final class HandshakeListener { } switch (packet.nextState()) { - case 1 -> connection.setConnectionState(ConnectionState.STATUS); + case 1 -> { + connection.setClientState(ConnectionState.STATUS); + connection.setServerState(ConnectionState.STATUS); + } case 2 -> { if (packet.protocolVersion() == MinecraftServer.PROTOCOL_VERSION) { - connection.setConnectionState(ConnectionState.LOGIN); + connection.setClientState(ConnectionState.LOGIN); + connection.setServerState(ConnectionState.LOGIN); } else { // Incorrect client version disconnect(connection, INVALID_VERSION_TEXT); diff --git a/src/main/java/net/minestom/server/listener/preplay/LoginListener.java b/src/main/java/net/minestom/server/listener/preplay/LoginListener.java index dfe2b193c..887c15eda 100644 --- a/src/main/java/net/minestom/server/listener/preplay/LoginListener.java +++ b/src/main/java/net/minestom/server/listener/preplay/LoginListener.java @@ -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.ClientLoginPluginResponsePacket; 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.configuration.FinishConfigurationPacket; import net.minestom.server.network.packet.server.configuration.RegistryDataPacket; @@ -79,7 +80,6 @@ public final class LoginListener { return; } final PlayerSocketConnection socketConnection = (PlayerSocketConnection) connection; - socketConnection.setConnectionState(ConnectionState.LOGIN); final byte[] publicKey = MojangAuth.getKeyPair().getPublic().getEncoded(); byte[] nonce = new byte[4]; @@ -221,7 +221,7 @@ public final class LoginListener { } public static void loginAckListener(@NotNull ClientLoginAcknowledgedPacket packet, @NotNull PlayerConnection connection) { - connection.setConnectionState(ConnectionState.CONFIGURATION); + connection.setClientState(ConnectionState.CONFIGURATION); CONNECTION_MANAGER.registerPlayer(connection.getPlayer()); @@ -262,6 +262,7 @@ public final class LoginListener { AsyncUtils.runAsync(() -> { //todo event + connection.setServerState(ConnectionState.PLAY); connection.sendPacket(new FinishConfigurationPacket()); }); } diff --git a/src/main/java/net/minestom/server/network/ConnectionManager.java b/src/main/java/net/minestom/server/network/ConnectionManager.java index c917240ee..43a7eb131 100644 --- a/src/main/java/net/minestom/server/network/ConnectionManager.java +++ b/src/main/java/net/minestom/server/network/ConnectionManager.java @@ -220,12 +220,14 @@ public final class ConnectionManager { // Send login success packet LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(player.getUuid(), player.getUsername(), 0); playerConnection.sendPacket(loginSuccessPacket); -// playerConnection.setConnectionState(ConnectionState.CONFIGURATION); -// if (register) registerPlayer(player); // WHEN ENTERING CONFIGURATION THIS SHOULD BE SET -// this.waitingPlayers.relaxedOffer(player); + playerConnection.setServerState(ConnectionState.CONFIGURATION); }); } + public void startPlayState(@NotNull Player player) { + this.waitingPlayers.relaxedOffer(player); + } + /** * Calls the player initialization callbacks and the event {@link AsyncPlayerPreLoginEvent}. *
@@ -262,7 +264,7 @@ public final class ConnectionManager {
// Send login success packet
LoginSuccessPacket loginSuccessPacket = new LoginSuccessPacket(player.getUuid(), player.getUsername(), 0);
playerConnection.sendPacket(loginSuccessPacket);
- playerConnection.setConnectionState(ConnectionState.PLAY);
+// playerConnection.setConnectionState(ConnectionState.PLAY);
if (register) registerPlayer(player);
this.waitingPlayers.relaxedOffer(player);
});
@@ -300,13 +302,12 @@ public final class ConnectionManager {
EventDispatcher.call(loginEvent);
final Instance spawningInstance = loginEvent.getSpawningInstance();
Check.notNull(spawningInstance, "You need to specify a spawning instance in the PlayerLoginEvent");
+
// Spawn the player at Player#getRespawnPoint
- if (DebugUtils.INSIDE_TEST) {
- // Required to get the exact moment the player spawns
- waitingPlayer.UNSAFE_init(spawningInstance).join();
- } else {
- waitingPlayer.UNSAFE_init(spawningInstance);
- }
+ CompletableFuture
- * 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 channel the packet channel
* @throws IllegalStateException if a messageId with the value {@code messageId} already exists for this connection
*/
public void addPluginRequestEntry(int messageId, @NotNull String channel) {
- if (!getConnectionState().equals(ConnectionState.LOGIN)) {
+ if (getServerState() != ConnectionState.LOGIN) {
return;
}
Check.stateCondition(pluginRequestMap.containsKey(messageId), "You cannot have two messageId with the same value");
@@ -317,10 +320,10 @@ public class PlayerSocketConnection extends PlayerConnection {
}
@Override
- public void setConnectionState(@NotNull ConnectionState connectionState) {
- super.setConnectionState(connectionState);
+ public void setClientState(@NotNull ConnectionState state) {
+ super.setClientState(state);
// Clear the plugin request map (since it is not used anymore)
- if (connectionState.equals(ConnectionState.PLAY)) {
+ if (state == ConnectionState.PLAY) {
this.pluginRequestMap.clear();
}
}
@@ -338,7 +341,7 @@ public class PlayerSocketConnection extends PlayerConnection {
final Player player = getPlayer();
// Outgoing event
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);
outgoing.call(event);
if (event.isCancelled()) return;
@@ -348,15 +351,18 @@ public class PlayerSocketConnection extends PlayerConnection {
writeServerPacketSync(serverPacket, compressed);
} else if (packet instanceof FramedPacket framedPacket) {
var buffer = framedPacket.body();
- System.out.println("SEND " + getConnectionState() + " " + "UNKNOWN FRAMED");
+ System.out.println("SEND " + getServerState() + " " + "UNKNOWN FRAMED");
writeBufferSync(buffer, 0, buffer.limit());
} else if (packet instanceof CachedPacket cachedPacket) {
- var buffer = cachedPacket.body(getConnectionState());
+ var buffer = cachedPacket.body(getServerState());
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());
}
- else writeServerPacketSync(cachedPacket.packet(getConnectionState()), compressed);
+ else writeServerPacketSync(cachedPacket.packet(getServerState()), compressed);
} else if (packet instanceof LazyPacket lazyPacket) {
writeServerPacketSync(lazyPacket.packet(), compressed);
} else {
@@ -373,8 +379,8 @@ public class PlayerSocketConnection extends PlayerConnection {
}
}
try (var hold = ObjectPool.PACKET_POOL.hold()) {
- var buffer = PacketUtils.createFramedPacket(getConnectionState(), hold.get(), serverPacket, compressed);
- System.out.println("SEND " + getConnectionState() + " " + serverPacket.getClass().getSimpleName() + " " + buffer.limit());
+ var buffer = PacketUtils.createFramedPacket(getServerState(), hold.get(), serverPacket, compressed);
+ System.out.println("SEND " + getServerState() + " cls=" + serverPacket.getClass().getSimpleName() + " id=" + serverPacket.getId(getServerState()));
writeBufferSync(buffer, 0, buffer.limit());
}
}
diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java
index 426e6bcdb..b009d7de5 100644
--- a/src/main/java/net/minestom/server/utils/PacketUtils.java
+++ b/src/main/java/net/minestom/server/utils/PacketUtils.java
@@ -324,7 +324,7 @@ public final class PacketUtils {
private synchronized void append(Viewable viewable, ServerPacket serverPacket, Player player) {
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();
if (packetSize >= buffer.capacity()) {
process(viewable);