Search in sources :

Example 1 with CommandInvocation

use of org.cubeengine.butler.CommandInvocation in project core by CubeEngine.

the class ConversationCommand method onChatHandler.

@Listener
public void onChatHandler(MessageChannelEvent.Chat event, @First Player player) {
    if (this.hasUser(player)) {
        player.sendMessage(Text.of(DARK_PURPLE, "[", WHITE, getDescriptor().getName(), DARK_PURPLE, "] ", WHITE, event.getMessage()));
        Text message = event.getRawMessage();
        // TODO
        CommandInvocation invocation = newInvocation(player, message.toPlain());
        this.execute(invocation);
        event.setCancelled(true);
    }
}
Also used : Text(org.spongepowered.api.text.Text) CommandInvocation(org.cubeengine.butler.CommandInvocation) Listener(org.spongepowered.api.event.Listener)

Example 2 with CommandInvocation

use of org.cubeengine.butler.CommandInvocation in project core by CubeEngine.

the class HelpCommand method execute.

@Override
public boolean execute(CommandInvocation invocation) {
    if (!(invocation.getCommandSource() instanceof CommandSource)) {
        return false;
    }
    CommandDescriptor descriptor = helpTarget.getDescriptor();
    CommandSource sender = (CommandSource) invocation.getCommandSource();
    TextFormat formatGray = NONE.color(GRAY);
    i18n.send(sender, formatGray, "Description: {input}", i18n.translate(sender, MessageType.NONE, descriptor.getDescription()).toPlain());
    List<String> labels = new ArrayList<>(invocation.getLabels());
    if (labels.isEmpty()) {
        labels.add("");
    }
    if ("?".equals(labels.get(labels.size() - 1))) {
        labels.remove(labels.size() - 1);
    }
    i18n.send(sender, formatGray, "Usage: {input}", descriptor.getUsage(invocation, labels.toArray(new String[labels.size()])));
    sender.sendMessage(Text.of());
    if (helpTarget instanceof DispatcherCommand) {
        Set<CommandBase> commands = helpTarget.getCommands();
        if (!commands.isEmpty() && (commands.size() != 1 || // is Empty ignoring HelpCommand
        !(commands.iterator().next() instanceof HelpCommand))) {
            i18n.send(sender, NEUTRAL, "The following sub-commands are available:");
            sender.sendMessage(Text.of());
            commands.stream().filter(command -> !(command instanceof HelpCommand || command instanceof AliasCommand && commands.contains(((AliasCommand) command).getTarget()))).filter(command -> !(command.getDescriptor() instanceof CubeCommandDescriptor && ((CubeCommandDescriptor) command.getDescriptor()).isCheckPerm() && !sender.hasPermission(((CubeCommandDescriptor) command.getDescriptor()).getPermission().getName()))).forEach(command -> sender.sendMessage(Text.of(YELLOW, command.getDescriptor().getName()).toBuilder().onClick(TextActions.runCommand("/" + (String.join(" ", labels) + " " + command.getDescriptor().getName()).trim() + " ?")).append(Text.of(WHITE, ": ", GRAY, i18n.translate(sender, TextFormat.NONE, command.getDescriptor().getDescription()))).build()));
            sender.sendMessage(Text.of());
        } else if (helpTarget instanceof ParametricContainerCommand) {
            i18n.send(sender, MessageType.NEGATIVE, "No actions are available");
            sender.sendMessage(Text.of());
        }
    }
    /*
        if (descriptor instanceof CubeDescriptor)
        {
            sender.sendTranslated(GRAY, "Detailed help: {input#link:color=INDIGO}", "http://engine.cubeisland.de/c/" + ((CubeDescriptor)descriptor).getModule().getInformation().getName().toLowerCase() + "/" + StringUtils.implode("/", labels));
        }
        */
    return true;
}
Also used : TextActions(org.spongepowered.api.text.action.TextActions) I18n(org.cubeengine.libcube.service.i18n.I18n) CommandSource(org.spongepowered.api.command.CommandSource) CommandBase(org.cubeengine.butler.CommandBase) MessageType(org.cubeengine.libcube.service.i18n.formatter.MessageType) Set(java.util.Set) NEUTRAL(org.cubeengine.libcube.service.i18n.formatter.MessageType.NEUTRAL) Dispatcher(org.cubeengine.butler.Dispatcher) SimpleCommandDescriptor(org.cubeengine.butler.SimpleCommandDescriptor) CommandInvocation(org.cubeengine.butler.CommandInvocation) ArrayList(java.util.ArrayList) CommandDescriptor(org.cubeengine.butler.CommandDescriptor) List(java.util.List) NONE(org.cubeengine.libcube.service.i18n.formatter.MessageType.NONE) Text(org.spongepowered.api.text.Text) TextFormat(org.spongepowered.api.text.format.TextFormat) AliasCommand(org.cubeengine.butler.alias.AliasCommand) DispatcherCommand(org.cubeengine.butler.DispatcherCommand) TextColors(org.spongepowered.api.text.format.TextColors) ParametricContainerCommand(org.cubeengine.butler.parametric.ParametricContainerCommand) ArrayList(java.util.ArrayList) CommandSource(org.spongepowered.api.command.CommandSource) SimpleCommandDescriptor(org.cubeengine.butler.SimpleCommandDescriptor) CommandDescriptor(org.cubeengine.butler.CommandDescriptor) AliasCommand(org.cubeengine.butler.alias.AliasCommand) CommandBase(org.cubeengine.butler.CommandBase) TextFormat(org.spongepowered.api.text.format.TextFormat) ParametricContainerCommand(org.cubeengine.butler.parametric.ParametricContainerCommand) DispatcherCommand(org.cubeengine.butler.DispatcherCommand)

Example 3 with CommandInvocation

use of org.cubeengine.butler.CommandInvocation in project core by CubeEngine.

the class ProxyCallable method process.

@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
    try {
        CommandInvocation invocation = newInvocation(source, arguments.isEmpty() ? alias : alias + " " + arguments);
        long delta = System.currentTimeMillis();
        boolean ran;
        try (Timing timing = Timings.ofStart(manager.getPlugin(), "CE Command Execute " + alias);
            Summary.Timer t = commandTimeSummary.startTimer()) {
            ran = manager.execute(invocation);
        }
        delta = System.currentTimeMillis() - delta;
        if (// third of a tick
        delta > 1000 / 20 / 3) {
            logger.warn("Command Execute Timing: {} {} | {}ms ({}%)", this.alias, arguments, delta, delta * 100 / (1000 / 20));
        }
        manager.logExecution(source, ran, alias, arguments);
        return CommandResult.success();
    } catch (Exception e) {
        logger.error(e, "An Unknown Exception occurred while executing a command! Command: {}", alias + " " + arguments);
        return CommandResult.empty();
    }
}
Also used : Summary(io.prometheus.client.Summary) Timing(co.aikar.timings.Timing) CommandException(org.spongepowered.api.command.CommandException) CommandInvocation(org.cubeengine.butler.CommandInvocation)

Example 4 with CommandInvocation

use of org.cubeengine.butler.CommandInvocation in project core by CubeEngine.

the class EnchantmentParser method suggest.

@Override
public List<String> suggest(Class clazz, CommandInvocation invocation) {
    CommandSource sender = (CommandSource) invocation.getCommandSource();
    ItemStack item = sender instanceof Player ? ((Player) sender).getItemInHand(HandTypes.MAIN_HAND).orElse(null) : null;
    String token = invocation.currentToken();
    return Sponge.getRegistry().getAllOf(EnchantmentType.class).stream().filter(e -> item == null || e.canBeAppliedToStack(item)).map(e -> e.getTranslation().get().replace(" ", "")).filter(name -> name.toLowerCase().startsWith(token.toLowerCase())).collect(Collectors.toList());
}
Also used : SilentException(org.cubeengine.butler.exception.SilentException) ParserException(org.cubeengine.butler.parameter.argument.ParserException) Game(org.spongepowered.api.Game) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) NEUTRAL(org.cubeengine.libcube.service.i18n.formatter.MessageType.NEUTRAL) GameRegistry(org.spongepowered.api.GameRegistry) TextActions.showText(org.spongepowered.api.text.action.TextActions.showText) ArgumentParser(org.cubeengine.butler.parameter.argument.ArgumentParser) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Text(org.spongepowered.api.text.Text) Completer(org.cubeengine.butler.parameter.argument.Completer) YELLOW(org.spongepowered.api.text.format.TextColors.YELLOW) TextColors(org.spongepowered.api.text.format.TextColors) DefaultValue(org.cubeengine.butler.parameter.argument.DefaultValue) I18n(org.cubeengine.libcube.service.i18n.I18n) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) EnchantMatcher(org.cubeengine.libcube.service.matcher.EnchantMatcher) POSITIVE(org.cubeengine.libcube.service.i18n.formatter.MessageType.POSITIVE) CommandInvocation(org.cubeengine.butler.CommandInvocation) Collectors(java.util.stream.Collectors) List(java.util.List) HandTypes(org.spongepowered.api.data.type.HandTypes) Player(org.spongepowered.api.entity.living.player.Player) NEGATIVE(org.cubeengine.libcube.service.i18n.formatter.MessageType.NEGATIVE) TooFewArgumentsException(org.cubeengine.butler.parameter.TooFewArgumentsException) Player(org.spongepowered.api.entity.living.player.Player) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) CommandSource(org.spongepowered.api.command.CommandSource) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

CommandInvocation (org.cubeengine.butler.CommandInvocation)4 Text (org.spongepowered.api.text.Text)3 List (java.util.List)2 I18n (org.cubeengine.libcube.service.i18n.I18n)2 NEUTRAL (org.cubeengine.libcube.service.i18n.formatter.MessageType.NEUTRAL)2 CommandSource (org.spongepowered.api.command.CommandSource)2 TextColors (org.spongepowered.api.text.format.TextColors)2 Timing (co.aikar.timings.Timing)1 Summary (io.prometheus.client.Summary)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CommandBase (org.cubeengine.butler.CommandBase)1 CommandDescriptor (org.cubeengine.butler.CommandDescriptor)1 Dispatcher (org.cubeengine.butler.Dispatcher)1 DispatcherCommand (org.cubeengine.butler.DispatcherCommand)1 SimpleCommandDescriptor (org.cubeengine.butler.SimpleCommandDescriptor)1 AliasCommand (org.cubeengine.butler.alias.AliasCommand)1 SilentException (org.cubeengine.butler.exception.SilentException)1 TooFewArgumentsException (org.cubeengine.butler.parameter.TooFewArgumentsException)1