Merge pull request #2445 from benwoo1110/fix-help

Fix issue where special chars cause PatternSyntaxException
This commit is contained in:
nicegamer7 2021-07-16 09:51:21 -04:00 committed by GitHub
commit 9ce2dfd100
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 14 deletions

View File

@ -40,24 +40,16 @@ public class HelpCommand extends PaginatedCoreCommand<Command> {
@Override @Override
protected List<Command> getFilteredItems(List<Command> availableItems, String filter) { protected List<Command> getFilteredItems(List<Command> availableItems, String filter) {
String expression = "(?i).*" + cleanFilter(filter) + ".*";
List<Command> filtered = new ArrayList<Command>(); List<Command> filtered = new ArrayList<Command>();
for (Command c : availableItems) { 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); 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; return filtered;

View File

@ -13,12 +13,14 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
/** /**
* A generic paginated command. * A generic paginated command.
* @param <T> The type of items on the page. * @param <T> The type of items on the page.
*/ */
public abstract class PaginatedCommand<T> extends Command { public abstract class PaginatedCommand<T> extends Command {
private final Pattern REGEX_SPECIAL_CHARS = Pattern.compile("[.+*?\\[^\\]$(){}=!<>|:-\\\\]");
private static final int DEFAULT_ITEMS_PER_PAGE = 9; private static final int DEFAULT_ITEMS_PER_PAGE = 9;
/** /**
* The number of items per page. * The number of items per page.
@ -40,12 +42,23 @@ public abstract class PaginatedCommand<T> extends Command {
/** /**
* Gets filtered items. * Gets filtered items.
*
* @param availableItems All available items. * @param availableItems All available items.
* @param filter The filter-{@link String}. * @param filter The filter-{@link String}.
* @return A list of items that match the filter. * @return A list of items that match the filter.
*/ */
protected abstract List<T> getFilteredItems(List<T> availableItems, String 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. * Constructs a single string from a list of strings.
* *