diff --git a/api/src/main/java/net/thenextlvl/commander/CommandFinder.java b/api/src/main/java/net/thenextlvl/commander/CommandFinder.java index 8d2aafe..b7260e3 100644 --- a/api/src/main/java/net/thenextlvl/commander/CommandFinder.java +++ b/api/src/main/java/net/thenextlvl/commander/CommandFinder.java @@ -3,6 +3,9 @@ import org.jspecify.annotations.NullMarked; import java.util.Set; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; +import java.util.stream.Collectors; import java.util.stream.Stream; /** @@ -11,12 +14,25 @@ @NullMarked public interface CommandFinder { /** - * Finds commands based on the given input. + * Finds and returns a set of commands that match the given pattern. * - * @param input The input used to search for commands. - * @return A set of strings representing the found commands. + * @param pattern The pattern used to search for commands. + * @return A set of strings representing the commands that match the pattern. + */ + Set findCommands(Pattern pattern); + + /** + * Filters and finds commands from the provided stream that match the given pattern. + * + * @param commands The stream of commands to be searched. + * @param pattern The pattern used to filter matching commands. + * @return A set of strings representing the commands that match the given pattern. */ - Set findCommands(String input); + default Set findCommands(Stream commands, Pattern pattern) { + return commands.filter(command -> + pattern.matcher(command).matches() + ).collect(Collectors.toSet()); + } /** * This method finds commands based on a given input. @@ -25,5 +41,25 @@ public interface CommandFinder { * @param input The input used to search for commands. * @return A set of strings representing the found commands. */ - Set findCommands(Stream commands, String input); + default Set findCommands(Stream commands, String input) { + try { + return findCommands(commands, Pattern.compile(input)); + } catch (PatternSyntaxException e) { + return findCommands(commands, Pattern.compile(Pattern.quote(input))); + } + } + + /** + * Finds commands based on the given input. + * + * @param input The input used to search for commands. + * @return A set of strings representing the found commands. + */ + default Set findCommands(String input) { + try { + return findCommands(Pattern.compile(input)); + } catch (PatternSyntaxException e) { + return findCommands(Pattern.compile(Pattern.quote(input))); + } + } } diff --git a/paper/src/main/java/net/thenextlvl/commander/paper/implementation/PaperCommandFinder.java b/paper/src/main/java/net/thenextlvl/commander/paper/implementation/PaperCommandFinder.java index 4391109..ef56b93 100644 --- a/paper/src/main/java/net/thenextlvl/commander/paper/implementation/PaperCommandFinder.java +++ b/paper/src/main/java/net/thenextlvl/commander/paper/implementation/PaperCommandFinder.java @@ -7,26 +7,18 @@ import java.util.Set; import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; @NullMarked @RequiredArgsConstructor public class PaperCommandFinder implements CommandFinder { private final CommanderPlugin plugin; - public Set findCommands(String input) { + @Override + public Set findCommands(Pattern pattern) { return findCommands(plugin.getServer().getCommandMap().getKnownCommands().entrySet() .stream().mapMulti((entry, consumer) -> { consumer.accept(entry.getKey()); entry.getValue().getAliases().forEach(consumer); - }), input); - } - - public Set findCommands(Stream commands, String input) { - var pattern = Pattern.compile(input.replace("*", ".*")); - return commands.filter(command -> - pattern.matcher(command).matches() - ).collect(Collectors.toSet()); + }), pattern); } } diff --git a/velocity/src/main/java/net/thenextlvl/commander/velocity/implementation/ProxyCommandFinder.java b/velocity/src/main/java/net/thenextlvl/commander/velocity/implementation/ProxyCommandFinder.java index fe637b5..bbdc9a7 100644 --- a/velocity/src/main/java/net/thenextlvl/commander/velocity/implementation/ProxyCommandFinder.java +++ b/velocity/src/main/java/net/thenextlvl/commander/velocity/implementation/ProxyCommandFinder.java @@ -7,8 +7,6 @@ import java.util.Set; import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.Stream; @NullMarked @RequiredArgsConstructor @@ -16,15 +14,7 @@ public class ProxyCommandFinder implements CommandFinder { private final CommanderPlugin plugin; @Override - public Set findCommands(String input) { - return findCommands(plugin.server().getCommandManager().getAliases().stream(), input); - } - - @Override - public Set findCommands(Stream commands, String input) { - var pattern = Pattern.compile(input.replace("*", ".*")); - return commands.filter(command -> - pattern.matcher(command).matches() - ).collect(Collectors.toSet()); + public Set findCommands(Pattern pattern) { + return findCommands(plugin.server().getCommandManager().getAliases().stream(), pattern); } }