Java Code Examples for org.bukkit.util.StringUtil

The following examples show how to use org.bukkit.util.StringUtil. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) {
    List<String> completionList = new ArrayList<>();
    if (sender instanceof Player) {
        Player player = (Player) sender;
        if (args.length == 1) {
            for (BaseCommand c : Main.getCommands().values()) {
                if (c.hasPermission(player)) {
                    completionList.add(c.getName());
                }
            }
        } else if (args.length > 1) {
            ArrayList<String> arguments = new ArrayList<>(Arrays.asList(args));
            arguments.remove(0);
            BaseCommand bCommand = Main.getCommands().get(args[0]);
            if (bCommand != null) {
                if (bCommand.hasPermission(player)) {
                    bCommand.completeTab(completionList, sender, arguments);
                }
            }
        }
    }
    List<String> finalCompletionList = new ArrayList<>();
    StringUtil.copyPartialMatches(args[args.length - 1], completionList, finalCompletionList);
    return finalCompletionList;
}
 
Example 2
@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String alias, String[] args) {
    List<String> completionList = new ArrayList<>();
    if (sender instanceof Player) {
        Player player = (Player) sender;
        if (args.length == 1) {
            for (BaseCommand c : Main.getCommands().values()) {
                if (c.hasPermission(player)) {
                    completionList.add(c.getName());
                }
            }
        } else if (args.length > 1) {
            ArrayList<String> arguments = new ArrayList<>(Arrays.asList(args));
            arguments.remove(0);
            BaseCommand bCommand = Main.getCommands().get(args[0]);
            if (bCommand != null) {
                if (bCommand.hasPermission(player)) {
                    bCommand.completeTab(completionList, sender, arguments);
                }
            }
        }
    }
    List<String> finalCompletionList = new ArrayList<>();
    StringUtil.copyPartialMatches(args[args.length - 1], completionList, finalCompletionList);
    return finalCompletionList;
}
 
Example 3
Source Project: Kettle   Source File: VersionCommand.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 1) {
        List<String> completions = new ArrayList<>();
        String toComplete = args[0].toLowerCase(java.util.Locale.ENGLISH);
        for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
            if (StringUtil.startsWithIgnoreCase(plugin.getName(), toComplete)) {
                completions.add(plugin.getName());
            }
        }
        return completions;
    }
    return ImmutableList.of();
}
 
Example 4
Source Project: Kettle   Source File: Command.java    License: GNU General Public License v3.0 6 votes vote down vote up
private List<String> tabComplete0(CommandSender sender, String alias, String[] args, Location location) throws IllegalArgumentException {
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 0) {
        return ImmutableList.of();
    }

    String lastWord = args[args.length - 1];

    Player senderPlayer = sender instanceof Player ? (Player) sender : null;

    ArrayList<String> matchedPlayers = new ArrayList<String>();
    for (Player player : sender.getServer().getOnlinePlayers()) {
        String name = player.getName();
        if ((senderPlayer == null || senderPlayer.canSee(player)) && StringUtil.startsWithIgnoreCase(name, lastWord)) {
            matchedPlayers.add(name);
        }
    }

    Collections.sort(matchedPlayers, String.CASE_INSENSITIVE_ORDER);
    return matchedPlayers;
}
 
Example 5
Source Project: Kettle   Source File: CraftServer.java    License: GNU General Public License v3.0 6 votes vote down vote up
public List<String> tabCompleteChat(Player player, String message) {
    List<String> completions = new ArrayList<String>();
    PlayerChatTabCompleteEvent event = new PlayerChatTabCompleteEvent(player, message, completions);
    String token = event.getLastToken();
    for (Player p : getOnlinePlayers()) {
        if (player.canSee(p) && StringUtil.startsWithIgnoreCase(p.getName(), token)) {
            completions.add(p.getName());
        }
    }
    pluginManager.callEvent(event);

    Iterator<?> it = completions.iterator();
    while (it.hasNext()) {
        Object current = it.next();
        if (!(current instanceof String)) {
            // Sanity
            it.remove();
        }
    }
    Collections.sort(completions, String.CASE_INSENSITIVE_ORDER);
    return completions;
}
 
Example 6
Source Project: WorldEditSelectionVisualizer   Source File: CommandWesv.java    License: MIT License 6 votes vote down vote up
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
    if (args.length > 2 || !sender.hasPermission("wesv.use")) {
        return Collections.emptyList();
    }

    if (args.length == 1) {
        List<String> completions = new ArrayList<>();

        if (sender instanceof Player) {
            completions.add("toggle");
        }

        if (sender.hasPermission("wesv.reload")) {
            completions.add("reload");
        }

        return StringUtil.copyPartialMatches(args[0], completions, new ArrayList<>());
    }

    if (args.length == 2 && args[0].equalsIgnoreCase("toggle") && StringUtil.startsWithIgnoreCase("clipboard", args[1])) {
        return Collections.singletonList("clipboard");
    }

    return Collections.emptyList();
}
 
Example 7
Source Project: Thermos   Source File: SushchestvoCommand.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
{
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 1)
    {
        return StringUtil.copyPartialMatches(args[0], COMMANDS, new ArrayList<String>(COMMANDS.size()));
    }
    if (((args.length == 2) && "get".equalsIgnoreCase(args[0])) || "set".equalsIgnoreCase(args[0]))
    {
        return StringUtil.copyPartialMatches(args[1], MinecraftServer.getServer().sushchestvoConfig.getSettings().keySet(), new ArrayList<String>(MinecraftServer.getServer().sushchestvoConfig.getSettings().size()));
    }

    return ImmutableList.of();
}
 
Example 8
Source Project: Thermos   Source File: TileEntityCommand.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
{
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 1)
    {
        return StringUtil.copyPartialMatches(args[0], COMMANDS, new ArrayList<String>(COMMANDS.size()));
    }
    if (((args.length == 2) && "get".equalsIgnoreCase(args[0])) || "set".equalsIgnoreCase(args[0]))
    {
        return StringUtil.copyPartialMatches(args[1], MinecraftServer.getServer().tileEntityConfig.getSettings().keySet(), new ArrayList<String>(MinecraftServer.getServer().tileEntityConfig.getSettings().size()));
    }

    return ImmutableList.of();
}
 
Example 9
Source Project: Thermos   Source File: CauldronCommand.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args)
{
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 1)
    {
        return StringUtil.copyPartialMatches(args[0], COMMANDS, new ArrayList<String>(COMMANDS.size()));
    }
    if (((args.length == 2) && "get".equalsIgnoreCase(args[0])) || "set".equalsIgnoreCase(args[0]))
    {
        return StringUtil.copyPartialMatches(args[1], MinecraftServer.getServer().cauldronConfig.getSettings().keySet(), new ArrayList<String>(MinecraftServer.getServer().cauldronConfig.getSettings().size()));
    }
    else if ((args.length == 2) && "chunks".equalsIgnoreCase(args[0]))
    {
        return StringUtil.copyPartialMatches(args[1], CHUNK_COMMANDS, new ArrayList<String>(CHUNK_COMMANDS.size()));
    }

    return ImmutableList.of();
}
 
Example 10
Source Project: NBTEditor   Source File: Utils.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static List<String> getElementsWithPrefix(Collection<String> values, String prefix, String ignorePrefix, boolean sort) {
	List<String> result;
	if (prefix == null || prefix.isEmpty()) {
		result = new ArrayList<String>(values);
	} else {
		result = new ArrayList<String>();
		for (String string : values) {
			if (StringUtil.startsWithIgnoreCase(string, prefix)) {
				result.add(string);
			} else if (ignorePrefix != null && !ignorePrefix.isEmpty() && StringUtil.startsWithIgnoreCase(string, ignorePrefix)) {
				if (string.regionMatches(ignorePrefix.length(), prefix, 0, prefix.length())) {
					result.add(string);
				}
			}
		}
	}
	if (sort) {
		Collections.sort(result, String.CASE_INSENSITIVE_ORDER);
	}
	return Collections.unmodifiableList(result);
}
 
Example 11
Source Project: mcspring-boot   Source File: CommandUtils.java    License: MIT License 5 votes vote down vote up
/**
 * Find suggestions of positional arguments for autocompletion
 *
 * @param commandSpec The {@link picocli.CommandLine.Model.CommandSpec CommandSpec} to be analyzed
 * @param args        The partial arguments to be used on analysis
 * @return A stream of suggested values for the last argument (respecting
 * {@link PositionalParamSpec#completionCandidates() completionCandidates})
 */
public static Stream<String> getPossibleArguments(CommandSpec commandSpec, String[] args) {
    int requestedIndex = args.length - 1;
    return getPossibleCommands(commandSpec, args, 0)
            .map(specPair -> specPair.getKey().positionalParameters().stream()
                    .filter(paramSpec -> paramSpec.index().contains(Math.max(0,
                            requestedIndex - (specPair.getValue() + 1))))
                    .findFirst())
            .filter(Optional::isPresent)
            .map(Optional::get)
            .flatMap(CommandUtils::getSuggestedValues)
            .filter(value -> StringUtil.startsWithIgnoreCase(value, args[requestedIndex]));
}
 
Example 12
Source Project: mcspring-boot   Source File: CommandUtils.java    License: MIT License 5 votes vote down vote up
private static Stream<String> getPossibleSubcommands(CommandSpec spec, String[] args, int index) {
    if (args.length == 0) return Stream.empty();
    Stream<Map.Entry<String, CommandLine>> subcommandsStream = spec.subcommands().entrySet().stream();
    if (index + 1 == args.length) {
        return subcommandsStream
                .filter(entry -> StringUtil.startsWithIgnoreCase(entry.getKey(), args[index]))
                .flatMap(entry -> entry.getValue().getCommandSpec().names().stream());
    }
    return subcommandsStream
            .filter(entry -> entry.getKey().equalsIgnoreCase(args[index]))
            .flatMap(entry -> getPossibleSubcommands(entry.getValue().getCommandSpec(), args, index + 1));
}
 
Example 13
Source Project: Kettle   Source File: TimingsCommand.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) {
    Validate.notNull(sender, "Sender cannot be null");
    Validate.notNull(args, "Arguments cannot be null");
    Validate.notNull(alias, "Alias cannot be null");

    if (args.length == 1) {
        return StringUtil.copyPartialMatches(args[0], TIMINGS_SUBCOMMANDS, new ArrayList<String>(TIMINGS_SUBCOMMANDS.size()));
    }
    return ImmutableList.of();
}
 
Example 14
Source Project: Kettle   Source File: CraftMagicNumbers.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public List<String> tabCompleteInternalMaterialName(String token, List<String> completions) {
    ArrayList<String> results = Lists.newArrayList();
    for (ResourceLocation key : Item.REGISTRY.getKeys()) {
        results.add(key.toString());
    }
    return StringUtil.copyPartialMatches(token, results, completions);
}
 
Example 15
Source Project: Crazy-Crates   Source File: KeyTab.java    License: MIT License 5 votes vote down vote up
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String commandLable, String[] args) {
    List<String> completions = new ArrayList<>();
    if (args.length == 1) {// /key
        if (hasPermission(sender, "key")) {
            Bukkit.getOnlinePlayers().forEach(player -> completions.add(player.getName()));
        }
        return StringUtil.copyPartialMatches(args[0], completions, new ArrayList<>());
    }
    return new ArrayList<>();
}
 
Example 16
Source Project: Thermos   Source File: CraftServer.java    License: GNU General Public License v3.0 5 votes vote down vote up
public List<String> tabCompleteChat(Player player, String message) {
    List<String> completions = new ArrayList<String>();
    PlayerChatTabCompleteEvent event = new PlayerChatTabCompleteEvent(player, message, completions);
    String token = event.getLastToken();
    for (Player p : getOnlinePlayers()) {
        if (player.canSee(p) && StringUtil.startsWithIgnoreCase(p.getName(), token)) {
        	if (event.isPinging())
        	{
        		StringBuilder sb = new StringBuilder(1 + p.getName().length());
        		sb.append('@'); sb.append(p.getName());
        		completions.add(sb.toString());
        	}
        	else
        		completions.add(p.getName());
        }
    }
    pluginManager.callEvent(event);

    Iterator<?> it = completions.iterator();
    while (it.hasNext()) {
        Object current = it.next();
        if (!(current instanceof String)) {
            // Sanity
            it.remove();
        }
    }
    Collections.sort(completions, String.CASE_INSENSITIVE_ORDER);
    return completions;
}
 
Example 17
Source Project: UHC   Source File: ListTeamsCommand.java    License: MIT License 5 votes vote down vote up
public ListTeamsCommand(MessageTemplates messages, TeamModule teamModule) {
    super(messages);
    this.teamModule = teamModule;

    showAllSpec = parser
            .acceptsAll(ImmutableList.of("a", "all"), "Show all teams");

    emptyOnlySpec = parser
            .acceptsAll(ImmutableList.of("e", "empty"), "Show empty teams only");

    pageSpec = parser
            .acceptsAll(ImmutableList.of("p", "page"), "The page of results to show")
            .withRequiredArg()
            .withValuesConvertedBy(
                    new IntegerConverter()
                            .setPredicate(IntegerPredicates.GREATER_THAN_ZERO)
                            .setType("Integer > 0")
            )
            .defaultsTo(1);

    completers.put(pageSpec, new OptionsTabComplete() {
        @Override
        public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args,
                                          String complete, String[] others) {
            final List<String> results = Lists.newArrayList();

            final int pages = (int) Math.ceil(
                    (double) ListTeamsCommand.this.teamModule.getTeams().size() / (double) COUNT_PER_PAGE
            );

            for (int i = 1; i <= pages; i++) {
                results.add(String.valueOf(i));
            }

            return StringUtil.copyPartialMatches(complete, results, Lists.<String>newArrayList());
        }
    });
}
 
Example 18
Source Project: UHC   Source File: RequestResponseCommand.java    License: MIT License 5 votes vote down vote up
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
    return StringUtil.copyPartialMatches(
            args[args.length - 1],
            Iterables.transform(requestManager.getRequests(), GET_ID),
            Lists.<String>newArrayList()
    );
}
 
Example 19
Source Project: NBTEditor   Source File: AbstractNBTCommand.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static List<String> getVariableNames(BaseNBT base, String prefix) {
	List<String> names = new ArrayList<String>();
	for (NBTVariableContainer container : base.getAllVariables()) {
		for (String name : container.getVariableNames()) {
			if (StringUtil.startsWithIgnoreCase(name, prefix)) {
				names.add(name);
			}
		}
	}
	Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
	return names;
}
 
Example 20
Source Project: NBTEditor   Source File: CommandCustomItems.java    License: GNU General Public License v3.0 5 votes vote down vote up
private List<String> getCustomItemNamesList(String prefix) {
	ArrayList<String> names = new ArrayList<String>();
	for (CustomItem citem : CustomItemManager.getCustomItems()) {
		String slug = citem.getSlug();
		if (citem.isEnabled() && StringUtil.startsWithIgnoreCase(slug, prefix)) {
			names.add(slug);
		}
	}
	Collections.sort(names, String.CASE_INSENSITIVE_ORDER);
	return names;
}
 
Example 21
Source Project: Crazy-Crates   Source File: CCTab.java    License: MIT License 4 votes vote down vote up
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String commandLable, String[] args) {
    List<String> completions = new ArrayList<>();
    if (args.length == 1) {// /cc
        if (hasPermission(sender, "access")) completions.add("help");
        if (hasPermission(sender, "additem")) completions.add("additem");
        if (hasPermission(sender, "admin")) completions.add("admin");
        if (hasPermission(sender, "convert")) completions.add("convert");
        if (hasPermission(sender, "list")) completions.add("list");
        if (hasPermission(sender, "open")) completions.add("open");
        if (hasPermission(sender, "forceopen")) completions.add("forceopen");
        if (hasPermission(sender, "tp")) completions.add("tp");
        if (hasPermission(sender, "transfer")) completions.add("transfer");
        if (hasPermission(sender, "give")) completions.add("give");
        if (hasPermission(sender, "giveall")) completions.add("giveall");
        if (hasPermission(sender, "take")) completions.add("take");
        if (hasPermission(sender, "set")) completions.add("set");
        if (hasPermission(sender, "reload")) completions.add("reload");
        if (hasPermission(sender, "schematic")) completions.add("set1");
        if (hasPermission(sender, "schematic")) completions.add("set2");
        if (hasPermission(sender, "schematic")) completions.add("save");
        return StringUtil.copyPartialMatches(args[0], completions, new ArrayList<>());
    } else if (args.length == 2) {// /cc arg0
        switch (args[0].toLowerCase()) {
            case "additem":
            case "open":
            case "forceopen":
            case "transfer":
                cc.getCrates().forEach(crate -> completions.add(crate.getName()));
                completions.remove("Menu");//Takes out a crate that doesn't exist as a file.
                break;
            case "set":
                cc.getCrates().forEach(crate -> completions.add(crate.getName()));
                break;
            case "tp":
                cc.getCrateLocations().forEach(location -> completions.add(location.getID()));
                break;
            case "give":
            case "giveall":
            case "take":
                completions.add("physical");
                completions.add("p");
                completions.add("virtual");
                completions.add("v");
                break;
            case "save":
                completions.add("<Schematic Name>");
                break;
        }
        return StringUtil.copyPartialMatches(args[1], completions, new ArrayList<>());
    } else if (args.length == 3) {// /cc arg0 arg1
        switch (args[0].toLowerCase()) {
            case "additem":
                Crate crateFromName = cc.getCrateFromName(args[1]);
                if (crateFromName != null && crateFromName.getCrateType() != CrateType.MENU) {
                    cc.getCrateFromName(args[1]).getPrizes().forEach(prize -> completions.add(prize.getName()));
                }
                break;
            case "open":
            case "forceopen":
            case "transfer":
                Bukkit.getOnlinePlayers().forEach(player -> completions.add(player.getName()));
                break;
            case "give":
            case "giveall":
            case "take":
                cc.getCrates().forEach(crate -> completions.add(crate.getName()));
                completions.remove("Menu");//Takes out a crate that doesn't exist as a file.
                break;
        }
        return StringUtil.copyPartialMatches(args[2], completions, new ArrayList<>());
    } else if (args.length == 4) {// /cc arg0 arg1 arg2
        switch (args[0].toLowerCase()) {
            case "give":
            case "giveall":
            case "take":
            case "transfer":
                for (int i = 1; i <= 100; i++) completions.add(i + "");
                break;
        }
        return StringUtil.copyPartialMatches(args[3], completions, new ArrayList<>());
    } else if (args.length == 5) {// /cc arg0 arg1 arg2 arg3
        switch (args[0].toLowerCase()) {
            case "give":
            case "giveall":
            case "take":
                Bukkit.getOnlinePlayers().forEach(player -> completions.add(player.getName()));
                break;
        }
        return StringUtil.copyPartialMatches(args[4], completions, new ArrayList<>());
    }
    return new ArrayList<>();
}
 
Example 22
Source Project: Thermos   Source File: CraftMagicNumbers.java    License: GNU General Public License v3.0 4 votes vote down vote up
@Override
public List<String> tabCompleteInternalMaterialName(String token, List<String> completions) {
    return StringUtil.copyPartialMatches(token, net.minecraft.item.Item.itemRegistry.getKeys(), completions);
}
 
Example 23
Source Project: UHC   Source File: ModuleTabComplete.java    License: MIT License 4 votes vote down vote up
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args,
                                  String complete, String[] others) {
    return StringUtil.copyPartialMatches(complete, registry.getIds(), Lists.<String>newArrayList());
}