mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-22 02:25:41 +01:00
Merge pull request #2667 from Multiverse/content-display-improvements
A few tweaks to ContentDisplay and friends.
This commit is contained in:
commit
9541c757db
@ -8,11 +8,9 @@
|
||||
package com.onarandombox.MultiverseCore.commands;
|
||||
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
|
||||
import com.onarandombox.MultiverseCore.displaytools.ColorAlternator;
|
||||
import com.onarandombox.MultiverseCore.displaytools.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.displaytools.DisplayHandlers;
|
||||
import com.onarandombox.MultiverseCore.displaytools.DisplaySettings;
|
||||
import com.onarandombox.MultiverseCore.display.ColorAlternator;
|
||||
import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.display.settings.MapDisplaySettings;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.GameRule;
|
||||
@ -21,7 +19,6 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -76,14 +73,11 @@ public class GamerulesCommand extends MultiverseCommand {
|
||||
}
|
||||
}
|
||||
|
||||
new ContentDisplay.Builder<Map<String, Object>>()
|
||||
.sender(sender)
|
||||
ContentDisplay.forContent(getGameRuleMap(world))
|
||||
.header("=== Gamerules for %s%s%s ===", ChatColor.AQUA, world.getName(), ChatColor.WHITE)
|
||||
.contents(getGameRuleMap(world))
|
||||
.displayHandler(DisplayHandlers.INLINE_MAP)
|
||||
.colorTool(ColorAlternator.with(ChatColor.GREEN, ChatColor.GOLD))
|
||||
.setting(DisplaySettings.OPERATOR, ": ")
|
||||
.display();
|
||||
.setting(MapDisplaySettings.OPERATOR, ": ")
|
||||
.show(sender);
|
||||
}
|
||||
|
||||
private Map<String, Object> getGameRuleMap(World world) {
|
||||
|
@ -7,14 +7,13 @@
|
||||
|
||||
package com.onarandombox.MultiverseCore.commands;
|
||||
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import com.onarandombox.MultiverseCore.MultiverseCore;
|
||||
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
|
||||
import com.onarandombox.MultiverseCore.displaytools.ColorAlternator;
|
||||
import com.onarandombox.MultiverseCore.displaytools.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.displaytools.ContentFilter;
|
||||
import com.onarandombox.MultiverseCore.displaytools.DisplayHandlers;
|
||||
import com.onarandombox.MultiverseCore.displaytools.DisplaySettings;
|
||||
import com.onarandombox.MultiverseCore.display.ColorAlternator;
|
||||
import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.display.ContentFilter;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayHandlers;
|
||||
import com.onarandombox.MultiverseCore.display.settings.PagedDisplaySettings;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.command.CommandSender;
|
||||
@ -22,9 +21,7 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.PermissionDefault;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -68,18 +65,16 @@ public class ListCommand extends MultiverseCommand {
|
||||
}
|
||||
}
|
||||
|
||||
new ContentDisplay.Builder<Collection<String>>()
|
||||
.sender(sender)
|
||||
ContentDisplay.forContent(getListContents(sender))
|
||||
.header("%s====[ Multiverse World List ]====", ChatColor.GOLD)
|
||||
.contents(getListContents(sender))
|
||||
.displayHandler(DisplayHandlers.PAGE_LIST)
|
||||
.colorTool(ColorAlternator.with(ChatColor.AQUA, ChatColor.GOLD))
|
||||
.filter(filter)
|
||||
.setting(DisplaySettings.SHOW_PAGE, page)
|
||||
.display();
|
||||
.setting(PagedDisplaySettings.SHOW_PAGE, page)
|
||||
.show(sender);
|
||||
}
|
||||
|
||||
private List<String> getListContents(@NotNull CommandSender sender) {
|
||||
private Collection<String> getListContents(@NotNull CommandSender sender) {
|
||||
Player player = (sender instanceof Player) ? (Player) sender : null;
|
||||
|
||||
List<String> worldList = this.plugin.getMVWorldManager().getMVWorlds().stream()
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
package com.onarandombox.MultiverseCore.display;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.jetbrains.annotations.NotNull;
|
@ -1,10 +1,11 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
package com.onarandombox.MultiverseCore.display;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
/**
|
||||
* Tools to allow customisation.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ColorTool {
|
||||
|
||||
/**
|
@ -1,9 +1,9 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
package com.onarandombox.MultiverseCore.display;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.settings.DisplaySetting;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
@ -19,39 +19,66 @@ public class ContentDisplay<T> {
|
||||
|
||||
public static final String LINE_BREAK = "%br%";
|
||||
|
||||
private CommandSender sender;
|
||||
/**
|
||||
* Creates a ContentDisplay.Builder for the given content.
|
||||
*
|
||||
* @param content The content to be displayed.
|
||||
* @param <T> The type of the content which can be inferred.
|
||||
* @return A new Builder.
|
||||
*/
|
||||
public static <T> Builder<T> forContent(T content) {
|
||||
return new Builder<>(content);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ContentDisplay.Builder for the given collection of content.
|
||||
*
|
||||
* @param content The content to be displayed.
|
||||
* @return A new Builder.
|
||||
*/
|
||||
public static Builder<Collection<String>> forContent(Collection<String> content) {
|
||||
return new Builder<>(content).displayHandler(DisplayHandlers.LIST);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a ContentDisplay.Builder for the given map of content.
|
||||
*
|
||||
* @param content The content to be displayed.
|
||||
* @return A new Builder.
|
||||
*/
|
||||
public static Builder<Map<String, Object>> forContent(Map<String, Object> content) {
|
||||
return new Builder<>(content).displayHandler(DisplayHandlers.INLINE_MAP);
|
||||
}
|
||||
|
||||
private final T contents;
|
||||
|
||||
private String header;
|
||||
private T contents;
|
||||
private String emptyMessage = "No matching content to display.";
|
||||
private DisplayHandler<T> displayHandler;
|
||||
private ColorTool colorTool = ColorTool.DEFAULT;
|
||||
private ContentFilter filter = ContentFilter.DEFAULT;
|
||||
private final Map<DisplaySetting<?>, Object> settingsMap = new WeakHashMap<>();
|
||||
|
||||
private ContentDisplay() { }
|
||||
|
||||
/**
|
||||
* Do the actual displaying of contents to the sender.
|
||||
*/
|
||||
public void send() {
|
||||
Collection<String> formattedContent;
|
||||
try {
|
||||
formattedContent = (this.contents == null) ? null : this.displayHandler.format(this);
|
||||
} catch (DisplayFormatException e) {
|
||||
this.sender.sendMessage(String.format("%sError: %s", ChatColor.RED, e.getMessage()));
|
||||
return;
|
||||
}
|
||||
this.displayHandler.sendHeader(this);
|
||||
this.displayHandler.sendSubHeader(this);
|
||||
this.displayHandler.sendBody(this, formattedContent);
|
||||
private ContentDisplay(T contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Gets the target sender.
|
||||
* Do the actual displaying of contents to the sender.
|
||||
*
|
||||
* @param sender The CommandSender to show the display to.
|
||||
*/
|
||||
@NotNull
|
||||
public CommandSender getSender() {
|
||||
return sender;
|
||||
public void show(@NotNull CommandSender sender) {
|
||||
Collection<String> formattedContent;
|
||||
try {
|
||||
formattedContent = (this.contents == null) ? null : this.displayHandler.format(sender, this);
|
||||
} catch (DisplayFormatException e) {
|
||||
sender.sendMessage(String.format("%sError: %s", ChatColor.RED, e.getMessage()));
|
||||
return;
|
||||
}
|
||||
this.displayHandler.sendHeader(sender, this);
|
||||
this.displayHandler.sendSubHeader(sender, this);
|
||||
this.displayHandler.sendBody(sender, this, formattedContent);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -138,20 +165,8 @@ public class ContentDisplay<T> {
|
||||
|
||||
private final ContentDisplay<T> display;
|
||||
|
||||
public Builder() {
|
||||
this.display = new ContentDisplay<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets target sender to display message to. <b>Required.</b>
|
||||
*
|
||||
* @param sender The target sender.
|
||||
* @return The builder.
|
||||
*/
|
||||
@NotNull
|
||||
public Builder<T> sender(@NotNull CommandSender sender) {
|
||||
this.display.sender = sender;
|
||||
return this;
|
||||
private Builder(T content) {
|
||||
this.display = new ContentDisplay<>(content);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -167,18 +182,6 @@ public class ContentDisplay<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets content to be displayed.
|
||||
*
|
||||
* @param contents The contents.
|
||||
* @return The builder.
|
||||
*/
|
||||
@NotNull
|
||||
public Builder<T> contents(@Nullable T contents) {
|
||||
this.display.contents = contents;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the message to show when no content is available for display.
|
||||
*
|
||||
@ -249,16 +252,17 @@ public class ContentDisplay<T> {
|
||||
*/
|
||||
@NotNull
|
||||
public ContentDisplay<T> build() {
|
||||
Objects.requireNonNull(this.display.sender);
|
||||
Objects.requireNonNull(this.display.displayHandler);
|
||||
return this.display;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build and send the contents to the sender.
|
||||
* Build and show the content to the sender.
|
||||
*
|
||||
* @param sender The CommandSender to show the display to.
|
||||
*/
|
||||
public void display() {
|
||||
this.build().send();
|
||||
public void show(CommandSender sender) {
|
||||
this.build().show(sender);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
package com.onarandombox.MultiverseCore.display;
|
||||
|
||||
import com.dumptruckman.minecraft.util.Logging;
|
||||
import org.bukkit.ChatColor;
|
@ -1,4 +1,4 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
package com.onarandombox.MultiverseCore.display;
|
||||
|
||||
/**
|
||||
* Thrown when an issue occur while formatting content.
|
@ -0,0 +1,68 @@
|
||||
package com.onarandombox.MultiverseCore.display;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Handles the formatting and sending of all content by the {@link ContentDisplay}.
|
||||
*
|
||||
* @param <T> Type of content to display.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface DisplayHandler<T> {
|
||||
|
||||
/**
|
||||
* Formats the raw content into a {@link Collection<String>} for displaying to the given sender.
|
||||
*
|
||||
* @param sender The {@link CommandSender} who will the content will be displayed to.
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
* @return The formatted content.
|
||||
* @throws DisplayFormatException Issue occurred while formatting content. E.g. invalid page.
|
||||
*/
|
||||
Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display)
|
||||
throws DisplayFormatException;
|
||||
|
||||
/**
|
||||
* Sends the header.
|
||||
*
|
||||
* @param sender The {@link CommandSender} who will the header will be displayed to.
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
*/
|
||||
default void sendHeader(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display) {
|
||||
if (!Strings.isNullOrEmpty(display.getHeader())) {
|
||||
sender.sendMessage(display.getHeader());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends info such as filter and page.
|
||||
*
|
||||
* @param sender The {@link CommandSender} who will the sub header will be displayed to.
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
*/
|
||||
default void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display) {
|
||||
if (display.getFilter().hasFilter()) {
|
||||
sender.sendMessage(String.format("%s[ %s ]", ChatColor.GRAY, display.getFilter().getFormattedString()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the content.
|
||||
*
|
||||
* @param sender The {@link CommandSender} who will the body will be displayed to.
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
* @param formattedContent The content after being formatted by {@link #format(CommandSender, ContentDisplay)}
|
||||
*/
|
||||
default void sendBody(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display,
|
||||
Collection<String> formattedContent) {
|
||||
if (formattedContent == null || formattedContent.size() == 0) {
|
||||
sender.sendMessage(display.getEmptyMessage());
|
||||
return;
|
||||
}
|
||||
sender.sendMessage(formattedContent.toArray(new String[0]));
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.onarandombox.MultiverseCore.display;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.handlers.InlineListDisplayHandler;
|
||||
import com.onarandombox.MultiverseCore.display.handlers.InlineMapDisplayHandler;
|
||||
import com.onarandombox.MultiverseCore.display.handlers.ListDisplayHandler;
|
||||
import com.onarandombox.MultiverseCore.display.handlers.PagedListDisplayHandler;
|
||||
import com.onarandombox.MultiverseCore.display.settings.InlineDisplaySettings;
|
||||
import com.onarandombox.MultiverseCore.display.settings.PagedDisplaySettings;
|
||||
import com.onarandombox.MultiverseCore.display.settings.MapDisplaySettings;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Various implementations of {@link DisplayHandler}.
|
||||
*/
|
||||
public class DisplayHandlers {
|
||||
|
||||
/**
|
||||
* Standard list display.
|
||||
*
|
||||
* Supported settings: none.
|
||||
*/
|
||||
public static final DisplayHandler<Collection<String>> LIST = new ListDisplayHandler();
|
||||
|
||||
/**
|
||||
* List display with paging.
|
||||
*
|
||||
* Supported settings: {@link PagedDisplaySettings#SHOW_PAGE}, {@link PagedDisplaySettings#LINES_PER_PAGE},
|
||||
* {@link PagedDisplaySettings#PAGE_IN_CONSOLE}, {@link PagedDisplaySettings#DO_END_PADDING}.
|
||||
*/
|
||||
public static final DisplayHandler<Collection<String>> PAGE_LIST = new PagedListDisplayHandler();
|
||||
|
||||
/**
|
||||
* Display a list inline.
|
||||
*
|
||||
* Supported settings: {@link InlineDisplaySettings#SEPARATOR}.
|
||||
*/
|
||||
public static final DisplayHandler<Collection<String>> INLINE_LIST = new InlineListDisplayHandler();
|
||||
|
||||
/**
|
||||
* Display key value pair inline.
|
||||
*
|
||||
* Supported settings: {@link InlineDisplaySettings#SEPARATOR}, {@link MapDisplaySettings#OPERATOR}.
|
||||
*/
|
||||
public static final DisplayHandler<Map<String, Object>> INLINE_MAP = new InlineMapDisplayHandler();
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.onarandombox.MultiverseCore.display.handlers;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||
import com.onarandombox.MultiverseCore.display.settings.InlineDisplaySettings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class InlineListDisplayHandler implements DisplayHandler<Collection<String>> {
|
||||
|
||||
@Override
|
||||
public Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display)
|
||||
throws DisplayFormatException {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String separator = display.getSetting(InlineDisplaySettings.SEPARATOR);
|
||||
|
||||
for (Iterator<String> iterator = display.getContents().iterator(); iterator.hasNext(); ) {
|
||||
String content = iterator.next();
|
||||
if (!display.getFilter().checkMatch(content)) {
|
||||
continue;
|
||||
}
|
||||
builder.append(display.getColorTool().get()).append(content);
|
||||
if (iterator.hasNext()) {
|
||||
builder.append(separator);
|
||||
}
|
||||
}
|
||||
return (builder.length() == 0)
|
||||
? Collections.singletonList(display.getEmptyMessage())
|
||||
: Collections.singleton(builder.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.onarandombox.MultiverseCore.display.handlers;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||
import com.onarandombox.MultiverseCore.display.settings.InlineDisplaySettings;
|
||||
import com.onarandombox.MultiverseCore.display.settings.MapDisplaySettings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class InlineMapDisplayHandler implements DisplayHandler<Map<String, Object>> {
|
||||
|
||||
@Override
|
||||
public Collection<String> format(@NotNull CommandSender sender,
|
||||
@NotNull ContentDisplay<Map<String, Object>> display)
|
||||
throws DisplayFormatException {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String separator = display.getSetting(InlineDisplaySettings.SEPARATOR);
|
||||
String operator = display.getSetting(MapDisplaySettings.OPERATOR);
|
||||
|
||||
for (Iterator<Map.Entry<String, Object>> iterator = display.getContents().entrySet().iterator(); iterator.hasNext(); ) {
|
||||
Map.Entry<String, Object> entry = iterator.next();
|
||||
if (!display.getFilter().checkMatch(entry.getKey()) && !display.getFilter().checkMatch(entry.getValue())) {
|
||||
continue;
|
||||
}
|
||||
builder.append(display.getColorTool().get())
|
||||
.append(entry.getKey())
|
||||
.append(operator)
|
||||
.append(display.getColorTool().get())
|
||||
.append(entry.getValue());
|
||||
if (iterator.hasNext()) {
|
||||
builder.append(separator);
|
||||
}
|
||||
}
|
||||
return (builder.length() == 0)
|
||||
? Collections.singletonList(display.getEmptyMessage())
|
||||
: Collections.singleton(builder.toString());
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.onarandombox.MultiverseCore.display.handlers;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ListDisplayHandler implements DisplayHandler<Collection<String>> {
|
||||
|
||||
@Override
|
||||
public Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display)
|
||||
throws DisplayFormatException {
|
||||
return display.getContents().stream()
|
||||
.filter(display.getFilter()::checkMatch)
|
||||
.map(s -> (ContentDisplay.LINE_BREAK.equals(s)) ? "" : display.getColorTool().get() + s)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
package com.onarandombox.MultiverseCore.display.handlers;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
||||
import com.onarandombox.MultiverseCore.display.settings.PagedDisplaySettings;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class PagedListDisplayHandler extends ListDisplayHandler {
|
||||
|
||||
@Override
|
||||
public Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display)
|
||||
throws DisplayFormatException {
|
||||
if (dontNeedPaging(sender, display)) {
|
||||
return super.format(sender, display);
|
||||
}
|
||||
|
||||
int pages = 1;
|
||||
int currentLength = 0;
|
||||
int targetPage = display.getSetting(PagedDisplaySettings.SHOW_PAGE);
|
||||
int linesPerPage = display.getSetting(PagedDisplaySettings.LINES_PER_PAGE);
|
||||
List<String> content = new ArrayList<>(linesPerPage);
|
||||
|
||||
// Calculate the paging.
|
||||
for (String line : display.getContents()) {
|
||||
if (!display.getFilter().checkMatch(line)) {
|
||||
continue;
|
||||
}
|
||||
// When it's the next page.
|
||||
boolean isLineBreak = ContentDisplay.LINE_BREAK.equals(line);
|
||||
if (isLineBreak || ++currentLength > linesPerPage) {
|
||||
pages++;
|
||||
currentLength = 0;
|
||||
if (isLineBreak) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (pages == targetPage) {
|
||||
// Let first line be the header when no header is defined.
|
||||
if (display.getHeader() == null) {
|
||||
display.setHeader(line);
|
||||
currentLength--;
|
||||
continue;
|
||||
}
|
||||
content.add(display.getColorTool().get() + line);
|
||||
}
|
||||
}
|
||||
|
||||
// Page out of range.
|
||||
if (targetPage < 1 || targetPage > pages) {
|
||||
if (pages == 1) {
|
||||
throw new DisplayFormatException("There is only 1 page!");
|
||||
}
|
||||
throw new DisplayFormatException("Please enter a page from 1 to " + pages + ".");
|
||||
}
|
||||
|
||||
// No content
|
||||
if (content.size() == 0) {
|
||||
content.add(display.getEmptyMessage());
|
||||
}
|
||||
|
||||
// Add empty lines to make output length consistent.
|
||||
if (display.getSetting(PagedDisplaySettings.DO_END_PADDING)) {
|
||||
IntStream.range(0, linesPerPage - content.size()).forEach(i -> content.add(""));
|
||||
}
|
||||
display.setSetting(PagedDisplaySettings.TOTAL_PAGE, pages);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display) {
|
||||
if (dontNeedPaging(sender, display)) {
|
||||
super.sendSubHeader(sender, display);
|
||||
return;
|
||||
}
|
||||
|
||||
if (display.getFilter().hasFilter()) {
|
||||
sender.sendMessage(String.format("%s[ Page %s of %s, %s ]",
|
||||
ChatColor.GRAY,
|
||||
display.getSetting(PagedDisplaySettings.SHOW_PAGE),
|
||||
display.getSetting(PagedDisplaySettings.TOTAL_PAGE),
|
||||
display.getFilter().getFormattedString())
|
||||
);
|
||||
return;
|
||||
}
|
||||
sender.sendMessage(String.format("%s[ Page %s of %s ]",
|
||||
ChatColor.GRAY,
|
||||
display.getSetting(PagedDisplaySettings.SHOW_PAGE),
|
||||
display.getSetting(PagedDisplaySettings.TOTAL_PAGE))
|
||||
);
|
||||
}
|
||||
|
||||
private boolean dontNeedPaging(CommandSender sender, ContentDisplay<Collection<String>> display) {
|
||||
return sender instanceof ConsoleCommandSender
|
||||
&& !display.getSetting(PagedDisplaySettings.PAGE_IN_CONSOLE);
|
||||
}
|
||||
}
|
@ -1,10 +1,13 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
package com.onarandombox.MultiverseCore.display.settings;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||
|
||||
/**
|
||||
* Represents a setting option that can be used by {@link DisplayHandler}.
|
||||
*
|
||||
* @param <T>
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface DisplaySetting<T> {
|
||||
|
||||
/**
|
@ -0,0 +1,15 @@
|
||||
package com.onarandombox.MultiverseCore.display.settings;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
/**
|
||||
* Collection of {@link DisplaySetting} that are used by various {@link DisplayHandler}.
|
||||
*/
|
||||
public class InlineDisplaySettings {
|
||||
|
||||
/**
|
||||
* Inline separator. E.g. '1, 2, 3'
|
||||
*/
|
||||
public static final DisplaySetting<String> SEPARATOR = () -> ChatColor.WHITE + ", ";
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.onarandombox.MultiverseCore.display.settings;
|
||||
|
||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
/**
|
||||
* Collection of {@link DisplaySetting} that are used by various {@link DisplayHandler}.
|
||||
*/
|
||||
public class MapDisplaySettings {
|
||||
|
||||
/**
|
||||
* The thing between a key value pair. E.g. 'Me = Smart'
|
||||
*/
|
||||
public static final DisplaySetting<String> OPERATOR = () -> ChatColor.WHITE + " = ";
|
||||
}
|
@ -1,11 +1,6 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
package com.onarandombox.MultiverseCore.display.settings;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
/**
|
||||
* Collection of {@link DisplaySetting} that are used by various {@link DisplayHandler}.
|
||||
*/
|
||||
public class DisplaySettings {
|
||||
public class PagedDisplaySettings {
|
||||
|
||||
/**
|
||||
* Page to display.
|
||||
@ -32,13 +27,4 @@ public class DisplaySettings {
|
||||
*/
|
||||
public static final DisplaySetting<Boolean> PAGE_IN_CONSOLE = () -> false;
|
||||
|
||||
/**
|
||||
* Inline separator. E.g. '1, 2, 3'
|
||||
*/
|
||||
public static final DisplaySetting<String> SEPARATOR = () -> ChatColor.WHITE + ", ";
|
||||
|
||||
/**
|
||||
* The thing between a key value pair. E.g. 'Me = Smart'
|
||||
*/
|
||||
public static final DisplaySetting<String> OPERATOR = () -> ChatColor.WHITE + " = ";
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* Handles the formatting and sending of all content by the {@link ContentDisplay}.
|
||||
*
|
||||
* @param <T> Type of content to display.
|
||||
*/
|
||||
public interface DisplayHandler<T> {
|
||||
|
||||
/**
|
||||
* Formats the raw content into a {@link Collection<String>} for displaying to sender.
|
||||
*
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
* @return The formatted content.
|
||||
* @throws DisplayFormatException Issue occurred while formatting content. E.g. invalid page.
|
||||
*/
|
||||
Collection<String> format(@NotNull ContentDisplay<T> display) throws DisplayFormatException;
|
||||
|
||||
/**
|
||||
* Sends the header.
|
||||
*
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
*/
|
||||
default void sendHeader(@NotNull ContentDisplay<T> display) {
|
||||
if (!Strings.isNullOrEmpty(display.getHeader())) {
|
||||
display.getSender().sendMessage(display.getHeader());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends info such as filter and page.
|
||||
*
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
*/
|
||||
default void sendSubHeader(@NotNull ContentDisplay<T> display) {
|
||||
if (display.getFilter().hasFilter()) {
|
||||
display.getSender().sendMessage(String.format("%s[ %s ]",
|
||||
ChatColor.GRAY, display.getFilter().getFormattedString()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the content.
|
||||
*
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
* @param formattedContent The content after being formatted by {@link #format(ContentDisplay)}
|
||||
*/
|
||||
default void sendBody(@NotNull ContentDisplay<T> display, Collection<String> formattedContent) {
|
||||
if (formattedContent == null || formattedContent.size() == 0) {
|
||||
display.getSender().sendMessage(display.getEmptyMessage());
|
||||
return;
|
||||
}
|
||||
display.getSender().sendMessage(formattedContent.toArray(new String[0]));
|
||||
}
|
||||
}
|
@ -1,179 +0,0 @@
|
||||
package com.onarandombox.MultiverseCore.displaytools;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
* Various implementations of {@link DisplayHandler}.
|
||||
*/
|
||||
public class DisplayHandlers {
|
||||
|
||||
/**
|
||||
* Standard list display.
|
||||
*
|
||||
* Supported settings: none.
|
||||
*/
|
||||
public static final DisplayHandler<Collection<String>> LIST = display -> display.getContents().stream()
|
||||
.filter(display.getFilter()::checkMatch)
|
||||
.map(s -> (ContentDisplay.LINE_BREAK.equals(s)) ? "" : display.getColorTool().get() + s)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
/**
|
||||
* List display with paging.
|
||||
*
|
||||
* Supported settings: {@link DisplaySettings#SHOW_PAGE}, {@link DisplaySettings#LINES_PER_PAGE},
|
||||
* {@link DisplaySettings#PAGE_IN_CONSOLE}, {@link DisplaySettings#DO_END_PADDING}.
|
||||
*/
|
||||
public static final DisplayHandler<Collection<String>> PAGE_LIST = new DisplayHandler<Collection<String>>() {
|
||||
@Override
|
||||
public Collection<String> format(@NotNull ContentDisplay<Collection<String>> display) throws DisplayFormatException {
|
||||
if (dontNeedPaging(display)) {
|
||||
return LIST.format(display);
|
||||
}
|
||||
|
||||
int pages = 1;
|
||||
int currentLength = 0;
|
||||
int targetPage = display.getSetting(DisplaySettings.SHOW_PAGE);
|
||||
int linesPerPage = display.getSetting(DisplaySettings.LINES_PER_PAGE);
|
||||
List<String> content = new ArrayList<>(linesPerPage);
|
||||
|
||||
// Calculate the paging.
|
||||
for (String line : display.getContents()) {
|
||||
if (!display.getFilter().checkMatch(line)) {
|
||||
continue;
|
||||
}
|
||||
// When it's the next page.
|
||||
boolean isLineBreak = ContentDisplay.LINE_BREAK.equals(line);
|
||||
if (isLineBreak || ++currentLength > linesPerPage) {
|
||||
pages++;
|
||||
currentLength = 0;
|
||||
if (isLineBreak) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (pages == targetPage) {
|
||||
// Let first line be the header when no header is defined.
|
||||
if (display.getHeader() == null) {
|
||||
display.setHeader(line);
|
||||
currentLength--;
|
||||
continue;
|
||||
}
|
||||
content.add(display.getColorTool().get() + line);
|
||||
}
|
||||
}
|
||||
|
||||
// Page out of range.
|
||||
if (targetPage < 1 || targetPage > pages) {
|
||||
if (pages == 1) {
|
||||
throw new DisplayFormatException("There is only 1 page!");
|
||||
}
|
||||
throw new DisplayFormatException("Please enter a page from 1 to " + pages + ".");
|
||||
}
|
||||
|
||||
// No content
|
||||
if (content.size() == 0) {
|
||||
content.add(display.getEmptyMessage());
|
||||
}
|
||||
|
||||
// Add empty lines to make output length consistent.
|
||||
if (display.getSetting(DisplaySettings.DO_END_PADDING)) {
|
||||
IntStream.range(0, linesPerPage - content.size()).forEach(i -> content.add(""));
|
||||
}
|
||||
display.setSetting(DisplaySettings.TOTAL_PAGE, pages);
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSubHeader(@NotNull ContentDisplay<Collection<String>> display) {
|
||||
if (dontNeedPaging(display)) {
|
||||
LIST.sendSubHeader(display);
|
||||
return;
|
||||
}
|
||||
|
||||
if (display.getFilter().hasFilter()) {
|
||||
display.getSender().sendMessage(String.format("%s[ Page %s of %s, %s ]",
|
||||
ChatColor.GRAY,
|
||||
display.getSetting(DisplaySettings.SHOW_PAGE),
|
||||
display.getSetting(DisplaySettings.TOTAL_PAGE),
|
||||
display.getFilter().getFormattedString())
|
||||
);
|
||||
return;
|
||||
}
|
||||
display.getSender().sendMessage(String.format("%s[ Page %s of %s ]",
|
||||
ChatColor.GRAY,
|
||||
display.getSetting(DisplaySettings.SHOW_PAGE),
|
||||
display.getSetting(DisplaySettings.TOTAL_PAGE))
|
||||
);
|
||||
}
|
||||
|
||||
private boolean dontNeedPaging(ContentDisplay<Collection<String>> display) {
|
||||
return display.getSender() instanceof ConsoleCommandSender
|
||||
&& !display.getSetting(DisplaySettings.PAGE_IN_CONSOLE);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Display a list inline.
|
||||
*
|
||||
* Supported settings: {@link DisplaySettings#SEPARATOR}.
|
||||
*/
|
||||
public static final DisplayHandler<Collection<String>> INLINE_LIST = display -> {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String separator = display.getSetting(DisplaySettings.SEPARATOR);
|
||||
|
||||
for (Iterator<String> iterator = display.getContents().iterator(); iterator.hasNext(); ) {
|
||||
String content = iterator.next();
|
||||
if (!display.getFilter().checkMatch(content)) {
|
||||
continue;
|
||||
}
|
||||
builder.append(display.getColorTool().get()).append(content);
|
||||
if (iterator.hasNext()) {
|
||||
builder.append(separator);
|
||||
}
|
||||
}
|
||||
return (builder.length() == 0)
|
||||
? Collections.singletonList(display.getEmptyMessage())
|
||||
: Collections.singleton(builder.toString());
|
||||
};
|
||||
|
||||
/**
|
||||
* Display key value pair inline.
|
||||
*
|
||||
* Supported settings: {@link DisplaySettings#SEPARATOR}, {@link DisplaySettings#OPERATOR}.
|
||||
*/
|
||||
public static final DisplayHandler<Map<String, Object>> INLINE_MAP = display -> {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String separator = display.getSetting(DisplaySettings.SEPARATOR);
|
||||
String operator = display.getSetting(DisplaySettings.OPERATOR);
|
||||
|
||||
for (Iterator<Entry<String, Object>> iterator = display.getContents().entrySet().iterator(); iterator.hasNext(); ) {
|
||||
Entry<String, Object> entry = iterator.next();
|
||||
if (!display.getFilter().checkMatch(entry.getKey()) && !display.getFilter().checkMatch(entry.getValue())) {
|
||||
continue;
|
||||
}
|
||||
builder.append(display.getColorTool().get())
|
||||
.append(entry.getKey())
|
||||
.append(operator)
|
||||
.append(display.getColorTool().get())
|
||||
.append(entry.getValue());
|
||||
if (iterator.hasNext()) {
|
||||
builder.append(separator);
|
||||
}
|
||||
}
|
||||
return (builder.length() == 0)
|
||||
? Collections.singletonList(display.getEmptyMessage())
|
||||
: Collections.singleton(builder.toString());
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user