Check if user/player is banned

Sorry for asking so many (rather stupid) questions but I just can’t figure out how to do this.

You’ll need to get an instance of BanService from the ServiceManager (after the server has started), and call isBanned with an InetAddress or GameProfile, depending on whether you want to check for an IP or GameProfile ban.

In code, this looks like:

@Listener
public void onStartServer(GameStartedServerEvent event) {
    Sponge.getGame().getServiceManager().provideUnchecked(BanService.class).isBanned(profile)
}

It’s safe to call provideUnchecked, since BanService is guaranteed to be present after the proper time (unlike the future EconomyService, which is always plugin-provided).

Not that you don’t have to do the check in GameStartedServer event, it’s just that it’s not safe to do it before that. Most events you will be listening to, such as player joining or entity/block interaction, always occur after the server has started. However, you’ll need to keep this in mind if you need access to the BanService from a different event, or early on in the game lifecycle.

Don’t worry about asking questions - that’s the only way to learn :slight_smile: We’re glad to help out with any other questions you might have!

3 Likes

It throws:
org.spongepowered.api.service.ProvisioningException No provider is registered for the service org.spongepowered.api.service.ban.BanService

when I initialize it. It’s weird because I initialize a UserStorageService the exact same way and it works.

That’s weird, because both services are registered at the same time.
Are you using the latest spongeforge build? Because the ban implementation is quite a recent addition.

It would be good to show your code if you still can’t get it to work

That could be the cause. I didn’t update the server for over a week.
Here is the code:

 BanService =  Sponge.getGame().getServiceManager().provideUnchecked(BanService.class);
 UserService =  Sponge.getGame().getServiceManager().provideUnchecked(UserStorageService.class);

Always keep dev builds up to date
BanService was implemented 4 days ago so that would be your problem there.

https://github.com/SpongePowered/SpongeCommon/commit/2465d1a260f90f7b166133b08ff5ecfd3b6aa17a

1 Like