From e4eca0c1c9dfc1027c544e707b9e78f7cb34e6c8 Mon Sep 17 00:00:00 2001 From: LoJoSho Date: Thu, 16 Jan 2025 13:48:13 -0600 Subject: [PATCH] feat: add getViewers in ServerUtils that uses new canSee method --- .../hibiscuscommons/util/ServerUtils.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/common/src/main/java/me/lojosho/hibiscuscommons/util/ServerUtils.java b/common/src/main/java/me/lojosho/hibiscuscommons/util/ServerUtils.java index e82436a..014e9bc 100644 --- a/common/src/main/java/me/lojosho/hibiscuscommons/util/ServerUtils.java +++ b/common/src/main/java/me/lojosho/hibiscuscommons/util/ServerUtils.java @@ -4,10 +4,14 @@ import com.comphenix.protocol.wrappers.WrappedGameProfile; import com.comphenix.protocol.wrappers.WrappedSignedProperty; import me.lojosho.hibiscuscommons.nms.NMSHandlers; import org.bukkit.Color; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; public class ServerUtils { @@ -92,6 +96,21 @@ public class ServerUtils { return nextYaw; } + /** + * Get the viewers of an entity (Players only) that can see the entity in the world. + * This ignores config view distances and checks directly with the server. + * @param entity + * @return + */ + @NotNull + public static List getViewers(@NotNull Entity entity) { + ArrayList viewers = new ArrayList<>(); + for (Player player : entity.getLocation().getWorld().getPlayers()) { + if (player.canSee(entity)) viewers.add(player); + } + return viewers; + } + public static boolean hasClass(String className) { try { Class.forName(className);