mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-29 05:55:48 +01:00
Pass CommandSender to send command.
This system is much more versatile when a single ContentDisplay instance can be used for multiple players.
This commit is contained in:
parent
d1ce8e57a0
commit
3010cc958e
@ -52,7 +52,6 @@ public class ContentDisplay<T> {
|
|||||||
|
|
||||||
private final T contents;
|
private final T contents;
|
||||||
|
|
||||||
private CommandSender sender;
|
|
||||||
private String header;
|
private String header;
|
||||||
private String emptyMessage = "No matching content to display.";
|
private String emptyMessage = "No matching content to display.";
|
||||||
private DisplayHandler<T> displayHandler;
|
private DisplayHandler<T> displayHandler;
|
||||||
@ -66,26 +65,20 @@ public class ContentDisplay<T> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Do the actual displaying of contents to the sender.
|
* Do the actual displaying of contents to the sender.
|
||||||
|
*
|
||||||
|
* @param sender The CommandSender to show the display to.
|
||||||
*/
|
*/
|
||||||
public void send() {
|
public void send(@NotNull CommandSender sender) {
|
||||||
Collection<String> formattedContent;
|
Collection<String> formattedContent;
|
||||||
try {
|
try {
|
||||||
formattedContent = (this.contents == null) ? null : this.displayHandler.format(this);
|
formattedContent = (this.contents == null) ? null : this.displayHandler.format(sender, this);
|
||||||
} catch (DisplayFormatException e) {
|
} catch (DisplayFormatException e) {
|
||||||
this.sender.sendMessage(String.format("%sError: %s", ChatColor.RED, e.getMessage()));
|
sender.sendMessage(String.format("%sError: %s", ChatColor.RED, e.getMessage()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.displayHandler.sendHeader(this);
|
this.displayHandler.sendHeader(sender, this);
|
||||||
this.displayHandler.sendSubHeader(this);
|
this.displayHandler.sendSubHeader(sender, this);
|
||||||
this.displayHandler.sendBody(this, formattedContent);
|
this.displayHandler.sendBody(sender, this, formattedContent);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Gets the target sender.
|
|
||||||
*/
|
|
||||||
@NotNull
|
|
||||||
public CommandSender getSender() {
|
|
||||||
return sender;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -176,18 +169,6 @@ public class ContentDisplay<T> {
|
|||||||
this.display = new ContentDisplay<>(content);
|
this.display = new ContentDisplay<>(content);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets header to be displayed.
|
* Sets header to be displayed.
|
||||||
*
|
*
|
||||||
@ -271,17 +252,17 @@ public class ContentDisplay<T> {
|
|||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
public ContentDisplay<T> build() {
|
public ContentDisplay<T> build() {
|
||||||
Objects.requireNonNull(this.display.sender);
|
|
||||||
Objects.requireNonNull(this.display.displayHandler);
|
Objects.requireNonNull(this.display.displayHandler);
|
||||||
return this.display;
|
return this.display;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build and show the content to the sender.
|
* Build and show the content to the sender.
|
||||||
|
*
|
||||||
|
* @param sender The CommandSender to show the display to.
|
||||||
*/
|
*/
|
||||||
public void show(CommandSender sender) {
|
public void show(CommandSender sender) {
|
||||||
this.sender(sender);
|
this.build().send(sender);
|
||||||
this.build().send();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.onarandombox.MultiverseCore.display;
|
|||||||
|
|
||||||
import com.google.common.base.Strings;
|
import com.google.common.base.Strings;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -15,48 +16,53 @@ import java.util.Collection;
|
|||||||
public interface DisplayHandler<T> {
|
public interface DisplayHandler<T> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Formats the raw content into a {@link Collection<String>} for displaying to sender.
|
* 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}.
|
* @param display The responsible {@link ContentDisplay}.
|
||||||
* @return The formatted content.
|
* @return The formatted content.
|
||||||
* @throws DisplayFormatException Issue occurred while formatting content. E.g. invalid page.
|
* @throws DisplayFormatException Issue occurred while formatting content. E.g. invalid page.
|
||||||
*/
|
*/
|
||||||
Collection<String> format(@NotNull ContentDisplay<T> display) throws DisplayFormatException;
|
Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display)
|
||||||
|
throws DisplayFormatException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends the header.
|
* Sends the header.
|
||||||
*
|
*
|
||||||
|
* @param sender The {@link CommandSender} who will the header will be displayed to.
|
||||||
* @param display The responsible {@link ContentDisplay}.
|
* @param display The responsible {@link ContentDisplay}.
|
||||||
*/
|
*/
|
||||||
default void sendHeader(@NotNull ContentDisplay<T> display) {
|
default void sendHeader(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display) {
|
||||||
if (!Strings.isNullOrEmpty(display.getHeader())) {
|
if (!Strings.isNullOrEmpty(display.getHeader())) {
|
||||||
display.getSender().sendMessage(display.getHeader());
|
sender.sendMessage(display.getHeader());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends info such as filter and page.
|
* 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}.
|
* @param display The responsible {@link ContentDisplay}.
|
||||||
*/
|
*/
|
||||||
default void sendSubHeader(@NotNull ContentDisplay<T> display) {
|
default void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display) {
|
||||||
if (display.getFilter().hasFilter()) {
|
if (display.getFilter().hasFilter()) {
|
||||||
display.getSender().sendMessage(String.format("%s[ %s ]",
|
sender.sendMessage(String.format("%s[ %s ]", ChatColor.GRAY, display.getFilter().getFormattedString()));
|
||||||
ChatColor.GRAY, display.getFilter().getFormattedString()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends the content.
|
* Sends the content.
|
||||||
*
|
*
|
||||||
|
* @param sender The {@link CommandSender} who will the body will be displayed to.
|
||||||
* @param display The responsible {@link ContentDisplay}.
|
* @param display The responsible {@link ContentDisplay}.
|
||||||
* @param formattedContent The content after being formatted by {@link #format(ContentDisplay)}
|
* @param formattedContent The content after being formatted by {@link #format(CommandSender, ContentDisplay)}
|
||||||
*/
|
*/
|
||||||
default void sendBody(@NotNull ContentDisplay<T> display, Collection<String> formattedContent) {
|
default void sendBody(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display,
|
||||||
|
Collection<String> formattedContent) {
|
||||||
if (formattedContent == null || formattedContent.size() == 0) {
|
if (formattedContent == null || formattedContent.size() == 0) {
|
||||||
display.getSender().sendMessage(display.getEmptyMessage());
|
sender.sendMessage(display.getEmptyMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
display.getSender().sendMessage(formattedContent.toArray(new String[0]));
|
sender.sendMessage(formattedContent.toArray(new String[0]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
|||||||
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
||||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||||
import com.onarandombox.MultiverseCore.display.DisplaySettings;
|
import com.onarandombox.MultiverseCore.display.DisplaySettings;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -13,7 +14,7 @@ import java.util.Iterator;
|
|||||||
public class InlineListDisplayHandler implements DisplayHandler<Collection<String>> {
|
public class InlineListDisplayHandler implements DisplayHandler<Collection<String>> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> format(@NotNull ContentDisplay<Collection<String>> display)
|
public Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display)
|
||||||
throws DisplayFormatException {
|
throws DisplayFormatException {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
String separator = display.getSetting(DisplaySettings.SEPARATOR);
|
String separator = display.getSetting(DisplaySettings.SEPARATOR);
|
||||||
|
@ -4,6 +4,7 @@ import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
|||||||
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
||||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||||
import com.onarandombox.MultiverseCore.display.DisplaySettings;
|
import com.onarandombox.MultiverseCore.display.DisplaySettings;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -14,7 +15,8 @@ import java.util.Map;
|
|||||||
public class InlineMapDisplayHandler implements DisplayHandler<Map<String, Object>> {
|
public class InlineMapDisplayHandler implements DisplayHandler<Map<String, Object>> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> format(@NotNull ContentDisplay<Map<String, Object>> display)
|
public Collection<String> format(@NotNull CommandSender sender,
|
||||||
|
@NotNull ContentDisplay<Map<String, Object>> display)
|
||||||
throws DisplayFormatException {
|
throws DisplayFormatException {
|
||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
String separator = display.getSetting(DisplaySettings.SEPARATOR);
|
String separator = display.getSetting(DisplaySettings.SEPARATOR);
|
||||||
|
@ -3,6 +3,7 @@ package com.onarandombox.MultiverseCore.display.handlers;
|
|||||||
import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
||||||
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
||||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
@ -11,7 +12,8 @@ import java.util.stream.Collectors;
|
|||||||
public class ListDisplayHandler implements DisplayHandler<Collection<String>> {
|
public class ListDisplayHandler implements DisplayHandler<Collection<String>> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> format(@NotNull ContentDisplay<Collection<String>> display) throws DisplayFormatException {
|
public Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display)
|
||||||
|
throws DisplayFormatException {
|
||||||
return display.getContents().stream()
|
return display.getContents().stream()
|
||||||
.filter(display.getFilter()::checkMatch)
|
.filter(display.getFilter()::checkMatch)
|
||||||
.map(s -> (ContentDisplay.LINE_BREAK.equals(s)) ? "" : display.getColorTool().get() + s)
|
.map(s -> (ContentDisplay.LINE_BREAK.equals(s)) ? "" : display.getColorTool().get() + s)
|
||||||
|
@ -4,6 +4,7 @@ import com.onarandombox.MultiverseCore.display.ContentDisplay;
|
|||||||
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
|
||||||
import com.onarandombox.MultiverseCore.display.DisplaySettings;
|
import com.onarandombox.MultiverseCore.display.DisplaySettings;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.ConsoleCommandSender;
|
import org.bukkit.command.ConsoleCommandSender;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -15,9 +16,10 @@ import java.util.stream.IntStream;
|
|||||||
public class PagedListDisplayHandler extends ListDisplayHandler {
|
public class PagedListDisplayHandler extends ListDisplayHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<String> format(@NotNull ContentDisplay<Collection<String>> display) throws DisplayFormatException {
|
public Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display)
|
||||||
if (dontNeedPaging(display)) {
|
throws DisplayFormatException {
|
||||||
return super.format(display);
|
if (dontNeedPaging(sender, display)) {
|
||||||
|
return super.format(sender, display);
|
||||||
}
|
}
|
||||||
|
|
||||||
int pages = 1;
|
int pages = 1;
|
||||||
@ -74,14 +76,14 @@ public class PagedListDisplayHandler extends ListDisplayHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sendSubHeader(@NotNull ContentDisplay<Collection<String>> display) {
|
public void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display) {
|
||||||
if (dontNeedPaging(display)) {
|
if (dontNeedPaging(sender, display)) {
|
||||||
super.sendSubHeader(display);
|
super.sendSubHeader(sender, display);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (display.getFilter().hasFilter()) {
|
if (display.getFilter().hasFilter()) {
|
||||||
display.getSender().sendMessage(String.format("%s[ Page %s of %s, %s ]",
|
sender.sendMessage(String.format("%s[ Page %s of %s, %s ]",
|
||||||
ChatColor.GRAY,
|
ChatColor.GRAY,
|
||||||
display.getSetting(DisplaySettings.SHOW_PAGE),
|
display.getSetting(DisplaySettings.SHOW_PAGE),
|
||||||
display.getSetting(DisplaySettings.TOTAL_PAGE),
|
display.getSetting(DisplaySettings.TOTAL_PAGE),
|
||||||
@ -89,15 +91,15 @@ public class PagedListDisplayHandler extends ListDisplayHandler {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
display.getSender().sendMessage(String.format("%s[ Page %s of %s ]",
|
sender.sendMessage(String.format("%s[ Page %s of %s ]",
|
||||||
ChatColor.GRAY,
|
ChatColor.GRAY,
|
||||||
display.getSetting(DisplaySettings.SHOW_PAGE),
|
display.getSetting(DisplaySettings.SHOW_PAGE),
|
||||||
display.getSetting(DisplaySettings.TOTAL_PAGE))
|
display.getSetting(DisplaySettings.TOTAL_PAGE))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean dontNeedPaging(ContentDisplay<Collection<String>> display) {
|
private boolean dontNeedPaging(CommandSender sender, ContentDisplay<Collection<String>> display) {
|
||||||
return display.getSender() instanceof ConsoleCommandSender
|
return sender instanceof ConsoleCommandSender
|
||||||
&& !display.getSetting(DisplaySettings.PAGE_IN_CONSOLE);
|
&& !display.getSetting(DisplaySettings.PAGE_IN_CONSOLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user