Search in sources :

Example 16 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class AvailableBaseCommand method executeCommand.

@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    MessageProvider mp = Nucleus.getNucleus().getMessageProvider();
    List<Text> types = Sponge.getRegistry().getAllOf(this.catalogType).stream().map(x -> mp.getTextMessageWithFormat("command.world.presets.item", x.getId(), x.getName())).collect(Collectors.toList());
    Util.getPaginationBuilder(src).title(mp.getTextMessageWithTextFormat(this.titleKey)).contents(types).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Collectors(java.util.stream.Collectors) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Util(io.github.nucleuspowered.nucleus.Util) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) Text(org.spongepowered.api.text.Text)

Example 17 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class LightningCommand method executeCommand.

@Override
public CommandResult executeCommand(final CommandSource src, CommandContext args) throws Exception {
    Collection<Living> playerCollection = args.getAll(player);
    // No argument, let's not smite the subject.
    if (playerCollection.isEmpty()) {
        if (!(src instanceof Player)) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.playeronly"));
            return CommandResult.empty();
        }
        Player pl = (Player) src;
        // 100 is a good limit here.
        BlockRay<World> playerBlockRay = BlockRay.from(pl).distanceLimit(100).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).build();
        Optional<BlockRayHit<World>> obh = playerBlockRay.end();
        Location<World> lightningLocation;
        // Smite above, but not on.
        lightningLocation = obh.map(BlockRayHit::getLocation).orElseGet(() -> pl.getLocation().add(0, 3, 0));
        return this.spawnLightning(lightningLocation, src, null);
    }
    int successCount = 0;
    for (Living pl : playerCollection) {
        CommandResult cr = this.spawnLightning(pl.getLocation(), src, pl instanceof Player ? (Player) pl : null);
        successCount += cr.getSuccessCount().orElse(0);
    }
    return CommandResult.builder().successCount(successCount).build();
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Living(org.spongepowered.api.entity.living.Living) World(org.spongepowered.api.world.World) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) CommandResult(org.spongepowered.api.command.CommandResult)

Example 18 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class AFKCommandInterceptor method onPostCommand.

@Override
public void onPostCommand(Class<? extends AbstractCommand<?>> commandClass, CommandSource source, CommandContext context, CommandResult result) {
    if (this.send && result.getSuccessCount().orElse(0) > 0 && commandClass.isAnnotationPresent(NotifyIfAFK.class)) {
        NotifyIfAFK annotation = commandClass.getAnnotation(NotifyIfAFK.class);
        Cause cause = CauseStackHelper.createCause(source);
        for (String key : annotation.value()) {
            context.getAll(key).stream().filter(x -> x instanceof User).map(x -> ((User) x).getPlayer().orElse(null)).filter(Objects::nonNull).filter(this.handler::isAFK).forEach(x -> {
                Text messageToSend = this.message == null ? null : message.getForCommandSource(x);
                AFKEvents.Notify event = new AFKEvents.Notify(x, messageToSend, cause);
                Sponge.getEventManager().post(event);
                event.getMessage().ifPresent(source::sendMessage);
            });
        }
    }
}
Also used : NotifyIfAFK(io.github.nucleuspowered.nucleus.internal.annotations.command.NotifyIfAFK) AFKConfig(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfig) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) CauseStackHelper(io.github.nucleuspowered.nucleus.util.CauseStackHelper) Sponge(org.spongepowered.api.Sponge) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) Objects(java.util.Objects) Cause(org.spongepowered.api.event.cause.Cause) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) ICommandInterceptor(io.github.nucleuspowered.nucleus.internal.command.ICommandInterceptor) AFKHandler(io.github.nucleuspowered.nucleus.modules.afk.handlers.AFKHandler) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) AFKConfigAdapter(io.github.nucleuspowered.nucleus.modules.afk.config.AFKConfigAdapter) Nullable(javax.annotation.Nullable) User(org.spongepowered.api.entity.living.player.User) Cause(org.spongepowered.api.event.cause.Cause) Objects(java.util.Objects) Text(org.spongepowered.api.text.Text) AFKEvents(io.github.nucleuspowered.nucleus.modules.afk.events.AFKEvents) NotifyIfAFK(io.github.nucleuspowered.nucleus.internal.annotations.command.NotifyIfAFK)

Example 19 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class AbstractCommand method startExecute.

@SuppressWarnings({ "unchecked" })
private CommandResult startExecute(T src, CommandContext args) throws Exception {
    CommandResult cr;
    boolean isSuccess = false;
    try {
        // Any pre-processing steps
        commandInterceptors.forEach(x -> x.onPreCommand((Class<? extends AbstractCommand<?>>) getClass(), src, args));
        // Execute the command in the specific executor.
        cr = executeCommand(src, args);
        isSuccess = cr.getSuccessCount().orElse(0) > 0;
    } catch (ReturnMessageException e) {
        Text t = e.getText();
        src.sendMessage((t == null) ? NucleusPlugin.getNucleus().getMessageProvider().getTextMessageWithFormat("command.error") : t);
        cr = CommandResult.empty();
    } finally {
        if (src instanceof Player) {
            // If the subject is subject to cooling down, apply the cooldown.
            @SuppressWarnings("unchecked") final Player p = (Player) src;
            if (isSuccess) {
                setCooldown(p, args);
            } else {
                // For the tests, keep this here so we can skip the hard to test
                // code below.
                final double cost = getCost(p, args);
                if (cost > 0) {
                    Sponge.getScheduler().createSyncExecutor(plugin).execute(() -> plugin.getEconHelper().depositInPlayer(p, cost));
                }
            }
        }
    }
    for (ICommandInterceptor x : commandInterceptors) {
        x.onPostCommand((Class<? extends AbstractCommand<?>>) getClass(), src, args, cr);
    }
    return cr;
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) CommandResult(org.spongepowered.api.command.CommandResult)

Example 20 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class BroadcastCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    String m = args.<String>getOne(message).get();
    NucleusTextTemplate textTemplate = NucleusTextTemplateFactory.createFromAmpersandString(m);
    Text p = bc.getPrefix().getForCommandSource(src);
    Text s = bc.getSuffix().getForCommandSource(src);
    new NucleusTextTemplateMessageSender(textTemplate, src, t -> TextParsingUtils.joinTextsWithColoursFlowing(p, t, s)).send();
    return CommandResult.success();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) NucleusTextTemplateFactory(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateFactory) CommandResult(org.spongepowered.api.command.CommandResult) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) RemainingStringsArgument(io.github.nucleuspowered.nucleus.argumentparsers.RemainingStringsArgument) AdminConfig(io.github.nucleuspowered.nucleus.modules.admin.config.AdminConfig) CommandSource(org.spongepowered.api.command.CommandSource) AdminConfigAdapter(io.github.nucleuspowered.nucleus.modules.admin.config.AdminConfigAdapter) TextParsingUtils(io.github.nucleuspowered.nucleus.internal.text.TextParsingUtils) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) AdminModule(io.github.nucleuspowered.nucleus.modules.admin.AdminModule) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) NucleusTextTemplateMessageSender(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateMessageSender) BroadcastConfig(io.github.nucleuspowered.nucleus.modules.admin.config.BroadcastConfig) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NucleusTextTemplateMessageSender(io.github.nucleuspowered.nucleus.internal.text.NucleusTextTemplateMessageSender) NucleusTextTemplate(io.github.nucleuspowered.nucleus.api.text.NucleusTextTemplate) Text(org.spongepowered.api.text.Text)

Aggregations

CommandResult (org.spongepowered.api.command.CommandResult)62 Text (org.spongepowered.api.text.Text)46 CommandContext (org.spongepowered.api.command.args.CommandContext)40 List (java.util.List)39 CommandSource (org.spongepowered.api.command.CommandSource)37 Player (org.spongepowered.api.entity.living.player.Player)36 Sponge (org.spongepowered.api.Sponge)34 Optional (java.util.Optional)33 Collectors (java.util.stream.Collectors)33 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)26 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)26 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)26 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)26 TextColors (org.spongepowered.api.text.format.TextColors)25 CommandElement (org.spongepowered.api.command.args.CommandElement)21 Util (io.github.nucleuspowered.nucleus.Util)20 GenericArguments (org.spongepowered.api.command.args.GenericArguments)20 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)18 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)18 CommandException (org.spongepowered.api.command.CommandException)18