11package xyz .webmc .wlib .util ;
22
3+ import java .util .List ;
34import java .util .Map ;
45
5- import dev .colbster937 .reflect .Mirror ;
6-
6+ import dev .colbster937 .reflect .MirrorSafe ;
77import org .bukkit .Bukkit ;
88import org .bukkit .command .Command ;
9+ import org .bukkit .command .CommandException ;
910import org .bukkit .command .CommandMap ;
11+ import org .bukkit .command .CommandSender ;
1012import org .bukkit .plugin .Plugin ;
11- import org .bukkit .plugin .PluginManager ;
1213
1314public final class CommandUtil {
1415 private static Plugin plugin ;
@@ -18,51 +19,60 @@ public static final void init(final Plugin plugin) {
1819 }
1920
2021 public static final void registerCommand (final Plugin plugin , final Command command ) {
21- commandMap ().register (plugin .getName (), command );
22+ getCommandMap ().register (plugin .getName (), command );
2223 syncCommands ();
2324 }
2425
2526 public static final void registerCommand (final Command command ) {
2627 registerCommand (plugin , command );
2728 }
2829
30+ public static final void registerCommands (final Plugin plugin , final List <Command > commands ) {
31+ getCommandMap ().registerAll (plugin .getName (), commands );
32+ syncCommands ();
33+ }
34+
35+ public static final void registerCommands (final List <Command > commands ) {
36+ registerCommands (plugin , commands );
37+ }
38+
2939 public static final void unregisterCommand (final String commandStr ) {
30- try {
31- final Command command = knownCommands ().remove (commandStr );
32- if (command != null ) {
33- command .unregister (commandMap ());
34- }
35- syncCommands ();
36- } catch (final ReflectiveOperationException ex ) {
37- throw new RuntimeException (ex );
40+ final Command command = getKnownCommands ().remove (commandStr );
41+
42+ if (command != null ) {
43+ command .unregister (getCommandMap ());
3844 }
45+
46+ syncCommands ();
3947 }
4048
41- private static final CommandMap commandMap () {
42- final CommandMap commandMap ;
49+ public static final boolean dispatch (final CommandSender sender , final String cmd ) throws CommandException {
50+ return getCommandMap ().dispatch (sender , cmd );
51+ }
4352
44- try {
45- final PluginManager pm = Bukkit .getPluginManager ();
46- commandMap = Mirror .getFieldValue (pm , "commandMap" );
47- } catch (final ReflectiveOperationException ex ) {
48- throw new RuntimeException (ex );
49- }
53+ public static final List <String > tabComplete (final CommandSender sender , final String cmd ) throws IllegalArgumentException {
54+ return getCommandMap ().tabComplete (sender , cmd );
55+ }
56+
57+ public static final Command getCommand (final String cmd ) {
58+ return getCommandMap ().getCommand (cmd );
59+ }
60+
61+ public static final void sendUnknownCommandString (final CommandSender sender ) {
62+ final Class <?> clazz = MirrorSafe .getClass ("org.spigotmc.SpigotConfig" );
63+ final String msg = MirrorSafe .getFieldValue (clazz , "unknownCommandMessage" );
64+ sender .sendMessage (msg );
65+ }
5066
51- return commandMap ;
67+ private static CommandMap getCommandMap () {
68+ return MirrorSafe .getFieldValue (Bukkit .getPluginManager (), "commandMap" );
5269 }
5370
54- private static final Map <String , Command > knownCommands ()
55- throws ReflectiveOperationException {
56- return Mirror .getFieldValue (commandMap (), "knownCommands" );
71+ private static Map <String , Command > getKnownCommands () {
72+ return MirrorSafe .getFieldValue (getCommandMap (), "knownCommands" );
5773 }
5874
59- private static final void syncCommands () {
60- SchedulerUtil .runAsync (() -> {
61- try {
62- Mirror .invokeMethod (Bukkit .getServer (), "syncCommands" );
63- } catch (final ReflectiveOperationException ex ) {
64- throw new RuntimeException (ex );
65- }
66- });
75+ private static void syncCommands () {
76+ MirrorSafe .invokeMethod (Bukkit .getServer (), "syncCommands" );
6777 }
6878}
0 commit comments