use of org.spongepowered.api.util.TextMessageException 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();
}
use of org.spongepowered.api.util.TextMessageException in project LanternServer by LanternPowered.
the class LanternCommandManager method process.
@Override
public CommandResult process(CommandSource source, String commandLine) {
checkNotNull(source, "source");
final String[] argSplit = commandLine.split(" ", 2);
final CauseStack causeStack = CauseStack.currentOrEmpty();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.pushCause(source);
final SendCommandEvent event = SpongeEventFactory.createSendCommandEvent(frame.getCurrentCause(), argSplit.length > 1 ? argSplit[1] : "", argSplit[0], CommandResult.empty());
Sponge.getGame().getEventManager().post(event);
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 {
return this.dispatcher.process(source, commandLine);
} 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);
mapping.ifPresent(commandMapping -> source.sendMessage(error(t("commands.generic.usage", t("/%s %s", argSplit[0], commandMapping.getCallable().getUsage(source))))));
}
}
} catch (Throwable thr) {
final Text.Builder excBuilder;
if (thr instanceof TextMessageException) {
final Text text = ((TextMessageException) thr).getText();
excBuilder = text == null ? Text.builder("null") : Text.builder().append(text);
} 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(LanternTexts.toLegacy(t("Error occurred while executing command '%s' for source %s: %s", commandLine, source.toString(), String.valueOf(thr.getMessage()))), thr);
}
return CommandResult.empty();
}
use of org.spongepowered.api.util.TextMessageException in project Nucleus by NucleusPowered.
the class InfoCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
// Sponge versions
List<String> information = Lists.newArrayList();
String separator = "------------";
information.add(separator);
information.add("Nucleus Diagnostics");
information.add(separator);
information.add("This file contains information about Nucleus and the environment it runs in.");
information.add(separator);
information.add("Environment");
information.add(separator);
Platform platform = Sponge.getPlatform();
PluginContainer game = platform.getContainer(Platform.Component.GAME);
PluginContainer implementation = platform.getContainer(Platform.Component.IMPLEMENTATION);
PluginContainer api = platform.getContainer(Platform.Component.API);
information.add(String.format("Minecraft Version: %s %s", game.getName(), game.getVersion().orElse("unknown")));
information.add(String.format("Sponge Version: %s %s", implementation.getName(), implementation.getVersion().orElse("unknown")));
information.add(String.format("Sponge API Version: %s %s", api.getName(), api.getVersion().orElse("unknown")));
information.add("Nucleus Version: " + PluginInfo.VERSION + " (Git: " + PluginInfo.GIT_HASH + ")");
information.add(separator);
information.add("Plugins");
information.add(separator);
Sponge.getPluginManager().getPlugins().forEach(x -> information.add(x.getName() + " (" + x.getId() + ") version " + x.getVersion().orElse("unknown")));
information.add(separator);
information.add("Registered Commands");
information.add(separator);
final Map<String, String> commands = Maps.newHashMap();
final Map<String, String> plcmds = Maps.newHashMap();
final CommandManager manager = Sponge.getCommandManager();
manager.getPrimaryAliases().forEach(x -> {
Optional<? extends CommandMapping> ocm = manager.get(x);
if (ocm.isPresent()) {
Set<String> a = ocm.get().getAllAliases();
Optional<PluginContainer> optionalPC = manager.getOwner(ocm.get());
if (optionalPC.isPresent()) {
PluginContainer container = optionalPC.get();
String id = container.getId();
String info = " - " + container.getName() + " (" + id + ") version " + container.getVersion().orElse("unknown");
a.forEach(y -> {
if (y.startsWith(id + ":")) {
// /nucleus:<blah>
plcmds.put(y, "/" + y + info);
} else {
commands.put(y, "/" + y + info);
}
});
} else {
String info = " - unknown (plugin container not present)";
a.forEach(y -> commands.put(y, "/" + y + info));
}
} else {
commands.put(x, "/" + x + " - unknown (mapping not present)");
}
});
commands.entrySet().stream().sorted(Comparator.comparing(x -> x.getKey().toLowerCase())).forEachOrdered(x -> information.add(x.getValue()));
information.add(separator);
information.add("Namespaced commands");
information.add(separator);
plcmds.entrySet().stream().sorted(Comparator.comparing(x -> x.getKey().toLowerCase())).forEachOrdered(x -> information.add(x.getValue()));
information.add(separator);
information.add("Nucleus: Enabled Modules");
information.add(separator);
plugin.getModuleContainer().getModules().stream().sorted().forEach(information::add);
Set<String> disabled = plugin.getModuleContainer().getModules(ModuleContainer.ModuleStatusTristate.DISABLE);
if (!disabled.isEmpty()) {
information.add(separator);
information.add("Nucleus: Disabled Modules");
information.add(separator);
disabled.stream().sorted().forEach(information::add);
}
String fileName = "nucleus-info-" + DateTimeFormatter.BASIC_ISO_DATE.format(LocalDateTime.now()) + "-" + DateTimeFormatter.ofPattern("HHmmss").format(LocalDateTime.now()) + ".txt";
try (BufferedWriter fw = new BufferedWriter(new FileWriter(fileName, false))) {
for (String s : information) {
fw.write(s);
fw.newLine();
}
fw.flush();
} catch (Exception e) {
throw new TextMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.info.fileerror"), e);
}
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.info.saved", fileName));
return CommandResult.success();
}
Aggregations