chore: Refactor and fix some bugs with content display

This commit is contained in:
Ben Woo 2023-02-15 12:16:24 +08:00
parent 53f453b7ac
commit 09d8790804
7 changed files with 17 additions and 7 deletions

View File

@ -40,7 +40,7 @@ public class MVCommandContexts extends PaperCommandContexts {
private ContentFilter parseContentFilter(BukkitCommandExecutionContext context) { private ContentFilter parseContentFilter(BukkitCommandExecutionContext context) {
if (Strings.isNullOrEmpty(context.getFirstArg())) { if (Strings.isNullOrEmpty(context.getFirstArg())) {
return DefaultContentFilter.getInstance(); return DefaultContentFilter.get();
} }
String filterString = context.popFirstArg(); String filterString = context.popFirstArg();
return RegexContentFilter.fromString(filterString); return RegexContentFilter.fromString(filterString);

View File

@ -18,4 +18,11 @@ public interface ContentFilter {
* @return True if content should be filtered, false otherwise. * @return True if content should be filtered, false otherwise.
*/ */
boolean needToFilter(); boolean needToFilter();
/**
* Gets the string representation of this filter.
*
* @return The string representation of this filter.
*/
String toString();
} }

View File

@ -7,7 +7,7 @@ public class DefaultContentFilter implements ContentFilter {
public static DefaultContentFilter instance; public static DefaultContentFilter instance;
public static DefaultContentFilter getInstance() { public static DefaultContentFilter get() {
if (instance == null) { if (instance == null) {
instance = new DefaultContentFilter(); instance = new DefaultContentFilter();
} }

View File

@ -68,11 +68,11 @@ public class RegexContentFilter implements ContentFilter {
if (!hasValidRegex()) { if (!hasValidRegex()) {
return false; return false;
} }
String text = ChatColor.stripColor(String.valueOf(value)); String text = ChatColor.stripColor(String.valueOf(value)).toLowerCase();
try { try {
return regexPattern.matcher(text).find(); return regexPattern.matcher(text).find();
} catch (PatternSyntaxException ignored) { } catch (PatternSyntaxException ignored) {
Logging.fine("Error parsing regex '%s' for input '%s'", regexString, text); Logging.warning("Error parsing regex '%s' for input '%s'", regexString, text);
return false; return false;
} }
} }

View File

@ -19,7 +19,7 @@ import org.jetbrains.annotations.Nullable;
public abstract class BaseSendHandler<T extends BaseSendHandler<?>> implements SendHandler { public abstract class BaseSendHandler<T extends BaseSendHandler<?>> implements SendHandler {
protected String header = ""; protected String header = "";
protected ContentFilter filter = DefaultContentFilter.getInstance(); protected ContentFilter filter = DefaultContentFilter.get();
protected String noContentMessage = String.format("%sThere is no content to display.", ChatColor.RED); protected String noContentMessage = String.format("%sThere is no content to display.", ChatColor.RED);
/** /**

View File

@ -35,7 +35,7 @@ public class PagedSendHandler extends BaseSendHandler<PagedSendHandler> {
*/ */
@Override @Override
public void sendContent(@NotNull BukkitCommandIssuer issuer, @NotNull List<String> content) { public void sendContent(@NotNull BukkitCommandIssuer issuer, @NotNull List<String> content) {
if (!paginate || (issuer instanceof ConsoleCommandSender && !paginateInConsole)) { if (!paginate || (issuer.getIssuer() instanceof ConsoleCommandSender && !paginateInConsole)) {
sendNormal(issuer, content); sendNormal(issuer, content);
return; return;
} }

View File

@ -27,7 +27,7 @@ public class ListContentProvider<T> implements ContentProvider {
private final List<T> list; private final List<T> list;
private String format = "%s"; private String format = null;
public ListContentProvider(List<T> list) { public ListContentProvider(List<T> list) {
this.list = list; this.list = list;
@ -38,6 +38,9 @@ public class ListContentProvider<T> implements ContentProvider {
*/ */
@Override @Override
public Collection<String> parse(@NotNull BukkitCommandIssuer issuer) { public Collection<String> parse(@NotNull BukkitCommandIssuer issuer) {
if (format == null) {
return list.stream().map(Object::toString).collect(Collectors.toList());
}
return list.stream().map(element -> String.format(format, element)).collect(Collectors.toList()); return list.stream().map(element -> String.format(format, element)).collect(Collectors.toList());
} }