6
6
import org .bukkit .command .CommandMap ;
7
7
import org .bukkit .command .CommandSender ;
8
8
import org .jetbrains .annotations .NotNull ;
9
+ import org .jetbrains .annotations .Nullable ;
9
10
import java .lang .reflect .Field ;
10
11
import java .lang .reflect .Method ;
11
12
12
13
public class Internals {
13
14
public static final float MINECRAFT_VERSION ;
14
15
private static final CommandDispatcher <?> DISPATCHER ;
15
16
private static final CommandMap COMMAND_MAP ;
16
- private static final Method GET_BUKKIT_SENDER_METHOD ;
17
+ private static @ Nullable Method GET_BUKKIT_SENDER_METHOD = null ;
17
18
18
19
static {
19
20
try {
@@ -30,24 +31,30 @@ public class Internals {
30
31
Object nmsInstance = obcClass .getDeclaredMethod ("getServer" ).invoke (obcInstance );
31
32
Class <?> nmsClass = nmsInstance .getClass ().getSuperclass ();
32
33
33
- // Get "net.minecraft.server.CommandDispatcher" references
34
- Object nmsDispatcherInstance = nmsClass .getDeclaredField ("vanillaCommandDispatcher" ).get (nmsInstance );
35
- Class <?> nmsDispatcherClass = nmsDispatcherInstance .getClass ();
34
+ if (MINECRAFT_VERSION >= 20.6 ) {
35
+ // Get "net.minecraft.commands.Commands" references
36
+ Object nmsCommandsInstance = nmsClass .getDeclaredMethod ("getCommands" ).invoke (nmsInstance );
37
+ Class <?> nmsCommandsClass = nmsCommandsInstance .getClass ();
36
38
37
- // Get Brigadier dispatcher instance
38
- Method getDispatcherMethod = nmsDispatcherClass .getDeclaredMethod ("a" );
39
- getDispatcherMethod .setAccessible (true );
40
- DISPATCHER = (CommandDispatcher <?>) getDispatcherMethod .invoke (nmsDispatcherInstance );
39
+ // Get "com.mojang.brigadier.CommandDispatcher" instance
40
+ Field nmsDispatcherField = nmsCommandsClass .getDeclaredField ("dispatcher" );
41
+ nmsDispatcherField .setAccessible (true );
42
+ DISPATCHER = (CommandDispatcher <?>) nmsDispatcherField .get (nmsCommandsInstance );
43
+ } else {
44
+ // Get "net.minecraft.server.CommandDispatcher" references
45
+ Object nmsDispatcherInstance = nmsClass .getDeclaredField ("vanillaCommandDispatcher" ).get (nmsInstance );
46
+ Class <?> nmsDispatcherClass = nmsDispatcherInstance .getClass ();
47
+
48
+ // Get "com.mojang.brigadier.CommandDispatcher" instance
49
+ Method getDispatcherMethod = nmsDispatcherClass .getDeclaredMethod ("a" );
50
+ getDispatcherMethod .setAccessible (true );
51
+ DISPATCHER = (CommandDispatcher <?>) getDispatcherMethod .invoke (nmsDispatcherInstance );
52
+ }
41
53
42
54
// Get command map instance
43
55
Field commandMapField = obcClass .getDeclaredField ("commandMap" );
44
56
commandMapField .setAccessible (true );
45
57
COMMAND_MAP = (CommandMap ) commandMapField .get (obcInstance );
46
-
47
- // Get CommandListenerWrapper.getBukkitSender() method
48
- Class <?> clwClass = Class .forName (nmsDispatcherClass .getPackage ().getName () + ".CommandListenerWrapper" );
49
- GET_BUKKIT_SENDER_METHOD = clwClass .getDeclaredMethod ("getBukkitSender" );
50
- GET_BUKKIT_SENDER_METHOD .setAccessible (true );
51
58
} catch (Exception e ) {
52
59
throw new RuntimeException ("Failed to get internal classes due to incompatible Minecraft server" , e );
53
60
}
@@ -76,9 +83,12 @@ public class Internals {
76
83
*/
77
84
public static @ NotNull CommandSender getBukkitSender (@ NotNull Object source ) {
78
85
try {
86
+ if (GET_BUKKIT_SENDER_METHOD == null ) {
87
+ GET_BUKKIT_SENDER_METHOD = source .getClass ().getDeclaredMethod ("getBukkitSender" );
88
+ }
79
89
return (CommandSender ) GET_BUKKIT_SENDER_METHOD .invoke (source );
80
90
} catch (Exception e ) {
81
- throw new RuntimeException ("Failed to extract bukkit sender from source" , e );
91
+ throw new RuntimeException ("Failed to extract Bukkit sender from source" , e );
82
92
}
83
93
}
84
94
}
0 commit comments