From 0000000000000000000000000000000000000000 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


diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
index b6b15e8ce98603d6e580a31ba458fedc8b3f663a..91ca37605bf7ba65875b588fe9764b30214f63b6 100644
--- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
@@ -1574,7 +1574,11 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
                 // 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);
+                        }
+                        // Paper end - entity tracking events
                     }
                 } else if (this.seenBy.remove(player.connection)) {
                     this.serverEntity.removePairing(player);
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index 82531bd4ada8373d3c255758382d5850309913e2..f52d394c90bc326c2a58c4f623df632235e0c599 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -3869,7 +3869,14 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
 
     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());
