How to get all(Offline/Online) players by a permission

i want to use a for each loop like getOnlinePlayers with Player
but i couldnt find a way to get every player with … permission…

game.getServer().getOnlinePlayers.stream().filter(pl -> pl.hasPermission("permission.node")).forEach(pl -> // Code });

i need to get offline players too… this will only get online ones…

Closest that I can think of would be getting instance of User

UserStorageService userStorage = Sponge.getServiceManager().provide(UserStorageService.class).get();
Collection<GameProfile> profiles = userStorage.getAll();

for(GameProfile profile : profiles) {
    Optional<User> optionalUser = userStorage.get(profile);
		    
    if(optionalUser.isPresent()) {
        User user = optionalUser.get();
    }
}
1 Like

i get player entity with
Player player = user.getPlayer().get();
hope it works with your solution…

That will not work because player is offline. Why do you need Player? You might be able to make do with User.

1 Like

i want to set a permission for offline user then…
i was using getSubjectData… how can i do it with GameProfile?
Edit: ok it works same thanks :slight_smile:

Player extends User so many of the Player functions are in User

if i use user.isOnline() then get Player does it cause a problem?
Edit: if he is offline then ill only change a permission but if he is online i need to add his name on an array etc.

Not at all. That’s exactly what that method is for. As long as a Player is online you should be able to obtain an instance of that player

1 Like

Thanks you so much :smiley:

I will also add one more thing, just to prevent a future problem. Because userStorage.get(profile) returns an optional, you should probably add a check to make sure it’s present. I updated to code snippet in the solved post.

1 Like
if (user.isOnline() && user.getPlayer().isPresent()){
                Player player =user.getPlayer().get();

Thanks for the advice :slight_smile: