Cleanup display package and add docs.

This commit is contained in:
benwoo1110 2020-12-31 11:39:10 +08:00
parent b3517ad9ce
commit 3b9f1849d7
12 changed files with 196 additions and 64 deletions

View File

@ -9,30 +9,49 @@ package com.onarandombox.MultiverseCore.commandTools.display;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
/**
* Some helper class to alternate between 2 colours.
*/
public class ColourAlternator { public class ColourAlternator {
private boolean switcher; private boolean switcher;
private final ChatColor colorThis; private final ChatColor thisColour;
private final ChatColor colorThat; private final ChatColor thatColour;
public ColourAlternator(ChatColor colorThis, ChatColor colorThat) { public ColourAlternator(ChatColor colorThis, ChatColor colorThat) {
this.colorThis = colorThis; this.thisColour = colorThis;
this.colorThat = colorThat; this.thatColour = colorThat;
} }
/**
* Gives you {@link ColourAlternator#thisColour} or {@link ColourAlternator#thatColour}.
*
* @return Opposite of the previous colour.
*/
public ChatColor get() { public ChatColor get() {
return (switcher ^= true) ? colorThis : colorThat; return (switcher ^= true) ? thisColour : thatColour;
} }
/**
* Set back to be {@link ColourAlternator#thisColour} when {@link ColourAlternator#get()} is called.
*/
public void reset() { public void reset() {
switcher = false; switcher = false;
} }
public ChatColor getColorThis() { /**
return colorThis; *
* @return {@link ColourAlternator#thisColour}.
*/
public ChatColor getThis() {
return thisColour;
} }
public ChatColor getColorThat() { /**
return colorThat; *
* @return {@link ColourAlternator#thatColour}.
*/
public ChatColor getThat() {
return thatColour;
} }
} }

View File

@ -1,5 +1,10 @@
package com.onarandombox.MultiverseCore.commandTools.display; package com.onarandombox.MultiverseCore.commandTools.display;
/**
* Generate content for displaying with {@link ContentDisplay}.
*
* @param <T> Type of content to create.
*/
@FunctionalInterface @FunctionalInterface
public interface ContentCreator<T> { public interface ContentCreator<T> {
T generateContent(); T generateContent();

View File

@ -5,6 +5,11 @@ import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
/**
* Displays various types of content to sender.
*
* @param <T> Type of content to display.
*/
public abstract class ContentDisplay<T> { public abstract class ContentDisplay<T> {
protected final Plugin plugin; protected final Plugin plugin;
@ -28,15 +33,26 @@ public abstract class ContentDisplay<T> {
this.colours = colours; this.colours = colours;
} }
/**
* Display the content to the {@link ContentDisplay#sender}.
*/
public void showContent() { public void showContent() {
getShowPageRunnable().runTask(this.plugin); getShowRunnable().runTask(this.plugin);
} }
/**
* Display the content to the {@link ContentDisplay#sender} with a asynchronous task.
*/
public void showContentAsync() { public void showContentAsync() {
getShowPageRunnable().runTaskAsynchronously(this.plugin); getShowRunnable().runTaskAsynchronously(this.plugin);
} }
public abstract ShowRunnable<T> getShowPageRunnable(); /**
* Runnable used to format and display contents to
*
* @return {@link ShowRunnable}
*/
public abstract ShowRunnable<? extends ContentDisplay<T>, T> getShowRunnable();
public CommandSender getSender() { public CommandSender getSender() {
return sender; return sender;

View File

@ -8,6 +8,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
/**
* Filter content and text based on regex.
*/
public class ContentFilter { public class ContentFilter {
private final String filterString; private final String filterString;
private Pattern filterPattern; private Pattern filterPattern;
@ -32,6 +35,9 @@ public class ContentFilter {
parseFilter(); parseFilter();
} }
/**
* Compile regex pattern based on {@link ContentFilter#filterString}.
*/
private void parseFilter() { private void parseFilter() {
if (filterString == null) { if (filterString == null) {
return; return;
@ -41,10 +47,12 @@ public class ContentFilter {
parseCustomFilter(); parseCustomFilter();
return; return;
} }
String cleanedFilter = REGEX_SPECIAL_CHARS.matcher(filterString.toLowerCase()).replaceAll("\\\\$0"); parseContainsFilter();
this.filterPattern = Pattern.compile("(?i).*" + cleanedFilter + ".*");
} }
/**
* When prefixed with 'r=', use {@link ContentFilter#filterString} as the full regex pattern.
*/
private void parseCustomFilter() { private void parseCustomFilter() {
try { try {
this.filterPattern = Pattern.compile(filterString.substring(2)); this.filterPattern = Pattern.compile(filterString.substring(2));
@ -55,14 +63,25 @@ public class ContentFilter {
} }
} }
public boolean checkMatch(@Nullable String text) { /**
if (text == null) { * Set pattern that matches any text that contains {@link ContentFilter#filterString}.
return false; */
private void parseContainsFilter() {
String cleanedFilter = REGEX_SPECIAL_CHARS.matcher(filterString.toLowerCase()).replaceAll("\\\\$0");
this.filterPattern = Pattern.compile("(?i).*" + cleanedFilter + ".*");
} }
/**
* Do regex matching.
*
* @param text String to check regex on.
* @return True of matches regex pattern, false otherwise.
*/
public boolean checkMatch(@Nullable String text) {
if (!hasFilter()) { if (!hasFilter()) {
return true; return true;
} }
if (!hasValidPattern()) { if (text == null || !hasValidPattern()) {
return false; return false;
} }
text = ChatColor.stripColor(text); text = ChatColor.stripColor(text);
@ -93,12 +112,16 @@ public class ContentFilter {
return exactMatch; return exactMatch;
} }
/**
* Nicely format the filter string to be used for showing the sender.
*
* @return formatted filter string.
*/
@NotNull @NotNull
public String getFormattedString() { public String getFormattedString() {
return String.format("%sFilter: '%s'%s", ChatColor.ITALIC, filterString, ChatColor.RESET); return String.format("%sFilter: '%s'%s", ChatColor.ITALIC, filterString, ChatColor.RESET);
} }
@Override @Override
public String toString() { public String toString() {
return "ContentFilter{" + return "ContentFilter{" +

View File

@ -2,30 +2,70 @@ package com.onarandombox.MultiverseCore.commandTools.display;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
public abstract class ShowRunnable<T> extends BukkitRunnable { /**
*
* @param <D> {@link ContentDisplay} type that is targted to.
* @param <T> Type of content its displaying.
*/
public abstract class ShowRunnable<D extends ContentDisplay<T>, T> extends BukkitRunnable {
protected final T contents; protected final D display;
protected T contents;
protected ShowRunnable(ContentDisplay<T> display) { protected ShowRunnable(D display) {
this.contents = display.getCreator().generateContent(); this.display = display;
} }
/**
* Run the showing of {@link ContentDisplay}.
*/
@Override @Override
public void run() { public void run() {
this.contents = this.display.getCreator().generateContent();
calculateContent(); calculateContent();
if (!validateContent()) {
return;
}
if (!hasContentToShow()) {
this.display.getSender().sendMessage("No matching content to display.");
return;
}
display(); display();
} }
/**
* Show header and contents to sender.
*/
public void display() { public void display() {
showHeader(); showHeader();
showContent(); showContent();
} }
/**
* Generate the content to show based on filter, pages or other factors depending on implementation.
*/
public abstract void calculateContent(); public abstract void calculateContent();
/**
* Check if there is anything to show after {@link ShowRunnable#calculateContent()}.
*
* @return True if content is present, false otherwise.
*/
public abstract boolean hasContentToShow();
/**
*
* @return True if valid, false otherwise.
*/
public abstract boolean validateContent(); public abstract boolean validateContent();
/**
* Displays header to the sender.
*/
public abstract void showHeader(); public abstract void showHeader();
/**
* Displays content to the sender.
*/
public abstract void showContent(); public abstract void showContent();
} }

View File

@ -4,7 +4,6 @@ import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator;
import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator; import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator;
import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay; import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay;
import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter;
import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -12,6 +11,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.Map; import java.util.Map;
/**
* Used to display config/property values pair, each separated with a comma.
*/
public class KeyValueDisplay extends ContentDisplay<Map<String, Object>> { public class KeyValueDisplay extends ContentDisplay<Map<String, Object>> {
private final String operator; private final String operator;
@ -28,8 +30,11 @@ public class KeyValueDisplay extends ContentDisplay<Map<String, Object>> {
this.operator = operator; this.operator = operator;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public ShowRunnable<Map<String, Object>> getShowPageRunnable() { public ShowKeyValue getShowRunnable() {
return new ShowKeyValue(this); return new ShowKeyValue(this);
} }

View File

@ -1,20 +1,17 @@
package com.onarandombox.MultiverseCore.commandTools.display.kvpair; package com.onarandombox.MultiverseCore.commandTools.display.kvpair;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter;
import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable; import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import java.util.Map; import java.util.Map;
public class ShowKeyValue extends ShowRunnable<Map<String, Object>> { public class ShowKeyValue extends ShowRunnable<KeyValueDisplay, Map<String, Object>> {
private final StringBuilder contentBuilder; private final StringBuilder contentBuilder;
private final KeyValueDisplay display;
public ShowKeyValue(KeyValueDisplay display) { public ShowKeyValue(KeyValueDisplay display) {
super(display); super(display);
this.display = display;
this.contentBuilder = new StringBuilder(); this.contentBuilder = new StringBuilder();
} }
@ -32,19 +29,24 @@ public class ShowKeyValue extends ShowRunnable<Map<String, Object>> {
else { else {
contentBuilder.append(", "); contentBuilder.append(", ");
} }
contentBuilder.append(this.display.getColours().getColorThis()) contentBuilder.append(this.display.getColours().getThis())
.append(entry.getKey()) .append(entry.getKey())
.append(ChatColor.WHITE) .append(ChatColor.WHITE)
.append(this.display.getOperator()) .append(this.display.getOperator())
.append(this.display.getColours().getColorThat()) .append(this.display.getColours().getThat())
.append(entry.getValue()) .append(entry.getValue())
.append(ChatColor.WHITE); .append(ChatColor.WHITE);
} }
} }
@Override
public boolean hasContentToShow() {
return contentBuilder.length() == 0;
}
@Override @Override
public boolean validateContent() { public boolean validateContent() {
return false; return true;
} }
@Override @Override
@ -52,7 +54,6 @@ public class ShowKeyValue extends ShowRunnable<Map<String, Object>> {
if (this.display.getHeader() == null) { if (this.display.getHeader() == null) {
return; return;
} }
this.display.getSender().sendMessage(this.display.getHeader()); this.display.getSender().sendMessage(this.display.getHeader());
} }

View File

@ -7,12 +7,10 @@
package com.onarandombox.MultiverseCore.commandTools.display.page; package com.onarandombox.MultiverseCore.commandTools.display.page;
import com.onarandombox.MultiverseCore.commandTools.PageFilter;
import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator; import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator;
import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator; import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator;
import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay; import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay;
import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter;
import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -21,6 +19,9 @@ import org.jetbrains.annotations.Nullable;
import java.util.List; import java.util.List;
/**
* Used to display list of multiple lines with paging and filter.
*/
public class PageDisplay extends ContentDisplay<List<String>> { public class PageDisplay extends ContentDisplay<List<String>> {
private final int pageToShow; private final int pageToShow;
@ -45,8 +46,11 @@ public class PageDisplay extends ContentDisplay<List<String>> {
this.contentLinesPerPage = contentLinesPerPage; this.contentLinesPerPage = contentLinesPerPage;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public ShowRunnable<List<String>> getShowPageRunnable() { public ShowPage getShowRunnable() {
return (this.sender instanceof ConsoleCommandSender) return (this.sender instanceof ConsoleCommandSender)
? new ShowAllPage(this) ? new ShowAllPage(this)
: new ShowSelectedPage(this); : new ShowSelectedPage(this);

View File

@ -1,8 +1,6 @@
package com.onarandombox.MultiverseCore.commandTools.display.page; package com.onarandombox.MultiverseCore.commandTools.display.page;
import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter;
import com.onarandombox.MultiverseCore.commandTools.display.page.PageDisplay;
import com.onarandombox.MultiverseCore.commandTools.display.page.ShowPage;
public class ShowAllPage extends ShowPage { public class ShowAllPage extends ShowPage {
@ -10,6 +8,9 @@ public class ShowAllPage extends ShowPage {
super(display); super(display);
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void calculateContent() { public void calculateContent() {
int index = -1; int index = -1;
@ -22,11 +23,17 @@ public class ShowAllPage extends ShowPage {
} }
} }
/**
* {@inheritDoc}
*/
@Override @Override
public boolean validateContent() { public boolean validateContent() {
return true; return true;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void showHeader() { public void showHeader() {
if (this.display.getHeader() == null) { if (this.display.getHeader() == null) {

View File

@ -6,14 +6,12 @@ import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public abstract class ShowPage extends ShowRunnable<List<String>> { public abstract class ShowPage extends ShowRunnable<PageDisplay, List<String>> {
protected PageDisplay display;
protected final List<Integer> contentToShowIndex; protected final List<Integer> contentToShowIndex;
public ShowPage(PageDisplay display) { public ShowPage(PageDisplay display) {
super(display); super(display);
this.display = display;
this.contentToShowIndex = new ArrayList<>(); this.contentToShowIndex = new ArrayList<>();
if (this.display.getColours() != null) { if (this.display.getColours() != null) {
@ -21,19 +19,17 @@ public abstract class ShowPage extends ShowRunnable<List<String>> {
} }
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void run() { public boolean hasContentToShow() {
calculateContent(); return !contentToShowIndex.isEmpty();
if (!validateContent()) {
return;
}
if (contentToShowIndex.isEmpty()) {
this.display.getSender().sendMessage("No matching content to display.");
return;
}
display();
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void showContent() { public void showContent() {
ColourAlternator colours = this.display.getColours(); ColourAlternator colours = this.display.getColours();

View File

@ -12,12 +12,18 @@ public class ShowSelectedPage extends ShowPage {
super(display); super(display);
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void display() { public void display() {
super.display(); super.display();
doEndPadding(); doEndPadding();
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void calculateContent() { public void calculateContent() {
int lineCount = 0; int lineCount = 0;
@ -41,6 +47,9 @@ public class ShowSelectedPage extends ShowPage {
} }
} }
/**
* {@inheritDoc}
*/
@Override @Override
public boolean validateContent() { public boolean validateContent() {
return !pageOutOfRange(); return !pageOutOfRange();
@ -56,25 +65,32 @@ public class ShowSelectedPage extends ShowPage {
return false; return false;
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void showHeader() { public void showHeader() {
String theHeader; String header = getHeader();
if (this.display.getHeader() == null) {
theHeader = this.contents.get(contentToShowIndex.remove(0));
this.display.reduceContentLinesPerPage(1);
}
else {
theHeader = this.display.getHeader();
}
if (theHeader.contains(PageDisplay.PAGE_PLACEHOLDER)) { // Paging inline with header
this.display.getSender().sendMessage(theHeader.replace(PageDisplay.PAGE_PLACEHOLDER, parsePaging())); if (header.contains(PageDisplay.PAGE_PLACEHOLDER)) {
this.display.getSender().sendMessage(header.replace(PageDisplay.PAGE_PLACEHOLDER, parsePaging()));
return; return;
} }
this.display.getSender().sendMessage(theHeader);
this.display.getSender().sendMessage(header);
this.display.getSender().sendMessage(parsePaging()); this.display.getSender().sendMessage(parsePaging());
} }
private String getHeader() {
if (this.display.getHeader() != null) {
return this.display.getHeader();
}
// Let first content line be the header.
this.display.reduceContentLinesPerPage(1);
return this.contents.get(contentToShowIndex.remove(0));
}
private String parsePaging() { private String parsePaging() {
ContentFilter filter = this.display.getFilter(); ContentFilter filter = this.display.getFilter();
return (filter.hasFilter()) return (filter.hasFilter())

View File

@ -72,8 +72,8 @@ public class SubModulesCommand {
@NotNull ColourAlternator colours, @NotNull ColourAlternator colours,
@NotNull String downloadLink) { @NotNull String downloadLink) {
sender.sendMessage(colours.getColorThis() + pluginName + ChatColor.WHITE + sender.sendMessage(colours.getThis() + pluginName + ChatColor.WHITE +
" is not installed on this server. You can learn more and download it at:"); " is not installed on this server. You can learn more and download it at:");
sender.sendMessage(colours.getColorThat() + downloadLink); sender.sendMessage(colours.getThat() + downloadLink);
} }
} }