diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 8c500c441..84ea817c8 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -54,6 +54,7 @@ import net.minestom.server.utils.callback.OptionalCallback; import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.instance.InstanceUtils; +import net.minestom.server.utils.player.PlayerUtils; import net.minestom.server.utils.time.CooldownUtils; import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.UpdateOption; @@ -295,6 +296,13 @@ public class Player extends LivingEntity implements CommandSender { @Override public void update(long time) { + + // Flush all pending packets + if (PlayerUtils.isNettyClient(this)) { + ((NettyPlayerConnection) playerConnection).getChannel().flush(); + } + + // Network tick verification playerConnection.updateStats(); // Process received packets diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index 133585431..1d421bc58 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -82,10 +82,17 @@ public class NettyPlayerConnection extends PlayerConnection { channel.pipeline().addAfter("framer", "compressor", new PacketCompressor(threshold)); } + /** + * Writes a packet to the connection channel. + *
+ * All packets are flushed during {@link net.minestom.server.entity.Player#update(long)}. + * + * @param serverPacket the packet to write + */ @Override public void sendPacket(@NotNull ServerPacket serverPacket) { if (shouldSendPacket(serverPacket)) { - channel.writeAndFlush(serverPacket); + channel.write(serverPacket); } } diff --git a/src/test/java/demo/PlayerInit.java b/src/test/java/demo/PlayerInit.java index be19dd419..afb05045c 100644 --- a/src/test/java/demo/PlayerInit.java +++ b/src/test/java/demo/PlayerInit.java @@ -30,6 +30,7 @@ import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.world.DimensionType; import java.util.UUID; +import java.util.concurrent.ThreadLocalRandom; public class PlayerInit { @@ -43,7 +44,7 @@ public class PlayerInit { NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator(); instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD); instanceContainer.enableAutoChunkLoad(true); - instanceContainer.setChunkGenerator(noiseTestGenerator); + instanceContainer.setChunkGenerator(chunkGeneratorDemo); // Load some chunks beforehand final int loopStart = -3; @@ -162,6 +163,8 @@ public class PlayerInit { player.addEventCallback(PlayerLoginEvent.class, event -> { event.setSpawningInstance(instanceContainer); + int x = ThreadLocalRandom.current().nextInt()%10000; + player.setRespawnPoint(new Position(x, 64f, 0)); /*player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> { if (slot == -999) @@ -174,9 +177,6 @@ public class PlayerInit { player.addEventCallback(PlayerSpawnEvent.class, event -> { player.setGameMode(GameMode.SURVIVAL); - if (event.isFirstSpawn()) { - player.teleport(new Position(0, 64f, 0)); - } ItemStack itemStack = new ItemStack(Material.DIAMOND_BLOCK, (byte) 64); NbtDataImpl data = new NbtDataImpl();