mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-12 18:31:41 +01:00
Improve redirection match
This commit is contained in:
parent
a8bb20fca6
commit
36d548ff35
@ -315,7 +315,7 @@ public final class CommandManager {
|
||||
nodes.add(rootNode);
|
||||
|
||||
Map<Command, Integer> commandIdentityMap = new IdentityHashMap<>();
|
||||
Map<Argument<?>, DeclareCommandsPacket.Node[]> argumentIdentityMap = new IdentityHashMap<>();
|
||||
Map<Argument<?>, Integer> argumentIdentityMap = new IdentityHashMap<>();
|
||||
|
||||
List<Pair<String, NodeMaker.Request>> nodeRequests = new ArrayList<>();
|
||||
|
||||
@ -338,25 +338,18 @@ public final class CommandManager {
|
||||
}
|
||||
|
||||
final ArgumentQueryResult queryResult = CommandParser.findEligibleArgument(commandQueryResult.command,
|
||||
commandQueryResult.args, input, true, argument -> true);
|
||||
commandQueryResult.args, input, false, true, syntax -> true, argument -> true);
|
||||
if (queryResult == null) {
|
||||
// Invalid argument, return command node (default to root)
|
||||
int commandNode = commandIdentityMap.getOrDefault(commandQueryResult.command, 0);
|
||||
final int commandNode = commandIdentityMap.getOrDefault(commandQueryResult.command, 0);
|
||||
request.retrieve(commandNode);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Retrieve argument node
|
||||
Argument<?> argument = queryResult.argument;
|
||||
DeclareCommandsPacket.Node[] argNodes = argumentIdentityMap.get(argument);
|
||||
for (DeclareCommandsPacket.Node argNode : argNodes) {
|
||||
final int node = nodes.indexOf(argNode);
|
||||
request.retrieve(node);
|
||||
break;
|
||||
}
|
||||
|
||||
// Unexpected issue, redirect to root
|
||||
//request.retrieve(0);
|
||||
final Argument<?> argument = queryResult.argument;
|
||||
final int argumentNode = argumentIdentityMap.getOrDefault(argument, 0);
|
||||
request.retrieve(argumentNode);
|
||||
}
|
||||
|
||||
// Pair<CommandName,EnabledTracking>
|
||||
@ -417,7 +410,7 @@ public final class CommandManager {
|
||||
List<DeclareCommandsPacket.Node> nodes,
|
||||
IntList rootChildren,
|
||||
Map<Command, Integer> commandIdentityMap,
|
||||
Map<Argument<?>, DeclareCommandsPacket.Node[]> argumentIdentityMap,
|
||||
Map<Argument<?>, Integer> argumentIdentityMap,
|
||||
List<Pair<String, NodeMaker.Request>> nodeRequests) {
|
||||
// Check if player should see this command
|
||||
final CommandCondition commandCondition = command.getCondition();
|
||||
@ -480,7 +473,7 @@ public final class CommandManager {
|
||||
@NotNull String name,
|
||||
@NotNull Collection<CommandSyntax> syntaxes,
|
||||
@NotNull IntList rootChildren,
|
||||
@NotNull Map<Argument<?>, DeclareCommandsPacket.Node[]> argumentIdentityMap,
|
||||
@NotNull Map<Argument<?>, Integer> argumentIdentityMap,
|
||||
@NotNull List<Pair<String, NodeMaker.Request>> nodeRequests) {
|
||||
|
||||
DeclareCommandsPacket.Node literalNode = createMainNode(name, syntaxes.isEmpty());
|
||||
@ -592,8 +585,15 @@ public final class CommandManager {
|
||||
syntaxesArguments.add(arguments);
|
||||
}
|
||||
|
||||
storedArgumentsNodes.forEach((argument, nodes1) ->
|
||||
argumentIdentityMap.put(argument, nodes1.get(nodes1.size() - 2)));
|
||||
storedArgumentsNodes.forEach((argument, argNodes) -> {
|
||||
int value = 0;
|
||||
for (DeclareCommandsPacket.Node[] n1 : argNodes) {
|
||||
for (DeclareCommandsPacket.Node n2 : n1) {
|
||||
value = nodes.indexOf(n2);
|
||||
}
|
||||
}
|
||||
argumentIdentityMap.put(argument, value);
|
||||
});
|
||||
|
||||
literalNode.children = ArrayUtils.toArray(cmdChildren);
|
||||
return literalNode;
|
||||
|
@ -1,5 +1,6 @@
|
||||
package net.minestom.server.command.builder.arguments;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.command.builder.CommandDispatcher;
|
||||
import net.minestom.server.command.builder.CommandResult;
|
||||
@ -68,6 +69,7 @@ public class ArgumentCommand extends Argument<CommandResult> {
|
||||
return shortcut;
|
||||
}
|
||||
|
||||
@Beta
|
||||
public ArgumentCommand setShortcut(@NotNull String shortcut) {
|
||||
this.shortcut = shortcut;
|
||||
return this;
|
||||
|
@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class CommandParser {
|
||||
|
||||
@ -165,13 +165,15 @@ public class CommandParser {
|
||||
|
||||
@Nullable
|
||||
public static ArgumentQueryResult findEligibleArgument(@NotNull Command command, String[] args, String commandString,
|
||||
boolean trailingSpace, Function<Argument<?>, Boolean> eligibilityFunction) {
|
||||
boolean trailingSpace, boolean forceCorrect,
|
||||
Predicate<CommandSyntax> syntaxPredicate,
|
||||
Predicate<Argument<?>> argumentPredicate) {
|
||||
final Collection<CommandSyntax> syntaxes = command.getSyntaxes();
|
||||
|
||||
Int2ObjectRBTreeMap<ArgumentQueryResult> suggestions = new Int2ObjectRBTreeMap<>(Collections.reverseOrder());
|
||||
|
||||
for (CommandSyntax syntax : syntaxes) {
|
||||
if (!syntax.hasSuggestion()) {
|
||||
if (!syntaxPredicate.test(syntax)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -202,7 +204,8 @@ public class CommandParser {
|
||||
}
|
||||
|
||||
// Save result
|
||||
if (eligibilityFunction.apply(argument)) {
|
||||
if ((!forceCorrect || argumentResult.correct) &&
|
||||
argumentPredicate.test(argument)) {
|
||||
ArgumentQueryResult queryResult = new ArgumentQueryResult();
|
||||
queryResult.syntax = syntax;
|
||||
queryResult.argument = argument;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package net.minestom.server.listener;
|
||||
|
||||
import net.minestom.server.command.CommandManager;
|
||||
import net.minestom.server.command.builder.CommandSyntax;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.command.builder.parser.ArgumentQueryResult;
|
||||
import net.minestom.server.command.builder.parser.CommandParser;
|
||||
@ -32,7 +33,8 @@ public class TabCompleteListener {
|
||||
}
|
||||
|
||||
final ArgumentQueryResult queryResult = CommandParser.findEligibleArgument(commandQueryResult.command,
|
||||
commandQueryResult.args, commandString, text.endsWith(StringUtils.SPACE), Argument::hasSuggestion);
|
||||
commandQueryResult.args, commandString, text.endsWith(StringUtils.SPACE), false,
|
||||
CommandSyntax::hasSuggestion, Argument::hasSuggestion);
|
||||
if (queryResult == null) {
|
||||
// Suggestible argument not found
|
||||
return;
|
||||
|
@ -28,7 +28,7 @@ public class TestCommand extends Command {
|
||||
|
||||
addSyntax((sender, context) -> {
|
||||
System.out.println("cmd syntax");
|
||||
}, Literal("debug"), Command("cmd").setShortcut("testcmd test a"));
|
||||
}, Literal("debug"), Command("cmd").setShortcut("testcmd test"));
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user