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