Skip to content

Commit 7726b9a

Browse files
committed
Fixed command dispatcher in MC 1.20.6
- Updated Internals class > Related to #125
1 parent a90aad5 commit 7726b9a

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/main/java/io/josemmo/bukkit/plugin/utils/Internals.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import org.bukkit.command.CommandMap;
77
import org.bukkit.command.CommandSender;
88
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
910
import java.lang.reflect.Field;
1011
import java.lang.reflect.Method;
1112

1213
public class Internals {
1314
public static final float MINECRAFT_VERSION;
1415
private static final CommandDispatcher<?> DISPATCHER;
1516
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;
1718

1819
static {
1920
try {
@@ -30,24 +31,30 @@ public class Internals {
3031
Object nmsInstance = obcClass.getDeclaredMethod("getServer").invoke(obcInstance);
3132
Class<?> nmsClass = nmsInstance.getClass().getSuperclass();
3233

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();
3638

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+
}
4153

4254
// Get command map instance
4355
Field commandMapField = obcClass.getDeclaredField("commandMap");
4456
commandMapField.setAccessible(true);
4557
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);
5158
} catch (Exception e) {
5259
throw new RuntimeException("Failed to get internal classes due to incompatible Minecraft server", e);
5360
}
@@ -76,9 +83,12 @@ public class Internals {
7683
*/
7784
public static @NotNull CommandSender getBukkitSender(@NotNull Object source) {
7885
try {
86+
if (GET_BUKKIT_SENDER_METHOD == null) {
87+
GET_BUKKIT_SENDER_METHOD = source.getClass().getDeclaredMethod("getBukkitSender");
88+
}
7989
return (CommandSender) GET_BUKKIT_SENDER_METHOD.invoke(source);
8090
} 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);
8292
}
8393
}
8494
}

0 commit comments

Comments
 (0)