mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-15 20:51:34 +01:00
Optimized PacketCompressor
This commit is contained in:
parent
310733b747
commit
540405e796
@ -420,11 +420,6 @@ public class Player extends LivingEntity implements CommandSender {
|
||||
entityPositionAndRotationPacket.pitch = position.getPitch();
|
||||
entityPositionAndRotationPacket.onGround = onGround;
|
||||
|
||||
lastX = position.getX();
|
||||
lastY = position.getY();
|
||||
lastZ = position.getZ();
|
||||
lastYaw = position.getYaw();
|
||||
lastPitch = position.getPitch();
|
||||
updatePacket = entityPositionAndRotationPacket;
|
||||
} else if (positionChanged) {
|
||||
EntityPositionPacket entityPositionPacket = new EntityPositionPacket();
|
||||
@ -433,9 +428,7 @@ public class Player extends LivingEntity implements CommandSender {
|
||||
entityPositionPacket.deltaY = (short) ((position.getY() * 32 - lastY * 32) * 128);
|
||||
entityPositionPacket.deltaZ = (short) ((position.getZ() * 32 - lastZ * 32) * 128);
|
||||
entityPositionPacket.onGround = onGround;
|
||||
lastX = position.getX();
|
||||
lastY = position.getY();
|
||||
lastZ = position.getZ();
|
||||
|
||||
updatePacket = entityPositionPacket;
|
||||
} else {
|
||||
// View changed
|
||||
@ -445,8 +438,6 @@ public class Player extends LivingEntity implements CommandSender {
|
||||
entityRotationPacket.pitch = position.getPitch();
|
||||
entityRotationPacket.onGround = onGround;
|
||||
|
||||
lastYaw = position.getYaw();
|
||||
lastPitch = position.getPitch();
|
||||
updatePacket = entityRotationPacket;
|
||||
}
|
||||
|
||||
@ -473,6 +464,16 @@ public class Player extends LivingEntity implements CommandSender {
|
||||
}
|
||||
}
|
||||
|
||||
if (positionChanged) {
|
||||
lastX = position.getX();
|
||||
lastY = position.getY();
|
||||
lastZ = position.getZ();
|
||||
}
|
||||
if (viewChanged) {
|
||||
lastYaw = position.getYaw();
|
||||
lastPitch = position.getPitch();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -335,7 +335,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
||||
/**
|
||||
* Refreshes an inventory slot.
|
||||
*
|
||||
* @param slot the packet slot
|
||||
* @param slot the packet slot,
|
||||
* see {@link net.minestom.server.utils.inventory.PlayerInventoryUtils#convertToPacketSlot(int)}
|
||||
* @param itemStack the item stack in the slot
|
||||
*/
|
||||
|
@ -8,6 +8,7 @@ import net.minestom.server.network.packet.client.play.ClientPlayerPositionPacket
|
||||
import net.minestom.server.network.packet.client.play.ClientPlayerRotationPacket;
|
||||
import net.minestom.server.utils.Position;
|
||||
import net.minestom.server.utils.chunk.ChunkUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PlayerPositionListener {
|
||||
|
||||
@ -47,7 +48,7 @@ public class PlayerPositionListener {
|
||||
processMovement(player, x, y, z, yaw, pitch, onGround);
|
||||
}
|
||||
|
||||
private static void processMovement(Player player, float x, float y, float z,
|
||||
private static void processMovement(@NotNull Player player, float x, float y, float z,
|
||||
float yaw, float pitch, boolean onGround) {
|
||||
|
||||
// Try to move in an unloaded chunk, prevent it
|
||||
|
@ -27,45 +27,38 @@ import java.util.List;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.Inflater;
|
||||
|
||||
// TODO Optimize
|
||||
public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
||||
|
||||
private final byte[] buffer = new byte[8192];
|
||||
|
||||
private final int threshold;
|
||||
|
||||
private final Inflater inflater;
|
||||
private final Deflater deflater;
|
||||
private byte[] buffer = new byte[8192];
|
||||
|
||||
private Deflater deflater = new Deflater();
|
||||
private Inflater inflater = new Inflater();
|
||||
|
||||
public PacketCompressor(int threshold) {
|
||||
this.inflater = new Inflater();
|
||||
this.deflater = new Deflater();
|
||||
|
||||
this.threshold = threshold;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf from, ByteBuf to) {
|
||||
int i = from.readableBytes();
|
||||
final int packetLength = from.readableBytes();
|
||||
|
||||
if (i < this.threshold) {
|
||||
if (packetLength < this.threshold) {
|
||||
Utils.writeVarIntBuf(to, 0);
|
||||
to.writeBytes(from);
|
||||
} else {
|
||||
byte[] abyte = new byte[i];
|
||||
from.readBytes(abyte);
|
||||
Utils.writeVarIntBuf(to, packetLength);
|
||||
|
||||
Utils.writeVarIntBuf(to, abyte.length);
|
||||
this.deflater.setInput(abyte, 0, i);
|
||||
this.deflater.finish();
|
||||
deflater.setInput(from.nioBuffer());
|
||||
deflater.finish();
|
||||
|
||||
while (!this.deflater.finished()) {
|
||||
int j = this.deflater.deflate(this.buffer);
|
||||
|
||||
to.writeBytes(this.buffer, 0, j);
|
||||
while (!deflater.finished()) {
|
||||
final int length = deflater.deflate(buffer);
|
||||
to.writeBytes(buffer, 0, length);
|
||||
}
|
||||
|
||||
this.deflater.reset();
|
||||
deflater.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@ -85,16 +78,18 @@ public class PacketCompressor extends ByteToMessageCodec<ByteBuf> {
|
||||
throw new DecoderException("Badly compressed packet - size of " + i + " is larger than protocol maximum of 2097152");
|
||||
}
|
||||
|
||||
// TODO optimize to do not initialize arrays each time
|
||||
|
||||
byte[] abyte = new byte[buf.readableBytes()];
|
||||
buf.readBytes(abyte);
|
||||
|
||||
this.inflater.setInput(abyte);
|
||||
inflater.setInput(abyte);
|
||||
byte[] abyte1 = new byte[i];
|
||||
|
||||
this.inflater.inflate(abyte1);
|
||||
inflater.inflate(abyte1);
|
||||
out.add(Unpooled.wrappedBuffer(abyte1));
|
||||
|
||||
this.inflater.reset();
|
||||
inflater.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user