fix-commodore (#27)

This commit is contained in:
Alexander Söderberg 2020-10-06 12:39:06 +02:00 committed by GitHub
parent 8f8f98b189
commit c3469706ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 435 additions and 11 deletions

View file

@ -76,6 +76,8 @@ You can check whether or not the running server supports Brigadier, by using `bu
## cloud-paper
An example plugin using the `cloud-paper` API can be found [here](https://github.com/Sauilitired/cloud/tree/master/examples/example-bukkit).
`cloud-paper`works on all Bukkit derivatives and has graceful fallbacks for cases where Paper specific features are missing. It is initialized the same way as the Bukkit manager, except `PaperCommandManager`is used instead. When using Paper 1.15+ Brigadier mappings are available even without commodore present.
### dependency

View file

@ -256,6 +256,7 @@ public final class CloudBrigadierManager<C, S> {
* @param label Command label
* @param cloudCommand Cloud command instance
* @param permissionChecker Permission checker
* @param forceRegister Whether or not to force register an executor at every node
* @param executor Command executor
* @return Literal command node
*/
@ -263,6 +264,7 @@ public final class CloudBrigadierManager<C, S> {
final @NonNull Command<C> cloudCommand,
final @NonNull BiPredicate<@NonNull S,
@NonNull CommandPermission> permissionChecker,
final boolean forceRegister,
final com.mojang.brigadier.@NonNull Command<S> executor) {
final CommandTree.Node<CommandArgument<C, ?>> node = this.commandManager
.getCommandTree().getNamedNode(cloudCommand.getArguments().get(0).getName());
@ -272,10 +274,13 @@ public final class CloudBrigadierManager<C, S> {
.requires(sender -> permissionChecker.test(sender, (CommandPermission) node.getNodeMeta()
.getOrDefault("permission",
Permission.empty())));
if (forceRegister || (node.getValue() != null && node.getValue().getOwningCommand() != null)) {
literalArgumentBuilder.executes(executor);
}
literalArgumentBuilder.executes(executor);
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();
for (final CommandTree.Node<CommandArgument<C, ?>> child : node.getChildren()) {
constructedRoot.addChild(this.constructCommandNode(true, child,
constructedRoot.addChild(this.constructCommandNode(forceRegister, child,
permissionChecker, executor, provider).build());
}
return constructedRoot;

View file

@ -72,14 +72,16 @@ public class BukkitPluginRegistrationHandler<C> implements CommandRegistrationHa
public final boolean registerCommand(final @NonNull Command<?> command) {
/* We only care about the root command argument */
final CommandArgument<?, ?> commandArgument = command.getArguments().get(0);
if (this.registeredCommands.containsKey(commandArgument)) {
if (!(this.bukkitCommandManager.getCommandRegistrationHandler() instanceof CloudCommodoreManager)
&& this.registeredCommands.containsKey(commandArgument)) {
return false;
}
final String label;
final String prefixedLabel = String.format("%s:%s", this.bukkitCommandManager.getOwningPlugin().getName(),
commandArgument.getName()).toLowerCase();
if (bukkitCommands.containsKey(commandArgument.getName())) {
if (!(this.bukkitCommandManager.getCommandRegistrationHandler() instanceof CloudCommodoreManager)
&& bukkitCommands.containsKey(commandArgument.getName())) {
label = prefixedLabel;
} else {
label = commandArgument.getName();

View file

@ -26,11 +26,17 @@ package cloud.commandframework.bukkit;
import cloud.commandframework.Command;
import cloud.commandframework.brigadier.CloudBrigadierManager;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.permission.CommandPermission;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.LiteralCommandNode;
import me.lucko.commodore.Commodore;
import me.lucko.commodore.CommodoreProvider;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Collections;
@SuppressWarnings("ALL")
class CloudCommodoreManager<C> extends BukkitPluginRegistrationHandler<C> {
@ -57,9 +63,28 @@ class CloudCommodoreManager<C> extends BukkitPluginRegistrationHandler<C> {
final @NonNull BukkitCommand<C> bukkitCommand) {
final com.mojang.brigadier.Command<?> cmd = o -> 1;
final LiteralCommandNode<?> literalCommandNode = this.brigadierManager
.createLiteralCommandNode(label, command, (o, p) -> true, cmd);
this.commodore.register(bukkitCommand, literalCommandNode, p ->
this.commandManager.hasPermission(commandManager.getCommandSenderMapper().apply(p),
command.getCommandPermission()));
.<Object>createLiteralCommandNode(label, command, (o, p) -> {
final CommandSender sender = this.commodore.getBukkitSender(o);
return this.commandManager.hasPermission(this.commandManager.getCommandSenderMapper().apply(sender),
(CommandPermission) p);
}, false, cmd);
final CommandNode existingNode = this.commodore.getDispatcher().findNode(Collections.singletonList(label));
if (existingNode != null) {
this.mergeChildren(existingNode, literalCommandNode);
} else {
this.commodore.register(literalCommandNode);
}
}
private void mergeChildren(@Nullable final CommandNode<?> existingNode, @Nullable final CommandNode<?> node) {
for (final CommandNode child : node.getChildren()) {
final CommandNode<?> existingChild = existingNode.getChild(child.getName());
if (existingChild == null) {
existingNode.addChild(child);
} else {
this.mergeChildren(existingChild, child);
}
}
}
}

View file

@ -66,4 +66,14 @@ public abstract class EntitySelector {
public @NonNull String getSelector() {
return this.selector;
}
/**
* Check whether the selector selected at least one entity
*
* @return {@code true} if at least one entity was selected, else {@code false}
*/
public boolean hasAny() {
return !this.entities.isEmpty();
}
}

View file

@ -72,7 +72,7 @@ final class VelocityPluginRegistrationHandler<C> implements CommandRegistrationH
this.brigadierManager.createLiteralCommandNode(command.getArguments().get(0).getName(), (Command<C>) command,
(c, p) -> this.manager.hasPermission(
this.manager.getCommandSenderMapper()
.apply(c), p),
.apply(c), p), true,
commandContext -> {
final CommandSource source = commandContext.getSource();
final String input = commandContext.getInput();