11package gg .quartzdev .mc .lib .qlibpaper .commands ;
22
3- import gg .quartzdev .mc .lib .qlibpaper .Sender ;
4- import gg .quartzdev .mc .lib .qlibpaper .lang .GenericMessages ;
5- import gg .quartzdev .mc .lib .qlibpaper .lang .QPlaceholder ;
6- import org .bukkit .Bukkit ;
7- import org .bukkit .command .Command ;
8- import org .bukkit .command .CommandSender ;
9- import org .bukkit .util .StringUtil ;
10- import org .jetbrains .annotations .NotNull ;
3+ import io .papermc .paper .plugin .lifecycle .event .types .LifecycleEvents ;
4+ import org .bukkit .plugin .java .JavaPlugin ;
5+ import org .jetbrains .annotations .Nullable ;
116
12- import java .util .ArrayList ;
13- import java .util .Collections ;
7+ import java .util .Collection ;
148import java .util .HashMap ;
15- import java .util .HashSet ;
16- import java .util .List ;
17- import java .util .Map ;
18- import java .util .Set ;
199
20- public class CommandManager extends Command
10+ public class CommandManager
2111{
22- HashMap <String , QCommand > subCommands ;
2312
24- public CommandManager (String name , @ NotNull List <String > aliases )
25- {
26- super (name );
27- super .setAliases (aliases );
28- subCommands = new HashMap <>();
29- Bukkit .getCommandMap ().register (name , this );
30- }
31-
32- public void addSubCommand (String label , QCommand subCommand )
33- {
34- subCommands .put (label , subCommand );
35- }
13+ private final HashMap <String , QCMD > commands = new HashMap <>();
14+ private final JavaPlugin plugin ;
3615
37- @ Override
38- public boolean execute (@ NotNull CommandSender sender , @ NotNull String labelOrAlias , @ NotNull String [] args )
39- {
40- // Send base command
41- if (args .length == 0 )
42- {
43- return subCommands .get ("" ).run (sender , labelOrAlias , args );
44- }
4516
46- // Get subcommand from args
47- QCommand cmd = subCommands .get (args [0 ]);
48-
49- if (cmd == null )
50- {
51- Sender .message (sender , GenericMessages .CMD_NOT_FOUND .parse (QPlaceholder .COMMAND , args [0 ]));
52- return false ;
53- }
54-
55- // Run the command
56- return cmd .run (sender , labelOrAlias , args );
17+ public CommandManager (JavaPlugin plugin ) {
18+ this .plugin = plugin ;
5719 }
5820
59- @ Override
60- public @ NotNull List <String > tabComplete (@ NotNull CommandSender sender , @ NotNull String labelOrAlias , String [] args ) throws IllegalArgumentException
61- {
62- List <String > completions = new ArrayList <>();
21+ /**
22+ * Adds a command along with its aliases. After adding all commands call {@link #registerCommands()} to register them.
23+ * @param command the command to add
24+ * @param aliases the aliases for the command
25+ */
26+ public void add (QCMD command , @ Nullable Collection <String > aliases ) {
27+ if (aliases != null ) command .aliases (aliases );
28+ commands .put (command .label (), command );
29+ }
6330
64- // Only tab complete a sub command if the player has permission
65- if (args .length == 1 )
66- {
67- Set <String > allowedSubCommands = new HashSet <>();
68- for (Map .Entry <String , QCommand > entry : subCommands .entrySet ())
69- {
70- String commandName = entry .getKey ();
71- QCommand cmd = entry .getValue ();
72- if (cmd .hasPermission (sender ))
31+ @ SuppressWarnings ("UnstableApiUsage" )
32+ public void registerCommands (){
33+ plugin .getLifecycleManager ().registerEventHandler (
34+ LifecycleEvents .COMMANDS ,
35+ event ->
7336 {
74- allowedSubCommands .add (commandName );
37+ for (QCMD command : commands .values ()){
38+ event .registrar ().register (command .label (), command .description (), command .aliases (), command );
39+ }
7540 }
76- }
77- StringUtil .copyPartialMatches (args [0 ], allowedSubCommands , completions );
78- }
79-
80- // Let the subcommand handle tab completion
81- if (args .length > 1 )
82- {
83- QCommand cmd = subCommands .get (args [0 ]);
84-
85- if (cmd == null )
86- {
87- return completions ;
88- }
89-
90- Iterable <String > rawCompletions = cmd .getTabCompletions (sender , args );
91- if (rawCompletions != null )
92- {
93- StringUtil .copyPartialMatches (args [args .length - 1 ], rawCompletions , completions );
94- }
95- }
96-
97- Collections .sort (completions );
98- return completions ;
41+ );
9942 }
10043
101- }
102-
44+ public void unregister (QCMD command ) {
45+ commands .remove (command .label ());
46+ }
47+ }
0 commit comments