use of org.spongepowered.api.command.CommandSource in project SpongeAPI by SpongePowered.
the class CommandFlags method tabCompleteLongFlag.
@Nullable
private List<String> tabCompleteLongFlag(String longFlag, CommandSource src, CommandArgs args, CommandContext context) {
if (longFlag.contains("=")) {
final String[] flagSplit = longFlag.split("=", 2);
longFlag = flagSplit[0];
String value = flagSplit[1];
CommandElement element = this.longFlags.get(longFlag.toLowerCase());
if (element == null) {
// Whole flag is specified, we'll go to value
context.putArg(longFlag, value);
} else {
args.insertArg(value);
final String finalLongFlag = longFlag;
Object position = args.getState();
try {
element.parse(src, args, context);
} catch (ArgumentParseException ex) {
args.setState(position);
return ImmutableList.copyOf(element.complete(src, args, context).stream().map(input -> "--" + finalLongFlag + "=" + input).collect(Collectors.toList()));
}
}
} else {
CommandElement element = this.longFlags.get(longFlag.toLowerCase());
if (element == null) {
List<String> retStrings = this.longFlags.keySet().stream().filter(new StartsWithPredicate(longFlag)).map(arg -> "--" + arg).collect(ImmutableList.toImmutableList());
if (retStrings.isEmpty() && this.unknownLongFlagBehavior == UnknownFlagBehavior.ACCEPT_VALUE) {
// Then we probably have a
// following arg specified, if there's anything
args.nextIfPresent();
return null;
}
return retStrings;
}
boolean complete = false;
Object state = args.getState();
try {
element.parse(src, args, context);
} catch (ArgumentParseException ex) {
complete = true;
}
if (!args.hasNext()) {
complete = true;
}
if (complete) {
args.setState(state);
return element.complete(src, args, context);
}
}
return null;
}
use of org.spongepowered.api.command.CommandSource in project SpongeAPI by SpongePowered.
the class PatternMatchingCommandElement method parseValue.
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
final String unformattedPattern = args.next();
Iterable<String> choices = getChoices(source);
// Check for an exact match before we create the regex.
// We do this because anything with ^[abc] would not match [abc]
Optional<Object> exactMatch = getExactMatch(choices, unformattedPattern);
if (exactMatch.isPresent()) {
// Return this as a collection as this can get transformed by the subclass.
return Collections.singleton(exactMatch.get());
}
Pattern pattern = getFormattedPattern(unformattedPattern);
Iterable<Object> ret = Iterables.transform(Iterables.filter(choices, element -> pattern.matcher(element).find()), this::getValue);
if (!ret.iterator().hasNext()) {
throw args.createError(t("No values matching pattern '%s' present for %s!", unformattedPattern, getKey() == null ? nullKeyArg : getKey()));
}
return ret;
}
use of org.spongepowered.api.command.CommandSource in project commands by aikar.
the class ACFSpongeUtil method findPlayerSmart.
public static Player findPlayerSmart(CommandIssuer issuer, String search) {
CommandSource requester = issuer.getIssuer();
if (search == null) {
return null;
}
String name = ACFUtil.replace(search, ":confirm", "");
if (name.length() < 3) {
issuer.sendError(MinecraftMessageKeys.USERNAME_TOO_SHORT);
return null;
}
if (!isValidName(name)) {
issuer.sendError(MinecraftMessageKeys.IS_NOT_A_VALID_NAME, "{name}", name);
return null;
}
List<Player> matches = matchPlayer(name);
List<Player> confirmList = new ArrayList<>();
findMatches(search, requester, matches, confirmList);
if (matches.size() > 1 || confirmList.size() > 1) {
String allMatches = matches.stream().map(Player::getName).collect(Collectors.joining(", "));
issuer.sendError(MinecraftMessageKeys.MULTIPLE_PLAYERS_MATCH, "{search}", name, "{all}", allMatches);
return null;
}
if (matches.isEmpty()) {
if (confirmList.isEmpty()) {
issuer.sendError(MinecraftMessageKeys.NO_PLAYER_FOUND_SERVER, "{search}", name);
return null;
} else {
Player player = Iterables.getOnlyElement(confirmList);
issuer.sendInfo(MinecraftMessageKeys.PLAYER_IS_VANISHED_CONFIRM, "{vanished}", player.getName());
return null;
}
}
return matches.get(0);
}
use of org.spongepowered.api.command.CommandSource in project LuckPerms by lucko.
the class SpongePlatformListener method onSendCommand.
@Listener
public void onSendCommand(SendCommandEvent e) {
CommandSource source = e.getCause().first(CommandSource.class).orElse(null);
if (source == null)
return;
final String name = e.getCommand().toLowerCase();
if (((name.equals("op") || name.equals("minecraft:op")) && source.hasPermission("minecraft.command.op")) || ((name.equals("deop") || name.equals("minecraft:deop")) && source.hasPermission("minecraft.command.deop"))) {
Message.OP_DISABLED_SPONGE.send(this.plugin.getSenderFactory().wrap(source));
}
}
use of org.spongepowered.api.command.CommandSource in project ProjectWorlds by trentech.
the class CommandLoad method process.
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if (arguments.equalsIgnoreCase("load")) {
throw new CommandException(getHelp().getUsageText());
}
if (arguments.equalsIgnoreCase("--help")) {
help.execute(source);
return CommandResult.success();
}
Optional<WorldProperties> optionalWorld = Sponge.getServer().getWorldProperties(arguments);
if (!optionalWorld.isPresent()) {
throw new CommandException(Text.of(TextColors.RED, arguments, " does not exist"), false);
}
WorldProperties world = optionalWorld.get();
if (Sponge.getServer().getWorld(world.getUniqueId()).isPresent()) {
throw new CommandException(Text.of(TextColors.RED, world.getWorldName(), " is already loaded"), false);
}
WorldData worldData = new WorldData(world.getWorldName());
if (!worldData.exists()) {
throw new CommandException(Text.of(TextColors.RED, world.getWorldName(), " does not exist"), false);
}
SpongeData spongeData = new SpongeData(world.getWorldName());
if (!spongeData.exists()) {
source.sendMessage(Text.of(TextColors.RED, "Foriegn world detected"));
source.sendMessage(Text.builder().color(TextColors.YELLOW).onHover(TextActions.showText(Text.of("Click command for more information "))).onClick(TextActions.runCommand("/pjw:world import")).append(Text.of(" /world import")).build());
return CommandResult.success();
}
source.sendMessage(Text.of(TextColors.YELLOW, "Preparing spawn area. This may take a minute."));
Task.builder().delayTicks(20).execute(c -> {
Optional<World> load = Sponge.getServer().loadWorld(world);
if (!load.isPresent()) {
source.sendMessage(Text.of(TextColors.RED, "Could not load ", world.getWorldName()));
return;
}
if (CommandCreate.worlds.contains(world.getWorldName())) {
Utils.createPlatform(load.get().getSpawnLocation().getRelative(Direction.DOWN));
CommandCreate.worlds.remove(world.getWorldName());
}
source.sendMessage(Text.of(TextColors.DARK_GREEN, world.getWorldName(), " loaded successfully"));
}).submit(Main.getPlugin());
return CommandResult.success();
}
Aggregations