8000 Hotfix CommandAPI throwing errors because of Paper's Brigadier command API by DerEchtePilz · Pull Request #555 · CommandAPI/CommandAPI · GitHub
[go: up one dir, main page]

Skip to content

Hotfix CommandAPI throwing errors because of Paper's Brigadier command API #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Comment on lines +97 to +107
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A field that could be null that doesn't have @Nullable or wrapped in an Optional! 🫢


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