diff --git a/src/main/java/io/josemmo/bukkit/plugin/utils/Internals.java b/src/main/java/io/josemmo/bukkit/plugin/utils/Internals.java index 7461a33..ad2c8fc 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/utils/Internals.java +++ b/src/main/java/io/josemmo/bukkit/plugin/utils/Internals.java @@ -6,6 +6,7 @@ import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -13,7 +14,7 @@ public class Internals { public static final float MINECRAFT_VERSION; private static final CommandDispatcher DISPATCHER; private static final CommandMap COMMAND_MAP; - private static final Method GET_BUKKIT_SENDER_METHOD; + private static @Nullable Method GET_BUKKIT_SENDER_METHOD = null; static { try { @@ -30,24 +31,30 @@ public class Internals { Object nmsInstance = obcClass.getDeclaredMethod("getServer").invoke(obcInstance); Class nmsClass = nmsInstance.getClass().getSuperclass(); - // Get "net.minecraft.server.CommandDispatcher" references - Object nmsDispatcherInstance = nmsClass.getDeclaredField("vanillaCommandDispatcher").get(nmsInstance); - Class nmsDispatcherClass = nmsDispatcherInstance.getClass(); + if (MINECRAFT_VERSION >= 20.6) { + // Get "net.minecraft.commands.Commands" references + Object nmsCommandsInstance = nmsClass.getDeclaredMethod("getCommands").invoke(nmsInstance); + Class nmsCommandsClass = nmsCommandsInstance.getClass(); - // Get Brigadier dispatcher instance - Method getDispatcherMethod = nmsDispatcherClass.getDeclaredMethod("a"); - getDispatcherMethod.setAccessible(true); - DISPATCHER = (CommandDispatcher) getDispatcherMethod.invoke(nmsDispatcherInstance); + // Get "com.mojang.brigadier.CommandDispatcher" instance + Field nmsDispatcherField = nmsCommandsClass.getDeclaredField("dispatcher"); + nmsDispatcherField.setAccessible(true); + DISPATCHER = (CommandDispatcher) nmsDispatcherField.get(nmsCommandsInstance); + } else { + // Get "net.minecraft.server.CommandDispatcher" references + Object nmsDispatcherInstance = nmsClass.getDeclaredField("vanillaCommandDispatcher").get(nmsInstance); + Class nmsDispatcherClass = nmsDispatcherInstance.getClass(); + + // Get "com.mojang.brigadier.CommandDispatcher" instance + Method getDispatcherMethod = nmsDispatcherClass.getDeclaredMethod("a"); + getDispatcherMethod.setAccessible(true); + DISPATCHER = (CommandDispatcher) getDispatcherMethod.invoke(nmsDispatcherInstance); + } // Get command map instance Field commandMapField = obcClass.getDeclaredField("commandMap"); commandMapField.setAccessible(true); COMMAND_MAP = (CommandMap) commandMapField.get(obcInstance); - - // Get CommandListenerWrapper.getBukkitSender() method - Class clwClass = Class.forName(nmsDispatcherClass.getPackage().getName() + ".CommandListenerWrapper"); - GET_BUKKIT_SENDER_METHOD = clwClass.getDeclaredMethod("getBukkitSender"); - GET_BUKKIT_SENDER_METHOD.setAccessible(true); } catch (Exception e) { throw new RuntimeException("Failed to get internal classes due to incompatible Minecraft server", e); } @@ -76,9 +83,12 @@ public class Internals { */ public static @NotNull CommandSender getBukkitSender(@NotNull Object source) { try { + if (GET_BUKKIT_SENDER_METHOD == null) { + GET_BUKKIT_SENDER_METHOD = source.getClass().getDeclaredMethod("getBukkitSender"); + } return (CommandSender) GET_BUKKIT_SENDER_METHOD.invoke(source); } catch (Exception e) { - throw new RuntimeException("Failed to extract bukkit sender from source", e); + throw new RuntimeException("Failed to extract Bukkit sender from source", e); } } }