Search in sources :

Example 51 with CommandSource

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;
}
Also used : Iterator(java.util.Iterator) CommandSource(org.spongepowered.api.command.CommandSource) GenericArguments.string(org.spongepowered.api.command.args.GenericArguments.string) HashMap(java.util.HashMap) GenericArguments.markTrue(org.spongepowered.api.command.args.GenericArguments.markTrue) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) List(java.util.List) SpongeApiTranslationHelper.t(org.spongepowered.api.util.SpongeApiTranslationHelper.t) ImmutableList(com.google.common.collect.ImmutableList) Text(org.spongepowered.api.text.Text) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) Map(java.util.Map) Optional(java.util.Optional) GenericArguments.requiringPermission(org.spongepowered.api.command.args.GenericArguments.requiringPermission) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) Nullable(javax.annotation.Nullable)

Example 52 with CommandSource

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;
}
Also used : Iterables(com.google.common.collect.Iterables) List(java.util.List) SpongeApiTranslationHelper.t(org.spongepowered.api.util.SpongeApiTranslationHelper.t) ImmutableList(com.google.common.collect.ImmutableList) Text(org.spongepowered.api.text.Text) CommandSource(org.spongepowered.api.command.CommandSource) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) Pattern(java.util.regex.Pattern) Nullable(javax.annotation.Nullable)

Example 53 with CommandSource

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);
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) ArrayList(java.util.ArrayList) CommandSource(org.spongepowered.api.command.CommandSource)

Example 54 with CommandSource

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));
    }
}
Also used : CommandSource(org.spongepowered.api.command.CommandSource) Listener(org.spongepowered.api.event.Listener)

Example 55 with CommandSource

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();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) Location(org.spongepowered.api.world.Location) Utils(com.gmail.trentech.pjw.utils.Utils) CommandSource(org.spongepowered.api.command.CommandSource) WorldData(com.gmail.trentech.pjw.io.WorldData) CommandCallable(org.spongepowered.api.command.CommandCallable) Sponge(org.spongepowered.api.Sponge) ArrayList(java.util.ArrayList) CommandException(org.spongepowered.api.command.CommandException) Direction(org.spongepowered.api.util.Direction) List(java.util.List) Text(org.spongepowered.api.text.Text) Main(com.gmail.trentech.pjw.Main) World(org.spongepowered.api.world.World) Task(org.spongepowered.api.scheduler.Task) WorldProperties(org.spongepowered.api.world.storage.WorldProperties) Help(com.gmail.trentech.pjc.help.Help) Optional(java.util.Optional) SpongeData(com.gmail.trentech.pjw.io.SpongeData) TextColors(org.spongepowered.api.text.format.TextColors) Optional(java.util.Optional) SpongeData(com.gmail.trentech.pjw.io.SpongeData) CommandException(org.spongepowered.api.command.CommandException) WorldData(com.gmail.trentech.pjw.io.WorldData) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

CommandSource (org.spongepowered.api.command.CommandSource)91 Text (org.spongepowered.api.text.Text)60 CommandResult (org.spongepowered.api.command.CommandResult)48 List (java.util.List)47 CommandContext (org.spongepowered.api.command.args.CommandContext)45 Collectors (java.util.stream.Collectors)37 Sponge (org.spongepowered.api.Sponge)37 Player (org.spongepowered.api.entity.living.player.Player)36 Optional (java.util.Optional)35 TextColors (org.spongepowered.api.text.format.TextColors)30 CommandElement (org.spongepowered.api.command.args.CommandElement)27 TextActions (org.spongepowered.api.text.action.TextActions)27 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)25 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)24 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)23 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)23 GenericArguments (org.spongepowered.api.command.args.GenericArguments)23 Util (io.github.nucleuspowered.nucleus.Util)20 Nullable (javax.annotation.Nullable)20 Map (java.util.Map)19