Skip to content

Commit

Permalink
Merge pull request #555 from JorelAli/dev/paper-hotfix
Browse files Browse the repository at this point in the history
Hotfix CommandAPI throwing errors because of Paper's Brigadier command API
  • Loading branch information
DerEchtePilz committed May 12, 2024
2 parents 36ddedb + 44f58cb commit 8a763c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public class NMS_1_20_R4 extends NMS_Common {
private static final Field entitySelectorUsesSelector;
// private static final SafeVarHandle<ItemInput, CompoundTag> itemInput;
private static final Field serverFunctionLibraryDispatcher;
private static final Field vanillaCommandDispatcher;

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

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

@Override
public Command wrapToVanillaCommandWrapper(CommandNode<CommandSourceStack> node) {
return new VanillaCommandWrapper(this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher, node);
if (vanillaCommandDispatcher != null) {
return new VanillaCommandWrapper(this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher, node);
} else {
return new VanillaCommandWrapper(this.<MinecraftServer>getMinecraftServer().getCommands(), node);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.mojang.brigadier.tree.CommandNode;
import dev.jorel.commandapi.CommandAPIBukkit;
import dev.jorel.commandapi.CommandAPIHandler;
import dev.jorel.commandapi.SafeVarHandle;
import dev.jorel.commandapi.arguments.ArgumentSubType;
import dev.jorel.commandapi.arguments.SuggestionProviders;
import dev.jorel.commandapi.commandsenders.AbstractCommandSender;
Expand Down Expand Up @@ -69,6 +70,7 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
Expand All @@ -92,6 +94,18 @@
*/
public abstract class NMS_Common extends CommandAPIBukkit<CommandSourceStack> {

private static final Field commandDispatcher;

static {
Field temporary;
try {
temporary = MinecraftServer.class.getDeclaredField("vanillaCommandDispatcher");
} catch (Exception e) {
temporary = null;
}
commandDispatcher = temporary;
}

private static NamespacedKey fromResourceLocation(ResourceLocation key) {
return NamespacedKey.fromString(key.getNamespace() + ":" + key.getPath());
}
Expand Down Expand Up @@ -350,7 +364,12 @@ public abstract Predicate<Block> getBlockPredicate(CommandContext<CommandSourceS

@Override
public final CommandDispatcher<CommandSourceStack> getBrigadierDispatcher() {
return this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher();
if (commandDispatcher != null) {
return this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher();
} else {
// Hoping this actually acts as a hotfix and commands at least register with Paper 1.20.6 build 65
return getResourcesDispatcher();
}
}

@Override
Expand Down

0 comments on commit 8a763c9

Please sign in to comment.