Skip to content

Commit 8d78d77

Browse files
committed
🎨 Refined our argument types, tab completion is now consistent.
1 parent d5601dd commit 8d78d77

26 files changed

Lines changed: 125 additions & 166 deletions
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package xyz.bitsquidd.bits.example;
22

33
import org.bukkit.plugin.Plugin;
4-
import xyz.bitsquidd.bits.lib.command.examples.ClearAllCommandsCommand;
5-
import xyz.bitsquidd.bits.lib.command.examples.TeleportCommand;
6-
import xyz.bitsquidd.bits.lib.command.examples.TestFormattingCommand;
4+
import xyz.bitsquidd.bits.lib.command.examples.ExampleTeleportCommand;
75
import xyz.bitsquidd.bits.lib.command.registering.CommandManager;
86

97
public class ExampleCommandManager extends CommandManager {
@@ -13,8 +11,6 @@ public ExampleCommandManager(Plugin plugin) {
1311

1412
@Override
1513
public void registerCommands() {
16-
register(new TestFormattingCommand());
17-
register(new TeleportCommand());
18-
register(new ClearAllCommandsCommand());
14+
register(new ExampleTeleportCommand());
1915
}
2016
}

‎src/main/java/xyz/bitsquidd/bits/lib/command/AbstractCommand.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ private void executeCommand(CommandContext commandContext) {
6666

6767
try {
6868
hasExecutedPath = defaultExecute(commandContext);
69+
Set<CommandPath> validPaths = getValidPaths(commandContext);
6970

70-
if (!commandContext.isEmpty()) {
71-
for (CommandPath path : getValidPaths(commandContext)) {
72-
hasExecutedPath = hasExecutedPath || path.execute(commandContext);
73-
}
71+
for (CommandPath path : validPaths) {
72+
hasExecutedPath = hasExecutedPath || path.execute(commandContext);
7473
}
7574
} catch (Exception e) {
76-
Text.of("<b>Error executing command:</b> " + e.getMessage(), new CommandReturnDecorator(CommandReturnType.ERROR)).send(commandContext.getSender());
75+
// Text.of("<b>Error executing command:</b> " + e.getMessage(), new CommandReturnDecorator(CommandReturnType.ERROR)).send(commandContext.getSender());
7776
LogController.exception(e);
7877
hasExecutedPath = false;
7978
}

‎src/main/java/xyz/bitsquidd/bits/lib/command/CommandArgumentInfo.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
import org.jetbrains.annotations.ApiStatus;
44
import org.jetbrains.annotations.NotNull;
55
import org.jetbrains.annotations.Nullable;
6-
import xyz.bitsquidd.bits.lib.command.arguments.CommandArgument;
6+
import xyz.bitsquidd.bits.lib.command.arguments.ICommandArgument;
77

88
// Used to store params and their names in commands.
99
public class CommandArgumentInfo<T> {
1010
public final @NotNull String name;
11-
public final @NotNull CommandArgument<T> param;
11+
public final @NotNull ICommandArgument<T> param;
1212
@ApiStatus.Experimental public final @Nullable T defaultValue;
1313

14-
public CommandArgumentInfo(@NotNull String name, @NotNull CommandArgument<T> param) {
14+
public CommandArgumentInfo(@NotNull String name, @NotNull ICommandArgument<T> param) {
1515
this(name, param, null);
1616
}
1717

1818
@ApiStatus.Experimental
19-
public CommandArgumentInfo(@NotNull String name, @NotNull CommandArgument<T> param, @Nullable T defaultValue) {
19+
public CommandArgumentInfo(@NotNull String name, @NotNull ICommandArgument<T> param, @Nullable T defaultValue) {
2020
this.name = name;
2121
this.param = param;
2222
this.defaultValue = defaultValue;

‎src/main/java/xyz/bitsquidd/bits/lib/command/CommandPath.java‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import xyz.bitsquidd.bits.lib.command.exceptions.ArgumentParseException;
66
import xyz.bitsquidd.bits.lib.command.requirements.CommandRequirement;
77

8+
import java.util.ArrayList;
89
import java.util.List;
910

1011
// Sub-command paths.
@@ -27,11 +28,17 @@ public CommandPath(@NotNull String name, @NotNull String description, @NotNull L
2728

2829

2930
public boolean matchesPartial(CommandContext commandContext) {
30-
if (commandContext.getArgsLength() > getArgLength()) {
31+
int argLength = commandContext.getArgLength();
32+
33+
if (getArgLength() == 0 && argLength == 0) {
34+
return true;
35+
}
36+
37+
if (argLength > getArgLength()) {
3138
return false;
3239
}
3340

34-
for (int i = 0; i < commandContext.getArgsLength(); i++) {
41+
for (int i = 0; i < argLength; i++) {
3542
CommandArgumentInfo<?> commandArgumentInfo = getCommandParamAtIndex(i);
3643
if (!commandContext.getArg(i).isEmpty() && !commandArgumentInfo.param.canParseArg(commandContext, i)) {
3744
return false;
@@ -43,6 +50,10 @@ public boolean matchesPartial(CommandContext commandContext) {
4350
public boolean matchesFully(CommandContext commandContext) {
4451
int argLength = commandContext.getArgLength();
4552

53+
if (getArgLength() == 0 && argLength == 0) {
54+
return true;
55+
}
56+
4657
if (argLength != getArgLength()) {
4758
return false;
4859
}
@@ -109,7 +120,11 @@ public boolean execute(CommandContext commandContext) {
109120

110121
public List<String> tabComplete(@NotNull CommandContext commandContext) {
111122
CommandArgumentInfo<?> commandArgumentInfo = getCommandParamAtIndex(commandContext.getArgLength()-1);
112-
return commandArgumentInfo.param.tabComplete(commandContext, getCommandParamIndex(commandArgumentInfo));
123+
124+
ArrayList<String> availableCompletions = new ArrayList<>(commandArgumentInfo.param.getAddedTabCompletions());
125+
availableCompletions.addAll(commandArgumentInfo.param.tabComplete(commandContext, getCommandParamIndex(commandArgumentInfo)));
126+
127+
return availableCompletions;
113128
}
114129

115130
public boolean hasPermissions(CommandContext commandContext) {

‎src/main/java/xyz/bitsquidd/bits/lib/command/arguments/BooleanArgument.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@
44
import xyz.bitsquidd.bits.lib.command.CommandContext;
55
import xyz.bitsquidd.bits.lib.command.exceptions.ArgumentParseException;
66

7-
import java.util.Arrays;
8-
import java.util.HashSet;
9-
import java.util.Set;
7+
import java.util.*;
108

11-
public class BooleanArgument implements CommandArgument<Boolean> {
9+
public class BooleanArgument extends CommandArgument<Boolean> {
1210
public static final BooleanArgument INSTANCE = new BooleanArgument();
1311

1412
private static final Set<String> TRUE_VALUES = new HashSet<>(Arrays.asList(
@@ -52,4 +50,12 @@ public boolean canParseArg(@NotNull CommandContext context, int argIndex) {
5250
String input = context.getArgs()[argIndex].toLowerCase();
5351
return TRUE_VALUES.contains(input) || FALSE_VALUES.contains(input);
5452
}
53+
54+
@Override
55+
public @NotNull List<String> tabComplete(CommandContext context, int startIndex) {
56+
ArrayList<String> completions = new ArrayList<>(TRUE_VALUES);
57+
completions.addAll(FALSE_VALUES);
58+
59+
return completions;
60+
}
5561
}
Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
11
package xyz.bitsquidd.bits.lib.command.arguments;
22

3-
import xyz.bitsquidd.bits.lib.command.CommandContext;
4-
import xyz.bitsquidd.bits.lib.command.exceptions.ArgumentParseException;
3+
import org.jetbrains.annotations.NotNull;
54

6-
import java.util.Collections;
5+
import java.util.ArrayList;
76
import java.util.List;
87

9-
public interface CommandArgument<T> {
10-
String getTypeName();
8+
public abstract class CommandArgument<T> implements ICommandArgument<T> {
9+
protected ArrayList<String> addedTabCompletions = new ArrayList<>();
1110

12-
T parse(CommandContext context, int startIndex) throws ArgumentParseException;
13-
boolean canParseArg(CommandContext context, int argIndex);
14-
default int getRequiredArgs() {
15-
return 1;
16-
}
17-
default List<String> tabComplete(CommandContext context, int startIndex) {
18-
return Collections.emptyList();
11+
@Override
12+
public void addTabCompletions(List<String> completions) {
13+
addedTabCompletions.addAll(completions);
1914
}
2015

21-
default boolean canParseFull(CommandContext context, int startIndex) {
22-
try {
23-
parse(context, startIndex);
24-
return true;
25-
} catch (ArgumentParseException e) {
26-
return false;
27-
}
16+
@Override
17+
public @NotNull List<String> getAddedTabCompletions() {
18+
return addedTabCompletions;
2819
}
29-
30-
}
20+
}

‎src/main/java/xyz/bitsquidd/bits/lib/command/arguments/DoubleArgument.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import xyz.bitsquidd.bits.lib.command.CommandContext;
55
import xyz.bitsquidd.bits.lib.command.exceptions.ArgumentParseException;
66

7-
public class DoubleArgument implements CommandArgument<Double> {
7+
public class DoubleArgument extends CommandArgument<Double> {
88
public static final DoubleArgument INSTANCE = new DoubleArgument();
99

1010
@Override

‎src/main/java/xyz/bitsquidd/bits/lib/command/arguments/EnumArgument.java‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.Set;
1010
import java.util.stream.Collectors;
1111

12-
public class EnumArgument<T extends Enum<T>> implements CommandArgument<T> {
12+
public class EnumArgument<T extends Enum<T>> extends CommandArgument<T> {
1313
private final Class<T> enumClass;
1414
private final Set<T> allowedValues;
1515

@@ -63,17 +63,15 @@ public boolean canParseArg(@NotNull CommandContext context, int index) {
6363

6464
@Override
6565
public @NotNull List<String> tabComplete(@NotNull CommandContext context, int index) {
66-
String input = context.getArg(index).toUpperCase();
6766
if (allowedValues.isEmpty()) {
6867
return Arrays.stream(enumClass.getEnumConstants())
6968
.map(Enum::name)
70-
.filter(name -> name.startsWith(input))
69+
.collect(Collectors.toList());
70+
} else {
71+
return allowedValues.stream()
72+
.map(Enum::name)
7173
.collect(Collectors.toList());
7274
}
73-
return allowedValues.stream()
74-
.map(Enum::name)
75-
.filter(name -> name.startsWith(input))
76-
.collect(Collectors.toList());
7775
}
7876

7977
private String getAllowedValuesString() {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package xyz.bitsquidd.bits.lib.command.arguments;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import xyz.bitsquidd.bits.lib.command.CommandContext;
5+
import xyz.bitsquidd.bits.lib.command.exceptions.ArgumentParseException;
6+
7+
import java.util.Collections;
8+
import java.util.List;
9+
10+
public interface ICommandArgument<T> {
11+
String getTypeName();
12+
13+
T parse(CommandContext context, int startIndex) throws ArgumentParseException;
14+
boolean canParseArg(CommandContext context, int argIndex);
15+
default int getRequiredArgs() {
16+
return 1;
17+
}
18+
default @NotNull List<String> tabComplete(CommandContext context, int startIndex) {
19+
return Collections.emptyList();
20+
}
21+
22+
void addTabCompletions(List<String> completions);
23+
@NotNull List<String> getAddedTabCompletions();
24+
25+
default boolean canParseFull(CommandContext context, int startIndex) {
26+
try {
27+
parse(context, startIndex);
28+
return true;
29+
} catch (ArgumentParseException e) {
30+
return false;
31+
}
32+
}
33+
34+
}

‎src/main/java/xyz/bitsquidd/bits/lib/command/arguments/IntegerArgument.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import xyz.bitsquidd.bits.lib.command.CommandContext;
55
import xyz.bitsquidd.bits.lib.command.exceptions.ArgumentParseException;
66

7-
public class IntegerArgument implements CommandArgument<Integer> {
7+
public class IntegerArgument extends CommandArgument<Integer> {
88
public static final IntegerArgument INSTANCE = new IntegerArgument();
99

1010
@Override

0 commit comments

Comments
 (0)