--- a/net/minecraft/server/level/ServerEntity.java
+++ b/net/minecraft/server/level/ServerEntity.java
@@ -65,13 +_,15 @@
     private Vec3 lastSentMovement;
     private int tickCount;
     private int teleportDelay;
-    private List<Entity> lastPassengers = Collections.emptyList();
+    private List<Entity> lastPassengers = com.google.common.collect.ImmutableList.of(); // Paper - optimize passenger checks
     private boolean wasRiding;
     private boolean wasOnGround;
     @Nullable
     private List<SynchedEntityData.DataValue<?>> trackedDataValues;
+    private final Set<net.minecraft.server.network.ServerPlayerConnection> trackedPlayers; // Paper
 
-    public ServerEntity(ServerLevel level, Entity entity, int updateInterval, boolean trackDelta, ServerEntity.Synchronizer synchronizer) {
+    public ServerEntity(ServerLevel level, Entity entity, int updateInterval, boolean trackDelta, ServerEntity.Synchronizer synchronizer, final Set<net.minecraft.server.network.ServerPlayerConnection> trackedPlayers) { // Paper
+        this.trackedPlayers = trackedPlayers; // Paper
         this.level = level;
         this.synchronizer = synchronizer;
         this.entity = entity;
@@ -86,7 +_,19 @@
         this.trackedDataValues = entity.getEntityData().getNonDefaultValues();
     }
 
+    // Paper start - fix desync when a player is added to the tracker
+    private boolean forceStateResync;
+    public void onPlayerAdd() {
+        this.forceStateResync = true;
+    }
+    // Paper end - fix desync when a player is added to the tracker
+
     public void sendChanges() {
+        // Paper start - optimise collisions
+        if (((ca.spottedleaf.moonrise.patches.chunk_system.entity.ChunkSystemEntity)this.entity).moonrise$isHardColliding()) {
+            this.teleportDelay = 9999;
+        }
+        // Paper end - optimise collisions
         List<Entity> passengers = this.entity.getPassengers();
         if (!passengers.equals(this.lastPassengers)) {
             this.synchronizer
@@ -94,16 +_,22 @@
                     new ClientboundSetPassengersPacket(this.entity),
                     serverPlayer1 -> passengers.contains(serverPlayer1) == this.lastPassengers.contains(serverPlayer1)
                 );
+            // Paper start - Allow riding players
+            if (this.entity instanceof ServerPlayer player) {
+                player.connection.send(new ClientboundSetPassengersPacket(this.entity));
+            }
+            // Paper end - Allow riding players
             this.lastPassengers = passengers;
         }
 
-        if (this.entity instanceof ItemFrame itemFrame && this.tickCount % 10 == 0) {
+        if (!this.trackedPlayers.isEmpty() && this.entity instanceof ItemFrame itemFrame /*&& this.tickCount % 10 == 0*/) { // CraftBukkit - moved tickCount below // Paper - Perf: Only tick item frames if players can see it
             ItemStack item = itemFrame.getItem();
-            if (item.getItem() instanceof MapItem) {
-                MapId mapId = item.get(DataComponents.MAP_ID);
+            if (this.level.paperConfig().maps.itemFrameCursorUpdateInterval > 0 && this.tickCount % this.level.paperConfig().maps.itemFrameCursorUpdateInterval == 0 && item.getItem() instanceof MapItem) { // CraftBukkit - Moved this.tickCounter % 10 logic here so item frames do not enter the other blocks // Paper - Make item frame map cursor update interval configurable
+                MapId mapId = itemFrame.cachedMapId; // Paper - Perf: Cache map ids on item frames
                 MapItemSavedData savedData = MapItem.getSavedData(mapId, this.level);
                 if (savedData != null) {
-                    for (ServerPlayer serverPlayer : this.level.players()) {
+                    for (final net.minecraft.server.network.ServerPlayerConnection connection : this.trackedPlayers) { // Paper
+                        final ServerPlayer serverPlayer = connection.getPlayer(); // Paper
                         savedData.tickCarriedBy(serverPlayer, item);
                         Packet<?> updatePacket = savedData.getUpdatePacket(mapId, serverPlayer);
                         if (updatePacket != null) {
@@ -116,7 +_,7 @@
             this.sendDirtyEntityData();
         }
 
-        if (this.tickCount % this.updateInterval == 0 || this.entity.hasImpulse || this.entity.getEntityData().isDirty()) {
+        if (this.forceStateResync || this.tickCount % this.updateInterval == 0 || this.entity.hasImpulse || this.entity.getEntityData().isDirty()) { // Paper - fix desync when a player is added to the tracker
             byte b = Mth.packDegrees(this.entity.getYRot());
             byte b1 = Mth.packDegrees(this.entity.getXRot());
             boolean flag = Math.abs(b - this.lastSentYRot) >= 1 || Math.abs(b1 - this.lastSentXRot) >= 1;
@@ -136,7 +_,13 @@
             } else {
                 this.teleportDelay++;
                 Vec3 vec3 = this.entity.trackingPosition();
-                boolean flag1 = this.positionCodec.delta(vec3).lengthSqr() >= 7.6293945E-6F;
+                // Paper start - reduce allocation of Vec3D here
+                Vec3 base = this.positionCodec.base;
+                double vec3_dx = vec3.x - base.x;
+                double vec3_dy = vec3.y - base.y;
+                double vec3_dz = vec3.z - base.z;
+                boolean flag1 = (vec3_dx * vec3_dx + vec3_dy * vec3_dy + vec3_dz * vec3_dz) >= 7.62939453125E-6D;
+                // Paper end - reduce allocation of Vec3D here
                 Packet<ClientGamePacketListener> packet = null;
                 boolean flag2 = flag1 || this.tickCount % 60 == 0;
                 boolean flag3 = false;
@@ -145,7 +_,7 @@
                 long l1 = this.positionCodec.encodeY(vec3);
                 long l2 = this.positionCodec.encodeZ(vec3);
                 boolean flag5 = l < -32768L || l > 32767L || l1 < -32768L || l1 > 32767L || l2 < -32768L || l2 > 32767L;
-                if (this.entity.getRequiresPrecisePosition()
+                if (this.forceStateResync || this.entity.getRequiresPrecisePosition() // Paper - fix desync when a player is added to the tracker
                     || flag5
                     || this.teleportDelay > 400
                     || this.wasRiding
@@ -214,10 +_,30 @@
             }
 
             this.entity.hasImpulse = false;
+            this.forceStateResync = false; // Paper - fix desync when a player is added to the tracker
         }
 
         this.tickCount++;
         if (this.entity.hurtMarked) {
+            // CraftBukkit start - Create PlayerVelocity event
+            boolean cancelled = false;
+
+            if (this.entity instanceof ServerPlayer) {
+                org.bukkit.entity.Player player = (org.bukkit.entity.Player) this.entity.getBukkitEntity();
+                org.bukkit.util.Vector velocity = player.getVelocity();
+
+                org.bukkit.event.player.PlayerVelocityEvent event = new org.bukkit.event.player.PlayerVelocityEvent(player, velocity.clone());
+                if (!event.callEvent()) {
+                    cancelled = true;
+                } else if (!velocity.equals(event.getVelocity())) {
+                    player.setVelocity(event.getVelocity());
+                }
+            }
+
+            if (cancelled) {
+                return;
+            }
+            // CraftBukkit end
             this.entity.hurtMarked = false;
             this.synchronizer.sendToTrackingPlayersAndSelf(new ClientboundSetEntityMotionPacket(this.entity));
         }
@@ -268,7 +_,10 @@
 
     public void sendPairingData(ServerPlayer player, Consumer<Packet<ClientGamePacketListener>> consumer) {
         if (this.entity.isRemoved()) {
-            LOGGER.warn("Fetching packet for removed entity {}", this.entity);
+            // CraftBukkit start - Remove useless error spam, just return
+            // LOGGER.warn("Fetching packet for removed entity {}", this.entity);
+            return;
+            // CraftBukkit end
         }
 
         Packet<ClientGamePacketListener> addEntityPacket = this.entity.getAddEntityPacket(this);
@@ -279,6 +_,11 @@
 
         if (this.entity instanceof LivingEntity livingEntity) {
             Collection<AttributeInstance> syncableAttributes = livingEntity.getAttributes().getSyncableAttributes();
+            // CraftBukkit start - If sending own attributes send scaled health instead of current maximum health
+            if (this.entity.getId() == player.getId()) {
+                ((ServerPlayer) this.entity).getBukkitEntity().injectScaledMaxHealth(syncableAttributes, false);
+            }
+            // CraftBukkit end
             if (!syncableAttributes.isEmpty()) {
                 consumer.accept(new ClientboundUpdateAttributesPacket(this.entity.getId(), syncableAttributes));
             }
@@ -295,8 +_,9 @@
             }
 
             if (!list.isEmpty()) {
-                consumer.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list));
+                consumer.accept(new ClientboundSetEquipmentPacket(this.entity.getId(), list, true)); // Paper - data sanitization
             }
+            ((LivingEntity) this.entity).detectEquipmentUpdates(); // CraftBukkit - SPIGOT-3789: sync again immediately after sending
         }
 
         if (!this.entity.getPassengers().isEmpty()) {
@@ -343,6 +_,11 @@
         if (this.entity instanceof LivingEntity) {
             Set<AttributeInstance> attributesToSync = ((LivingEntity)this.entity).getAttributes().getAttributesToSync();
             if (!attributesToSync.isEmpty()) {
+                // CraftBukkit start - Send scaled max health
+                if (this.entity instanceof ServerPlayer serverPlayer) {
+                    serverPlayer.getBukkitEntity().injectScaledMaxHealth(attributesToSync, false);
+                }
+                // CraftBukkit end
                 this.synchronizer.sendToTrackingPlayersAndSelf(new ClientboundUpdateAttributesPacket(this.entity.getId(), attributesToSync));
             }
 
