From 589079f8f677ade959b960146e0a6a406f9b5799 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Fri, 29 May 2020 15:31:11 +0200 Subject: [PATCH] Updated netty + fix network crash --- build.gradle | 6 +++--- src/main/java/net/minestom/server/UpdateManager.java | 2 +- src/main/java/net/minestom/server/entity/Player.java | 6 ++++-- .../listener/manager/PacketListenerManager.java | 6 ++++-- .../net/minestom/server/network/PacketProcessor.java | 2 +- .../minestom/server/network/PacketWriterUtils.java | 4 +++- .../server/network/netty/channel/ClientChannel.java | 11 +++-------- .../network/packet/client/ClientPlayPacket.java | 5 ++++- .../server/network/player/PlayerConnection.java | 10 ++++++++++ .../java/net/minestom/server/utils/MathUtils.java | 12 ++++++++++++ .../minestom/server/utils/buffer/BufferUtils.java | 4 ++-- 11 files changed, 47 insertions(+), 21 deletions(-) diff --git a/build.gradle b/build.gradle index 1d5f1f464..2ab08fc95 100644 --- a/build.gradle +++ b/build.gradle @@ -18,9 +18,9 @@ dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' // https://mvnrepository.com/artifact/io.netty/netty-all - api group: 'io.netty', name: 'netty-all', version: '4.1.48.Final' + api group: 'io.netty', name: 'netty-all', version: '4.1.50.Final' - api 'com.github.jhg023:Pbbl:1.0.1' + api 'com.github.jhg023:Pbbl:1.0.2' // https://mvnrepository.com/artifact/it.unimi.dsi/fastutil api group: 'it.unimi.dsi', name: 'fastutil', version: '8.3.0' @@ -28,7 +28,7 @@ dependencies { api 'com.github.Querz:NBT:4.1' // https://mvnrepository.com/artifact/com.google.code.gson/gson - api group: 'com.google.code.gson', name: 'gson', version: '2.8.5' + api group: 'com.google.code.gson', name: 'gson', version: '2.8.6' api 'com.github.TheMode:CommandBuilder:f893cfbfe4' diff --git a/src/main/java/net/minestom/server/UpdateManager.java b/src/main/java/net/minestom/server/UpdateManager.java index cd7c8a4e1..7b7bf7662 100644 --- a/src/main/java/net/minestom/server/UpdateManager.java +++ b/src/main/java/net/minestom/server/UpdateManager.java @@ -43,7 +43,7 @@ public class UpdateManager { player.refreshKeepAlive(time); player.getPlayerConnection().sendPacket(keepAlivePacket); } else if (lastKeepAlive >= KEEP_ALIVE_KICK) { - TextComponent textComponent = TextComponent.of("No Keep Alive answer") + TextComponent textComponent = TextComponent.of("Timeout") .color(TextColor.RED); player.kick(textComponent); } diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 09d79770c..148089abf 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -249,9 +249,9 @@ public class Player extends LivingEntity { /** * Used to initialize the player connection - * mostly used by {@link net.minestom.server.entity.fakeplayer.FakePlayer} */ protected void playerConnectionInit() { + this.playerConnection.setPlayer(this); } @Override @@ -282,9 +282,10 @@ public class Player extends LivingEntity { // Target block stage if (targetCustomBlock != null) { - final int animationCount = 10; + final byte animationCount = 10; long since = System.currentTimeMillis() - targetBlockTime; byte stage = (byte) (since / (blockBreakTime / animationCount) - 1); + stage = MathUtils.setBetween(stage, (byte) -1, animationCount); if (stage != targetLastStage) { sendBlockBreakAnimation(targetBlockPosition, stage); } @@ -468,6 +469,7 @@ public class Player extends LivingEntity { @Override public void remove() { super.remove(); + this.packets.clear(); clearBossBars(); if (getOpenInventory() != null) getOpenInventory().removeViewer(this); diff --git a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java index 99d624553..abc74f776 100644 --- a/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java +++ b/src/main/java/net/minestom/server/listener/manager/PacketListenerManager.java @@ -46,11 +46,13 @@ public class PacketListenerManager { public void process(T packet, Player player) { - PacketListenerConsumer packetListenerConsumer = listeners.get(packet.getClass()); + final Class clazz = packet.getClass(); + + PacketListenerConsumer packetListenerConsumer = listeners.get(clazz); // Listener can be null if none has been set before, call PacketConsumer anyway if (packetListenerConsumer == null) { - System.err.println("Packet " + packet.getClass() + " does not have any default listener!"); + System.err.println("Packet " + clazz + " does not have any default listener!"); } diff --git a/src/main/java/net/minestom/server/network/PacketProcessor.java b/src/main/java/net/minestom/server/network/PacketProcessor.java index e7b672eb7..149798afe 100644 --- a/src/main/java/net/minestom/server/network/PacketProcessor.java +++ b/src/main/java/net/minestom/server/network/PacketProcessor.java @@ -62,7 +62,7 @@ public class PacketProcessor { switch (connectionState) { case PLAY: - Player player = connectionManager.getPlayer(playerConnection); + Player player = playerConnection.getPlayer(); ClientPlayPacket playPacket = (ClientPlayPacket) playPacketsHandler.getPacketInstance(id); playPacket.read(packetReader); diff --git a/src/main/java/net/minestom/server/network/PacketWriterUtils.java b/src/main/java/net/minestom/server/network/PacketWriterUtils.java index 9558c801d..4897e8bfe 100644 --- a/src/main/java/net/minestom/server/network/PacketWriterUtils.java +++ b/src/main/java/net/minestom/server/network/PacketWriterUtils.java @@ -38,6 +38,7 @@ public class PacketWriterUtils { playerConnection.sendPacket(serverPacket); } } + buffer.release(); }); } @@ -45,7 +46,8 @@ public class PacketWriterUtils { batchesPool.execute(() -> { if (PlayerUtils.isNettyClient(playerConnection)) { ByteBuf buffer = PacketUtils.writePacket(serverPacket); - playerConnection.sendPacket(buffer); + playerConnection.writePacket(buffer); + buffer.release(); } else { playerConnection.sendPacket(serverPacket); } diff --git a/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java b/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java index dbb737790..9e7ebf2a0 100644 --- a/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java +++ b/src/main/java/net/minestom/server/network/netty/channel/ClientChannel.java @@ -28,8 +28,9 @@ public class ClientChannel extends ChannelInboundHandlerAdapter { } @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) { - PacketHandler packetHandler = (PacketHandler) msg; + public void channelRead(ChannelHandlerContext ctx, Object obj) { + PacketHandler packetHandler = (PacketHandler) obj; + int packetLength = packetHandler.length; ByteBuf buffer = packetHandler.buffer; @@ -49,11 +50,6 @@ public class ClientChannel extends ChannelInboundHandlerAdapter { buffer.release(); } - @Override - public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - super.channelReadComplete(ctx); - } - @Override public void channelInactive(ChannelHandlerContext ctx) { PlayerConnection playerConnection = packetProcessor.getPlayerConnection(ctx); @@ -62,7 +58,6 @@ public class ClientChannel extends ChannelInboundHandlerAdapter { Player player = connectionManager.getPlayer(playerConnection); if (player != null) { player.remove(); - connectionManager.removePlayer(playerConnection); } packetProcessor.removePlayerConnection(ctx); diff --git a/src/main/java/net/minestom/server/network/packet/client/ClientPlayPacket.java b/src/main/java/net/minestom/server/network/packet/client/ClientPlayPacket.java index 15f513969..2f5df038b 100644 --- a/src/main/java/net/minestom/server/network/packet/client/ClientPlayPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/ClientPlayPacket.java @@ -2,11 +2,14 @@ package net.minestom.server.network.packet.client; import net.minestom.server.MinecraftServer; import net.minestom.server.entity.Player; +import net.minestom.server.listener.manager.PacketListenerManager; public abstract class ClientPlayPacket implements ClientPacket { + private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager(); + public void process(Player player) { - MinecraftServer.getPacketListenerManager().process(this, player); + PACKET_LISTENER_MANAGER.process(this, player); } } diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java index 2f33ce6af..1392a2020 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java @@ -1,6 +1,7 @@ package net.minestom.server.network.player; import io.netty.buffer.ByteBuf; +import net.minestom.server.entity.Player; import net.minestom.server.network.ConnectionState; import net.minestom.server.network.packet.server.ServerPacket; @@ -12,6 +13,7 @@ import java.net.SocketAddress; */ public abstract class PlayerConnection { + private Player player; private ConnectionState connectionState; private boolean online; @@ -35,6 +37,14 @@ public abstract class PlayerConnection { */ public abstract void disconnect(); + public Player getPlayer() { + return player; + } + + public void setPlayer(Player player) { + this.player = player; + } + public boolean isOnline() { return online; } diff --git a/src/main/java/net/minestom/server/utils/MathUtils.java b/src/main/java/net/minestom/server/utils/MathUtils.java index af6f821e5..5116df902 100644 --- a/src/main/java/net/minestom/server/utils/MathUtils.java +++ b/src/main/java/net/minestom/server/utils/MathUtils.java @@ -47,4 +47,16 @@ public class MathUtils { return number >= min && number <= max; } + public static byte setBetween(byte number, byte min, byte max) { + return number > max ? max : number < min ? min : number; + } + + public static int setBetween(int number, int min, int max) { + return number > max ? max : number < min ? min : number; + } + + public static float setBetween(float number, float min, float max) { + return number > max ? max : number < min ? min : number; + } + } diff --git a/src/main/java/net/minestom/server/utils/buffer/BufferUtils.java b/src/main/java/net/minestom/server/utils/buffer/BufferUtils.java index be03d5476..7ffd61e2c 100644 --- a/src/main/java/net/minestom/server/utils/buffer/BufferUtils.java +++ b/src/main/java/net/minestom/server/utils/buffer/BufferUtils.java @@ -1,12 +1,12 @@ package net.minestom.server.utils.buffer; -import pbbl.heap.HeapByteBufferPool; +import com.github.pbbl.heap.ByteBufferPool; import java.nio.ByteBuffer; public class BufferUtils { - private static HeapByteBufferPool pool = new HeapByteBufferPool(); + private static ByteBufferPool pool = new ByteBufferPool(); public static BufferWrapper getBuffer(int size) { return new BufferWrapper(pool.take(size));