mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-26 04:25:37 +01:00
Cleanup display package and add docs.
This commit is contained in:
parent
b3517ad9ce
commit
3b9f1849d7
@ -9,30 +9,49 @@ package com.onarandombox.MultiverseCore.commandTools.display;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
/**
|
||||
* Some helper class to alternate between 2 colours.
|
||||
*/
|
||||
public class ColourAlternator {
|
||||
|
||||
private boolean switcher;
|
||||
private final ChatColor colorThis;
|
||||
private final ChatColor colorThat;
|
||||
private final ChatColor thisColour;
|
||||
private final ChatColor thatColour;
|
||||
|
||||
public ColourAlternator(ChatColor colorThis, ChatColor colorThat) {
|
||||
this.colorThis = colorThis;
|
||||
this.colorThat = colorThat;
|
||||
this.thisColour = colorThis;
|
||||
this.thatColour = colorThat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives you {@link ColourAlternator#thisColour} or {@link ColourAlternator#thatColour}.
|
||||
*
|
||||
* @return Opposite of the previous colour.
|
||||
*/
|
||||
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() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.onarandombox.MultiverseCore.commandTools.display;
|
||||
|
||||
/**
|
||||
* Generate content for displaying with {@link ContentDisplay}.
|
||||
*
|
||||
* @param <T> Type of content to create.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ContentCreator<T> {
|
||||
T generateContent();
|
||||
|
@ -5,6 +5,11 @@ import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Displays various types of content to sender.
|
||||
*
|
||||
* @param <T> Type of content to display.
|
||||
*/
|
||||
public abstract class ContentDisplay<T> {
|
||||
|
||||
protected final Plugin plugin;
|
||||
@ -28,15 +33,26 @@ public abstract class ContentDisplay<T> {
|
||||
this.colours = colours;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the content to the {@link ContentDisplay#sender}.
|
||||
*/
|
||||
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() {
|
||||
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() {
|
||||
return sender;
|
||||
|
@ -8,6 +8,9 @@ import org.jetbrains.annotations.Nullable;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
/**
|
||||
* Filter content and text based on regex.
|
||||
*/
|
||||
public class ContentFilter {
|
||||
private final String filterString;
|
||||
private Pattern filterPattern;
|
||||
@ -32,6 +35,9 @@ public class ContentFilter {
|
||||
parseFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile regex pattern based on {@link ContentFilter#filterString}.
|
||||
*/
|
||||
private void parseFilter() {
|
||||
if (filterString == null) {
|
||||
return;
|
||||
@ -41,10 +47,12 @@ public class ContentFilter {
|
||||
parseCustomFilter();
|
||||
return;
|
||||
}
|
||||
String cleanedFilter = REGEX_SPECIAL_CHARS.matcher(filterString.toLowerCase()).replaceAll("\\\\$0");
|
||||
this.filterPattern = Pattern.compile("(?i).*" + cleanedFilter + ".*");
|
||||
parseContainsFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* When prefixed with 'r=', use {@link ContentFilter#filterString} as the full regex pattern.
|
||||
*/
|
||||
private void parseCustomFilter() {
|
||||
try {
|
||||
this.filterPattern = Pattern.compile(filterString.substring(2));
|
||||
@ -55,14 +63,25 @@ public class ContentFilter {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean checkMatch(@Nullable String text) {
|
||||
if (text == null) {
|
||||
return false;
|
||||
/**
|
||||
* Set pattern that matches any text that contains {@link ContentFilter#filterString}.
|
||||
*/
|
||||
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()) {
|
||||
return true;
|
||||
}
|
||||
if (!hasValidPattern()) {
|
||||
if (text == null || !hasValidPattern()) {
|
||||
return false;
|
||||
}
|
||||
text = ChatColor.stripColor(text);
|
||||
@ -93,12 +112,16 @@ public class ContentFilter {
|
||||
return exactMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Nicely format the filter string to be used for showing the sender.
|
||||
*
|
||||
* @return formatted filter string.
|
||||
*/
|
||||
@NotNull
|
||||
public String getFormattedString() {
|
||||
return String.format("%sFilter: '%s'%s", ChatColor.ITALIC, filterString, ChatColor.RESET);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ContentFilter{" +
|
||||
|
@ -2,30 +2,70 @@ package com.onarandombox.MultiverseCore.commandTools.display;
|
||||
|
||||
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) {
|
||||
this.contents = display.getCreator().generateContent();
|
||||
protected ShowRunnable(D display) {
|
||||
this.display = display;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the showing of {@link ContentDisplay}.
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
this.contents = this.display.getCreator().generateContent();
|
||||
calculateContent();
|
||||
if (!validateContent()) {
|
||||
return;
|
||||
}
|
||||
if (!hasContentToShow()) {
|
||||
this.display.getSender().sendMessage("No matching content to display.");
|
||||
return;
|
||||
}
|
||||
display();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show header and contents to sender.
|
||||
*/
|
||||
public void display() {
|
||||
showHeader();
|
||||
showContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the content to show based on filter, pages or other factors depending on implementation.
|
||||
*/
|
||||
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();
|
||||
|
||||
/**
|
||||
* Displays header to the sender.
|
||||
*/
|
||||
public abstract void showHeader();
|
||||
|
||||
/**
|
||||
* Displays content to the sender.
|
||||
*/
|
||||
public abstract void showContent();
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator;
|
||||
import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator;
|
||||
import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter;
|
||||
import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@ -12,6 +11,9 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Used to display config/property values pair, each separated with a comma.
|
||||
*/
|
||||
public class KeyValueDisplay extends ContentDisplay<Map<String, Object>> {
|
||||
|
||||
private final String operator;
|
||||
@ -28,8 +30,11 @@ public class KeyValueDisplay extends ContentDisplay<Map<String, Object>> {
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public ShowRunnable<Map<String, Object>> getShowPageRunnable() {
|
||||
public ShowKeyValue getShowRunnable() {
|
||||
return new ShowKeyValue(this);
|
||||
}
|
||||
|
||||
|
@ -1,20 +1,17 @@
|
||||
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.ShowRunnable;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
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 KeyValueDisplay display;
|
||||
|
||||
public ShowKeyValue(KeyValueDisplay display) {
|
||||
super(display);
|
||||
this.display = display;
|
||||
this.contentBuilder = new StringBuilder();
|
||||
}
|
||||
|
||||
@ -32,19 +29,24 @@ public class ShowKeyValue extends ShowRunnable<Map<String, Object>> {
|
||||
else {
|
||||
contentBuilder.append(", ");
|
||||
}
|
||||
contentBuilder.append(this.display.getColours().getColorThis())
|
||||
contentBuilder.append(this.display.getColours().getThis())
|
||||
.append(entry.getKey())
|
||||
.append(ChatColor.WHITE)
|
||||
.append(this.display.getOperator())
|
||||
.append(this.display.getColours().getColorThat())
|
||||
.append(this.display.getColours().getThat())
|
||||
.append(entry.getValue())
|
||||
.append(ChatColor.WHITE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasContentToShow() {
|
||||
return contentBuilder.length() == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validateContent() {
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -52,7 +54,6 @@ public class ShowKeyValue extends ShowRunnable<Map<String, Object>> {
|
||||
if (this.display.getHeader() == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.display.getSender().sendMessage(this.display.getHeader());
|
||||
}
|
||||
|
||||
|
@ -7,12 +7,10 @@
|
||||
|
||||
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.ContentCreator;
|
||||
import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter;
|
||||
import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
@ -21,6 +19,9 @@ import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Used to display list of multiple lines with paging and filter.
|
||||
*/
|
||||
public class PageDisplay extends ContentDisplay<List<String>> {
|
||||
|
||||
private final int pageToShow;
|
||||
@ -45,8 +46,11 @@ public class PageDisplay extends ContentDisplay<List<String>> {
|
||||
this.contentLinesPerPage = contentLinesPerPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public ShowRunnable<List<String>> getShowPageRunnable() {
|
||||
public ShowPage getShowRunnable() {
|
||||
return (this.sender instanceof ConsoleCommandSender)
|
||||
? new ShowAllPage(this)
|
||||
: new ShowSelectedPage(this);
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.onarandombox.MultiverseCore.commandTools.display.page;
|
||||
|
||||
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 {
|
||||
|
||||
@ -10,6 +8,9 @@ public class ShowAllPage extends ShowPage {
|
||||
super(display);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void calculateContent() {
|
||||
int index = -1;
|
||||
@ -22,11 +23,17 @@ public class ShowAllPage extends ShowPage {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean validateContent() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void showHeader() {
|
||||
if (this.display.getHeader() == null) {
|
||||
|
@ -6,14 +6,12 @@ import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable;
|
||||
import java.util.ArrayList;
|
||||
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;
|
||||
|
||||
public ShowPage(PageDisplay display) {
|
||||
super(display);
|
||||
this.display = display;
|
||||
this.contentToShowIndex = new ArrayList<>();
|
||||
|
||||
if (this.display.getColours() != null) {
|
||||
@ -21,19 +19,17 @@ public abstract class ShowPage extends ShowRunnable<List<String>> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void run() {
|
||||
calculateContent();
|
||||
if (!validateContent()) {
|
||||
return;
|
||||
}
|
||||
if (contentToShowIndex.isEmpty()) {
|
||||
this.display.getSender().sendMessage("No matching content to display.");
|
||||
return;
|
||||
}
|
||||
display();
|
||||
public boolean hasContentToShow() {
|
||||
return !contentToShowIndex.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void showContent() {
|
||||
ColourAlternator colours = this.display.getColours();
|
||||
|
@ -12,12 +12,18 @@ public class ShowSelectedPage extends ShowPage {
|
||||
super(display);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void display() {
|
||||
super.display();
|
||||
doEndPadding();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void calculateContent() {
|
||||
int lineCount = 0;
|
||||
@ -41,6 +47,9 @@ public class ShowSelectedPage extends ShowPage {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public boolean validateContent() {
|
||||
return !pageOutOfRange();
|
||||
@ -56,25 +65,32 @@ public class ShowSelectedPage extends ShowPage {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public void showHeader() {
|
||||
String theHeader;
|
||||
if (this.display.getHeader() == null) {
|
||||
theHeader = this.contents.get(contentToShowIndex.remove(0));
|
||||
this.display.reduceContentLinesPerPage(1);
|
||||
}
|
||||
else {
|
||||
theHeader = this.display.getHeader();
|
||||
}
|
||||
String header = getHeader();
|
||||
|
||||
if (theHeader.contains(PageDisplay.PAGE_PLACEHOLDER)) {
|
||||
this.display.getSender().sendMessage(theHeader.replace(PageDisplay.PAGE_PLACEHOLDER, parsePaging()));
|
||||
// Paging inline with header
|
||||
if (header.contains(PageDisplay.PAGE_PLACEHOLDER)) {
|
||||
this.display.getSender().sendMessage(header.replace(PageDisplay.PAGE_PLACEHOLDER, parsePaging()));
|
||||
return;
|
||||
}
|
||||
this.display.getSender().sendMessage(theHeader);
|
||||
|
||||
this.display.getSender().sendMessage(header);
|
||||
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() {
|
||||
ContentFilter filter = this.display.getFilter();
|
||||
return (filter.hasFilter())
|
||||
|
@ -72,8 +72,8 @@ public class SubModulesCommand {
|
||||
@NotNull ColourAlternator colours,
|
||||
@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:");
|
||||
sender.sendMessage(colours.getColorThat() + downloadLink);
|
||||
sender.sendMessage(colours.getThat() + downloadLink);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user