Search in sources :

Example 1 with CommandBase

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

the class CubeCommandManager method addCommand.

@Override
public boolean addCommand(CommandBase command) {
    if (command instanceof AliasCommand) {
        Set<CommandBase> cmds = commands.computeIfAbsent(((AliasCommand) command).getTarget(), k -> new HashSet<>());
        cmds.add(command);
    }
    if (command.getDescriptor() instanceof CubeDescriptor) {
        CubeDescriptor descriptor = (CubeDescriptor) command.getDescriptor();
        String name = mm.getModuleName(descriptor.getOwner()).orElse(descriptor.getOwner().getSimpleName());
        Permission parent = pm.register(descriptor.getOwner(), "command", "Allows using all commands of " + name, null);
        descriptor.registerPermission(pm, parent);
    }
    boolean b = super.addCommand(command);
    if (!(command instanceof AliasCommand) || ((AliasDescriptor) command.getDescriptor()).mainDescriptor().getDispatcher() != this) {
        Optional<CommandMapping> mapping = registerSpongeCommand(command.getDescriptor());
        if (mapping.isPresent()) {
            mappings.put(command, mapping.get());
            commandLogger.debug("Registered command: " + mapping.get().getPrimaryAlias());
            return b;
        }
        commandLogger.warn("Command was not registered successfully!");
    }
    return b;
}
Also used : AliasCommand(org.cubeengine.butler.alias.AliasCommand) CommandBase(org.cubeengine.butler.CommandBase) CommandMapping(org.spongepowered.api.command.CommandMapping) Permission(org.cubeengine.libcube.service.permission.Permission)

Example 2 with CommandBase

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

the class CubeCommandManager method addCommands.

/**
 * Creates {@link org.cubeengine.butler.parametric.BasicParametricCommand} for all methods annotated as a command
 * in the given commandHolder and add them to the given dispatcher
 *
 * @param dispatcher    the dispatcher to add the commands to
 * @param owner        the module owning the commands
 * @param commandHolder the command holder containing the command-methods
 */
@SuppressWarnings("unchecked")
@Override
public void addCommands(Dispatcher dispatcher, Object owner, Object commandHolder) {
    Set<CommandBase> cmds = this.commands.get(owner);
    if (cmds == null) {
        cmds = new HashSet<>();
    }
    this.commands.put(owner, cmds);
    dispatcher = new OwnedDispatcher(dispatcher, owner);
    for (Method method : ParametricBuilder.getMethods(commandHolder.getClass())) {
        CommandBuilder<InvokableMethod> builder = getProviders().getBuilder(InvokableMethod.class);
        CommandBase cmd = builder.buildCommand(dispatcher, new InvokableMethod(method, commandHolder));
        if (cmd != null) {
            dispatcher.addCommand(cmd);
            cmds.add(cmd);
        }
    }
}
Also used : CommandBase(org.cubeengine.butler.CommandBase) Method(java.lang.reflect.Method) InvokableMethod(org.cubeengine.butler.parametric.InvokableMethod) InvokableMethod(org.cubeengine.butler.parametric.InvokableMethod)

Example 3 with CommandBase

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

the class CubeCommandManager method injectCommands.

public void injectCommands(Injector injector, Object module, List<Field> fields) {
    for (Field field : fields) {
        Class<? extends ContainerCommand> parent = field.getAnnotation(ModuleCommand.class).value();
        Dispatcher dispatcher = parent == ContainerCommand.class ? this : getDispatcher(parent);
        Object command = injector.getInstance(field.getType());
        boolean isCommand = CommandBase.class.isAssignableFrom(command.getClass());
        if (isCommand) {
            dispatcher.addCommand(((CommandBase) command));
            if (command instanceof Dispatcher) {
                this.injectedDispatchers.put(command.getClass(), ((Dispatcher) command));
            }
        } else {
            addCommands(dispatcher, module, command);
        }
        try {
            field.setAccessible(true);
            field.set(module, command);
        } catch (IllegalAccessException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : Field(java.lang.reflect.Field) CommandBase(org.cubeengine.butler.CommandBase) Dispatcher(org.cubeengine.butler.Dispatcher)

Example 4 with CommandBase

use of org.cubeengine.butler.CommandBase 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)

Aggregations

CommandBase (org.cubeengine.butler.CommandBase)4 Dispatcher (org.cubeengine.butler.Dispatcher)2 AliasCommand (org.cubeengine.butler.alias.AliasCommand)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 CommandDescriptor (org.cubeengine.butler.CommandDescriptor)1 CommandInvocation (org.cubeengine.butler.CommandInvocation)1 DispatcherCommand (org.cubeengine.butler.DispatcherCommand)1 SimpleCommandDescriptor (org.cubeengine.butler.SimpleCommandDescriptor)1 InvokableMethod (org.cubeengine.butler.parametric.InvokableMethod)1 ParametricContainerCommand (org.cubeengine.butler.parametric.ParametricContainerCommand)1 I18n (org.cubeengine.libcube.service.i18n.I18n)1 MessageType (org.cubeengine.libcube.service.i18n.formatter.MessageType)1 NEUTRAL (org.cubeengine.libcube.service.i18n.formatter.MessageType.NEUTRAL)1 NONE (org.cubeengine.libcube.service.i18n.formatter.MessageType.NONE)1 Permission (org.cubeengine.libcube.service.permission.Permission)1 CommandMapping (org.spongepowered.api.command.CommandMapping)1