Only flush during player update

This commit is contained in:
themode 2020-11-16 12:03:22 +01:00
parent 540405e796
commit 798b5d4853
3 changed files with 20 additions and 5 deletions

View File

@ -54,6 +54,7 @@ import net.minestom.server.utils.callback.OptionalCallback;
import net.minestom.server.utils.chunk.ChunkCallback; import net.minestom.server.utils.chunk.ChunkCallback;
import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.chunk.ChunkUtils;
import net.minestom.server.utils.instance.InstanceUtils; 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.CooldownUtils;
import net.minestom.server.utils.time.TimeUnit; import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.utils.time.UpdateOption; import net.minestom.server.utils.time.UpdateOption;
@ -295,6 +296,13 @@ public class Player extends LivingEntity implements CommandSender {
@Override @Override
public void update(long time) { public void update(long time) {
// Flush all pending packets
if (PlayerUtils.isNettyClient(this)) {
((NettyPlayerConnection) playerConnection).getChannel().flush();
}
// Network tick verification
playerConnection.updateStats(); playerConnection.updateStats();
// Process received packets // Process received packets

View File

@ -82,10 +82,17 @@ public class NettyPlayerConnection extends PlayerConnection {
channel.pipeline().addAfter("framer", "compressor", new PacketCompressor(threshold)); channel.pipeline().addAfter("framer", "compressor", new PacketCompressor(threshold));
} }
/**
* Writes a packet to the connection channel.
* <p>
* All packets are flushed during {@link net.minestom.server.entity.Player#update(long)}.
*
* @param serverPacket the packet to write
*/
@Override @Override
public void sendPacket(@NotNull ServerPacket serverPacket) { public void sendPacket(@NotNull ServerPacket serverPacket) {
if (shouldSendPacket(serverPacket)) { if (shouldSendPacket(serverPacket)) {
channel.writeAndFlush(serverPacket); channel.write(serverPacket);
} }
} }

View File

@ -30,6 +30,7 @@ import net.minestom.server.utils.time.TimeUnit;
import net.minestom.server.world.DimensionType; import net.minestom.server.world.DimensionType;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public class PlayerInit { public class PlayerInit {
@ -43,7 +44,7 @@ public class PlayerInit {
NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator(); NoiseTestGenerator noiseTestGenerator = new NoiseTestGenerator();
instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD); instanceContainer = MinecraftServer.getInstanceManager().createInstanceContainer(DimensionType.OVERWORLD);
instanceContainer.enableAutoChunkLoad(true); instanceContainer.enableAutoChunkLoad(true);
instanceContainer.setChunkGenerator(noiseTestGenerator); instanceContainer.setChunkGenerator(chunkGeneratorDemo);
// Load some chunks beforehand // Load some chunks beforehand
final int loopStart = -3; final int loopStart = -3;
@ -162,6 +163,8 @@ public class PlayerInit {
player.addEventCallback(PlayerLoginEvent.class, event -> { player.addEventCallback(PlayerLoginEvent.class, event -> {
event.setSpawningInstance(instanceContainer); event.setSpawningInstance(instanceContainer);
int x = ThreadLocalRandom.current().nextInt()%10000;
player.setRespawnPoint(new Position(x, 64f, 0));
/*player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> { /*player.getInventory().addInventoryCondition((p, slot, clickType, inventoryConditionResult) -> {
if (slot == -999) if (slot == -999)
@ -174,9 +177,6 @@ public class PlayerInit {
player.addEventCallback(PlayerSpawnEvent.class, event -> { player.addEventCallback(PlayerSpawnEvent.class, event -> {
player.setGameMode(GameMode.SURVIVAL); player.setGameMode(GameMode.SURVIVAL);
if (event.isFirstSpawn()) {
player.teleport(new Position(0, 64f, 0));
}
ItemStack itemStack = new ItemStack(Material.DIAMOND_BLOCK, (byte) 64); ItemStack itemStack = new ItemStack(Material.DIAMOND_BLOCK, (byte) 64);
NbtDataImpl data = new NbtDataImpl(); NbtDataImpl data = new NbtDataImpl();