Skip to content

Commit 02f02dd

Browse files
committed
Added MissingSubcommandException, plus some more internal changes
1 parent fd54913 commit 02f02dd

File tree

16 files changed

+145
-11
lines changed

16 files changed

+145
-11
lines changed

src/main/java/fr/zcraft/quartzlib/components/commands/CommandGroup.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
package fr.zcraft.quartzlib.components.commands;
22

33
import fr.zcraft.quartzlib.components.commands.exceptions.CommandException;
4+
import fr.zcraft.quartzlib.components.commands.exceptions.MissingSubcommandException;
45
import java.lang.reflect.Field;
56
import java.util.Arrays;
7+
import java.util.Collection;
68
import java.util.HashMap;
79
import java.util.Map;
810
import java.util.function.Supplier;
911
import org.bukkit.command.CommandSender;
1012
import org.jetbrains.annotations.Nullable;
1113

12-
class CommandGroup extends CommandNode {
14+
public class CommandGroup extends CommandNode {
1315
private final Class<?> commandGroupClass;
1416

1517
@Nullable
@@ -52,7 +54,7 @@ private CommandGroup(
5254
DiscoveryUtils.getSubCommands(this, typeCollection).forEach(this::addSubCommand);
5355
}
5456

55-
public Iterable<CommandNode> getSubCommands() {
57+
public Collection<CommandNode> getSubCommands() {
5658
return this.subCommands.values();
5759
}
5860

@@ -91,6 +93,10 @@ void run(Object parentInstance, CommandSender sender, String[] args) throws Comm
9193
}
9294

9395
private void runSelf(Object instance, CommandSender sender, String[] args) throws CommandException {
96+
if (args.length == 0) {
97+
throw new MissingSubcommandException(this);
98+
}
99+
94100
String commandName = args[0];
95101
CommandNode subCommand = subCommands.get(commandName);
96102
// TODO: handle null

src/main/java/fr/zcraft/quartzlib/components/commands/CommandMethod.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CommandMethod {
2929
if (parameter.isAnnotationPresent(Sender.class)) { // TODO: check for multiple sender arguments
3030
senderArgument = new CommandMethodSenderArgument(parameter, i, typeCollection);
3131
} else {
32-
arguments.add(new CommandMethodArgument(parameter, i, typeCollection));
32+
arguments.add(new CommandMethodArgument(this, parameter, i, typeCollection));
3333
}
3434
}
3535

@@ -69,4 +69,8 @@ private Object[] parseArguments(CommandSender sender, String[] args)
6969
public CommandMethodArgument[] getArguments() {
7070
return arguments;
7171
}
72+
73+
public Method getMethod() {
74+
return method;
75+
}
7276
}

src/main/java/fr/zcraft/quartzlib/components/commands/CommandMethodArgument.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package fr.zcraft.quartzlib.components.commands;
22

33
import fr.zcraft.quartzlib.components.commands.exceptions.ArgumentParseException;
4+
import fr.zcraft.quartzlib.components.commands.exceptions.UnknownArgumentTypeException;
45
import java.lang.reflect.Parameter;
56

67
public class CommandMethodArgument {
78
private final Parameter parameter;
89
private final int position;
910
private final ArgumentTypeWrapper<?> typeHandler;
1011

11-
public CommandMethodArgument(Parameter parameter, int position, TypeCollection typeCollection) {
12+
public CommandMethodArgument(
13+
CommandMethod parent,
14+
Parameter parameter,
15+
int position,
16+
TypeCollection typeCollection
17+
) {
1218
this.parameter = parameter;
1319
this.position = position;
14-
this.typeHandler = typeCollection.findArgumentType(parameter.getType()).get(); // FIXME: handle unknown types
20+
this.typeHandler = typeCollection.findArgumentType(parameter.getType())
21+
.orElseThrow(() -> new UnknownArgumentTypeException(parent.getMethod(), parameter.getType()));
1522
}
1623

1724
public Object parse(String raw) throws ArgumentParseException {

src/main/java/fr/zcraft/quartzlib/components/commands/CommandNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.bukkit.command.CommandSender;
55
import org.jetbrains.annotations.Nullable;
66

7-
abstract class CommandNode {
7+
public abstract class CommandNode {
88
private final String name;
99
@Nullable private final CommandGroup parent;
1010

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fr.zcraft.quartzlib.components.commands;
2+
3+
import org.bukkit.command.CommandSender;
4+
5+
public class ExecutionContext {
6+
private final CommandSender sender;
7+
private final String[] fullArgs;
8+
9+
public ExecutionContext(CommandSender sender, String[] fullArgs) {
10+
this.sender = sender;
11+
this.fullArgs = fullArgs;
12+
}
13+
}

src/main/java/fr/zcraft/quartzlib/components/commands/QuartzCommandExecutor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fr.zcraft.quartzlib.components.commands;
22

33
import fr.zcraft.quartzlib.components.commands.exceptions.CommandException;
4+
import fr.zcraft.quartzlib.tools.text.RawMessage;
45
import org.bukkit.command.Command;
56
import org.bukkit.command.CommandExecutor;
67
import org.bukkit.command.CommandSender;
@@ -19,7 +20,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
1920
try {
2021
group.run(sender, args);
2122
} catch (CommandException e) {
22-
throw new RuntimeException(e); // TODO
23+
RawMessage.send(sender, e.display(sender).build());
2324
}
2425
return true;
2526
}

src/main/java/fr/zcraft/quartzlib/components/commands/TypeCollection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ private void registerNativeTypes() {
6262
register(new ArgumentTypeWrapper<>(Integer.class, new IntegerArgumentType()));
6363
register(new ArgumentTypeWrapper<>(String.class, s -> s));
6464

65+
register(new ArgumentTypeWrapper<>(int.class, new IntegerArgumentType()));
66+
6567
// Generic types
6668
register(new EnumArgumentType());
6769

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package fr.zcraft.quartzlib.components.commands.exceptions;
22

3+
import fr.zcraft.quartzlib.components.rawtext.RawText;
4+
import org.bukkit.command.CommandSender;
5+
36
public class ArgumentParseException extends CommandException {
7+
@Override
8+
public RawText display(CommandSender sender) {
9+
return null;
10+
}
411
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
package fr.zcraft.quartzlib.components.commands.exceptions;
22

3+
import fr.zcraft.quartzlib.components.rawtext.RawText;
4+
import org.bukkit.command.CommandSender;
5+
36
public abstract class CommandException extends Exception {
7+
public abstract RawText display(CommandSender sender);
48
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
package fr.zcraft.quartzlib.components.commands.exceptions;
22

3+
import fr.zcraft.quartzlib.components.rawtext.RawText;
4+
import org.bukkit.command.CommandSender;
5+
36
public class InvalidSenderException extends CommandException {
7+
@Override
8+
public RawText display(CommandSender sender) {
9+
return null;
10+
}
411
}

0 commit comments

Comments
 (0)