Skip to content

Commit

Permalink
Merge pull request #59 from TheNextLvl-net/pattern-matching
Browse files Browse the repository at this point in the history
Refactor command-finding methods to use `Pattern` directly
  • Loading branch information
NonSwag authored Dec 13, 2024
2 parents 0ee444f + 76f6ef2 commit 6774850
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
46 changes: 41 additions & 5 deletions api/src/main/java/net/thenextlvl/commander/CommandFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<String> 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<String> findCommands(String input);
default Set<String> findCommands(Stream<String> commands, Pattern pattern) {
return commands.filter(command ->
pattern.matcher(command).matches()
).collect(Collectors.toSet());
}

/**
* This method finds commands based on a given input.
Expand All @@ -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<String> findCommands(Stream<String> commands, String input);
default Set<String> findCommands(Stream<String> 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<String> findCommands(String input) {
try {
return findCommands(Pattern.compile(input));
} catch (PatternSyntaxException e) {
return findCommands(Pattern.compile(Pattern.quote(input)));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> findCommands(String input) {
@Override
public Set<String> 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<String> findCommands(Stream<String> commands, String input) {
var pattern = Pattern.compile(input.replace("*", ".*"));
return commands.filter(command ->
pattern.matcher(command).matches()
).collect(Collectors.toSet());
}), pattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,14 @@

import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@NullMarked
@RequiredArgsConstructor
public class ProxyCommandFinder implements CommandFinder {
private final CommanderPlugin plugin;

@Override
public Set<String> findCommands(String input) {
return findCommands(plugin.server().getCommandManager().getAliases().stream(), input);
}

@Override
public Set<String> findCommands(Stream<String> commands, String input) {
var pattern = Pattern.compile(input.replace("*", ".*"));
return commands.filter(command ->
pattern.matcher(command).matches()
).collect(Collectors.toSet());
public Set<String> findCommands(Pattern pattern) {
return findCommands(plugin.server().getCommandManager().getAliases().stream(), pattern);
}
}

0 comments on commit 6774850

Please sign in to comment.