Skip to content

Commit 2970b2f

Browse files
committed
feat: java 8, precedents args in tab completion, new system to add args
1 parent f5bc404 commit 2970b2f

File tree

16 files changed

+198
-78
lines changed

16 files changed

+198
-78
lines changed

build.gradle

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,17 @@ repositories {
1818
}
1919

2020
dependencies {
21-
compileOnly "org.spigotmc:spigot-api:1.21-R0.1-SNAPSHOT"
21+
compileOnly "org.spigotmc:spigot-api:1.21.1-R0.1-SNAPSHOT"
22+
}
23+
24+
def targetJavaVersion = 8
25+
java {
26+
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
27+
sourceCompatibility = javaVersion
28+
targetCompatibility = javaVersion
29+
if (JavaVersion.current() < javaVersion) {
30+
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
31+
}
2232
}
2333

2434
tasks.register('generateVersionProperties') {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=1.5.1
1+
version=1.6.0

src/main/java/fr/traqueur/commands/api/Command.java

Lines changed: 94 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -143,39 +143,39 @@ public void unregister(boolean subcommands) {
143143
* This method is called to get the name of the command.
144144
* @return The name of the command.
145145
*/
146-
protected final String getName() {
146+
public final String getName() {
147147
return name;
148148
}
149149

150150
/**
151151
* This method is called to get the description of the command.
152152
* @return The description of the command.
153153
*/
154-
protected final String getDescription() {
154+
public final String getDescription() {
155155
return description;
156156
}
157157

158158
/**
159159
* This method is called to get the permission of the command.
160160
* @return The permission of the command.
161161
*/
162-
protected final String getPermission() {
162+
public final String getPermission() {
163163
return permission;
164164
}
165165

166166
/**
167167
* This method is called to get the usage of the command.
168168
* @return The usage of the command.
169169
*/
170-
protected final String getUsage() {
170+
public final String getUsage() {
171171
return usage;
172172
}
173173

174174
/**
175175
* This method is called to get the aliases of the command.
176176
* @return The aliases of the command.
177177
*/
178-
protected final List<String> getAliases() {
178+
public final List<String> getAliases() {
179179
return aliases;
180180
}
181181

@@ -184,47 +184,47 @@ protected final List<String> getAliases() {
184184
* This method is called to get the subcommands of the command.
185185
* @return The subcommands of the command.
186186
*/
187-
protected final List<Command<?>> getSubcommands() {
187+
public final List<Command<?>> getSubcommands() {
188188
return subcommands;
189189
}
190190

191191
/**
192192
* This method is called to get the arguments of the command.
193193
* @return The arguments of the command.
194194
*/
195-
protected final List<Argument> getArgs() {
195+
public final List<Argument> getArgs() {
196196
return args;
197197
}
198198

199199
/**
200200
* This method is called to get the optional arguments of the command.
201201
* @return The optional arguments of the command.
202202
*/
203-
protected final List<Argument> getOptinalArgs() {
203+
public final List<Argument> getOptinalArgs() {
204204
return optionalArgs;
205205
}
206206

207207
/**
208208
* This method is called to check if the command is only to use in game.
209209
* @return If the command is only to use in game.
210210
*/
211-
protected final boolean inGameOnly() {
211+
public final boolean inGameOnly() {
212212
return gameOnly;
213213
}
214214

215215
/**
216216
* This method is called to get the requirements of the command.
217217
* @return The requirements of the command.
218218
*/
219-
protected final List<Requirement> getRequirements() {
219+
public final List<Requirement> getRequirements() {
220220
return requirements;
221221
}
222222

223223
/**
224224
* This method is called to check if the command has infinite arguments.
225225
* @return If the command has infinite arguments.
226226
*/
227-
protected final boolean isInfiniteArgs() {
227+
public final boolean isInfiniteArgs() {
228228
return infiniteArgs;
229229
}
230230

@@ -290,49 +290,110 @@ public final void addSubCommand(Command<?>... commands) {
290290
* This method is called to add arguments to the command.
291291
* @param args The arguments to add.
292292
*/
293-
public final void addArgs(String... args) {
294-
Arrays.asList(args).forEach(arg -> this.addArgs(arg, null));
293+
public final void addArgs(Object... args) {
294+
if (args.length % 2 != 0 && !(args[1] instanceof String)) {
295+
throw new IllegalArgumentException("You must provide a type for the argument.");
296+
}
297+
298+
for (int i = 0; i < args.length; i += 2) {
299+
if(!(args[i] instanceof String && args[i + 1] instanceof Class<?>)) {
300+
throw new IllegalArgumentException("You must provide a type for the argument.");
301+
}
302+
this.addArgs((String) args[i], (Class<?>) args[i + 1]);
303+
}
304+
}
305+
306+
public final void addArgs(String arg) {
307+
this.addArgs(arg, null, null);
308+
}
309+
310+
public final void addArgs(String arg, Class<?> type) {
311+
this.addArgs(arg, type,null);
312+
}
313+
314+
public final void addArgs(String arg, TabConverter converter) {
315+
this.addArgs(arg, null, converter);
295316
}
296317

297318
/**
298319
* This method is called to add arguments to the command.
299320
* @param arg The argument to add.
300321
* @param converter The converter of the argument.
301322
*/
302-
public final void addArgs(String arg, TabConverter converter) {
303-
try {
304-
if (this.infiniteArgs) {
305-
throw new ArgsWithInfiniteArgumentException(false);
306-
}
323+
public final void addArgs(String arg, Class<?> type, TabConverter converter) {
324+
if (arg.contains(CommandManager.TYPE_PARSER) && type != null) {
325+
throw new IllegalArgumentException("You can't use the type parser in the command arguments.");
326+
}
327+
if(type == null && !arg.contains(CommandManager.TYPE_PARSER)) {
328+
throw new IllegalArgumentException("You must provide a type for the argument.");
329+
}
307330

308-
if (arg.contains(":infinite")) {
309-
this.infiniteArgs = true;
310-
}
311-
this.args.add(new Argument(arg, converter));
312-
} catch (ArgsWithInfiniteArgumentException e) {
313-
this.plugin.getLogger().severe(e.getMessage());
331+
if(type != null) {
332+
arg = arg + CommandManager.TYPE_PARSER + type.getSimpleName().toLowerCase();
314333
}
334+
335+
this.add(arg, converter, false);
315336
}
316337

317338
/**
318-
* This method is called to add optional arguments to the command.
319-
* @param args The optional arguments to add.
339+
* This method is called to add arguments to the command.
340+
* @param args The arguments to add.
320341
*/
321-
public final void addOptinalArgs(String... args) {
322-
Arrays.asList(args).forEach(arg -> this.addOptinalArgs(arg, null));
342+
public final void addOptionalArgs(Object... args) {
343+
if (args.length % 2 != 0 && !(args[1] instanceof String)) {
344+
throw new IllegalArgumentException("You must provide a type for the argument.");
345+
}
346+
347+
for (int i = 0; i < args.length; i += 2) {
348+
if(!(args[i] instanceof String && args[i + 1] instanceof Class<?>)) {
349+
throw new IllegalArgumentException("You must provide a type for the argument.");
350+
}
351+
this.addOptionalArgs((String) args[i], (Class<?>) args[i + 1]);
352+
}
353+
}
354+
355+
public final void addOptionalArgs(String arg) {
356+
this.addOptionalArgs(arg, null, null);
357+
}
358+
359+
public final void addOptionalArgs(String arg, Class<?> type) {
360+
this.addOptionalArgs(arg, type,null);
361+
}
362+
363+
public final void addOptionalArgs(String arg, TabConverter converter) {
364+
this.addOptionalArgs(arg, null, converter);
323365
}
324366

325367
/**
326-
* This method is called to add optional arguments to the command.
327-
* @param arg The optional argument to add.
368+
* This method is called to add arguments to the command.
369+
* @param arg The argument to add.
328370
* @param converter The converter of the argument.
329371
*/
330-
public final void addOptinalArgs(String arg, TabConverter converter) {
372+
public final void addOptionalArgs(String arg, Class<?> type, TabConverter converter) {
373+
if (arg.contains(CommandManager.TYPE_PARSER) && type != null) {
374+
throw new IllegalArgumentException("You can't use the type parser in the command arguments.");
375+
}
376+
if(type != null) {
377+
arg = arg + CommandManager.TYPE_PARSER + type.getSimpleName().toLowerCase();
378+
}
379+
380+
this.add(arg, converter, true);
381+
}
382+
383+
private void add(String arg, TabConverter converter, boolean opt) {
331384
try {
332385
if (this.infiniteArgs) {
333-
throw new ArgsWithInfiniteArgumentException(true);
386+
throw new ArgsWithInfiniteArgumentException(false);
387+
}
388+
389+
if (arg.contains(":infinite")) {
390+
this.infiniteArgs = true;
391+
}
392+
if(opt) {
393+
this.optionalArgs.add(new Argument(arg, converter));
394+
} else {
395+
this.args.add(new Argument(arg, converter));
334396
}
335-
this.optionalArgs.add(new Argument(arg, converter));
336397
} catch (ArgsWithInfiniteArgumentException e) {
337398
this.plugin.getLogger().severe(e.getMessage());
338399
}

src/main/java/fr/traqueur/commands/api/CommandManager.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.lang.reflect.Field;
2626
import java.lang.reflect.InvocationTargetException;
2727
import java.util.*;
28+
import java.util.stream.Collectors;
2829

2930
/**
3031
* This class is the command manager.
@@ -33,7 +34,7 @@
3334
*/
3435
public class CommandManager {
3536

36-
private static final String TYPE_PARSER = ":";
37+
public static final String TYPE_PARSER = ":";
3738
private static final String INFINITE = "infinite";
3839

3940
/**
@@ -114,13 +115,14 @@ public CommandManager(JavaPlugin plugin) {
114115
* Register the internal converters of the command manager.
115116
*/
116117
private void registerInternalConverters() {
117-
this.registerConverter(String.class, "string", (s) -> s);
118-
this.registerConverter(Boolean.class, "boolean", new BooleanArgument());
119-
this.registerConverter(Integer.class, "int",new IntegerArgument());
120-
this.registerConverter(Double.class, "double",new DoubleArgument());
121-
this.registerConverter(Long.class, "long", new LongArgument());
122-
this.registerConverter(Player.class, "player", new PlayerArgument());
123-
this.registerConverter(OfflinePlayer.class, "offlineplayer", new OfflinePlayerArgument());
118+
this.registerConverter(String.class, (s) -> s);
119+
this.registerConverter(Boolean.class, new BooleanArgument());
120+
this.registerConverter(Integer.class, "int", new IntegerArgument());
121+
this.registerConverter(Integer.class, new IntegerArgument());
122+
this.registerConverter(Double.class, new DoubleArgument());
123+
this.registerConverter(Long.class, new LongArgument());
124+
this.registerConverter(Player.class, new PlayerArgument());
125+
this.registerConverter(OfflinePlayer.class, new OfflinePlayerArgument());
124126
this.registerConverter(String.class, INFINITE, s -> s);
125127
}
126128

@@ -221,11 +223,21 @@ public void unregisterCommand(Command<?> command, boolean subcommands) {
221223
/**
222224
* Register an argument converter in the command manager.
223225
* @param typeClass The class of the type.
224-
* @param type The type of the argument.
225226
* @param converter The converter of the argument.
226227
* @param <T> The type of the argument.
227228
*/
228-
public <T> void registerConverter(Class<T> typeClass, String type, ArgumentConverter<T> converter) {
229+
public <T> void registerConverter(Class<T> typeClass, ArgumentConverter<T> converter) {
230+
this.typeConverters.put(typeClass.getSimpleName().toLowerCase(), new AbstractMap.SimpleEntry<>(typeClass, converter));
231+
}
232+
233+
/**
234+
* Register an argument converter in the command manager.
235+
* @param typeClass The class of the type.
236+
* @param converter The converter of the argument.
237+
* @param <T> The type of the argument.
238+
*/
239+
@Deprecated
240+
public <T> void registerConverter(Class<T> typeClass, String type, ArgumentConverter<T> converter) {
229241
this.typeConverters.put(type, new AbstractMap.SimpleEntry<>(typeClass, converter));
230242
}
231243

@@ -307,7 +319,7 @@ private void addCommand(Command<?> command, String label) throws TypeArgumentNot
307319
commands.put(label.toLowerCase(), command);
308320

309321
String originCmdLabel = cmdLabel;
310-
for (Command<?> value : commands.values().stream().filter(commandInner -> !commandInner.isSubCommand()).toList()) {
322+
for (Command<?> value : commands.values().stream().filter(commandInner -> !commandInner.isSubCommand()).collect(Collectors.toList())) {
311323
if(value.getAliases().contains(cmdLabel)) {
312324
originCmdLabel = value.getName();
313325
}
@@ -353,7 +365,7 @@ private void addCompletionsForLabel(String[] labelParts) {
353365
}
354366
currentLabel.append(labelParts[i]);
355367
String completionPart = labelParts[i + 1];
356-
this.addCompletion(currentLabel.toString(), i + 1, (sender) -> Lists.newArrayList(completionPart));
368+
this.addCompletion(currentLabel.toString(), i + 1, (sender, args) -> Lists.newArrayList(completionPart));
357369
}
358370
}
359371

@@ -372,10 +384,11 @@ private void addCompletionForArgs(String label, int commandSize, List<Argument>
372384
TabConverter argConverter = arg.tabConverter();
373385
if (argConverter != null) {
374386
this.addCompletion(label,commandSize + i, argConverter);
375-
} else if (converter instanceof TabConverter tabConverter) {
387+
} else if (converter instanceof TabConverter) {
388+
TabConverter tabConverter = (TabConverter) converter;
376389
this.addCompletion(label,commandSize + i, tabConverter);
377390
} else {
378-
this.addCompletion(label, commandSize + i, (sender) -> new ArrayList<>());
391+
this.addCompletion(label, commandSize + i, (sender, argsInner) -> new ArrayList<>());
379392
}
380393
}
381394
}
@@ -391,9 +404,9 @@ private void addCompletion(String label, int commandSize, TabConverter converter
391404
TabConverter newConverter;
392405
TabConverter converterInner = mapInner.getOrDefault(commandSize, null);
393406
if(converterInner != null) {
394-
newConverter = (sender) -> {
395-
List<String> completions = new ArrayList<>(converterInner.onCompletion(sender));
396-
completions.addAll(converter.onCompletion(sender));
407+
newConverter = (sender, args) -> {
408+
List<String> completions = new ArrayList<>(converterInner.onCompletion(sender, args));
409+
completions.addAll(converter.onCompletion(sender, args));
397410
return completions;
398411
};
399412
} else {
@@ -408,7 +421,7 @@ private void addCompletion(String label, int commandSize, TabConverter converter
408421
* @param args The arguments to check.
409422
*/
410423
private boolean checkTypeForArgs(List<Argument> args) throws TypeArgumentNotExistException {
411-
for(String arg: args.stream().map(Argument::arg).toList()) {
424+
for(String arg: args.stream().map(Argument::arg).collect(Collectors.toList())) {
412425
String[] parts = arg.split(TYPE_PARSER);
413426

414427
if (parts.length != 2) {

0 commit comments

Comments
 (0)