mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-25 12:05:14 +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 CommandSender sender;
|
||||
private String header;
|
||||
private String emptyMessage = "No matching content to display.";
|
||||
private DisplayHandler<T> displayHandler;
|
||||
@ -66,26 +65,20 @@ public class ContentDisplay<T> {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
try {
|
||||
formattedContent = (this.contents == null) ? null : this.displayHandler.format(this);
|
||||
formattedContent = (this.contents == null) ? null : this.displayHandler.format(sender, this);
|
||||
} 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;
|
||||
}
|
||||
this.displayHandler.sendHeader(this);
|
||||
this.displayHandler.sendSubHeader(this);
|
||||
this.displayHandler.sendBody(this, formattedContent);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Gets the target sender.
|
||||
*/
|
||||
@NotNull
|
||||
public CommandSender getSender() {
|
||||
return sender;
|
||||
this.displayHandler.sendHeader(sender, this);
|
||||
this.displayHandler.sendSubHeader(sender, this);
|
||||
this.displayHandler.sendBody(sender, this, formattedContent);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,18 +169,6 @@ public class ContentDisplay<T> {
|
||||
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.
|
||||
*
|
||||
@ -271,17 +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 show the content to the sender.
|
||||
*
|
||||
* @param sender The CommandSender to show the display to.
|
||||
*/
|
||||
public void show(CommandSender sender) {
|
||||
this.sender(sender);
|
||||
this.build().send();
|
||||
this.build().send(sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ 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;
|
||||
@ -15,48 +16,53 @@ import java.util.Collection;
|
||||
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 display The responsible {@link ContentDisplay}.
|
||||
* @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 ContentDisplay<T> display) throws DisplayFormatException;
|
||||
Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<T> display)
|
||||
throws DisplayFormatException;
|
||||
|
||||
/**
|
||||
* Sends the header.
|
||||
*
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
* @param sender The {@link CommandSender} who will the header will be displayed to.
|
||||
* @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())) {
|
||||
display.getSender().sendMessage(display.getHeader());
|
||||
sender.sendMessage(display.getHeader());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends info such as filter and page.
|
||||
*
|
||||
* @param display The responsible {@link ContentDisplay}.
|
||||
* @param sender The {@link CommandSender} who will the sub header will be displayed to.
|
||||
* @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()) {
|
||||
display.getSender().sendMessage(String.format("%s[ %s ]",
|
||||
ChatColor.GRAY, display.getFilter().getFormattedString()));
|
||||
sender.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)}
|
||||
* @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 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) {
|
||||
display.getSender().sendMessage(display.getEmptyMessage());
|
||||
sender.sendMessage(display.getEmptyMessage());
|
||||
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.DisplayHandler;
|
||||
import com.onarandombox.MultiverseCore.display.DisplaySettings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -13,7 +14,7 @@ import java.util.Iterator;
|
||||
public class InlineListDisplayHandler implements DisplayHandler<Collection<String>> {
|
||||
|
||||
@Override
|
||||
public Collection<String> format(@NotNull ContentDisplay<Collection<String>> display)
|
||||
public Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display)
|
||||
throws DisplayFormatException {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
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.DisplayHandler;
|
||||
import com.onarandombox.MultiverseCore.display.DisplaySettings;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -14,7 +15,8 @@ import java.util.Map;
|
||||
public class InlineMapDisplayHandler implements DisplayHandler<Map<String, Object>> {
|
||||
|
||||
@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 {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
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.DisplayFormatException;
|
||||
import com.onarandombox.MultiverseCore.display.DisplayHandler;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@ -11,7 +12,8 @@ import java.util.stream.Collectors;
|
||||
public class ListDisplayHandler implements DisplayHandler<Collection<String>> {
|
||||
|
||||
@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()
|
||||
.filter(display.getFilter()::checkMatch)
|
||||
.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.DisplaySettings;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.ConsoleCommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@ -15,9 +16,10 @@ import java.util.stream.IntStream;
|
||||
public class PagedListDisplayHandler extends ListDisplayHandler {
|
||||
|
||||
@Override
|
||||
public Collection<String> format(@NotNull ContentDisplay<Collection<String>> display) throws DisplayFormatException {
|
||||
if (dontNeedPaging(display)) {
|
||||
return super.format(display);
|
||||
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;
|
||||
@ -74,14 +76,14 @@ public class PagedListDisplayHandler extends ListDisplayHandler {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSubHeader(@NotNull ContentDisplay<Collection<String>> display) {
|
||||
if (dontNeedPaging(display)) {
|
||||
super.sendSubHeader(display);
|
||||
public void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display) {
|
||||
if (dontNeedPaging(sender, display)) {
|
||||
super.sendSubHeader(sender, display);
|
||||
return;
|
||||
}
|
||||
|
||||
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,
|
||||
display.getSetting(DisplaySettings.SHOW_PAGE),
|
||||
display.getSetting(DisplaySettings.TOTAL_PAGE),
|
||||
@ -89,15 +91,15 @@ public class PagedListDisplayHandler extends ListDisplayHandler {
|
||||
);
|
||||
return;
|
||||
}
|
||||
display.getSender().sendMessage(String.format("%s[ Page %s of %s ]",
|
||||
sender.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
|
||||
private boolean dontNeedPaging(CommandSender sender, ContentDisplay<Collection<String>> display) {
|
||||
return sender instanceof ConsoleCommandSender
|
||||
&& !display.getSetting(DisplaySettings.PAGE_IN_CONSOLE);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user