Search in sources :

Example 1 with ComponentMessageThrowable

use of net.kyori.adventure.util.ComponentMessageThrowable in project SpongeCommon by SpongePowered.

the class SpongeCommandManager method process.

public CommandResult process(final CommandCause cause, final String arguments) throws CommandException, CommandSyntaxException {
    final String[] splitArg = arguments.split(" ", 2);
    final String originalCommand = splitArg[0];
    final String originalArgs = splitArg.length == 2 ? splitArg[1] : "";
    final String command;
    final String args;
    final ExecuteCommandEvent.Pre preEvent = SpongeEventFactory.createExecuteCommandEventPre(cause.cause(), originalArgs, originalArgs, originalCommand, originalCommand, cause, Optional.empty(), false);
    if (this.game.eventManager().post(preEvent)) {
        return preEvent.result().orElse(SpongeCommandManager.UNKNOWN_ERROR);
    }
    command = preEvent.command();
    args = preEvent.arguments();
    final SpongeCommandMapping mapping = this.commandMappings.get(command.toLowerCase());
    if (mapping == null) {
        throw new CommandException(Component.text("Unknown command. Type /help for a list of commands."));
    }
    final Object source = cause.cause().root();
    final CommandResult result;
    try (final CommandPhaseContext context = GeneralPhase.State.COMMAND.createPhaseContext(PhaseTracker.getInstance()).source(source).command(args).commandMapping(mapping)) {
        if (source instanceof ServerPlayer) {
            final ServerPlayer serverPlayer = (ServerPlayer) source;
            context.creator(serverPlayer.uniqueId());
            context.notifier(serverPlayer.uniqueId());
        }
        context.buildAndSwitch();
        try {
            result = this.processCommand(cause, mapping, arguments, command, args);
        } catch (final CommandException exception) {
            final CommandResult errorResult = CommandResult.builder().result(0).error(exception.componentMessage()).build();
            this.postExecuteCommandPostEvent(cause, originalArgs, args, originalCommand, command, errorResult);
            if (SpongeCommandManager.ALWAYS_PRINT_STACKTRACES) {
                this.prettyPrintThrowableError(exception, command, args, cause);
            }
            throw exception;
        } catch (final CommandSyntaxException cse) {
            final CommandResult errorResult = CommandResult.builder().result(0).error(SpongeAdventure.asAdventure(cse.getRawMessage())).build();
            this.postExecuteCommandPostEvent(cause, originalArgs, args, originalCommand, command, errorResult);
            if (SpongeCommandManager.ALWAYS_PRINT_STACKTRACES) {
                this.prettyPrintThrowableError(cse, command, args, cause);
            }
            throw cse;
        } catch (final net.minecraft.commands.CommandRuntimeException ex) {
            final CommandResult errorResult = CommandResult.builder().result(0).error(SpongeAdventure.asAdventure(ex.getComponent())).build();
            this.postExecuteCommandPostEvent(cause, originalArgs, args, originalCommand, command, errorResult);
            if (SpongeCommandManager.ALWAYS_PRINT_STACKTRACES) {
                this.prettyPrintThrowableError(ex, command, args, cause);
            }
            throw ex;
        } catch (final Throwable thr) {
            this.prettyPrintThrowableError(thr, command, args, cause);
            Component excBuilder;
            if (thr instanceof ComponentMessageThrowable) {
                final Component text = ((ComponentMessageThrowable) thr).componentMessage();
                excBuilder = text == null ? Component.text("null") : text;
            } else {
                excBuilder = Component.text(String.valueOf(thr.getMessage()));
            }
            if (cause.hasPermission(Constants.Permissions.DEBUG_HOVER_STACKTRACE)) {
                final StringWriter writer = new StringWriter();
                thr.printStackTrace(new PrintWriter(writer));
                excBuilder = excBuilder.hoverEvent(HoverEvent.showText(Component.text(writer.toString().replace("\t", "    ").replace("\r\n", "\n").replace("\r", // I mean I guess somebody could be running this on like OS 9?
                "\n"))));
            }
            final Component error = Component.text().content("Unexpected error occurred while executing command: ").append(excBuilder).build();
            this.postExecuteCommandPostEvent(cause, originalArgs, args, originalCommand, command, CommandResult.error(error));
            throw new CommandException(error, thr);
        }
        this.postExecuteCommandPostEvent(cause, originalArgs, args, originalCommand, command, result);
        if (!result.isSuccess()) {
            cause.sendMessage(Identity.nil(), result.errorMessage().map(x -> x.colorIfAbsent(NamedTextColor.RED)).orElseGet(() -> Component.text().content(String.format("An empty error result was returned while executing the command \"%s\"", arguments)).color(NamedTextColor.RED).build()));
        }
        return result;
    }
}
Also used : ExecuteCommandEvent(org.spongepowered.api.event.command.ExecuteCommandEvent) CommandException(org.spongepowered.api.command.exception.CommandException) SpongeCommandResult(org.spongepowered.common.command.result.SpongeCommandResult) CommandResult(org.spongepowered.api.command.CommandResult) ComponentMessageThrowable(net.kyori.adventure.util.ComponentMessageThrowable) StringWriter(java.io.StringWriter) CommandPhaseContext(org.spongepowered.common.event.tracking.phase.general.CommandPhaseContext) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) ComponentMessageThrowable(net.kyori.adventure.util.ComponentMessageThrowable) Component(net.kyori.adventure.text.Component) SpongeCommandSyntaxException(org.spongepowered.common.command.exception.SpongeCommandSyntaxException) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) PrintWriter(java.io.PrintWriter)

Aggregations

CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Component (net.kyori.adventure.text.Component)1 ComponentMessageThrowable (net.kyori.adventure.util.ComponentMessageThrowable)1 CommandResult (org.spongepowered.api.command.CommandResult)1 CommandException (org.spongepowered.api.command.exception.CommandException)1 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)1 ExecuteCommandEvent (org.spongepowered.api.event.command.ExecuteCommandEvent)1 SpongeCommandSyntaxException (org.spongepowered.common.command.exception.SpongeCommandSyntaxException)1 SpongeCommandResult (org.spongepowered.common.command.result.SpongeCommandResult)1 CommandPhaseContext (org.spongepowered.common.event.tracking.phase.general.CommandPhaseContext)1