diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java index 75c9dab0..894f1d90 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java +++ b/src/main/java/com/onarandombox/MultiverseCore/display/ContentDisplay.java @@ -52,7 +52,6 @@ public class ContentDisplay { private final T contents; - private CommandSender sender; private String header; private String emptyMessage = "No matching content to display."; private DisplayHandler displayHandler; @@ -66,26 +65,20 @@ public class ContentDisplay { /** * 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 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 { this.display = new ContentDisplay<>(content); } - /** - * Sets target sender to display message to. Required. - * - * @param sender The target sender. - * @return The builder. - */ - @NotNull - public Builder sender(@NotNull CommandSender sender) { - this.display.sender = sender; - return this; - } - /** * Sets header to be displayed. * @@ -271,17 +252,17 @@ public class ContentDisplay { */ @NotNull public ContentDisplay 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); } } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandler.java index 4704f25e..7971a11f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandler.java +++ b/src/main/java/com/onarandombox/MultiverseCore/display/DisplayHandler.java @@ -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 { /** - * Formats the raw content into a {@link Collection} for displaying to sender. + * Formats the raw content into a {@link Collection} 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 format(@NotNull ContentDisplay display) throws DisplayFormatException; + Collection format(@NotNull CommandSender sender, @NotNull ContentDisplay 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 display) { + default void sendHeader(@NotNull CommandSender sender, @NotNull ContentDisplay 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 display) { + default void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay 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 display, Collection formattedContent) { + default void sendBody(@NotNull CommandSender sender, @NotNull ContentDisplay display, + Collection 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])); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineListDisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineListDisplayHandler.java index bcde176f..d25f1ef2 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineListDisplayHandler.java +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineListDisplayHandler.java @@ -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> { @Override - public Collection format(@NotNull ContentDisplay> display) + public Collection format(@NotNull CommandSender sender, @NotNull ContentDisplay> display) throws DisplayFormatException { StringBuilder builder = new StringBuilder(); String separator = display.getSetting(DisplaySettings.SEPARATOR); diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineMapDisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineMapDisplayHandler.java index b292e465..8bab0600 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineMapDisplayHandler.java +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/InlineMapDisplayHandler.java @@ -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> { @Override - public Collection format(@NotNull ContentDisplay> display) + public Collection format(@NotNull CommandSender sender, + @NotNull ContentDisplay> display) throws DisplayFormatException { StringBuilder builder = new StringBuilder(); String separator = display.getSetting(DisplaySettings.SEPARATOR); diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/ListDisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/ListDisplayHandler.java index d1c00f00..c2b0979f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/ListDisplayHandler.java +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/ListDisplayHandler.java @@ -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> { @Override - public Collection format(@NotNull ContentDisplay> display) throws DisplayFormatException { + public Collection format(@NotNull CommandSender sender, @NotNull ContentDisplay> display) + throws DisplayFormatException { return display.getContents().stream() .filter(display.getFilter()::checkMatch) .map(s -> (ContentDisplay.LINE_BREAK.equals(s)) ? "" : display.getColorTool().get() + s) diff --git a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedListDisplayHandler.java b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedListDisplayHandler.java index 8c89a2e0..328d3610 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedListDisplayHandler.java +++ b/src/main/java/com/onarandombox/MultiverseCore/display/handlers/PagedListDisplayHandler.java @@ -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 format(@NotNull ContentDisplay> display) throws DisplayFormatException { - if (dontNeedPaging(display)) { - return super.format(display); + public Collection format(@NotNull CommandSender sender, @NotNull ContentDisplay> 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> display) { - if (dontNeedPaging(display)) { - super.sendSubHeader(display); + public void sendSubHeader(@NotNull CommandSender sender, @NotNull ContentDisplay> 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> display) { - return display.getSender() instanceof ConsoleCommandSender + private boolean dontNeedPaging(CommandSender sender, ContentDisplay> display) { + return sender instanceof ConsoleCommandSender && !display.getSetting(DisplaySettings.PAGE_IN_CONSOLE); } }