Skip to content

Commit f9a8dcc

Browse files
committed
Fix SpigotCommandRegistration having two copies of brigadierDispatcher
It seems the underlying field accessed by running `this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()` changes at some point. So, calling this during load actually results in just another copy of the `brigadierDispatcher`. However, if we call it later, it correctly returns the `resourcesDispatcher`.
1 parent a0b1046 commit f9a8dcc

File tree

16 files changed

+35
-19
lines changed

16 files changed

+35
-19
lines changed

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/SpigotCommandRegistration.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.bukkit.command.SimpleCommandMap;
1212

1313
import java.util.*;
14+
import java.util.function.Supplier;
1415

1516
/**
1617
* Handles logic for registering commands on Spigot and old versions of Paper
@@ -21,7 +22,7 @@ public class SpigotCommandRegistration<Source> extends CommandRegistrationStrate
2122

2223
// References to necessary objects
2324
private final CommandDispatcher<Source> brigadierDispatcher;
24-
private final CommandDispatcher<Source> resourcesDispatcher;
25+
private final Supplier<CommandDispatcher<Source>> getResourcesDispatcher;
2526

2627
// Namespaces
2728
private final Set<String> namespacesToFix = new HashSet<>();
@@ -30,11 +31,11 @@ public class SpigotCommandRegistration<Source> extends CommandRegistrationStrate
3031
// Reflection
3132
private final SafeVarHandle<SimpleCommandMap, Map<String, Command>> commandMapKnownCommands;
3233

33-
public SpigotCommandRegistration(CommandAPIBukkit<Source> commandAPIBukkit, CommandDispatcher<Source> brigadierDispatcher, CommandDispatcher<Source> resourcesDispatcher) {
34+
public SpigotCommandRegistration(CommandAPIBukkit<Source> commandAPIBukkit, CommandDispatcher<Source> brigadierDispatcher, Supplier<CommandDispatcher<Source>> getResourcesDispatcher) {
3435
this.commandAPIBukkit = commandAPIBukkit;
3536

3637
this.brigadierDispatcher = brigadierDispatcher;
37-
this.resourcesDispatcher = resourcesDispatcher;
38+
this.getResourcesDispatcher = getResourcesDispatcher;
3839

3940
this.commandMapKnownCommands = SafeVarHandle.ofOrNull(SimpleCommandMap.class, "knownCommands", "knownCommands", Map.class);
4041
}
@@ -69,6 +70,7 @@ public void runTasksAfterServerStart() {
6970

7071
private void fixNamespaces() {
7172
Map<String, Command> knownCommands = commandMapKnownCommands.get((SimpleCommandMap) commandAPIBukkit.getPaper().getCommandMap());
73+
CommandDispatcher<Source> resourcesDispatcher = getResourcesDispatcher.get();
7274

7375
// Remove namespaces
7476
for (String command : namespacesToFix) {
@@ -143,7 +145,7 @@ public void postCommandRegistration(RegisteredCommand registeredCommand, Literal
143145
// and avoiding doing things twice for existing commands, this is a distilled version of those methods.
144146

145147
Map<String, Command> knownCommands = commandMapKnownCommands.get((SimpleCommandMap) commandAPIBukkit.getPaper().getCommandMap());
146-
RootCommandNode<Source> root = resourcesDispatcher.getRoot();
148+
RootCommandNode<Source> root = getResourcesDispatcher.get().getRoot();
147149

148150
String name = resultantNode.getLiteral();
149151
String namespace = registeredCommand.namespace();
@@ -291,7 +293,7 @@ public void unregister(String commandName, boolean unregisterNamespaces, boolean
291293
// Remove commands from the resources dispatcher
292294
// If we are unregistering a Bukkit command, ONLY unregister BukkitCommandWrappers
293295
// If we are unregistering a Vanilla command, DO NOT unregister BukkitCommandWrappers
294-
removeBrigadierCommands(resourcesDispatcher, commandName, unregisterNamespaces,
296+
removeBrigadierCommands(getResourcesDispatcher.get(), commandName, unregisterNamespaces,
295297
c -> !unregisterBukkit ^ commandAPIBukkit.isBukkitCommandWrapper(c));
296298
}
297299
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.16.1/src/main/java/dev/jorel/commandapi/nms/NMS_1_16_R1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ public BlockData getBlockState(CommandContext<CommandListenerWrapper> cmdCtx, St
533533
public CommandRegistrationStrategy<CommandListenerWrapper> createCommandRegistrationStrategy() {
534534
return new SpigotCommandRegistration<>(this,
535535
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.a(),
536-
this.<MinecraftServer>getMinecraftServer().getCommandDispatcher().a()
536+
() -> this.<MinecraftServer>getMinecraftServer().getCommandDispatcher().a()
537537
);
538538
}
539539

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.16.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_16_R2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ public BlockData getBlockState(CommandContext<CommandListenerWrapper> cmdCtx, St
531531
public CommandRegistrationStrategy<CommandListenerWrapper> createCommandRegistrationStrategy() {
532532
return new SpigotCommandRegistration<>(this,
533533
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.a(),
534-
this.<MinecraftServer>getMinecraftServer().getCommandDispatcher().a()
534+
() -> this.<MinecraftServer>getMinecraftServer().getCommandDispatcher().a()
535535
);
536536
}
537537

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.16.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_16_4_R3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ public BlockData getBlockState(CommandContext<CommandListenerWrapper> cmdCtx, St
487487
public CommandRegistrationStrategy<CommandListenerWrapper> createCommandRegistrationStrategy() {
488488
return new SpigotCommandRegistration<>(this,
489489
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.a(),
490-
this.<MinecraftServer>getMinecraftServer().getCommandDispatcher().a()
490+
() -> this.<MinecraftServer>getMinecraftServer().getCommandDispatcher().a()
491491
);
492492
}
493493

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public BlockData getBlockState(CommandContext<CommandSourceStack> cmdCtx, String
316316
public CommandRegistrationStrategy<CommandSourceStack> createCommandRegistrationStrategy() {
317317
return new SpigotCommandRegistration<>(this,
318318
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher(),
319-
this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
319+
() -> this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
320320
);
321321
}
322322

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.18.2/src/main/java/dev/jorel/commandapi/nms/NMS_1_18_R2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public BlockData getBlockState(CommandContext<CommandSourceStack> cmdCtx, String
376376
public CommandRegistrationStrategy<CommandSourceStack> createCommandRegistrationStrategy() {
377377
return new SpigotCommandRegistration<>(this,
378378
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher(),
379-
this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
379+
() -> this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
380380
);
381381
}
382382

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.18/src/main/java/dev/jorel/commandapi/nms/NMS_1_18_R1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public BlockData getBlockState(CommandContext<CommandSourceStack> cmdCtx, String
324324
public CommandRegistrationStrategy<CommandSourceStack> createCommandRegistrationStrategy() {
325325
return new SpigotCommandRegistration<>(this,
326326
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher(),
327-
this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
327+
() -> this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
328328
);
329329
}
330330

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public final BlockData getBlockState(CommandContext<CommandSourceStack> cmdCtx,
446446
public CommandRegistrationStrategy<CommandSourceStack> createCommandRegistrationStrategy() {
447447
return new SpigotCommandRegistration<>(this,
448448
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher(),
449-
this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
449+
() -> this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
450450
);
451451
}
452452

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.19.3/src/main/java/dev/jorel/commandapi/nms/NMS_1_19_3_R2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public final BlockData getBlockState(CommandContext<CommandSourceStack> cmdCtx,
321321
public CommandRegistrationStrategy<CommandSourceStack> createCommandRegistrationStrategy() {
322322
return new SpigotCommandRegistration<>(this,
323323
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher(),
324-
this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
324+
() -> this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
325325
);
326326
}
327327

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-nms/commandapi-bukkit-1.19.4/src/main/java/dev/jorel/commandapi/nms/NMS_1_19_4_R3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ public final BlockData getBlockState(CommandContext<CommandSourceStack> cmdCtx,
318318
public CommandRegistrationStrategy<CommandSourceStack> createCommandRegistrationStrategy() {
319319
return new SpigotCommandRegistration<>(this,
320320
this.<MinecraftServer>getMinecraftServer().vanillaCommandDispatcher.getDispatcher(),
321-
this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
321+
() -> this.<MinecraftServer>getMinecraftServer().getCommands().getDispatcher()
322322
);
323323
}
324324

0 commit comments

Comments
 (0)