mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-22 02:25:41 +01:00
Merge pull request #2445 from benwoo1110/fix-help
Fix issue where special chars cause PatternSyntaxException
This commit is contained in:
commit
9ce2dfd100
@ -40,24 +40,16 @@ public class HelpCommand extends PaginatedCoreCommand<Command> {
|
||||
|
||||
@Override
|
||||
protected List<Command> getFilteredItems(List<Command> availableItems, String filter) {
|
||||
String expression = "(?i).*" + cleanFilter(filter) + ".*";
|
||||
List<Command> filtered = new ArrayList<Command>();
|
||||
|
||||
for (Command c : availableItems) {
|
||||
if (stitchThisString(c.getKeyStrings()).matches("(?i).*" + filter + ".*")) {
|
||||
if (stitchThisString(c.getKeyStrings()).matches(expression)
|
||||
|| c.getCommandName().matches(expression)
|
||||
|| c.getCommandDesc().matches(expression)
|
||||
|| c.getCommandUsage().matches(expression)
|
||||
|| c.getCommandExamples().stream().anyMatch(eg -> eg.matches(expression))) {
|
||||
filtered.add(c);
|
||||
} else if (c.getCommandName().matches("(?i).*" + filter + ".*")) {
|
||||
filtered.add(c);
|
||||
} else if (c.getCommandDesc().matches("(?i).*" + filter + ".*")) {
|
||||
filtered.add(c);
|
||||
} else if (c.getCommandUsage().matches("(?i).*" + filter + ".*")) {
|
||||
filtered.add(c);
|
||||
} else {
|
||||
for (String example : c.getCommandExamples()) {
|
||||
if (example.matches("(?i).*" + filter + ".*")) {
|
||||
filtered.add(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
|
@ -13,12 +13,14 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* A generic paginated command.
|
||||
* @param <T> The type of items on the page.
|
||||
*/
|
||||
public abstract class PaginatedCommand<T> extends Command {
|
||||
private final Pattern REGEX_SPECIAL_CHARS = Pattern.compile("[.+*?\\[^\\]$(){}=!<>|:-\\\\]");
|
||||
private static final int DEFAULT_ITEMS_PER_PAGE = 9;
|
||||
/**
|
||||
* The number of items per page.
|
||||
@ -40,12 +42,23 @@ public abstract class PaginatedCommand<T> extends Command {
|
||||
|
||||
/**
|
||||
* Gets filtered items.
|
||||
*
|
||||
* @param availableItems All available items.
|
||||
* @param filter The filter-{@link String}.
|
||||
* @return A list of items that match the filter.
|
||||
*/
|
||||
protected abstract List<T> getFilteredItems(List<T> availableItems, String filter);
|
||||
|
||||
/**
|
||||
* Escape regex special characters from filter
|
||||
*
|
||||
* @param filter The filter-{@link String}.
|
||||
* @return String with regex characters escaped
|
||||
*/
|
||||
protected String cleanFilter(String filter) {
|
||||
return REGEX_SPECIAL_CHARS.matcher(filter).replaceAll("\\\\$0");
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a single string from a list of strings.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user