Skip to content

Commit 8a763c9

Browse files
authored
Merge pull request CommandAPI#555 from JorelAli/dev/paper-hotfix
Hotfix CommandAPI throwing errors because of Paper's Brigadier command API
2 parents 36ddedb + 44f58cb commit 8a763c9

File tree

2 files changed

+33
-2
lines changed
  • commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms
    • commandapi-bukkit-1.20.5/src/main/java/dev/jorel/commandapi/nms
    • commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms

2 files changed

+33
-2
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.20.5/src/main/java/dev/jorel/commandapi/nms/NMS_1_20_R4.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public class NMS_1_20_R4 extends NMS_Common {
210210
private static final Field entitySelectorUsesSelector;
211211
// private static final SafeVarHandle<ItemInput, CompoundTag> itemInput;
212212
private static final Field serverFunctionLibraryDispatcher;
213+
private static final Field vanillaCommandDispatcher;
213214

214215
// Derived from net.minecraft.commands.Commands;
215216
private static final CommandBuildContext COMMAND_BUILD_CONTEXT;
@@ -230,6 +231,13 @@ public class NMS_1_20_R4 extends NMS_Common {
230231
// itemInput = SafeVarHandle.ofOrNull(ItemInput.class, "c", "tag", CompoundTag.class);
231232
// For some reason, MethodHandles fails for this field, but Field works okay
232233
serverFunctionLibraryDispatcher = CommandAPIHandler.getField(ServerFunctionLibrary.class, "g", "dispatcher");
234+
Field commandDispatcher;
235+
try {
236+
commandDispatcher = MinecraftServer.class.getDeclaredField("vanillaCommandDispatcher");
237+
} catch (NoSuchFieldException | SecurityException e) {
238+
commandDispatcher = null;
239+
}
240+
vanillaCommandDispatcher = commandDispatcher;
233241
}
234242

235243
private static NamespacedKey fromResourceLocation(ResourceLocation key) {
@@ -895,7 +903,11 @@ public final boolean isVanillaCommandWrapper(Command command) {
895903

896904
@Override
897905
public Command wrapToVanillaCommandWrapper(CommandNode<CommandSourceStack> node) {
898-
return new VanillaCommandWrapper(this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher, node);
906+
if (vanillaCommandDispatcher != null) {
907+
return new VanillaCommandWrapper(this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher, node);
908+
} else {
909+
return new VanillaCommandWrapper(this.<MinecraftServer>getMinecraftServer().getCommands(), node);
910+
}
899911
}
900912

901913
@Override

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-nms-common/src/main/java/dev/jorel/commandapi/nms/NMS_Common.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.mojang.brigadier.tree.CommandNode;
2929
import dev.jorel.commandapi.CommandAPIBukkit;
3030
import dev.jorel.commandapi.CommandAPIHandler;
31+
import dev.jorel.commandapi.SafeVarHandle;
3132
import dev.jorel.commandapi.arguments.ArgumentSubType;
3233
import dev.jorel.commandapi.arguments.SuggestionProviders;
3334
import dev.jorel.commandapi.commandsenders.AbstractCommandSender;
@@ -69,6 +70,7 @@
6970

7071
import java.io.File;
7172
import java.io.IOException;
73+
import java.lang.reflect.Field;
7274
import java.util.*;
7375
import java.util.function.Function;
7476
import java.util.function.Predicate;
@@ -92,6 +94,18 @@
9294
*/
9395
public abstract class NMS_Common extends CommandAPIBukkit<CommandSourceStack> {
9496

97+
private static final Field commandDispatcher;
98+
99+
static {
100+
Field temporary;
101+
try {
102+
temporary = MinecraftServer.class.getDeclaredField("vanillaCommandDispatcher");
103+
} catch (Exception e) {
104+
temporary = null;
105+
}
106+
commandDispatcher = temporary;
107+
}
108+
95109
private static NamespacedKey fromResourceLocation(ResourceLocation key) {
96110
return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath());
97111
}
@@ -350,7 +364,12 @@ public abstract Predicate<Block> getBlockPredicate(CommandContext<CommandSourceS
350364

351365
@Override
352366
public final CommandDispatcher<CommandSourceStack> getBrigadierDispatcher() {
353-
return this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher();
367+
if (commandDispatcher != null) {
368+
return this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher();
369+
} else {
370+
// Hoping this actually acts as a hotfix and commands at least register with Paper 1.20.6 build 65
371+
return getResourcesDispatcher();
372+
}
354373
}
355374

356375
@Override

0 commit comments

Comments
 (0)