Add ability to have custom empty message, and mroe cleanup.

This commit is contained in:
benwoo1110 2021-01-02 23:47:59 +08:00
parent 6987208241
commit cdfe317517
12 changed files with 56 additions and 47 deletions

View File

@ -35,7 +35,7 @@ public class ColorAlternator {
*
* @return Opposite of the previous colour.
*/
public ChatColor get() {
public @NotNull ChatColor get() {
return (switcher ^= true) ? thisColour : thatColour;
}
@ -50,8 +50,7 @@ public class ColorAlternator {
*
* @return {@link ColorAlternator#thisColour}.
*/
@NotNull
public ChatColor getThis() {
public @NotNull ChatColor getThis() {
return thisColour;
}
@ -59,8 +58,7 @@ public class ColorAlternator {
*
* @return {@link ColorAlternator#thatColour}.
*/
@NotNull
public ChatColor getThat() {
public @NotNull ChatColor getThat() {
return thatColour;
}
}

View File

@ -1,5 +1,7 @@
package com.onarandombox.MultiverseCore.commandTools.display;
import org.jetbrains.annotations.NotNull;
/**
* Generate content for displaying with {@link ContentDisplay}.
*
@ -7,5 +9,5 @@ package com.onarandombox.MultiverseCore.commandTools.display;
*/
@FunctionalInterface
public interface ContentCreator<T> {
T generateContent();
@NotNull T generateContent();
}

View File

@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore.commandTools.display;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Displays various types of content to sender.
@ -15,6 +16,9 @@ public abstract class ContentDisplay<C extends ContentDisplay<?, T>, T> {
protected ContentCreator<T> creator;
protected ContentFilter filter = ContentFilter.EMPTY;
protected ColorAlternator colours;
protected String emptyMessage = DEFAULT_EMPTY_MESSAGE;
public static final String DEFAULT_EMPTY_MESSAGE = "No matching content to display.";
/**
* Build into a runnable for showing of content.
@ -45,48 +49,57 @@ public abstract class ContentDisplay<C extends ContentDisplay<?, T>, T> {
*/
protected abstract @NotNull ShowRunnable<C, T> getShowRunnable();
public C withSender(CommandSender sender) {
public @NotNull C withSender(@NotNull CommandSender sender) {
this.sender = sender;
return (C) this;
}
public C withHeader(String header) {
public @NotNull C withHeader(@NotNull String header) {
this.header = header;
return (C) this;
}
public C withCreator(ContentCreator<T> creator) {
public @NotNull C withCreator(@NotNull ContentCreator<T> creator) {
this.creator = creator;
return (C) this;
}
public C withFilter(ContentFilter filter) {
public @NotNull C withFilter(@NotNull ContentFilter filter) {
this.filter = filter;
return (C) this;
}
public C withColors(ColorAlternator colours) {
public @NotNull C withColors(@NotNull ColorAlternator colours) {
this.colours = colours;
return (C) this;
}
public CommandSender getSender() {
public @NotNull C withEmptyMessage(@NotNull String emptyMessage) {
this.emptyMessage = emptyMessage;
return (C) this;
}
public @NotNull CommandSender getSender() {
return sender;
}
public String getHeader() {
public @Nullable String getHeader() {
return header;
}
public ContentCreator<T> getCreator() {
public @NotNull ContentCreator<T> getCreator() {
return creator;
}
public ContentFilter getFilter() {
public @NotNull ContentFilter getFilter() {
return filter;
}
public ColorAlternator getColours() {
public @NotNull ColorAlternator getColours() {
return colours;
}
public @NotNull String getEmptyMessage() {
return emptyMessage;
}
}

View File

@ -94,13 +94,11 @@ public class ContentFilter {
return filterPattern != null;
}
@Nullable
public String getString() {
public @Nullable String getString() {
return filterString;
}
@Nullable
public Pattern getPattern() {
public @Nullable Pattern getPattern() {
return filterPattern;
}
@ -113,8 +111,7 @@ public class ContentFilter {
*
* @return formatted filter string.
*/
@NotNull
public String getFormattedString() {
public @NotNull String getFormattedString() {
return String.format("%sFilter: '%s'%s", ChatColor.ITALIC, filterString, ChatColor.RESET);
}

View File

@ -27,10 +27,6 @@ public abstract class ShowRunnable<D extends ContentDisplay<?, T>, T> extends Bu
if (!validateContent()) {
return;
}
if (!hasContentToShow()) {
this.display.getSender().sendMessage("No matching content to display.");
return;
}
display();
}
@ -39,6 +35,10 @@ public abstract class ShowRunnable<D extends ContentDisplay<?, T>, T> extends Bu
*/
protected void display() {
showHeader();
if (!hasContentToShow()) {
this.display.getSender().sendMessage(this.display.getEmptyMessage());
return;
}
showContent();
}

View File

@ -22,7 +22,7 @@ public class KeyValueDisplay extends ContentDisplay<KeyValueDisplay, Map<String,
return new ShowKeyValue(this);
}
public KeyValueDisplay withOperator(String operator) {
public @NotNull KeyValueDisplay withOperator(@NotNull String operator) {
this.operator = operator;
return this;
}

View File

@ -2,12 +2,13 @@ package com.onarandombox.MultiverseCore.commandTools.display.inline;
import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay;
import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable;
import org.jetbrains.annotations.NotNull;
public abstract class ShowInline<D extends ContentDisplay<?, T>, T> extends ShowRunnable<D, T> {
protected final StringBuilder contentBuilder;
public ShowInline(D display) {
public ShowInline(@NotNull D display) {
super(display);
this.contentBuilder = new StringBuilder();
}

View File

@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore.commandTools.display.inline;
import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter;
import org.bukkit.ChatColor;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
@ -9,7 +10,7 @@ public class ShowKeyValue extends ShowInline<KeyValueDisplay, Map<String, Object
private final StringBuilder contentBuilder;
public ShowKeyValue(KeyValueDisplay display) {
public ShowKeyValue(@NotNull KeyValueDisplay display) {
super(display);
this.contentBuilder = new StringBuilder();
}

View File

@ -1,10 +1,12 @@
package com.onarandombox.MultiverseCore.commandTools.display.inline;
import org.jetbrains.annotations.NotNull;
import java.util.List;
public class ShowList extends ShowInline<ListDisplay, List<String>> {
public ShowList(ListDisplay display) {
public ShowList(@NotNull ListDisplay display) {
super(display);
}

View File

@ -38,18 +38,18 @@ public class PageDisplay extends ContentDisplay<PageDisplay, List<String>> {
: new ShowSelectedPage(this);
}
public PageDisplay withPageFilter(PageFilter pageFilter) {
public @NotNull PageDisplay withPageFilter(@NotNull PageFilter pageFilter) {
this.pageToShow = pageFilter.getPage();
this.filter = pageFilter.getFilter();
return this;
}
public PageDisplay withPage(int pageToShow) {
public @NotNull PageDisplay withPage(int pageToShow) {
this.pageToShow = pageToShow;
return this;
}
public PageDisplay withLinesPerPage(int contentLinesPerPage) {
public @NotNull PageDisplay withLinesPerPage(int contentLinesPerPage) {
this.contentLinesPerPage = contentLinesPerPage;
return this;
}

View File

@ -24,6 +24,13 @@ public class ShowSelectedPage extends ShowPage {
doEndPadding();
}
private void doEndPadding() {
IntStream.range(0, this.display.getContentLinesPerPage() - contentToShowIndex.size())
.unordered()
.mapToObj(i -> " ")
.forEach(this.display.getSender()::sendMessage);
}
/**
* {@inheritDoc}
*/
@ -100,11 +107,4 @@ public class ShowSelectedPage extends ShowPage {
? String.format("[ Page %s of %s, %s ]", this.display.getPageToShow(), totalPages, filter.getFormattedString())
: String.format("[ Page %s of %s ]", this.display.getPageToShow(), totalPages);
}
private void doEndPadding() {
IntStream.range(0, this.display.getContentLinesPerPage() - contentToShowIndex.size())
.unordered()
.mapToObj(i -> " ")
.forEach(this.display.getSender()::sendMessage);
}
}

View File

@ -82,6 +82,7 @@ public class WhoCommand extends MultiverseCommand {
.withHeader(String.format("%s===[ Players in %s%s ]===", ChatColor.AQUA, world.getColoredWorldString(), ChatColor.AQUA))
.withCreator(buildPlayerList(world, player))
.withFilter(filter)
.withEmptyMessage(String.format("%sNo players found.", ChatColor.GRAY))
.build()
.runTaskAsynchronously(this.plugin);
}
@ -92,16 +93,10 @@ public class WhoCommand extends MultiverseCommand {
return () -> {
Set<Player> visiblePlayers = getVisiblePlayers(player);
List<String> players = world.getCBWorld().getPlayers().stream()
return world.getCBWorld().getPlayers().stream()
.filter(visiblePlayers::contains)
.map(Player::getDisplayName)
.collect(Collectors.toList());
if (players.isEmpty()) {
players.add(String.format("%sNo players found.", ChatColor.GRAY));
}
return players;
};
}