Fix ghost player, trim chunk/light packets

This commit is contained in:
TheMode 2021-08-04 21:02:59 +02:00
parent 538d641d4b
commit 120b58db6a
4 changed files with 7 additions and 12 deletions

View File

@ -133,8 +133,10 @@ public class DynamicChunk extends Chunk {
if (connection instanceof NettyPlayerConnection) { if (connection instanceof NettyPlayerConnection) {
final long lastChange = getLastChangeTime(); final long lastChange = getLastChangeTime();
if (lastChange > cachedPacketTime || (cachedChunkBuffer == null || cachedLightBuffer == null)) { if (lastChange > cachedPacketTime || (cachedChunkBuffer == null || cachedLightBuffer == null)) {
this.cachedChunkBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createChunkPacket()); final var tempChunk = PacketUtils.createFramedPacket(createChunkPacket());
this.cachedLightBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createLightPacket()); this.cachedChunkBuffer = ByteBuffer.allocate(tempChunk.position()).put(tempChunk.flip());
final var tempLight = PacketUtils.createFramedPacket(createLightPacket());
this.cachedLightBuffer = ByteBuffer.allocate(tempLight.position()).put(tempLight.flip());
this.cachedPacketTime = lastChange; this.cachedPacketTime = lastChange;
} }
NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) connection; NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) connection;

View File

@ -178,8 +178,7 @@ public class NettyPlayerConnection extends PlayerConnection {
*/ */
@Override @Override
public void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating) { public void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating) {
if (!channel.isConnected()) if (!channel.isConnected()) return;
return;
if (shouldSendPacket(serverPacket)) { if (shouldSendPacket(serverPacket)) {
final Player player = getPlayer(); final Player player = getPlayer();
if (player != null) { if (player != null) {
@ -207,7 +206,6 @@ public class NettyPlayerConnection extends PlayerConnection {
this.channel.write(tickBuffer.flip()); this.channel.write(tickBuffer.flip());
this.tickBuffer.clear().put(buffer); this.tickBuffer.clear().put(buffer);
} catch (IOException ex) { } catch (IOException ex) {
disconnect();
MinecraftServer.getExceptionManager().handleException(ex); MinecraftServer.getExceptionManager().handleException(ex);
} }
} }
@ -229,7 +227,6 @@ public class NettyPlayerConnection extends PlayerConnection {
this.tickBuffer.clear(); this.tickBuffer.clear();
PacketUtils.writeFramedPacket(tickBuffer, packet, compressed); PacketUtils.writeFramedPacket(tickBuffer, packet, compressed);
} catch (IOException ex) { } catch (IOException ex) {
disconnect();
MinecraftServer.getExceptionManager().handleException(ex); MinecraftServer.getExceptionManager().handleException(ex);
} }
} }
@ -255,7 +252,6 @@ public class NettyPlayerConnection extends PlayerConnection {
this.channel.write(tickBuffer.flip()); this.channel.write(tickBuffer.flip());
this.tickBuffer.clear(); this.tickBuffer.clear();
} catch (IOException e) { } catch (IOException e) {
disconnect();
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);
} }
} }

View File

@ -85,11 +85,9 @@ public final class Worker {
} }
public void disconnect(NettyPlayerConnection connection, SocketChannel channel) throws IOException { public void disconnect(NettyPlayerConnection connection, SocketChannel channel) throws IOException {
if (connectionMap.remove(channel) == null) return;
// Client close // Client close
connection.refreshOnline(false);
channel.close(); channel.close();
this.connectionMap.remove(channel);
// Remove the connection
connection.refreshOnline(false); connection.refreshOnline(false);
Player player = connection.getPlayer(); Player player = connection.getPlayer();
if (player != null) { if (player != null) {

View File

@ -102,7 +102,6 @@ public final class PacketUtils {
connection.sendPacket(packet); connection.sendPacket(packet);
} }
} }
finalBuffer.clear(); // Clear packet to be reused
} else { } else {
// Write the same packet for each individual players // Write the same packet for each individual players
for (Player player : players) { for (Player player : players) {
@ -179,6 +178,6 @@ public final class PacketUtils {
} }
public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) { public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) {
return createFramedPacket(BUFFER.get(), packet); return createFramedPacket(BUFFER.get().clear(), packet);
} }
} }