Skip to content

Commit faf3ec3

Browse files
committed
Move CompleterHelper class to main package and added more detailed javadoc
1 parent 2cf9608 commit faf3ec3

File tree

3 files changed

+162
-119
lines changed

3 files changed

+162
-119
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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;
20+
21+
import org.bukkit.Bukkit;
22+
import org.bukkit.entity.Player;
23+
import org.bukkit.util.StringUtil;
24+
import org.jetbrains.annotations.*;
25+
26+
import java.util.ArrayList;
27+
import java.util.Collection;
28+
import java.util.List;
29+
30+
import org.jetbrains.annotations.Contract;
31+
import org.jetbrains.annotations.NotNull;
32+
33+
/**
34+
* A utility class designed to simplify the process of handling tab completion for commands.
35+
* <p>
36+
* This helper provides methods to easily filter lists of strings (like player names or
37+
* command sub-arguments) based on the user's current input.
38+
* <p>
39+
* * @author Despical
40+
* * <p>
41+
* * Created at 2.12.2025
42+
*/
43+
public final class CompleterHelper {
44+
45+
private final CommandArguments arguments;
46+
47+
@ApiStatus.Internal
48+
public CompleterHelper(CommandArguments arguments) {
49+
this.arguments = arguments;
50+
}
51+
52+
/**
53+
* Retrieves a list of names for all players currently online on the server.
54+
* <p>
55+
* This is commonly used for tab completion where a target player name is required.
56+
*
57+
* @return An unmodifiable list containing the names of online players.
58+
* Returns an empty list if no players are online. Never returns null.
59+
*/
60+
@NotNull
61+
@Contract(pure = true)
62+
public List<String> playerNames() {
63+
return Bukkit.getServer().getOnlinePlayers().stream()
64+
.map(Player::getName)
65+
.toList();
66+
}
67+
68+
/**
69+
* Filters a collection of strings based on the argument at the specified index.
70+
* <p>
71+
* This is a convenience overload that creates a new {@link ArrayList} to store results.
72+
*
73+
* @param <T> The type of the collection (inferred).
74+
* @param index The index of the command argument to use as the filter token.
75+
* @param originals The source collection of strings to filter (e.g., all possible sub-commands).
76+
* @return A new {@link ArrayList} containing only the strings from {@code originals}
77+
* that start with the argument at the given {@code index}.
78+
* @see #copyMatches(int, Iterable, Collection)
79+
*/
80+
@NotNull
81+
@Contract(pure = true)
82+
@SuppressWarnings("unchecked")
83+
public <T extends Collection<? super String>> T copyMatches(
84+
final int index,
85+
@NotNull final Iterable<String> originals
86+
) {
87+
return copyMatches(index, originals, (T) new ArrayList<>());
88+
}
89+
90+
/**
91+
* Filters a collection of strings based on the argument at the specified index
92+
* and adds matches to the provided collection.
93+
* <p>
94+
* If the argument at the specified index is null or empty, the original collection
95+
* is returned as is (or added to).
96+
*
97+
* @param <T> The type of the collection to return.
98+
* @param index The index of the command argument to use as the filter token.
99+
* @param originals The source iterable of strings to filter.
100+
* @param collection The target collection where matches will be added.
101+
* @return The {@code collection} provided, now containing the filtered matches.
102+
* @throws IllegalArgumentException if any parameter is null.
103+
*/
104+
@NotNull
105+
@Contract(pure = true)
106+
public <T extends Collection<? super String>> T copyMatches(
107+
final int index,
108+
@NotNull final Iterable<String> originals,
109+
@NotNull final T collection
110+
) {
111+
String argument = arguments.getArgument(index);
112+
113+
if (argument == null) {
114+
return collection;
115+
}
116+
117+
return StringUtil.copyPartialMatches(argument, originals, collection);
118+
}
119+
120+
/**
121+
* Filters a collection of strings based on a specific token string.
122+
* <p>
123+
* This is a convenience overload that creates a new {@link ArrayList} to store results.
124+
*
125+
* @param <T> The type of the collection (inferred).
126+
* @param token The string token to search for (starts-with match).
127+
* @param originals The source collection of strings to filter.
128+
* @return A new {@link ArrayList} containing only the strings that match the token.
129+
*/
130+
@NotNull
131+
@Contract(pure = true)
132+
@SuppressWarnings("unchecked")
133+
public <T extends Collection<? super String>> T copyMatches(
134+
@NotNull final String token,
135+
@NotNull final Iterable<String> originals
136+
) {
137+
return copyMatches(token, originals, (T) new ArrayList<>());
138+
}
139+
140+
/**
141+
* Filters a collection of strings based on a specific token string and adds
142+
* matches to the provided collection.
143+
* <p>
144+
* This delegates directly to Bukkit's {@link StringUtil#copyPartialMatches(String, Iterable, Collection)}.
145+
*
146+
* @param <T> The type of the collection to return.
147+
* @param token The string token to search for (starts-with match).
148+
* @param originals The source iterable of strings to filter.
149+
* @param collection The target collection where matches will be added.
150+
* @return The {@code collection} provided, populated with matches.
151+
* @throws IllegalArgumentException if originals contains a null element.
152+
*/
153+
@NotNull
154+
@Contract(pure = true)
155+
public <T extends Collection<? super String>> T copyMatches(
156+
@NotNull final String token,
157+
@NotNull final Iterable<String> originals,
158+
@NotNull final T collection
159+
) {
160+
return StringUtil.copyPartialMatches(token, originals, collection);
161+
}
162+
}

src/main/java/me/despical/commandframework/ParameterHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import me.despical.commandframework.annotations.Default;
2323
import me.despical.commandframework.annotations.Param;
2424
import me.despical.commandframework.exceptions.CommandException;
25-
import me.despical.commandframework.utils.CompleterHelper;
2625
import org.jetbrains.annotations.ApiStatus;
2726
import org.jetbrains.annotations.NotNull;
2827

src/main/java/me/despical/commandframework/utils/CompleterHelper.java

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)