1
1
package fr .zcraft .quartzlib .components .commands ;
2
2
3
3
import fr .zcraft .quartzlib .components .commands .exceptions .CommandException ;
4
+ import java .lang .reflect .Field ;
4
5
import java .util .Arrays ;
5
6
import java .util .HashMap ;
6
7
import java .util .Map ;
7
8
import java .util .function .Supplier ;
8
9
import org .bukkit .command .CommandSender ;
10
+ import org .jetbrains .annotations .Nullable ;
9
11
10
12
class CommandGroup extends CommandNode {
11
13
private final Class <?> commandGroupClass ;
14
+
15
+ @ Nullable
12
16
private final Supplier <?> classInstanceSupplier ;
17
+ @ Nullable
18
+ private final GroupClassInstanceSupplier groupClassInstanceSupplier ;
13
19
14
20
private final Map <String , CommandNode > subCommands = new HashMap <>();
15
21
16
- public CommandGroup (Class <?> commandGroupClass , Supplier <?> classInstanceSupplier , String name ,
17
- TypeCollection typeCollection ) {
18
- this (commandGroupClass , classInstanceSupplier , name , typeCollection , null );
22
+ CommandGroup (Class <?> commandGroupClass , Supplier <?> classInstanceSupplier , String name ,
23
+ TypeCollection typeCollection ) {
24
+ this (commandGroupClass , classInstanceSupplier , null , name , typeCollection , null );
25
+ }
26
+
27
+ CommandGroup (Class <?> commandGroupClass , GroupClassInstanceSupplier classInstanceSupplier , String name ,
28
+ CommandGroup parent , TypeCollection typeCollection ) {
29
+ this (commandGroupClass , null , classInstanceSupplier , name , typeCollection , parent );
30
+ }
31
+
32
+ CommandGroup (CommandGroup parent , Field backingField , TypeCollection typeCollection ) {
33
+ this (
34
+ backingField .getType (),
35
+ GroupClassInstanceSupplier .backingField (backingField ),
36
+ backingField .getName (),
37
+ parent ,
38
+ typeCollection
39
+ );
19
40
}
20
41
21
- public CommandGroup (Class <?> commandGroupClass , Supplier <?> classInstanceSupplier , String name ,
22
- TypeCollection typeCollection , CommandGroup parent ) {
42
+ private CommandGroup (
43
+ Class <?> commandGroupClass ,
44
+ @ Nullable Supplier <?> classInstanceSupplier ,
45
+ @ Nullable GroupClassInstanceSupplier groupClassInstanceSupplier , String name ,
46
+ TypeCollection typeCollection , CommandGroup parent ) {
23
47
super (name , parent );
24
48
this .commandGroupClass = commandGroupClass ;
25
49
this .classInstanceSupplier = classInstanceSupplier ;
50
+ this .groupClassInstanceSupplier = groupClassInstanceSupplier ;
26
51
DiscoveryUtils .getCommandMethods (commandGroupClass , typeCollection ).forEach (this ::addMethod );
52
+ DiscoveryUtils .getSubCommands (this , typeCollection ).forEach (this ::addSubCommand );
27
53
}
28
54
29
55
public Iterable <CommandNode > getSubCommands () {
@@ -41,15 +67,36 @@ private void addMethod(CommandMethod method) {
41
67
endpoint .addMethod (method );
42
68
}
43
69
70
+ private void addSubCommand (CommandGroup commandGroup ) {
71
+ subCommands .put (commandGroup .getName (), commandGroup );
72
+ }
73
+
44
74
void run (CommandSender sender , String ... args ) throws CommandException {
75
+ if (classInstanceSupplier == null ) {
76
+ throw new IllegalStateException ("This command group comes from a parent and cannot instanciate itself." );
77
+ }
78
+
45
79
Object commandObject = classInstanceSupplier .get ();
46
- run (commandObject , sender , args );
80
+ runSelf (commandObject , sender , args );
47
81
}
48
82
49
83
@ Override
50
- void run (Object instance , CommandSender sender , String [] args ) throws CommandException {
84
+ void run (Object parentInstance , CommandSender sender , String [] args ) throws CommandException {
85
+ if (this .groupClassInstanceSupplier == null ) {
86
+ throw new IllegalStateException ("This command group cannot be ran from a parent" );
87
+ }
88
+
89
+ Object instance = this .groupClassInstanceSupplier .supply (parentInstance );
90
+ runSelf (instance , sender , args );
91
+ }
92
+
93
+ private void runSelf (Object instance , CommandSender sender , String [] args ) throws CommandException {
51
94
String commandName = args [0 ];
52
95
CommandNode subCommand = subCommands .get (commandName );
53
96
subCommand .run (instance , sender , Arrays .copyOfRange (args , 1 , args .length ));
54
97
}
98
+
99
+ public Class <?> getCommandGroupClass () {
100
+ return commandGroupClass ;
101
+ }
55
102
}
0 commit comments