Add cancellability to PlayerTrackEntityEvent (#8605)

This commit is contained in:
Yannick Lamprecht 2023-08-21 09:38:55 +02:00 committed by GitHub
parent 88891c37b1
commit 5bf82aa136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 33 deletions

View File

@ -6,25 +6,29 @@ Subject: [PATCH] Player Entity Tracking Events
diff --git a/src/main/java/io/papermc/paper/event/player/PlayerTrackEntityEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerTrackEntityEvent.java diff --git a/src/main/java/io/papermc/paper/event/player/PlayerTrackEntityEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerTrackEntityEvent.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..167be75dc616578db2df1a88f1a54b17707b3a88 index 0000000000000000000000000000000000000000..db0a011815daf2690deb4ad2aff08227664fc6b8
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/event/player/PlayerTrackEntityEvent.java +++ b/src/main/java/io/papermc/paper/event/player/PlayerTrackEntityEvent.java
@@ -0,0 +1,42 @@ @@ -0,0 +1,56 @@
+package io.papermc.paper.event.player; +package io.papermc.paper.event.player;
+ +
+import org.bukkit.entity.Entity; +import org.bukkit.entity.Entity;
+import org.bukkit.entity.Player; +import org.bukkit.entity.Player;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.HandlerList; +import org.bukkit.event.HandlerList;
+import org.bukkit.event.player.PlayerEvent; +import org.bukkit.event.player.PlayerEvent;
+import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.NotNull;
+ +
+/** +/**
+ * Is called when a {@link Player} tracks an {@link Entity}. + * Is called when a {@link Player} tracks an {@link Entity}.
+ * <p>
+ * If cancelled entity is not shown to the player and interaction in both directions is not possible.
+ */ + */
+public class PlayerTrackEntityEvent extends PlayerEvent { +public class PlayerTrackEntityEvent extends PlayerEvent implements Cancellable {
+ +
+ private static final HandlerList handlers = new HandlerList(); + private static final HandlerList handlers = new HandlerList();
+ private final Entity entity; + private final Entity entity;
+ private boolean cancelled;
+ +
+ public PlayerTrackEntityEvent(@NotNull Player player, @NotNull Entity entity) { + public PlayerTrackEntityEvent(@NotNull Player player, @NotNull Entity entity) {
+ super(player); + super(player);
@ -51,6 +55,16 @@ index 0000000000000000000000000000000000000000..167be75dc616578db2df1a88f1a54b17
+ public Entity getEntity() { + public Entity getEntity() {
+ return entity; + return entity;
+ } + }
+
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ @Override
+ public void setCancelled(boolean cancel) {
+ this.cancelled = cancel;
+ }
+} +}
diff --git a/src/main/java/io/papermc/paper/event/player/PlayerUntrackEntityEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerUntrackEntityEvent.java diff --git a/src/main/java/io/papermc/paper/event/player/PlayerUntrackEntityEvent.java b/src/main/java/io/papermc/paper/event/player/PlayerUntrackEntityEvent.java
new file mode 100644 new file mode 100644

View File

@ -4,31 +4,26 @@ Date: Wed, 30 Mar 2022 18:16:52 +0200
Subject: [PATCH] Player Entity Tracking Events Subject: [PATCH] Player Entity Tracking Events
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java diff --git a/src/main/java/net/minecraft/server/level/ChunkMap.java b/src/main/java/net/minecraft/server/level/ChunkMap.java
index 32093c96a22221f17411aaf260db071f27350966..58eb0a7be2e5b4dd2aba8aebc0f7a57ff1348135 100644 index 469083208110d5735e1bbda7a15c55a032d0e8cc..a98c134ce2ea51ae6d6f92959e57a4f0e03b0f6c 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java --- a/src/main/java/net/minecraft/server/level/ChunkMap.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/server/level/ChunkMap.java
@@ -4018,9 +4018,21 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { @@ -1543,9 +1543,18 @@ public class ChunkMap extends ChunkStorage implements ChunkHolder.PlayerProvider
EnchantmentHelper.doPostDamageEffects(attacker, target); // 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
}
} else if (this.seenBy.remove(player.connection)) {
+ // Paper start - entity tracking events
+ if (io.papermc.paper.event.player.PlayerUntrackEntityEvent.getHandlerList().getRegisteredListeners().length > 0) {
+ new io.papermc.paper.event.player.PlayerUntrackEntityEvent(player.getBukkitEntity(), this.entity.getBukkitEntity()).callEvent();
+ }
+ // Paper end
this.serverEntity.removePairing(player);
}
- public void startSeenByPlayer(ServerPlayer player) {}
+ // Paper start
+ public void startSeenByPlayer(ServerPlayer player) {
+ if (io.papermc.paper.event.player.PlayerTrackEntityEvent.getHandlerList().getRegisteredListeners().length > 0) {
+ new io.papermc.paper.event.player.PlayerTrackEntityEvent(player.getBukkitEntity(), this.getBukkitEntity()).callEvent();
+ }
+ }
+ // Paper end
- public void stopSeenByPlayer(ServerPlayer player) {}
+ // Paper start
+ public void stopSeenByPlayer(ServerPlayer player) {
+ 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
public float rotate(Rotation rotation) {
float f = Mth.wrapDegrees(this.getYRot());

View File

@ -1158,7 +1158,7 @@ index 8547e7ff2f1f5b7701fb0f3c3010c14601a5f83e..fff7ad7a45f310783ac96b44575ad3db
this.players.remove(entityplayer); this.players.remove(entityplayer);
this.playersByName.remove(entityplayer.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot this.playersByName.remove(entityplayer.getScoreboardName().toLowerCase(java.util.Locale.ROOT)); // Spigot
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index c014fe8808f19195d8c26925227ff28bd6738039..ccc674a3da006fc3f6edd3b479eaa86c1cc67e90 100644 index cf9393dd02565134a1ce428e84a9f26ac2d39a0d..e19010627a030753b08aa8451363c6bc4631bf72 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java --- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java +++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -246,11 +246,23 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { @@ -246,11 +246,23 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
@ -1186,7 +1186,7 @@ index c014fe8808f19195d8c26925227ff28bd6738039..ccc674a3da006fc3f6edd3b479eaa86c
@Override @Override
public CommandSender getBukkitSender(CommandSourceStack wrapper) { public CommandSender getBukkitSender(CommandSourceStack wrapper) {
return this.getBukkitEntity(); return this.getBukkitEntity();
@@ -4656,6 +4668,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { @@ -4644,6 +4656,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
return; return;
} }
// Paper end - rewrite chunk system // Paper end - rewrite chunk system
@ -1194,7 +1194,7 @@ index c014fe8808f19195d8c26925227ff28bd6738039..ccc674a3da006fc3f6edd3b479eaa86c
if (this.removalReason == null) { if (this.removalReason == null) {
this.removalReason = reason; this.removalReason = reason;
} }
@@ -4666,12 +4679,28 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource { @@ -4654,12 +4667,28 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
if (reason != RemovalReason.UNLOADED_TO_CHUNK) this.getPassengers().forEach(Entity::stopRiding); // Paper - chunk system - don't adjust passenger state when unloading, it's just not safe (and messes with our logic in entity chunk unload) if (reason != RemovalReason.UNLOADED_TO_CHUNK) this.getPassengers().forEach(Entity::stopRiding); // Paper - chunk system - don't adjust passenger state when unloading, it's just not safe (and messes with our logic in entity chunk unload)
this.levelCallback.onRemove(reason); this.levelCallback.onRemove(reason);