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 1 commit
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
Prev Previous commit
Fix optional argument registration error
+ fixed exception appearing in server log, this isn't done with VarHandles but I think because this is a hotfix, it is fine for now.
I will handle all this properly on dev/commandapi-paper and #517
  • Loading branch information
DerEchtePilz committed May 12, 2024
commit 44f58cb4da51890d0e6f2e064058ca81b4ca4d22
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 @@ -70,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 @@ -93,10 +94,16 @@
*/
public abstract class NMS_Common extends CommandAPIBukkit<CommandSourceStack> {

private static final SafeVarHandle<MinecraftServer, CommandDispatcher> commandDispatcher;
private static final Field commandDispatcher;

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

private static NamespacedKey fromResourceLocation(ResourceLocation key) {
Expand Down
0