Search in sources :

Example 1 with Reloadable

use of org.lanternpowered.server.util.Reloadable in project LanternServer by LanternPowered.

the class CommandWhitelist method completeSpec.

@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
    specBuilder.child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("player"))).executor((src, args) -> {
        final String playerName = args.<String>getOne("player").get();
        final WhitelistService service = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
        Lantern.getGame().getGameProfileManager().get(playerName).whenComplete((profile, error) -> {
            if (error != null) {
                src.sendMessage(t("commands.whitelist.add.failed", playerName));
            } else {
                src.sendMessage(t("commands.whitelist.add.success", playerName));
                service.addProfile(((LanternGameProfile) profile).withoutProperties());
            }
        });
        return CommandResult.success();
    }).build(), "add").child(CommandSpec.builder().arguments(GenericArguments.string(Text.of("player"))).executor((src, args) -> {
        final String playerName = args.<String>getOne("player").get();
        final WhitelistService service = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
        Lantern.getGame().getGameProfileManager().get(playerName).whenComplete((profile, error) -> {
            if (error != null || !service.isWhitelisted(profile = ((LanternGameProfile) profile).withoutProperties())) {
                src.sendMessage(t("commands.whitelist.remove.failed", playerName));
            } else {
                src.sendMessage(t("commands.whitelist.remove.success", playerName));
                service.removeProfile(profile);
            }
        });
        return CommandResult.success();
    }).build(), "remove").child(CommandSpec.builder().executor((src, args) -> {
        final WhitelistService service = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
        final List<String> whitelisted = service.getWhitelistedProfiles().stream().map(p -> p.getName().get()).collect(Collectors.toList());
        src.sendMessage(t("commands.whitelist.list", whitelisted.size(), Sponge.getServer().getOnlinePlayers().size()));
        src.sendMessage(Text.of(Joiner.on(", ").join(whitelisted)));
        return CommandResult.success();
    }).build(), "list").child(CommandSpec.builder().executor((src, args) -> {
        Lantern.getGame().getGlobalConfig().setWhitelistEnabled(true);
        src.sendMessage(t("commands.whitelist.enabled"));
        return CommandResult.success();
    }).build(), "on").child(CommandSpec.builder().executor((src, args) -> {
        Lantern.getGame().getGlobalConfig().setWhitelistEnabled(false);
        src.sendMessage(t("commands.whitelist.disabled"));
        return CommandResult.success();
    }).build(), "off").child(CommandSpec.builder().executor((src, args) -> {
        final WhitelistService service = Sponge.getServiceManager().provideUnchecked(WhitelistService.class);
        if (service instanceof Reloadable) {
            try {
                ((Reloadable) service).reload();
            } catch (Exception e) {
                throw new CommandException(t("commands.whitelist.reload.failed", e.getMessage()), e);
            }
        } else {
            src.sendMessage(t("commands.whitelist.reload.not_supported"));
            return CommandResult.empty();
        }
        return CommandResult.success();
    }).build(), "reload");
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TranslationHelper.t(org.lanternpowered.server.text.translation.TranslationHelper.t) Sponge(org.spongepowered.api.Sponge) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Collectors(java.util.stream.Collectors) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) WhitelistService(org.spongepowered.api.service.whitelist.WhitelistService) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) Text(org.spongepowered.api.text.Text) Lantern(org.lanternpowered.server.game.Lantern) Reloadable(org.lanternpowered.server.util.Reloadable) PluginContainer(org.spongepowered.api.plugin.PluginContainer) Joiner(com.google.common.base.Joiner) WhitelistService(org.spongepowered.api.service.whitelist.WhitelistService) LanternGameProfile(org.lanternpowered.server.profile.LanternGameProfile) List(java.util.List) CommandException(org.spongepowered.api.command.CommandException) Reloadable(org.lanternpowered.server.util.Reloadable) CommandException(org.spongepowered.api.command.CommandException)

Aggregations

Joiner (com.google.common.base.Joiner)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Lantern (org.lanternpowered.server.game.Lantern)1 LanternGameProfile (org.lanternpowered.server.profile.LanternGameProfile)1 TranslationHelper.t (org.lanternpowered.server.text.translation.TranslationHelper.t)1 Reloadable (org.lanternpowered.server.util.Reloadable)1 Sponge (org.spongepowered.api.Sponge)1 CommandException (org.spongepowered.api.command.CommandException)1 CommandResult (org.spongepowered.api.command.CommandResult)1 GenericArguments (org.spongepowered.api.command.args.GenericArguments)1 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)1 PluginContainer (org.spongepowered.api.plugin.PluginContainer)1 WhitelistService (org.spongepowered.api.service.whitelist.WhitelistService)1 Text (org.spongepowered.api.text.Text)1