Paper commands should be built right before registration into command dispatcher (so the root command node exists in case we need it)

This commit is contained in:
Marc Baloup 2024-11-22 22:40:18 +01:00
parent e6b77bcad6
commit 974347cbde
2 changed files with 39 additions and 30 deletions

View File

@ -68,7 +68,9 @@
"1.20.5": 766, "1.20.5": 766,
"1.20.6": 766, "1.20.6": 766,
"1.21": 767, "1.21": 767,
"1.21.1": 767 "1.21.1": 767,
"1.21.2": 768,
"1.21.3": 768
}, },
"versionsOfProtocol": { "versionsOfProtocol": {
"4": [ "4": [
@ -223,6 +225,10 @@
"767": [ "767": [
"1.21", "1.21",
"1.21.1" "1.21.1"
],
"768": [
"1.21.2",
"1.21.3"
] ]
} }
} }

View File

@ -113,7 +113,7 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSour
/** /**
* The command node of this command. * The command node of this command.
*/ */
protected final LiteralCommandNode<CommandSourceStack> commandNode; protected LiteralCommandNode<CommandSourceStack> commandNode;
/** /**
* The command requested aliases. * The command requested aliases.
*/ */
@ -134,11 +134,9 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSour
public PaperBrigadierCommand(Plugin pl, RegistrationPolicy regPolicy) { public PaperBrigadierCommand(Plugin pl, RegistrationPolicy regPolicy) {
plugin = pl; plugin = pl;
registrationPolicy = regPolicy; registrationPolicy = regPolicy;
commandNode = buildCommand().build();
String[] aliasesTmp = getAliases(); String[] aliasesTmp = getAliases();
aliases = aliasesTmp == null ? new String[0] : aliasesTmp; aliases = aliasesTmp == null ? new String[0] : aliasesTmp;
description = getDescription(); description = getDescription();
postBuildCommand(commandNode);
register(); register();
//try { //try {
// PandalibPaperPermissions.addPermissionMapping("minecraft.command." + commandNode.getLiteral().toLowerCase(), getTargetPermission().toLowerCase()); // PandalibPaperPermissions.addPermissionMapping("minecraft.command." + commandNode.getLiteral().toLowerCase(), getTargetPermission().toLowerCase());
@ -160,6 +158,9 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSour
plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event -> { plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event -> {
updateVanillaPaperDispatcher(event.registrar().getDispatcher()); updateVanillaPaperDispatcher(event.registrar().getDispatcher());
commandNode = buildCommand().build();
postBuildCommand(commandNode);
if (vanillaPaperDispatcher.getRoot().getChild(commandNode.getName()) != null) { if (vanillaPaperDispatcher.getRoot().getChild(commandNode.getName()) != null) {
Log.info("Command /" + commandNode.getName() + " found in the vanilla dispatcher during initial command registration. Replacing it by force."); Log.info("Command /" + commandNode.getName() + " found in the vanilla dispatcher during initial command registration. Replacing it by force.");
vanillaPaperDispatcher.getRoot().getChildren().removeIf(c -> c.getName().equals(commandNode.getName())); vanillaPaperDispatcher.getRoot().getChildren().removeIf(c -> c.getName().equals(commandNode.getName()));
@ -177,40 +178,42 @@ public abstract class PaperBrigadierCommand extends BrigadierCommand<CommandSour
} }
} }
});
Bukkit.getServer().getScheduler().runTask(plugin, () -> { Bukkit.getServer().getScheduler().runTask(plugin, () -> {
if (vanillaPaperDispatcher == null) if (vanillaPaperDispatcher == null)
return; return;
Set<String> forceRegistrationAgain = new HashSet<>(); Set<String> forceRegistrationAgain = new HashSet<>();
forceRegistrationAgain.add(commandNode.getName()); forceRegistrationAgain.add(commandNode.getName());
if (registrationPolicy == RegistrationPolicy.ALL) if (registrationPolicy == RegistrationPolicy.ALL)
forceRegistrationAgain.addAll(List.of(aliases)); forceRegistrationAgain.addAll(List.of(aliases));
for (String aliasToForce : forceRegistrationAgain) { for (String aliasToForce : forceRegistrationAgain) {
CommandNode<CommandSourceStack> actualNode = vanillaPaperDispatcher.getRoot().getChild(aliasToForce); CommandNode<CommandSourceStack> actualNode = vanillaPaperDispatcher.getRoot().getChild(aliasToForce);
if (actualNode != null) { if (actualNode != null) {
//Log.info("Forcing registration of alias /" + aliasToForce + " for command /" + commandNode.getName() + ": replacing " + getCommandIdentity(actualNode) + "?"); //Log.info("Forcing registration of alias /" + aliasToForce + " for command /" + commandNode.getName() + ": replacing " + getCommandIdentity(actualNode) + "?");
if (PluginCommandNode.REFLECT.get().isInstance(actualNode)) { if (PluginCommandNode.REFLECT.get().isInstance(actualNode)) {
PluginCommandNode pcn = wrap(actualNode, PluginCommandNode.class); PluginCommandNode pcn = wrap(actualNode, PluginCommandNode.class);
if (pcn.getPlugin().equals(plugin)) if (pcn.getPlugin().equals(plugin))
return; return;
}
else if (BukkitCommandNode.REFLECT.get().isInstance(actualNode)) {
BukkitCommandNode bcn = wrap(actualNode, BukkitCommandNode.class);
if (bcn.getBukkitCommand() instanceof PluginCommand pc && pc.getPlugin().equals(plugin))
return;
}
vanillaPaperDispatcher.getRoot().getChildren().removeIf(c -> c.getName().equals(aliasToForce));
} }
else if (BukkitCommandNode.REFLECT.get().isInstance(actualNode)) {
BukkitCommandNode bcn = wrap(actualNode, BukkitCommandNode.class);
if (bcn.getBukkitCommand() instanceof PluginCommand pc && pc.getPlugin().equals(plugin))
return;
}
vanillaPaperDispatcher.getRoot().getChildren().removeIf(c -> c.getName().equals(aliasToForce));
}
/*else { /*else {
Log.info("Forcing registration of alias /" + aliasToForce + " for command /" + commandNode.getName() + ": no command found for alias. Adding alias."); Log.info("Forcing registration of alias /" + aliasToForce + " for command /" + commandNode.getName() + ": no command found for alias. Adding alias.");
}*/ }*/
LiteralCommandNode<CommandSourceStack> newPCN = unwrap(new PluginCommandNode(aliasToForce, plugin.getPluginMeta(), commandNode, description)); LiteralCommandNode<CommandSourceStack> newPCN = unwrap(new PluginCommandNode(aliasToForce, plugin.getPluginMeta(), commandNode, description));
vanillaPaperDispatcher.getRoot().addChild(newPCN); vanillaPaperDispatcher.getRoot().addChild(newPCN);
} }
});
}); });
} }