From 962833ad6023fbe5b4ccf4c1a5f93d0ddf2d18df Mon Sep 17 00:00:00 2001 From: wellnesscookie <46493763+wellnesscookie@users.noreply.github.com> Date: Sat, 27 Jul 2019 10:08:46 +0200 Subject: [PATCH] Added Island#get- and #hasPlayersOnIsland methods (#861) * Implements #get and #has - PlayersOnIsland method Implements methods for #860 * Optimises #hasVisitors and #hasPlayersOnIsland These methods will now check if there is at least one user that meets the conditions --- .../bentobox/database/objects/Island.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/database/objects/Island.java b/src/main/java/world/bentobox/bentobox/database/objects/Island.java index 5bef98bb8..165fb9523 100644 --- a/src/main/java/world/bentobox/bentobox/database/objects/Island.java +++ b/src/main/java/world/bentobox/bentobox/database/objects/Island.java @@ -592,9 +592,33 @@ public class Island implements DataObject { * @see #getVisitors() */ public boolean hasVisitors() { - return !getVisitors().isEmpty(); + return Bukkit.getOnlinePlayers().stream().anyMatch(player -> onIsland(player.getLocation()) && getRank(User.getInstance(player)) == RanksManager.VISITOR_RANK); } - + + /** + * Returns a list of players that are physically inside the island's protection range + * @return list of players + * @since 1.6.0 + */ + @NonNull + public List getPlayersOnIsland() { + return Bukkit.getOnlinePlayers().stream() + .filter(player -> onIsland(player.getLocation()) + .collect(Collectors.toList()); + } + + /** + * Returns whether this Island has players inside its protection range. + * Note this is equivalent to {@code !island.getPlayersOnIsland().isEmpty()}. + * @return {@code true} if there are players inside this Island's protection range, {@code false} otherwise. + * + * @since 1.6.0 + * @see #getPlayersOnIsland() + */ + public boolean hasPlayersOnIsland() { + return Bukkit.getOnlinePlayers().stream().anyMatch(player -> onIsland(player.getLocation())); + } + /** * Check if the flag is allowed or not * For flags that are for the island in general and not related to rank.