use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.
the class ListPowertoolCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
boolean toggle = inu.isPowertoolToggled();
Map<String, List<String>> powertools = inu.getPowertools();
if (powertools.isEmpty()) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.powertool.list.none"));
return CommandResult.success();
}
// Generate powertools.
List<Text> mesl = powertools.entrySet().stream().sorted((a, b) -> a.getKey().compareToIgnoreCase(b.getKey())).map(k -> from(inu, src, k.getKey(), k.getValue())).collect(Collectors.toList());
// Paginate the tools.
Util.getPaginationBuilder(src).title(plugin.getMessageProvider().getTextMessageWithFormat("command.powertool.list.header", toggle ? "&aenabled" : "&cdisabled")).padding(Text.of(TextColors.YELLOW, "-")).contents(mesl).sendTo(src);
return CommandResult.success();
}
use of org.spongepowered.api.command.CommandResult 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;
}
}
use of org.spongepowered.api.command.CommandResult in project Skree by Skelril.
the class MarketListCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
if (!optService.isPresent()) {
src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
return CommandResult.empty();
}
MarketService service = optService.get();
PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
Optional<String> optFilter = args.getOne("name");
String filter = optFilter.isPresent() ? optFilter.get() : "";
if (!filter.matches(MarketService.VALID_ALIAS_REGEX)) {
src.sendMessage(Text.of(TextColors.RED, "Invalid filter supplied."));
return CommandResult.empty();
}
List<ItemDescriptor> prices = filter.isEmpty() ? service.getPrices() : service.getPrices(filter + "%");
if (prices.isEmpty()) {
src.sendMessage(Text.of(TextColors.YELLOW, "No items matched."));
return CommandResult.success();
}
List<Text> result = prices.stream().filter(a -> filter.isEmpty() || a.getName().startsWith(filter)).sorted(Comparator.comparing(ItemDescriptor::getCurrentValue)).map(a -> createLine(a, service)).collect(Collectors.toList());
pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Item List")).padding(Text.of(" ")).sendTo(src);
return CommandResult.success();
}
use of org.spongepowered.api.command.CommandResult in project Skree by Skelril.
the class RegionAddMemberCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("You must be a player to use this command (for now ;) )!"));
return CommandResult.empty();
}
Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
if (!optService.isPresent()) {
src.sendMessage(Text.of(TextColors.DARK_RED, "The region service is not currently running."));
return CommandResult.empty();
}
RegionService service = optService.get();
Player player = (Player) src;
Optional<Region> optRef = service.getSelectedRegion(player);
if (!optRef.isPresent()) {
player.sendMessage(Text.of(TextColors.RED, "You do not currently have a region selected."));
return CommandResult.empty();
}
Region ref = optRef.get();
if (!ref.getMembers().contains(player.getUniqueId())) {
player.sendMessage(Text.of(TextColors.RED, "You must be a member of the region to modify it!"));
return CommandResult.empty();
}
List<UUID> newMembers = args.<User>getAll("player").stream().map(Identifiable::getUniqueId).filter(a -> !ref.getMembers().contains(a)).collect(Collectors.toList());
ref.addMember(newMembers);
player.sendMessage(Text.of(TextColors.YELLOW, "Added ", newMembers.size(), " players to the region."));
return CommandResult.success();
}
use of org.spongepowered.api.command.CommandResult in project Skree by Skelril.
the class RegionListMarkersCommand method execute.
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
if (!(src instanceof Player)) {
src.sendMessage(Text.of("You must be a player to use this command (for now ;) )!"));
return CommandResult.empty();
}
Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
if (!optService.isPresent()) {
src.sendMessage(Text.of(TextColors.DARK_RED, "The region service is not currently running."));
return CommandResult.empty();
}
RegionService service = optService.get();
Player player = (Player) src;
Optional<Region> optRef = service.getSelectedRegion(player);
if (!optRef.isPresent()) {
player.sendMessage(Text.of(TextColors.RED, "You do not currently have a region selected."));
return CommandResult.empty();
}
Region ref = optRef.get();
PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
List<RegionPoint> points = ref.getPoints();
List<Text> result = ref.getFullPoints().stream().map(a -> Text.of((points.contains(a) ? TextColors.GREEN : TextColors.RED), a.getFloorX(), ", ", a.getFloorY(), ", ", a.getFloorZ())).collect(Collectors.toList());
pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Region Markers")).padding(Text.of(" ")).sendTo(src);
return CommandResult.success();
}
Aggregations