8000 Fix optional argument registration error · CommandAPI/CommandAPI@44f58cb · GitHub
[go: up one dir, main page]

Skip to content

Commit 44f58cb

Browse files
committed
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
1 parent 7db2e9e commit 44f58cb

File tree

2 files changed

+22
-3
lines changed
  • commandapi-platforms/commandapi-bukkit/commandap 8000 i-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

+22
-3
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070

7171
import java.io.File;
7272
import java.io.IOException;
73+
import java.lang.reflect.Field;
7374
import java.util.*;
7475
import java.util.function.Function;
7576
import java.util.function.Predicate;
@@ -93,10 +94,16 @@
9394
*/
9495
public abstract class NMS_Common extends CommandAPIBukkit<CommandSourceStack> {
9596

96-
private static final SafeVarHandle<MinecraftServer, CommandDispatcher> commandDispatcher;
97+
private static final Field commandDispatcher;
9798

9899
static {
99-
commandDispatcher = SafeVarHandle.ofOrNull(MinecraftServer.class, "vanillaCommandDispatcher", "vanillaCommandDispatcher", CommandDispatcher.class);
100+
Field temporary;
101+
try {
102+
temporary = MinecraftServer.class.getDeclaredField("vanillaCommandDispatcher");
103+
} catch (Exception e) {
104+
temporary = null;
105+
}
106+
commandDispatcher = temporary;
100107
}
101108

102109
private static NamespacedKey fromResourceLocation(ResourceLocation key) {

0 commit comments

Comments
 (0)
0