API - Add "getVisiblePlayers" to VisibilityManager
SkytAsul opened this issue · 4 comments
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.
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.
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).