Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor command-finding methods to use Pattern directly #59

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading