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;
}
}
Aggregations