|
| 1 | +/* |
| 2 | + * Command Framework - Annotation based command framework |
| 3 | + * Copyright (C) 2025 Berke Akçen |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package me.despical.commandframework.utils; |
| 20 | + |
| 21 | +import me.despical.commandframework.CommandArguments; |
| 22 | +import org.bukkit.Bukkit; |
| 23 | +import org.bukkit.entity.Player; |
| 24 | +import org.bukkit.util.StringUtil; |
| 25 | +import org.jetbrains.annotations.Contract; |
| 26 | +import org.jetbrains.annotations.NotNull; |
| 27 | + |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Despical |
| 33 | + * <p> |
| 34 | + * Created at 2.12.2025 |
| 35 | + */ |
| 36 | +public final class CompleterHelper { |
| 37 | + |
| 38 | + private final CommandArguments arguments; |
| 39 | + |
| 40 | + public CompleterHelper(CommandArguments arguments) { |
| 41 | + this.arguments = arguments; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Retrieves the names of all currently online players on the server. |
| 46 | + * <p> |
| 47 | + * This method is useful for tab completion suggestions where a list of |
| 48 | + * player names is required. |
| 49 | + * |
| 50 | + * @return An unmodifiable list containing the names of online players. |
| 51 | + * Returns an empty list if no players are online. Never returns null. |
| 52 | + */ |
| 53 | + @NotNull |
| 54 | + @Contract(pure = true) |
| 55 | + public List<String> playerNames() { |
| 56 | + return Bukkit.getServer().getOnlinePlayers().stream().map(Player::getName).toList(); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Copies all elements from the iterable collection of originals to the |
| 61 | + * collection provided. |
| 62 | + * |
| 63 | + * @param <T> the collection of strings |
| 64 | + * @param index Argument index to search for |
| 65 | + * @param originals An iterable collection of strings to filter. |
| 66 | + * @param collection The collection to add matches to |
| 67 | + * @return the collection provided that would have the elements copied |
| 68 | + * into |
| 69 | + * @throws UnsupportedOperationException if the collection is immutable |
| 70 | + * and originals contains a string which starts with the specified |
| 71 | + * search string. |
| 72 | + * @throws IllegalArgumentException if any parameter is null |
| 73 | + * @throws IllegalArgumentException if originals contains a null element. |
| 74 | + * <b>Note: the collection may be modified before this is thrown</b> |
| 75 | + */ |
| 76 | + @NotNull |
| 77 | + @Contract(pure = true) |
| 78 | + public <T extends Collection<String>> T copyMatches( |
| 79 | + final int index, |
| 80 | + @NotNull final Iterable<String> originals, |
| 81 | + @NotNull final T collection |
| 82 | + ) { |
| 83 | + String argument = arguments.getArgument(index); |
| 84 | + |
| 85 | + if (argument == null) { |
| 86 | + return collection; |
| 87 | + } |
| 88 | + |
| 89 | + return StringUtil.copyPartialMatches(argument, originals, collection); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Copies all elements from the iterable collection of originals to the |
| 94 | + * collection provided. |
| 95 | + * |
| 96 | + * @param <T> the collection of strings |
| 97 | + * @param token String to search for |
| 98 | + * @param originals An iterable collection of strings to filter. |
| 99 | + * @param collection The collection to add matches to |
| 100 | + * @return the collection provided that would have the elements copied |
| 101 | + * into |
| 102 | + * @throws UnsupportedOperationException if the collection is immutable |
| 103 | + * and originals contains a string which starts with the specified |
| 104 | + * search string. |
| 105 | + * @throws IllegalArgumentException if any parameter is null |
| 106 | + * @throws IllegalArgumentException if originals contains a null element. |
| 107 | + * <b>Note: the collection may be modified before this is thrown</b> |
| 108 | + */ |
| 109 | + @NotNull |
| 110 | + @Contract(pure = true) |
| 111 | + public <T extends Collection<String>> T copyMatches( |
| 112 | + @NotNull final String token, |
| 113 | + @NotNull final Iterable<String> originals, |
| 114 | + @NotNull final T collection |
| 115 | + ) { |
| 116 | + return StringUtil.copyPartialMatches(token, originals, collection); |
| 117 | + } |
| 118 | +} |
0 commit comments