Holographic Displays

Holographic Displays

3M Downloads

API - Add "getVisiblePlayers" to VisibilityManager

SkytAsul opened this issue · 4 comments

commented

Description

In com.gmail.filoghost.holographicdisplays.api.VisibilityManager, it would benefit to the API to have a method Map<String, Boolean> getVisiblePlayers() so that we can get a list of players where the hologram is visible instead of having to test every single user/using reflection to get the underlying playersVisibilityMap field.

commented

I'm sorry, as part of the switch to maintenance-only mode (read here for more info), I'm no longer adding new features or enhancements to the plugin.

commented

What's a use case where you need to test every single player?

commented

In my case, to update the visibility status of a lot of players at a time with high performance.
Here is my code:

		public void setPlayersVisible(List<Player> players) {
			try {
				List<Player> all = new ArrayList<>(players);
				VisibilityManager visibility = hologram.getVisibilityManager();
				
				Field field = visibility.getClass().getDeclaredField("playersVisibilityMap");
				field.setAccessible(true);
				Map<String, Boolean> map = (Map<String, Boolean>) field.get(visibility);
				if (map == null) field.set(visibility, new ConcurrentHashMap<>());
				map = (Map<String, Boolean>) field.get(visibility);
				
				for (Entry<String, Boolean> en : map.entrySet()) {
					if (!en.getValue()) continue;
					Player p = Bukkit.getPlayer(en.getKey());
					if (p == null) continue;
					if (!all.contains(p)) {
						visibility.hideTo(p);
					}
					all.remove(p);
				}
				for (Player p : all) {
					if (p == null) continue;
					visibility.showTo(p);
				}
			}catch (ReflectiveOperationException ex) {
				ex.printStackTrace();
			}
		}

I receive periodically a list of players that can see the hologram but I don't have a list of the players that could, in the past, see the hologram. This is the workaround I've made, about three years ago (I didn't think about making an issue since then).

commented

I understand. Resetting the visibility of all players is not ideal here, as it would make the holograms "blink" for players who can already see them. A new method should be added to the API.