Skip to content

Commit

Permalink
Fixed critical brigadier dispatch bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqzn committed Oct 5, 2024
1 parent da27553 commit b3ffdef
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public <S extends Source> BrigadierNode withExecution(
setExecution((context) -> {
String input = context.getInput();
S sender = manager.wrapCommandSource(context.getSource());
dispatcher.dispatch(sender, context.getRootNode().getName(), input);
dispatcher.dispatch(sender, input);
return Command.SINGLE_SUCCESS;
});
return this;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {

allprojects {
group = "dev.velix"
version = "1.0.0-b.6"
version = "1.0.0-b.7"

ext {
def kyoriVersion = "4.17.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.velix.imperat.command.Command;
import dev.velix.imperat.command.CommandUsage;
import dev.velix.imperat.util.ImperatDebugger;
import dev.velix.imperat.util.StringUtils;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.plugin.Plugin;
Expand Down Expand Up @@ -67,7 +66,7 @@ public boolean execute(@NotNull CommandSender sender,

try {
BukkitSource source = dispatcher.wrapSender(sender);
dispatcher.dispatch(source, StringUtils.stripNamespace(label), raw);
dispatcher.dispatch(source, this.command, raw);
return true;
} catch (Exception ex) {
ImperatDebugger.error(InternalBukkitCommand.class, "execute", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -159,7 +159,7 @@ protected static <S> LiteralCommandNode<S> renameLiteralNode(LiteralCommandNode<
* @return the aliases
*/
protected static Collection<String> getAliases(Command command) {
Objects.requireNonNull(command, "command");
if (command == null) return Collections.emptyList();

Stream<String> aliasesStream = Stream.concat(
Stream.of(command.getLabel()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ default void register(C command, LiteralArgumentBuilder<?> argumentBuilder) {
* <p>Equivalent to calling
* {@link CommandDispatcher#register(LiteralArgumentBuilder)}.</p>
*
* <p>Prefer using {@link #register(C, LiteralCommandNode)}.</p>
* <p>Prefer using {@link Commodore#register(C, LiteralCommandNode)}.</p>
*
* @param node the argument data
*/
Expand All @@ -132,7 +132,7 @@ default void register(C command, LiteralArgumentBuilder<?> argumentBuilder) {
* <p>Equivalent to calling
* {@link CommandDispatcher#register(LiteralArgumentBuilder)}.</p>
*
* <p>Prefer using {@link #register(C, LiteralArgumentBuilder)}.</p>
* <p>Prefer using {@link Commodore#register(C, LiteralArgumentBuilder)}.</p>
*
* @param argumentBuilder the argument data
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,18 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

@SuppressWarnings("ALL")
final class ModernPaperCommodore extends AbstractCommodore<WrappedBukkitCommand> {

private final Plugin plugin;
private final LifecycleEventManager<Plugin> manager;
private final List<WrappedBrigNode> commands = new ArrayList<>();

ModernPaperCommodore(Plugin plugin) throws ClassNotFoundException {
Class.forName("io.papermc.paper.command.brigadier.Commands");
this.plugin = plugin;
this.manager = plugin.getLifecycleManager();
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
Commands registrar = event.registrar();
commands.forEach(command -> {
var bukkitCmd = command.command;
String desc;
List<String> aliases = new ArrayList<>();
if (bukkitCmd == null) {
desc = "";
} else {
desc = bukkitCmd.description().toString();
aliases = bukkitCmd.getAliases();
}
registrar.register(
plugin.getPluginMeta(), (LiteralCommandNode<CommandSourceStack>) command.node,
desc, aliases
);

});
});
}

/**
Expand All @@ -71,20 +49,17 @@ public void register(WrappedBukkitCommand command, LiteralCommandNode<?> node, P
Objects.requireNonNull(node, "node");
Objects.requireNonNull(permissionTest, "permissionTest");

Collection<String> aliases = getAliases(command);
if (!aliases.contains(node.getLiteral())) {
node = renameLiteralNode(node, command.getName());
}
for (String alias : aliases) {
WrappedBrigNode brigNode = new WrappedBrigNode(command,
node.getLiteral().equals(alias) ? node :
LiteralArgumentBuilder.literal(alias)
.redirect((LiteralCommandNode<Object>) node)
.build()
manager.registerEventHandler(LifecycleEvents.COMMANDS, event -> {
Commands registrar = event.registrar();

String desc = command == null ? null : command.description().toString();

registrar.register(
plugin.getPluginMeta(), (LiteralCommandNode<CommandSourceStack>) node,
desc, getAliases(command)
);

this.commands.add(brigNode);
}
});
}

/**
Expand All @@ -99,7 +74,7 @@ public void register(WrappedBukkitCommand command, LiteralCommandNode<?> node, P
*/
@Override
public void register(LiteralCommandNode<?> node) {
commands.add(new WrappedBrigNode(null, node));
register(null, node);
}

@Override
Expand All @@ -110,7 +85,4 @@ public CommandSender wrapNMSCommandSource(Object nmsCmdSource) {
throw new UnsupportedOperationException();
}

record WrappedBrigNode(WrappedBukkitCommand command, LiteralCommandNode<?> node) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ public void position(int position) {
@Override
public @NotNull CommandDispatch<S> contextMatch(Context<S> context) {
if (commandTree != null) {
return commandTree.contextMatch(context.arguments());
var copy = context.arguments().copy();
copy.removeIf((arg) -> arg.isEmpty() || arg.isBlank());
return commandTree.contextMatch(copy);
} else {
throw new IllegalCallerException("Cannot match a sub command in a root's execution !");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ static <S extends Source> CommandDispatch<S> empty() {
return of(Result.UNKNOWN);
}

static <S extends Source> CommandDispatch<S> incomplete() {
return of(Result.INCOMPLETE);
}

public void append(ParameterNode<S, ?> node) {
if (node == null) return;
if (parameters.contains(node.data)) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ private CompletableFuture<Collection<String>> addChildResults(
public @NotNull CommandDispatch<S> contextMatch(
ArgumentQueue input
) {
if (input.isEmpty())
return CommandDispatch.incomplete();

int depth = 0;

Expand Down

0 comments on commit b3ffdef

Please sign in to comment.