Methods to find all Windows & Viewers in GUI

This commit is contained in:
NichtStudioCode 2021-01-29 18:33:17 +01:00
parent 0a62cb85b6
commit 8889f30911
2 changed files with 27 additions and 1 deletions

@ -14,6 +14,7 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -230,6 +231,20 @@ public interface GUI {
*/ */
Set<GUIParent> getParents(); Set<GUIParent> getParents();
/**
* Finds all {@link Window}s that show this {@link GUI}.
*
* @return The list of {@link Window} that show this {@link GUI}
*/
List<Window> 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<Player> findAllViewers();
/** /**
* Plays an {@link Animation}. * Plays an {@link Animation}.
* *

@ -201,7 +201,8 @@ abstract class IndexedGUI implements GUI, GUIParent {
return parents; return parents;
} }
private List<Window> findAllWindows() { @Override
public List<Window> findAllWindows() {
List<Window> windows = new ArrayList<>(); List<Window> windows = new ArrayList<>();
List<GUIParent> parents = new ArrayList<>(this.parents); List<GUIParent> parents = new ArrayList<>(this.parents);
@ -217,6 +218,16 @@ abstract class IndexedGUI implements GUI, GUIParent {
return windows; return windows;
} }
@Override
public List<Player> findAllViewers() {
List<Player> players = new ArrayList<>();
findAllWindows().stream()
.flatMap(window -> window.getInventory().getViewers().stream())
.forEach(humanEntity -> players.add((Player) humanEntity));
return players;
}
@Override @Override
public void playAnimation(@NotNull Animation animation, @Nullable Predicate<ItemStackHolder> filter) { public void playAnimation(@NotNull Animation animation, @Nullable Predicate<ItemStackHolder> filter) {
if (animation != null) cancelAnimation(); if (animation != null) cancelAnimation();