From 8889f30911dc6557d768d81ecacdb9f9cc7297a3 Mon Sep 17 00:00:00 2001 From: NichtStudioCode <51272202+NichtStudioCode@users.noreply.github.com> Date: Fri, 29 Jan 2021 18:33:17 +0100 Subject: [PATCH] Methods to find all Windows & Viewers in GUI --- src/main/java/de/studiocode/invgui/gui/GUI.java | 15 +++++++++++++++ .../de/studiocode/invgui/gui/impl/IndexedGUI.java | 13 ++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/studiocode/invgui/gui/GUI.java b/src/main/java/de/studiocode/invgui/gui/GUI.java index bb0a592..74fd53e 100644 --- a/src/main/java/de/studiocode/invgui/gui/GUI.java +++ b/src/main/java/de/studiocode/invgui/gui/GUI.java @@ -14,6 +14,7 @@ import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; import java.util.Set; import java.util.function.Predicate; @@ -230,6 +231,20 @@ public interface GUI { */ Set getParents(); + /** + * Finds all {@link Window}s that show this {@link GUI}. + * + * @return The list of {@link Window} that show this {@link GUI} + */ + List findAllWindows(); + + /** + * Finds all {@link Player}s that are currently seeing this {@link Window}. + * + * @return The list of {@link Player}s that are currently seeing this {@link Window} + */ + List findAllViewers(); + /** * Plays an {@link Animation}. * diff --git a/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java b/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java index 93c487b..3c3dd7a 100644 --- a/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java +++ b/src/main/java/de/studiocode/invgui/gui/impl/IndexedGUI.java @@ -201,7 +201,8 @@ abstract class IndexedGUI implements GUI, GUIParent { return parents; } - private List findAllWindows() { + @Override + public List findAllWindows() { List windows = new ArrayList<>(); List parents = new ArrayList<>(this.parents); @@ -217,6 +218,16 @@ abstract class IndexedGUI implements GUI, GUIParent { return windows; } + @Override + public List findAllViewers() { + List players = new ArrayList<>(); + findAllWindows().stream() + .flatMap(window -> window.getInventory().getViewers().stream()) + .forEach(humanEntity -> players.add((Player) humanEntity)); + + return players; + } + @Override public void playAnimation(@NotNull Animation animation, @Nullable Predicate filter) { if (animation != null) cancelAnimation();