Add Entity#isTrackedBy (#12332)

This commit is contained in:
TonytheMacaroni 2025-04-14 04:12:48 -04:00 committed by GitHub
parent de64e70458
commit 1b889688a5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View File

@ -698,6 +698,15 @@ public interface Entity extends Metadatable, CommandSender, Nameable, Persistent
@NotNull @NotNull
Set<Player> getTrackedBy(); Set<Player> getTrackedBy();
/**
* Checks to see if a player is currently tracking this entity.
*
* @param player the player to check
* @return if the player is currently tracking this entity
* @see #getTrackedBy()
*/
boolean isTrackedBy(@NotNull Player player);
/** /**
* Sets whether the entity has a team colored (default: white) glow. * Sets whether the entity has a team colored (default: white) glow.
* *

View File

@ -725,6 +725,18 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
return players.build(); return players.build();
} }
@Override
public boolean isTrackedBy(final Player player) {
Preconditions.checkState(!this.entity.generation, "Cannot check tracking players during world generation");
Preconditions.checkArgument(player != null, "Player cannot be null");
ServerLevel world = ((CraftWorld) this.getWorld()).getHandle();
ChunkMap.TrackedEntity entityTracker = world.getChunkSource().chunkMap.entityMap.get(this.getEntityId());
if (entityTracker == null) return false;
return entityTracker.seenBy.contains(((CraftPlayer) player).getHandle().connection);
}
@Override @Override
public void sendMessage(String message) { public void sendMessage(String message) {