Update WindowManager.java

Updated javadoc
New method for finding all Windows belonging to a player
This commit is contained in:
NichtStudioCode 2021-01-25 18:26:42 +01:00
parent ea35d94dda
commit dd9232fc37

@ -10,10 +10,15 @@ import org.bukkit.event.inventory.*;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
/**
* Manages all {@link Window}s and provides methods for searching them.
*/
public class WindowManager implements Listener { public class WindowManager implements Listener {
private static WindowManager instance; private static WindowManager instance;
@ -57,10 +62,10 @@ public class WindowManager implements Listener {
} }
/** /**
* Finds the window to an inventory. * Finds the {@link Window} to an {@link Inventory}.
* *
* @param inventory The inventory * @param inventory The {@link Inventory}
* @return The window that belongs to that inventory * @return The {@link Window} that belongs to that {@link Inventory}
*/ */
public Optional<Window> findWindow(Inventory inventory) { public Optional<Window> findWindow(Inventory inventory) {
return windows.stream() return windows.stream()
@ -69,21 +74,34 @@ public class WindowManager implements Listener {
} }
/** /**
* Finds the window to a player. * Finds all {@link Window}s to a {@link Player}
* *
* @param player The player * @param player The {@link Player}
* @return The window that the player has open * @return A list of {@link Window}s that have the {@link Player} as their viewer.
*/ */
public Optional<Window> findWindow(Player player) { public List<Window> findWindows(Player player) {
return windows.stream() return windows.stream()
.filter(w -> w.getInventory().getViewers().stream().anyMatch(player::equals)) .filter(w -> w.getViewer().getUniqueId().equals(player.getUniqueId()))
.findFirst(); .collect(Collectors.toCollection(ArrayList::new));
} }
/** /**
* Gets a list of all currently active windows. * Finds the {@link Window} the {@link Player} has currently open.
*
* @param player The {@link Player}
* @return The {@link Window} the {@link Player} has currently open or <code>null</code>
* if there isn't one.
*/
public Window findWindow(Player player) {
return windows.stream()
.filter(w -> w.getInventory().getViewers().contains(player))
.findFirst().orElse(null);
}
/**
* Gets a list of all currently active {@link Window}s.
* *
* @return A list of all windows * @return A list of all {@link Window}s
*/ */
public List<Window> getWindows() { public List<Window> getWindows() {
return windows; return windows;