Search in sources :

Example 1 with CommandPhaseContext

use of org.spongepowered.common.event.tracking.phase.general.CommandPhaseContext in project SpongeCommon by SpongePowered.

the class SpongeCommandManager method process.

@Override
public CommandResult process(CommandSource source, String commandLine) {
    final String[] argSplit = commandLine.split(" ", 2);
    Sponge.getCauseStackManager().pushCause(source);
    final SendCommandEvent event = SpongeEventFactory.createSendCommandEvent(Sponge.getCauseStackManager().getCurrentCause(), argSplit.length > 1 ? argSplit[1] : "", argSplit[0], CommandResult.empty());
    Sponge.getGame().getEventManager().post(event);
    Sponge.getCauseStackManager().popCause();
    if (event.isCancelled()) {
        return event.getResult();
    }
    // Only the first part of argSplit is used at the moment, do the other in the future if needed.
    argSplit[0] = event.getCommand();
    commandLine = event.getCommand();
    if (!event.getArguments().isEmpty()) {
        commandLine = commandLine + ' ' + event.getArguments();
    }
    try {
        try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
            // Since we know we are in the main thread, this is safe to do without a thread check
            CommandPhaseContext context = GeneralPhase.State.COMMAND.createPhaseContext().source(source).addCaptures().addEntityDropCaptures().buildAndSwitch()) {
            if (source instanceof EntityPlayer) {
                // Enable player inventory capture
                ((IMixinInventoryPlayer) ((EntityPlayer) source).inventory).setCapture(true);
            }
            Sponge.getCauseStackManager().pushCause(source);
            final CommandResult result = this.dispatcher.process(source, commandLine);
            return result;
        } catch (InvocationCommandException ex) {
            if (ex.getCause() != null) {
                throw ex.getCause();
            }
        } catch (CommandPermissionException ex) {
            Text text = ex.getText();
            if (text != null) {
                source.sendMessage(error(text));
            }
        } catch (CommandException ex) {
            Text text = ex.getText();
            if (text != null) {
                source.sendMessage(error(text));
            }
            if (ex.shouldIncludeUsage()) {
                final Optional<CommandMapping> mapping = this.dispatcher.get(argSplit[0], source);
                if (mapping.isPresent()) {
                    Text usage;
                    if (ex instanceof ArgumentParseException.WithUsage) {
                        usage = ((ArgumentParseException.WithUsage) ex).getUsage();
                    } else {
                        usage = mapping.get().getCallable().getUsage(source);
                    }
                    source.sendMessage(error(t("Usage: /%s %s", argSplit[0], usage)));
                }
            }
        }
    } catch (Throwable thr) {
        Text.Builder excBuilder;
        if (thr instanceof TextMessageException) {
            Text text = ((TextMessageException) thr).getText();
            excBuilder = text == null ? Text.builder("null") : Text.builder();
        } else {
            excBuilder = Text.builder(String.valueOf(thr.getMessage()));
        }
        if (source.hasPermission("sponge.debug.hover-stacktrace")) {
            final StringWriter writer = new StringWriter();
            thr.printStackTrace(new PrintWriter(writer));
            excBuilder.onHover(TextActions.showText(Text.of(writer.toString().replace("\t", "    ").replace("\r\n", "\n").replace("\r", // I mean I guess somebody could be running this on like OS 9?
            "\n"))));
        }
        source.sendMessage(error(t("Error occurred while executing command: %s", excBuilder.build())));
        this.logger.error(TextSerializers.PLAIN.serialize(t("Error occurred while executing command '%s' for source %s: %s", commandLine, source.toString(), String.valueOf(thr.getMessage()))), thr);
    }
    return CommandResult.empty();
}
Also used : CommandPermissionException(org.spongepowered.api.command.CommandPermissionException) Optional(java.util.Optional) ArgumentParseException(org.spongepowered.api.command.args.ArgumentParseException) Text(org.spongepowered.api.text.Text) InvocationCommandException(org.spongepowered.api.command.InvocationCommandException) CommandException(org.spongepowered.api.command.CommandException) CommandResult(org.spongepowered.api.command.CommandResult) IMixinInventoryPlayer(org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer) InvocationCommandException(org.spongepowered.api.command.InvocationCommandException) StringWriter(java.io.StringWriter) CommandPhaseContext(org.spongepowered.common.event.tracking.phase.general.CommandPhaseContext) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) SendCommandEvent(org.spongepowered.api.event.command.SendCommandEvent) EntityPlayer(net.minecraft.entity.player.EntityPlayer) TextMessageException(org.spongepowered.api.util.TextMessageException) PrintWriter(java.io.PrintWriter)

Aggregations

PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Optional (java.util.Optional)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 CommandException (org.spongepowered.api.command.CommandException)1 CommandPermissionException (org.spongepowered.api.command.CommandPermissionException)1 CommandResult (org.spongepowered.api.command.CommandResult)1 InvocationCommandException (org.spongepowered.api.command.InvocationCommandException)1 ArgumentParseException (org.spongepowered.api.command.args.ArgumentParseException)1 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)1 SendCommandEvent (org.spongepowered.api.event.command.SendCommandEvent)1 Text (org.spongepowered.api.text.Text)1 TextMessageException (org.spongepowered.api.util.TextMessageException)1 CommandPhaseContext (org.spongepowered.common.event.tracking.phase.general.CommandPhaseContext)1 IMixinInventoryPlayer (org.spongepowered.common.interfaces.entity.player.IMixinInventoryPlayer)1