From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Sun, 14 Aug 2022 00:00:51 +0800 Subject: [PATCH] Dont send useless entity packets This patch is Powered by Purpur(https://github.com/PurpurMC/Purpur) diff --git a/src/main/java/net/minecraft/server/level/ServerEntity.java b/src/main/java/net/minecraft/server/level/ServerEntity.java index 3b144c820531122eb37d41be06c182b5f5dc0724..8eb4a062489b42b26076d5f3bdfbff368c1a199b 100644 --- a/src/main/java/net/minecraft/server/level/ServerEntity.java +++ b/src/main/java/net/minecraft/server/level/ServerEntity.java @@ -165,20 +165,29 @@ public class ServerEntity { boolean flag4 = k < -32768L || k > 32767L || l < -32768L || l > 32767L || i1 < -32768L || i1 > 32767L; if (!flag4 && this.teleportDelay <= 400 && !this.wasRiding && this.wasOnGround == this.entity.isOnGround() && !(io.papermc.paper.configuration.GlobalConfiguration.get().collisions.sendFullPosForHardCollidingEntities && this.entity.hardCollides())) { // Paper - send full pos for hard colliding entities to prevent collision problems due to desync - if ((!flag2 || !flag3) && !(this.entity instanceof AbstractArrow)) { - if (flag2) { - packet1 = new ClientboundMoveEntityPacket.Pos(this.entity.getId(), (short) ((int) k), (short) ((int) l), (short) ((int) i1), this.entity.isOnGround()); - } else if (flag3) { - packet1 = new ClientboundMoveEntityPacket.Rot(this.entity.getId(), (byte) i, (byte) j, this.entity.isOnGround()); + // Leaves start - Better checking + if (flag2 || flag3 || this.entity instanceof AbstractArrow) { + if ((!flag2 || !flag3) && !(this.entity instanceof AbstractArrow)) { + if (flag2) { + packet1 = new ClientboundMoveEntityPacket.Pos(this.entity.getId(), (short) ((int) k), (short) ((int) l), (short) ((int) i1), this.entity.isOnGround()); + } else if (flag3) { + packet1 = new ClientboundMoveEntityPacket.Rot(this.entity.getId(), (byte) i, (byte) j, this.entity.isOnGround()); + } + } else { + packet1 = new ClientboundMoveEntityPacket.PosRot(this.entity.getId(), (short) ((int) k), (short) ((int) l), (short) ((int) i1), (byte) i, (byte) j, this.entity.isOnGround()); } - } else { - packet1 = new ClientboundMoveEntityPacket.PosRot(this.entity.getId(), (short) ((int) k), (short) ((int) l), (short) ((int) i1), (byte) i, (byte) j, this.entity.isOnGround()); } + // Leaves end - Better checking } else { this.wasOnGround = this.entity.isOnGround(); this.teleportDelay = 0; packet1 = new ClientboundTeleportEntityPacket(this.entity); } + // Leaves start - dont send useless entity packets + if (top.leavesmc.leaves.LeavesConfig.dontSendUselessEntityPackets && isUselessPacket(packet1)) { + packet1 = null; + } + // Leaves end - dont send useless entity packets } if ((this.trackDelta || this.entity.hasImpulse || this.entity instanceof LivingEntity && ((LivingEntity) this.entity).isFallFlying()) && this.tickCount > 0) { @@ -245,6 +254,21 @@ public class ServerEntity { } + // Leaves start - dont send useless entity packets + private boolean isUselessPacket(Packet possibleUselessPacket) { + if (possibleUselessPacket instanceof ClientboundMoveEntityPacket packet) { + if (possibleUselessPacket instanceof ClientboundMoveEntityPacket.Pos) { + return packet.getXa() == 0 && packet.getYa() == 0 && packet.getZa() == 0; + } else if (possibleUselessPacket instanceof ClientboundMoveEntityPacket.PosRot) { + return packet.getXa() == 0 && packet.getYa() == 0 && packet.getZa() == 0 && packet.getyRot() == 0 && packet.getxRot() == 0; + } else if (possibleUselessPacket instanceof ClientboundMoveEntityPacket.Rot) { + return packet.getyRot() == 0 && packet.getxRot() == 0; + } + } + return false; + } + // Leaves end - dont send useless entity packets + public void removePairing(ServerPlayer player) { this.entity.stopSeenByPlayer(player); player.connection.send(new ClientboundRemoveEntitiesPacket(new int[]{this.entity.getId()})); diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java index 7430ba59d0d16c628a7c62100902e23b0470b419..26e23a21f7cbd8d5e9a2dbf4860adc03a04e6e0e 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -228,6 +228,11 @@ public final class LeavesConfig { asyncMobSpawning = getBoolean("settings.performance.async-mob-spawning", asyncMobSpawning); } + public static boolean dontSendUselessEntityPackets = true; + private static void dontSendUselessEntityPackets() { + dontSendUselessEntityPackets = getBoolean("settings.performance.dont-send-useless-entity-packets", dontSendUselessEntityPackets); + } + public static final class WorldConfig { public final String worldName;