From 3b9f1849d74fefc28ac6eb3aab623e27f42b4228 Mon Sep 17 00:00:00 2001 From: benwoo1110 <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 31 Dec 2020 11:39:10 +0800 Subject: [PATCH] Cleanup display package and add docs. --- .../display/ColourAlternator.java | 37 ++++++++++---- .../commandTools/display/ContentCreator.java | 5 ++ .../commandTools/display/ContentDisplay.java | 22 +++++++-- .../commandTools/display/ContentFilter.java | 37 +++++++++++--- .../commandTools/display/ShowRunnable.java | 48 +++++++++++++++++-- .../display/kvpair/KeyValueDisplay.java | 9 +++- .../display/kvpair/ShowKeyValue.java | 17 +++---- .../display/page/PageDisplay.java | 10 ++-- .../display/page/ShowAllPage.java | 11 ++++- .../commandTools/display/page/ShowPage.java | 22 ++++----- .../display/page/ShowSelectedPage.java | 38 ++++++++++----- .../commands/SubModulesCommand.java | 4 +- 12 files changed, 196 insertions(+), 64 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ColourAlternator.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ColourAlternator.java index 9270c2a7..444cd7b5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ColourAlternator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ColourAlternator.java @@ -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; } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentCreator.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentCreator.java index c2a9cff6..4804513e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentCreator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentCreator.java @@ -1,5 +1,10 @@ package com.onarandombox.MultiverseCore.commandTools.display; +/** + * Generate content for displaying with {@link ContentDisplay}. + * + * @param Type of content to create. + */ @FunctionalInterface public interface ContentCreator { T generateContent(); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentDisplay.java index 47e34ae5..ec309f81 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentDisplay.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentDisplay.java @@ -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 Type of content to display. + */ public abstract class ContentDisplay { protected final Plugin plugin; @@ -28,15 +33,26 @@ public abstract class ContentDisplay { 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 getShowPageRunnable(); + /** + * Runnable used to format and display contents to + * + * @return {@link ShowRunnable} + */ + public abstract ShowRunnable, T> getShowRunnable(); public CommandSender getSender() { return sender; diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentFilter.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentFilter.java index a17b8e00..26dc5be9 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentFilter.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentFilter.java @@ -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 { } } + /** + * 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 (text == null) { - return false; - } 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{" + diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowRunnable.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowRunnable.java index 11f06787..cf47e81b 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowRunnable.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowRunnable.java @@ -2,30 +2,70 @@ package com.onarandombox.MultiverseCore.commandTools.display; import org.bukkit.scheduler.BukkitRunnable; -public abstract class ShowRunnable extends BukkitRunnable { +/** + * + * @param {@link ContentDisplay} type that is targted to. + * @param Type of content its displaying. + */ +public abstract class ShowRunnable, T> extends BukkitRunnable { - protected final T contents; + protected final D display; + protected T contents; - protected ShowRunnable(ContentDisplay 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(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/KeyValueDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/KeyValueDisplay.java index f92b4c97..50a7e936 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/KeyValueDisplay.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/KeyValueDisplay.java @@ -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> { private final String operator; @@ -28,8 +30,11 @@ public class KeyValueDisplay extends ContentDisplay> { this.operator = operator; } + /** + * {@inheritDoc} + */ @Override - public ShowRunnable> getShowPageRunnable() { + public ShowKeyValue getShowRunnable() { return new ShowKeyValue(this); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/ShowKeyValue.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/ShowKeyValue.java index 54dc506d..10007847 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/ShowKeyValue.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/ShowKeyValue.java @@ -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> { +public class ShowKeyValue extends ShowRunnable> { 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> { 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> { if (this.display.getHeader() == null) { return; } - this.display.getSender().sendMessage(this.display.getHeader()); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/PageDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/PageDisplay.java index f1f6b296..1f150f8c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/PageDisplay.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/PageDisplay.java @@ -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> { private final int pageToShow; @@ -45,8 +46,11 @@ public class PageDisplay extends ContentDisplay> { this.contentLinesPerPage = contentLinesPerPage; } + /** + * {@inheritDoc} + */ @Override - public ShowRunnable> getShowPageRunnable() { + public ShowPage getShowRunnable() { return (this.sender instanceof ConsoleCommandSender) ? new ShowAllPage(this) : new ShowSelectedPage(this); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowAllPage.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowAllPage.java index 68f62614..cafaf11a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowAllPage.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowAllPage.java @@ -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) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowPage.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowPage.java index 76a3603f..a6d2ebab 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowPage.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowPage.java @@ -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> { +public abstract class ShowPage extends ShowRunnable> { - protected PageDisplay display; protected final List 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> { } } + /** + * {@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(); diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowSelectedPage.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowSelectedPage.java index 11307680..8fd5eca5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowSelectedPage.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowSelectedPage.java @@ -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()) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/SubModulesCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/SubModulesCommand.java index f4436c4e..4f3ac44c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/SubModulesCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/SubModulesCommand.java @@ -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); } }