From df65fbea3eee8088b8f545dbb2debed023dc27d5 Mon Sep 17 00:00:00 2001
From: Yannick Lamprecht <yannicklamprecht@live.de>
Date: Wed, 30 Mar 2022 18:16:52 +0200
Subject: [PATCH] Player Entity Tracking Events

---
 .../server/level/ChunkMap.java.patch          |  24 ++--
 .../minecraft/world/entity/Entity.java.patch  | 116 ++++++++++--------
 2 files changed, 82 insertions(+), 58 deletions(-)

diff --git a/paper-server/patches/sources/net/minecraft/server/level/ChunkMap.java.patch b/paper-server/patches/sources/net/minecraft/server/level/ChunkMap.java.patch
index fd8783a42d..cd5739e8ab 100644
--- a/paper-server/patches/sources/net/minecraft/server/level/ChunkMap.java.patch
+++ b/paper-server/patches/sources/net/minecraft/server/level/ChunkMap.java.patch
@@ -67,20 +67,19 @@
          this.mainThreadExecutor = mainThreadExecutor;
          ConsecutiveExecutor consecutiveexecutor = new ConsecutiveExecutor(executor, "worldgen");
  
-@@ -196,7 +233,13 @@
- 
-     private void setChunkUnsaved(ChunkPos pos) {
+@@ -198,6 +235,12 @@
          this.chunksToEagerlySave.add(pos.toLong());
-+    }
-+
+     }
+ 
 +    // Paper start
 +    public int getMobCountNear(final ServerPlayer player, final net.minecraft.world.entity.MobCategory mobCategory) {
 +        return -1;
-     }
++    }
 +    // Paper end
- 
++
      protected ChunkGenerator generator() {
          return this.worldGenContext.generator();
+     }
 @@ -325,7 +368,7 @@
                          throw this.debugFuturesAndCreateReportedException(new IllegalStateException("At least one of the chunk futures were null"), "n/a");
                      }
@@ -387,7 +386,7 @@
              if (player != this.entity) {
                  Vec3 vec3d = player.position().subtract(this.entity.position());
                  int i = ChunkMap.this.getPlayerViewDistance(player);
-@@ -1484,6 +1573,11 @@
+@@ -1484,9 +1573,18 @@
                  double d2 = d0 * d0;
                  boolean flag = d1 <= d2 && this.entity.broadcastToPlayer(player) && ChunkMap.this.isChunkTracked(player, this.entity.chunkPosition().x, this.entity.chunkPosition().z);
  
@@ -398,8 +397,15 @@
 +                // CraftBukkit end
                  if (flag) {
                      if (this.seenBy.add(player.connection)) {
++                        // Paper start - entity tracking events
++                        if (io.papermc.paper.event.player.PlayerTrackEntityEvent.getHandlerList().getRegisteredListeners().length == 0 || new io.papermc.paper.event.player.PlayerTrackEntityEvent(player.getBukkitEntity(), this.entity.getBukkitEntity()).callEvent()) {
                          this.serverEntity.addPairing(player);
-@@ -1506,6 +1600,7 @@
++                        }
++                        // Paper end - entity tracking events
+                     }
+                 } else if (this.seenBy.remove(player.connection)) {
+                     this.serverEntity.removePairing(player);
+@@ -1506,6 +1604,7 @@
              while (iterator.hasNext()) {
                  Entity entity = (Entity) iterator.next();
                  int j = entity.getType().clientTrackingRange() * 16;
diff --git a/paper-server/patches/sources/net/minecraft/world/entity/Entity.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/Entity.java.patch
index b8937b9668..68b26aec2d 100644
--- a/paper-server/patches/sources/net/minecraft/world/entity/Entity.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/entity/Entity.java.patch
@@ -61,7 +61,7 @@
 +// CraftBukkit end
  
  public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess, ScoreHolder {
- 
++
 +    // CraftBukkit start
 +    private static final int CURRENT_LEVEL = 2;
 +    public boolean preserveMotion = true; // Paper - Fix Entity Teleportation and cancel velocity if teleported; keep initial motion on first setPositionRotation
@@ -93,7 +93,7 @@
 +        public net.minecraft.world.level.levelgen.PositionalRandomFactory forkPositional() {
 +            return new net.minecraft.world.level.levelgen.LegacyRandomSource.LegacyPositionalRandomFactory(this.nextLong());
 +        }
-+
+ 
 +        // these below are added to fix reobf issues that I don't wanna deal with right now
 +        @Override
 +        public int next(int bits) {
@@ -269,7 +269,7 @@
          datawatcher_a.define(Entity.DATA_TICKS_FROZEN, 0);
          this.defineSynchedData(datawatcher_a);
          this.entityData = datawatcher_a.build();
-@@ -362,20 +561,36 @@
+@@ -362,12 +561,18 @@
      }
  
      public void kill(ServerLevel world) {
@@ -282,16 +282,15 @@
 -        this.remove(Entity.RemovalReason.DISCARDED);
 +        // CraftBukkit start - add Bukkit remove cause
 +        this.discard(null);
-     }
- 
++    }
++
 +    public final void discard(EntityRemoveEvent.Cause cause) {
 +        this.remove(Entity.RemovalReason.DISCARDED, cause);
 +        // CraftBukkit end
-+    }
-+
-     protected abstract void defineSynchedData(SynchedEntityData.Builder builder);
+     }
  
-     public SynchedEntityData getEntityData() {
+     protected abstract void defineSynchedData(SynchedEntityData.Builder builder);
+@@ -376,6 +581,16 @@
          return this.entityData;
      }
  
@@ -393,19 +392,21 @@
      }
  
      protected final AABB makeBoundingBox() {
-@@ -462,10 +716,20 @@
-         this.baseTick();
-     }
+@@ -460,12 +714,22 @@
  
+     public void tick() {
+         this.baseTick();
++    }
++
 +    // CraftBukkit start
 +    public void postTick() {
 +        // No clean way to break out of ticking once the entity has been copied to a new world, so instead we move the portalling later in the tick cycle
 +        if (!(this instanceof ServerPlayer) && this.isAlive()) { // Paper - don't attempt to teleport dead entities
 +            this.handlePortal();
 +        }
-+    }
+     }
 +    // CraftBukkit end
-+
+ 
      public void baseTick() {
          ProfilerFiller gameprofilerfiller = Profiler.get();
  
@@ -536,10 +537,13 @@
      }
  
      public boolean isFree(double offsetX, double offsetY, double offsetZ) {
-@@ -750,6 +1055,28 @@
-                     }
-                 }
+@@ -747,8 +1052,30 @@
  
+                     if (movement.y != vec3d1.y) {
+                         block.updateEntityMovementAfterFallOn(this.level(), this);
++                    }
++                }
++
 +                // CraftBukkit start
 +                if (this.horizontalCollision && this.getBukkitEntity() instanceof Vehicle) {
 +                    Vehicle vehicle = (Vehicle) this.getBukkitEntity();
@@ -553,18 +557,17 @@
 +                        bl = bl.getRelative(BlockFace.SOUTH);
 +                    } else if (movement.z < vec3d1.z) {
 +                        bl = bl.getRelative(BlockFace.NORTH);
-+                    }
+                     }
 +
 +                    if (!bl.getType().isAir()) {
 +                        VehicleBlockCollisionEvent event = new VehicleBlockCollisionEvent(vehicle, bl);
 +                        this.level.getCraftServer().getPluginManager().callEvent(event);
 +                    }
-+                }
+                 }
 +                // CraftBukkit end
-+
+ 
                  if (!this.level().isClientSide() || this.isControlledByLocalInstance()) {
                      Entity.MovementEmission entity_movementemission = this.getMovementEmission();
- 
 @@ -1133,6 +1460,20 @@
          return SoundEvents.GENERIC_SPLASH;
      }
@@ -653,7 +656,7 @@
          this.hasImpulse = true;
      }
  
-@@ -1858,8 +2224,20 @@
+@@ -1858,9 +2224,21 @@
      }
  
      public boolean isPushable() {
@@ -665,15 +668,16 @@
 +        // Paper end - Climbing should not bypass cramming gamerule
          return false;
      }
-+
+ 
 +    // CraftBukkit start - collidable API
 +    public boolean canCollideWithBukkit(Entity entity) {
 +        return this.isPushable();
 +    }
 +    // CraftBukkit end
- 
++
      public void awardKillScore(Entity entityKilled, DamageSource damageSource) {
          if (entityKilled instanceof ServerPlayer) {
+             CriteriaTriggers.ENTITY_KILLED_PLAYER.trigger((ServerPlayer) entityKilled, this, damageSource);
 @@ -1889,74 +2267,133 @@
      }
  
@@ -1447,7 +1451,7 @@
          Iterator iterator = this.getIndirectPassengers().iterator();
  
          while (iterator.hasNext()) {
-@@ -2995,22 +3753,45 @@
+@@ -2995,9 +3753,17 @@
      }
  
      protected void removeAfterChangingDimensions() {
@@ -1468,11 +1472,10 @@
          }
  
      }
- 
-     public Vec3 getRelativePortalPosition(Direction.Axis portalAxis, BlockUtil.FoundRectangle portalRect) {
+@@ -3006,11 +3772,26 @@
          return PortalShape.getRelativePosition(portalRect, portalAxis, this.position(), this.getDimensions(this.getPose()));
-+    }
-+
+     }
+ 
 +    // CraftBukkit start
 +    public CraftPortalEvent callPortalEvent(Entity entity, Location exit, PlayerTeleportEvent.TeleportCause cause, int searchRadius, int creationRadius) {
 +        org.bukkit.entity.Entity bukkitEntity = entity.getBukkitEntity();
@@ -1484,9 +1487,9 @@
 +            return null;
 +        }
 +        return new CraftPortalEvent(event);
-     }
++    }
 +    // CraftBukkit end
- 
++
      public boolean canUsePortal(boolean allowVehicles) {
          return (allowVehicles || !this.isPassenger()) && this.isAlive();
      }
@@ -1566,7 +1569,23 @@
          return this.getDimensions(pose).eyeHeight();
      }
  
-@@ -3335,7 +4141,7 @@
+@@ -3300,7 +4106,14 @@
+ 
+     public void startSeenByPlayer(ServerPlayer player) {}
+ 
+-    public void stopSeenByPlayer(ServerPlayer player) {}
++    // Paper start - entity tracking events
++    public void stopSeenByPlayer(ServerPlayer player) {
++        // Since this event cannot be cancelled, we should call it here to catch all "un-tracks"
++        if (io.papermc.paper.event.player.PlayerUntrackEntityEvent.getHandlerList().getRegisteredListeners().length > 0) {
++            new io.papermc.paper.event.player.PlayerUntrackEntityEvent(player.getBukkitEntity(), this.getBukkitEntity()).callEvent();
++        }
++    }
++    // Paper end - entity tracking events
+ 
+     public float rotate(Rotation rotation) {
+         float f = Mth.wrapDegrees(this.getYRot());
+@@ -3335,7 +4148,7 @@
      }
  
      @Nullable
@@ -1575,7 +1594,7 @@
          return null;
      }
  
-@@ -3373,20 +4179,34 @@
+@@ -3373,20 +4186,34 @@
      }
  
      private Stream<Entity> getIndirectPassengersStream() {
@@ -1610,7 +1629,7 @@
          return () -> {
              return this.getIndirectPassengersStream().iterator();
          };
-@@ -3399,6 +4219,7 @@
+@@ -3399,6 +4226,7 @@
      }
  
      public boolean hasExactlyOnePlayerPassenger() {
@@ -1618,7 +1637,7 @@
          return this.countPlayerPassengers() == 1;
      }
  
-@@ -3435,7 +4256,7 @@
+@@ -3435,7 +4263,7 @@
      }
  
      public boolean isControlledByLocalInstance() {
@@ -1627,7 +1646,7 @@
  
          if (entityliving instanceof Player entityhuman) {
              return entityhuman.isLocalPlayer();
-@@ -3445,7 +4266,7 @@
+@@ -3445,7 +4273,7 @@
      }
  
      public boolean isControlledByClient() {
@@ -1636,7 +1655,7 @@
  
          return entityliving != null && entityliving.isControlledByClient();
      }
-@@ -3463,7 +4284,7 @@
+@@ -3463,7 +4291,7 @@
          return new Vec3((double) f1 * d2 / (double) f3, 0.0D, (double) f2 * d2 / (double) f3);
      }
  
@@ -1645,7 +1664,7 @@
          return new Vec3(this.getX(), this.getBoundingBox().maxY, this.getZ());
      }
  
-@@ -3489,8 +4310,37 @@
+@@ -3489,8 +4317,37 @@
          return 1;
      }
  
@@ -1684,20 +1703,19 @@
      }
  
      public void lookAt(EntityAnchorArgument.Anchor anchorPoint, Vec3 target) {
-@@ -3550,7 +4400,12 @@
- 
+@@ -3551,6 +4408,11 @@
                                      vec3d = vec3d.add(vec3d1);
                                      ++k1;
-+                                }
+                                 }
 +                                // CraftBukkit start - store last lava contact location
 +                                if (tag == FluidTags.LAVA) {
 +                                    this.lastLavaContact = blockposition_mutableblockposition.immutable();
-                                 }
++                                }
 +                                // CraftBukkit end
                              }
                          }
                      }
-@@ -3613,7 +4468,7 @@
+@@ -3613,7 +4475,7 @@
          return new ClientboundAddEntityPacket(this, entityTrackerEntry);
      }
  
@@ -1706,7 +1724,7 @@
          return this.type.getDimensions();
      }
  
-@@ -3714,7 +4569,39 @@
+@@ -3714,7 +4576,39 @@
          return this.getZ((2.0D * this.random.nextDouble() - 1.0D) * widthScale);
      }
  
@@ -1746,7 +1764,7 @@
          if (this.position.x != x || this.position.y != y || this.position.z != z) {
              this.position = new Vec3(x, y, z);
              int i = Mth.floor(x);
-@@ -3732,6 +4619,12 @@
+@@ -3732,6 +4626,12 @@
              this.levelCallback.onMove();
          }
  
@@ -1759,7 +1777,7 @@
      }
  
      public void checkDespawn() {}
-@@ -3818,8 +4711,16 @@
+@@ -3818,8 +4718,16 @@
  
      @Override
      public final void setRemoved(Entity.RemovalReason reason) {
@@ -1777,7 +1795,7 @@
          }
  
          if (this.removalReason.shouldDestroy()) {
-@@ -3827,8 +4728,8 @@
+@@ -3827,8 +4735,8 @@
          }
  
          this.getPassengers().forEach(Entity::stopRiding);
@@ -1788,7 +1806,7 @@
      }
  
      public void unsetRemoved() {
-@@ -3887,7 +4788,7 @@
+@@ -3887,7 +4795,7 @@
      }
  
      public Vec3 getKnownMovement() {
@@ -1797,7 +1815,7 @@
  
          if (entityliving instanceof Player entityhuman) {
              if (this.isAlive()) {
-@@ -3962,4 +4863,14 @@
+@@ -3962,4 +4870,14 @@
  
          void accept(Entity entity, double x, double y, double z);
      }