From 3f8e514f51af8f9d1c867f8795d50cb7f2122929 Mon Sep 17 00:00:00 2001 From: benwoo1110 <30431861+benwoo1110@users.noreply.github.com> Date: Wed, 30 Dec 2020 15:37:25 +0800 Subject: [PATCH] Futher absract content display to add KeyValueDisplay. --- .../commandTools/display/ContentCreator.java | 6 + .../commandTools/display/ContentDisplay.java | 23 ++- .../commandTools/display/PageDisplay.java | 148 ------------------ .../commandTools/display/ShowRunnable.java | 31 ++++ .../display/kvpair/KeyValueDisplay.java | 39 +++++ .../display/kvpair/ShowKeyValue.java | 62 ++++++++ .../display/page/PageDisplay.java | 66 ++++++++ .../display/{ => page}/ShowAllPage.java | 10 +- .../display/{ => page}/ShowPage.java | 23 +-- .../display/{ => page}/ShowSelectedPage.java | 28 ++-- .../commands/AnchorCommand.java | 58 ++++--- .../commands/ConfigCommand.java | 28 ++-- .../MultiverseCore/commands/InfoCommand.java | 142 +++++++++-------- .../MultiverseCore/commands/ListCommand.java | 49 +++--- 14 files changed, 409 insertions(+), 304 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentCreator.java delete mode 100644 src/main/java/com/onarandombox/MultiverseCore/commandTools/display/PageDisplay.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowRunnable.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/KeyValueDisplay.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/ShowKeyValue.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/PageDisplay.java rename src/main/java/com/onarandombox/MultiverseCore/commandTools/display/{ => page}/ShowAllPage.java (67%) rename src/main/java/com/onarandombox/MultiverseCore/commandTools/display/{ => page}/ShowPage.java (71%) rename src/main/java/com/onarandombox/MultiverseCore/commandTools/display/{ => page}/ShowSelectedPage.java (91%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentCreator.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentCreator.java new file mode 100644 index 00000000..c2a9cff6 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentCreator.java @@ -0,0 +1,6 @@ +package com.onarandombox.MultiverseCore.commandTools.display; + +@FunctionalInterface +public interface ContentCreator { + T generateContent(); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentDisplay.java index 508f8203..47e34ae5 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentDisplay.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ContentDisplay.java @@ -5,27 +5,38 @@ import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public abstract class ContentDisplay { +public abstract class ContentDisplay { + protected final Plugin plugin; protected final CommandSender sender; protected final String header; + protected final ContentCreator creator; protected final ContentFilter filter; protected final ColourAlternator colours; - public ContentDisplay(@NotNull CommandSender sender, + public ContentDisplay(Plugin plugin, @NotNull CommandSender sender, @Nullable String header, + @NotNull ContentCreator creator, @NotNull ContentFilter filter, @Nullable ColourAlternator colours) { + this.plugin = plugin; this.sender = sender; this.header = header; + this.creator = creator; this.filter = filter; this.colours = colours; } - public abstract void showContent(); + public void showContent() { + getShowPageRunnable().runTask(this.plugin); + } - public abstract void showContentAsync(@NotNull Plugin plugin); + public void showContentAsync() { + getShowPageRunnable().runTaskAsynchronously(this.plugin); + } + + public abstract ShowRunnable getShowPageRunnable(); public CommandSender getSender() { return sender; @@ -35,6 +46,10 @@ public abstract class ContentDisplay { return header; } + public ContentCreator getCreator() { + return creator; + } + public ContentFilter getFilter() { return filter; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/PageDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/PageDisplay.java deleted file mode 100644 index d850fd38..00000000 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/PageDisplay.java +++ /dev/null @@ -1,148 +0,0 @@ -/****************************************************************************** - * Multiverse 2 Copyright (c) the Multiverse Team 2020. * - * Multiverse 2 is licensed under the BSD License. * - * For more information please check the README.md file included * - * with this project. * - ******************************************************************************/ - -package com.onarandombox.MultiverseCore.commandTools.display; - -import com.onarandombox.MultiverseCore.commandTools.PageFilter; -import org.bukkit.command.CommandSender; -import org.bukkit.command.ConsoleCommandSender; -import org.bukkit.plugin.Plugin; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.List; - -public class PageDisplay extends ContentDisplay { - - private final List contents; - private final int pageToShow; - private int contentLinesPerPage; // excludes header - - public static final int FIST_PAGE = 1; - public static final int DEFAULT_LINES_PER_PAGE = 8; - public static final String PAGE_PLACEHOLDER = "%page%"; - public static final String LINE_BREAK_PLACEHOLDER = "%lf%"; - - public PageDisplay(@NotNull CommandSender sender, - @Nullable String header, - @NotNull List contents) { - - this(sender, header, contents, FIST_PAGE, DEFAULT_LINES_PER_PAGE, new ContentFilter(null), null); - } - - public PageDisplay(@NotNull CommandSender sender, - @Nullable String header, - @NotNull List contents, - int pageToShow) { - - this(sender, header, contents, pageToShow, DEFAULT_LINES_PER_PAGE, new ContentFilter(null), null); - } - - public PageDisplay(@NotNull CommandSender sender, - @Nullable String header, - @NotNull List contents, - int pageToShow, - int contentLinesPerPage) { - - this(sender, header, contents, pageToShow, contentLinesPerPage, new ContentFilter(null), null); - } - - public PageDisplay(@NotNull CommandSender sender, - @Nullable String header, - @NotNull List contents, - int pageToShow, - int contentLinesPerPage, - @Nullable ColourAlternator colours) { - - this(sender, header, contents, pageToShow, contentLinesPerPage, new ContentFilter(null), colours); - } - - public PageDisplay(@NotNull CommandSender sender, - @Nullable String header, - @NotNull List contents, - @NotNull PageFilter pageFilter) { - - this(sender, header, contents, pageFilter.getPage(), DEFAULT_LINES_PER_PAGE, pageFilter.getFilter(), null); - } - - public PageDisplay(@NotNull CommandSender sender, - @Nullable String header, - @NotNull List contents, - @NotNull PageFilter pageFilter, - @Nullable ColourAlternator colours) { - - this(sender, header, contents, pageFilter.getPage(), DEFAULT_LINES_PER_PAGE, pageFilter.getFilter(), colours); - } - - public PageDisplay(@NotNull CommandSender sender, - @Nullable String header, - @NotNull List contents, - int pageToShow, - int contentLinesPerPage, - @NotNull ContentFilter filter, - @Nullable ColourAlternator colours) { - - super(sender, header, filter, colours); - this.contents = contents; - this.pageToShow = pageToShow; - this.contentLinesPerPage = contentLinesPerPage; - } - - @Override - public void showContent() { - getShowPageRunnable().run(); - } - - @Override - public void showContentAsync(@NotNull Plugin plugin) { - getShowPageRunnable().runTaskAsynchronously(plugin); - } - - @NotNull - public ShowPage getShowPageRunnable() { - return (sender instanceof ConsoleCommandSender) - ? new ShowAllPage(this) - : new ShowSelectedPage(this); - } - - public void reduceContentLinesPerPage(int by) { - this.contentLinesPerPage -= by; - } - - @NotNull - public CommandSender getSender() { - return sender; - } - - @Nullable - public String getHeader() { - return header; - } - - @NotNull - public List getContents() { - return contents; - } - - public int getPageToShow() { - return pageToShow; - } - - public int getContentLinesPerPage() { - return contentLinesPerPage; - } - - @NotNull - public ContentFilter getFilter() { - return filter; - } - - @Nullable - public ColourAlternator getColours() { - return colours; - } -} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowRunnable.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowRunnable.java new file mode 100644 index 00000000..11f06787 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowRunnable.java @@ -0,0 +1,31 @@ +package com.onarandombox.MultiverseCore.commandTools.display; + +import org.bukkit.scheduler.BukkitRunnable; + +public abstract class ShowRunnable extends BukkitRunnable { + + protected final T contents; + + protected ShowRunnable(ContentDisplay display) { + this.contents = display.getCreator().generateContent(); + } + + @Override + public void run() { + calculateContent(); + display(); + } + + public void display() { + showHeader(); + showContent(); + } + + public abstract void calculateContent(); + + public abstract boolean validateContent(); + + public abstract void showHeader(); + + public abstract void showContent(); +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/KeyValueDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/KeyValueDisplay.java new file mode 100644 index 00000000..f92b4c97 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/KeyValueDisplay.java @@ -0,0 +1,39 @@ +package com.onarandombox.MultiverseCore.commandTools.display.kvpair; + +import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator; +import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator; +import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay; +import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; +import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable; +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Map; + +public class KeyValueDisplay extends ContentDisplay> { + + private final String operator; + + public KeyValueDisplay(@NotNull Plugin plugin, + @NotNull CommandSender sender, + @Nullable String header, + @NotNull ContentCreator> creator, + @NotNull ContentFilter filter, + @Nullable ColourAlternator colours, + @NotNull String operator) { + + super(plugin, sender, header, creator, filter, colours); + this.operator = operator; + } + + @Override + public ShowRunnable> getShowPageRunnable() { + return new ShowKeyValue(this); + } + + public String getOperator() { + return operator; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/ShowKeyValue.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/ShowKeyValue.java new file mode 100644 index 00000000..9b76749f --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/kvpair/ShowKeyValue.java @@ -0,0 +1,62 @@ +package com.onarandombox.MultiverseCore.commandTools.display.kvpair; + +import com.dumptruckman.minecraft.util.Logging; +import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; +import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable; +import org.bukkit.ChatColor; + +import java.util.Map; + +public class ShowKeyValue extends ShowRunnable> { + + private final StringBuilder contentBuilder; + private final KeyValueDisplay display; + + public ShowKeyValue(KeyValueDisplay display) { + super(display); + this.display = display; + this.contentBuilder = new StringBuilder(); + } + + @Override + public void calculateContent() { + ContentFilter filter = this.display.getFilter(); + boolean isFirst = true; + for (Map.Entry entry : this.contents.entrySet()) { + if (!filter.checkMatch(entry.getKey()) && !filter.checkMatch(entry.getValue().toString())) { + continue; + } + if (isFirst) { + isFirst = false; + } + else { + contentBuilder.append(", "); + } + contentBuilder.append(this.display.getColours().getColorThis()) + .append(entry.getKey()) + .append(ChatColor.WHITE) + .append(this.display.getOperator()) + .append(this.display.getColours().getColorThat()) + .append(entry.getValue()); + } + } + + @Override + public boolean validateContent() { + return false; + } + + @Override + public void showHeader() { + if (this.display.getHeader() == null) { + return; + } + + this.display.getSender().sendMessage(this.display.getHeader()); + } + + @Override + public void showContent() { + this.display.getSender().sendMessage(contentBuilder.toString()); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/PageDisplay.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/PageDisplay.java new file mode 100644 index 00000000..f1f6b296 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/PageDisplay.java @@ -0,0 +1,66 @@ +/****************************************************************************** + * Multiverse 2 Copyright (c) the Multiverse Team 2020. * + * Multiverse 2 is licensed under the BSD License. * + * For more information please check the README.md file included * + * with this project. * + ******************************************************************************/ + +package com.onarandombox.MultiverseCore.commandTools.display.page; + +import com.onarandombox.MultiverseCore.commandTools.PageFilter; +import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator; +import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator; +import com.onarandombox.MultiverseCore.commandTools.display.ContentDisplay; +import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; +import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class PageDisplay extends ContentDisplay> { + + private final int pageToShow; + private int contentLinesPerPage; // excludes header + + public static final int FIST_PAGE = 1; + public static final int DEFAULT_LINES_PER_PAGE = 8; + public static final String PAGE_PLACEHOLDER = "%page%"; + public static final String LINE_BREAK_PLACEHOLDER = "%lf%"; + + public PageDisplay(@NotNull Plugin plugin, + @NotNull CommandSender sender, + @Nullable String header, + @NotNull ContentCreator> creator, + @NotNull ContentFilter filter, + @Nullable ColourAlternator colours, + int pageToShow, + int contentLinesPerPage) { + + super(plugin, sender, header, creator, filter, colours); + this.pageToShow = pageToShow; + this.contentLinesPerPage = contentLinesPerPage; + } + + @Override + public ShowRunnable> getShowPageRunnable() { + return (this.sender instanceof ConsoleCommandSender) + ? new ShowAllPage(this) + : new ShowSelectedPage(this); + } + + public void reduceContentLinesPerPage(int by) { + this.contentLinesPerPage -= by; + } + + public int getPageToShow() { + return pageToShow; + } + + public int getContentLinesPerPage() { + return contentLinesPerPage; + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowAllPage.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowAllPage.java similarity index 67% rename from src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowAllPage.java rename to src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowAllPage.java index 308afad0..68f62614 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowAllPage.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowAllPage.java @@ -1,4 +1,8 @@ -package com.onarandombox.MultiverseCore.commandTools.display; +package com.onarandombox.MultiverseCore.commandTools.display.page; + +import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; +import com.onarandombox.MultiverseCore.commandTools.display.page.PageDisplay; +import com.onarandombox.MultiverseCore.commandTools.display.page.ShowPage; public class ShowAllPage extends ShowPage { @@ -9,10 +13,10 @@ public class ShowAllPage extends ShowPage { @Override public void calculateContent() { int index = -1; - for (String line : this.display.getContents()) { + for (String line : this.contents) { index++; if (PageDisplay.LINE_BREAK_PLACEHOLDER.equals(line) - || this.display.getFilter().checkMatch(this.display.getContents().get(index))) { + || this.display.getFilter().checkMatch(this.contents.get(index))) { contentToShowIndex.add(index); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowPage.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowPage.java similarity index 71% rename from src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowPage.java rename to src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowPage.java index 79ce629f..76a3603f 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowPage.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowPage.java @@ -1,17 +1,18 @@ -package com.onarandombox.MultiverseCore.commandTools.display; +package com.onarandombox.MultiverseCore.commandTools.display.page; -import org.bukkit.ChatColor; -import org.bukkit.scheduler.BukkitRunnable; +import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator; +import com.onarandombox.MultiverseCore.commandTools.display.ShowRunnable; import java.util.ArrayList; import java.util.List; -public abstract class ShowPage extends BukkitRunnable { +public abstract class ShowPage extends ShowRunnable> { protected PageDisplay display; protected final List contentToShowIndex; public ShowPage(PageDisplay display) { + super(display); this.display = display; this.contentToShowIndex = new ArrayList<>(); @@ -33,22 +34,12 @@ public abstract class ShowPage extends BukkitRunnable { display(); } - public void display() { - showHeader(); - showContent(); - } - - public abstract void calculateContent(); - - public abstract boolean validateContent(); - - public abstract void showHeader(); - + @Override public void showContent() { ColourAlternator colours = this.display.getColours(); contentToShowIndex.stream() - .map(this.display.getContents()::get) + .map(this.contents::get) .map(line -> line.equals(PageDisplay.LINE_BREAK_PLACEHOLDER) ? "" : line.replace(PageDisplay.PAGE_PLACEHOLDER, "")) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowSelectedPage.java b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowSelectedPage.java similarity index 91% rename from src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowSelectedPage.java rename to src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowSelectedPage.java index cab7f715..11307680 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/ShowSelectedPage.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commandTools/display/page/ShowSelectedPage.java @@ -1,4 +1,6 @@ -package com.onarandombox.MultiverseCore.commandTools.display; +package com.onarandombox.MultiverseCore.commandTools.display.page; + +import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; import java.util.stream.IntStream; @@ -20,7 +22,7 @@ public class ShowSelectedPage extends ShowPage { public void calculateContent() { int lineCount = 0; int index = -1; - for (String line : this.display.getContents()) { + for (String line : this.contents) { index++; if (PageDisplay.LINE_BREAK_PLACEHOLDER.equals(line)) { lineCount = this.display.getContentLinesPerPage(); @@ -44,11 +46,21 @@ public class ShowSelectedPage extends ShowPage { return !pageOutOfRange(); } + private boolean pageOutOfRange() { + if (this.display.getPageToShow() < 0 || this.display.getPageToShow() > totalPages) { + this.display.getSender().sendMessage((totalPages == 1) + ? "There is only 1 page." + : String.format("Please enter a page from 1 to %s.", totalPages)); + return true; + } + return false; + } + @Override public void showHeader() { String theHeader; if (this.display.getHeader() == null) { - theHeader = this.display.getContents().get(contentToShowIndex.remove(0)); + theHeader = this.contents.get(contentToShowIndex.remove(0)); this.display.reduceContentLinesPerPage(1); } else { @@ -63,16 +75,6 @@ public class ShowSelectedPage extends ShowPage { this.display.getSender().sendMessage(parsePaging()); } - private boolean pageOutOfRange() { - if (this.display.getPageToShow() < 0 || this.display.getPageToShow() > totalPages) { - this.display.getSender().sendMessage((totalPages == 1) - ? "There is only 1 page." - : String.format("Please enter a page from 1 to %s.", totalPages)); - return true; - } - return false; - } - private String parsePaging() { ContentFilter filter = this.display.getFilter(); return (filter.hasFilter()) diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/AnchorCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/AnchorCommand.java index 01b1f3e3..9ca5ccfa 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/AnchorCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/AnchorCommand.java @@ -18,7 +18,8 @@ import co.aikar.commands.annotation.Syntax; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator; -import com.onarandombox.MultiverseCore.commandTools.display.PageDisplay; +import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator; +import com.onarandombox.MultiverseCore.commandTools.display.page.PageDisplay; import com.onarandombox.MultiverseCore.commandTools.PageFilter; import com.onarandombox.MultiverseCore.utils.AnchorManager; import org.bukkit.ChatColor; @@ -81,33 +82,42 @@ public class AnchorCommand extends MultiverseCommand { public void onListAnchorCommand(@NotNull CommandSender sender, @NotNull PageFilter pageFilter) { - Set anchors = (sender instanceof Player) - ? this.anchorManager.getAnchors((Player) sender) - : this.anchorManager.getAllAnchors(); - - List anchorContent = new ArrayList<>(); - for (String anchor : anchors) { - Location anchorLocation = this.anchorManager.getAnchorLocation(anchor); - World world = anchorLocation.getWorld(); // this.plugin.getMVWorldManager().getMVWorld(); - - String locationString = ChatColor.RED + "!!INVALID!!"; - if (world != null) { - MultiverseWorld mvworld = this.plugin.getMVWorldManager().getMVWorld(world); - locationString = (mvworld == null) - ? ChatColor.RED + world.getName() + "!!NOT MULTIVERSE WORLD!!" - : mvworld.getColoredWorldString() + " - " + this.plugin.getLocationManipulation().strAxis(anchorLocation); - } - anchorContent.add(anchor + ": " + locationString); - } - PageDisplay pageDisplay = new PageDisplay( + this.plugin, sender, ChatColor.LIGHT_PURPLE + "====[ Multiverse Anchor List ]====", - anchorContent, - pageFilter, - new ColourAlternator(ChatColor.YELLOW, ChatColor.DARK_AQUA) + buildAnchorList(sender), + pageFilter.getFilter(), + new ColourAlternator(ChatColor.YELLOW, ChatColor.DARK_AQUA), + pageFilter.getPage(), + 8 ); - pageDisplay.showContentAsync(this.plugin); + pageDisplay.showContentAsync(); + } + + private ContentCreator> buildAnchorList(@NotNull CommandSender sender) { + return () -> { + Set anchors = (sender instanceof Player) + ? this.anchorManager.getAnchors((Player) sender) + : this.anchorManager.getAllAnchors(); + + List anchorContent = new ArrayList<>(); + for (String anchor : anchors) { + Location anchorLocation = this.anchorManager.getAnchorLocation(anchor); + World world = anchorLocation.getWorld(); // this.plugin.getMVWorldManager().getMVWorld(); + + String locationString = ChatColor.RED + "!!INVALID!!"; + if (world != null) { + MultiverseWorld mvworld = this.plugin.getMVWorldManager().getMVWorld(world); + locationString = (mvworld == null) + ? ChatColor.RED + world.getName() + "!!NOT MULTIVERSE WORLD!!" + : mvworld.getColoredWorldString() + " - " + this.plugin.getLocationManipulation().strAxis(anchorLocation); + } + anchorContent.add(anchor + ": " + locationString); + } + + return anchorContent; + }; } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java index 3769be6b..00f6620c 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ConfigCommand.java @@ -16,6 +16,11 @@ import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import co.aikar.commands.annotation.Values; import com.onarandombox.MultiverseCore.MultiverseCore; +import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator; +import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator; +import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; +import com.onarandombox.MultiverseCore.commandTools.display.kvpair.KeyValueDisplay; +import net.milkbowl.vault.chat.Chat; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; @@ -36,18 +41,21 @@ public class ConfigCommand extends MultiverseCommand { @Subcommand("list") @Description("Show multiverse config values.") public void onShowCommand(@NotNull CommandSender sender) { - List configList = new ArrayList<>(); - Map serializedConfig = this.plugin.getMVConfig().serialize(); + KeyValueDisplay display = new KeyValueDisplay( + this.plugin, + sender, + ChatColor.LIGHT_PURPLE + "===[ Multiverse Config ]===", + getConfigMap(), + new ContentFilter("first"), + new ColourAlternator(ChatColor.GREEN, ChatColor.GOLD), + " = " + ); - for (Map.Entry entry : serializedConfig.entrySet()) { + display.showContentAsync(); + } - configList.add(ChatColor.GREEN + entry.getKey() - + ChatColor.WHITE + " = " - + ChatColor.GOLD + entry.getValue().toString() - + ChatColor.WHITE); - } - - sender.sendMessage(String.join(", ", configList)); + private ContentCreator> getConfigMap() { + return () -> this.plugin.getMVConfig().serialize(); } @Subcommand("set") diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java index 8ba0e2ff..5844e509 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/InfoCommand.java @@ -18,7 +18,9 @@ import co.aikar.commands.annotation.Syntax; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator; -import com.onarandombox.MultiverseCore.commandTools.display.PageDisplay; +import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator; +import com.onarandombox.MultiverseCore.commandTools.display.ContentFilter; +import com.onarandombox.MultiverseCore.commandTools.display.page.PageDisplay; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.World; @@ -51,94 +53,98 @@ public class InfoCommand extends MultiverseCommand { @Default("1") int page) { PageDisplay pageDisplay = new PageDisplay( + this.plugin, sender, null, buildWorldInfoContent(world), + new ContentFilter(null), + new ColourAlternator(ChatColor.YELLOW, ChatColor.AQUA), page, - 10, - new ColourAlternator(ChatColor.YELLOW, ChatColor.AQUA) + 10 ); - pageDisplay.showContentAsync(this.plugin); + pageDisplay.showContentAsync(); } - private List buildWorldInfoContent(MultiverseWorld world) { - List contents = new ArrayList<>(38); + private ContentCreator> buildWorldInfoContent(MultiverseWorld world) { + return () -> { + List contents = new ArrayList<>(38); - // Page 1 - contents.add(parseHeader("General Info")); - contents.add(String.format("World Name: %s%s", ChatColor.WHITE, world.getName())); - contents.add(String.format("World Alias: %s%s", ChatColor.WHITE, world.getColoredWorldString())); - contents.add(String.format("Game Mode: %s%s", ChatColor.WHITE, world.getGameMode().toString())); - contents.add(String.format("Difficulty: %s%s", ChatColor.WHITE, world.getDifficulty().toString())); + // Page 1 + contents.add(parseHeader("General Info")); + contents.add(String.format("World Name: %s%s", ChatColor.WHITE, world.getName())); + contents.add(String.format("World Alias: %s%s", ChatColor.WHITE, world.getColoredWorldString())); + contents.add(String.format("Game Mode: %s%s", ChatColor.WHITE, world.getGameMode().toString())); + contents.add(String.format("Difficulty: %s%s", ChatColor.WHITE, world.getDifficulty().toString())); - Location spawn = world.getSpawnLocation(); - contents.add(String.format("Spawn Location: %s%s", ChatColor.WHITE, this.plugin.getLocationManipulation().strCoords(spawn))); - contents.add(String.format("World Scale: %s%s", ChatColor.WHITE, world.getScaling())); - contents.add(String.format("World Seed: %s%s", ChatColor.WHITE, world.getSeed())); + Location spawn = world.getSpawnLocation(); + contents.add(String.format("Spawn Location: %s%s", ChatColor.WHITE, this.plugin.getLocationManipulation().strCoords(spawn))); + contents.add(String.format("World Scale: %s%s", ChatColor.WHITE, world.getScaling())); + contents.add(String.format("World Seed: %s%s", ChatColor.WHITE, world.getSeed())); - String priceString = (world.getPrice() == 0) - ? ChatColor.GREEN + "FREE!" - : plugin.getEconomist().formatPrice(-world.getPrice(), world.getCurrency()); + String priceString = (world.getPrice() == 0) + ? ChatColor.GREEN + "FREE!" + : plugin.getEconomist().formatPrice(-world.getPrice(), world.getCurrency()); - contents.add(String.format((world.getPrice() >= 0) - ? "Price to enter this world: %s%s" - : "Reward for entering this world: %s%s", ChatColor.WHITE, priceString)); + contents.add(String.format((world.getPrice() >= 0) + ? "Price to enter this world: %s%s" + : "Reward for entering this world: %s%s", ChatColor.WHITE, priceString)); - World respawnWorld = world.getRespawnToWorld(); - if (respawnWorld != null) { - MultiverseWorld respawn = this.plugin.getMVWorldManager().getMVWorld(respawnWorld); - String respawnWorldString = (respawn != null) - ? respawn.getColoredWorldString() - : ChatColor.RED + respawnWorld.getName() + " !!INVALID!!"; + World respawnWorld = world.getRespawnToWorld(); + if (respawnWorld != null) { + MultiverseWorld respawn = this.plugin.getMVWorldManager().getMVWorld(respawnWorld); + String respawnWorldString = (respawn != null) + ? respawn.getColoredWorldString() + : ChatColor.RED + respawnWorld.getName() + " !!INVALID!!"; - contents.add(String.format("Players will respawn in: %s%s", ChatColor.WHITE, respawnWorldString)); - } - contents.add("%lf%"); + contents.add(String.format("Players will respawn in: %s%s", ChatColor.WHITE, respawnWorldString)); + } + contents.add("%lf%"); - // Page 2 - contents.add(parseHeader("More World Settings")); - contents.add(String.format("World UID: %s%s", ChatColor.WHITE, world.getCBWorld().getUID())); - contents.add(String.format("World Type: %s%s", ChatColor.WHITE, world.getWorldType().toString())); - contents.add(String.format("Generator: %s%s", ChatColor.WHITE, world.getGenerator())); - contents.add(String.format("Structures: %s%s", ChatColor.WHITE, world.getCBWorld().canGenerateStructures())); - contents.add(String.format("Weather: %s%s", ChatColor.WHITE, world.isWeatherEnabled())); - contents.add(String.format("Players will get hungry: %s%s", ChatColor.WHITE, world.getHunger())); - contents.add(String.format("Keep spawn in memory: %s%s", ChatColor.WHITE, world.isKeepingSpawnInMemory())); - contents.add("%lf%"); + // Page 2 + contents.add(parseHeader("More World Settings")); + contents.add(String.format("World UID: %s%s", ChatColor.WHITE, world.getCBWorld().getUID())); + contents.add(String.format("World Type: %s%s", ChatColor.WHITE, world.getWorldType().toString())); + contents.add(String.format("Generator: %s%s", ChatColor.WHITE, world.getGenerator())); + contents.add(String.format("Structures: %s%s", ChatColor.WHITE, world.getCBWorld().canGenerateStructures())); + contents.add(String.format("Weather: %s%s", ChatColor.WHITE, world.isWeatherEnabled())); + contents.add(String.format("Players will get hungry: %s%s", ChatColor.WHITE, world.getHunger())); + contents.add(String.format("Keep spawn in memory: %s%s", ChatColor.WHITE, world.isKeepingSpawnInMemory())); + contents.add("%lf%"); - // Page 3 - contents.add(parseHeader("PVP Settings")); - contents.add(String.format("Multiverse Setting: %s%s", ChatColor.WHITE, world.isPVPEnabled())); - contents.add(String.format("Bukkit Setting: %s%s", ChatColor.WHITE, world.getCBWorld().getPVP())); - contents.add("%lf%"); + // Page 3 + contents.add(parseHeader("PVP Settings")); + contents.add(String.format("Multiverse Setting: %s%s", ChatColor.WHITE, world.isPVPEnabled())); + contents.add(String.format("Bukkit Setting: %s%s", ChatColor.WHITE, world.getCBWorld().getPVP())); + contents.add("%lf%"); - // Page 4 - contents.add(parseHeader("Monster Settings")); - contents.add(String.format("Multiverse Setting: %s%s", ChatColor.WHITE, world.canMonstersSpawn())); - contents.add(String.format("Bukkit Setting: %s%s", ChatColor.WHITE, world.getCBWorld().getAllowMonsters())); + // Page 4 + contents.add(parseHeader("Monster Settings")); + contents.add(String.format("Multiverse Setting: %s%s", ChatColor.WHITE, world.canMonstersSpawn())); + contents.add(String.format("Bukkit Setting: %s%s", ChatColor.WHITE, world.getCBWorld().getAllowMonsters())); - if (!world.getMonsterList().isEmpty()){ - contents.add(String.format((world.canMonstersSpawn()) - ? "Monsters that" + ChatColor.RED + " CAN NOT " + ChatColor.GREEN + "spawn: %s%s" - : "Monsters that" + ChatColor.GREEN + " CAN SPAWN: %s%s", - ChatColor.WHITE, toCommaSeparated(world.getMonsterList()))); - } - contents.add("%lf%"); + if (!world.getMonsterList().isEmpty()){ + contents.add(String.format((world.canMonstersSpawn()) + ? "Monsters that" + ChatColor.RED + " CAN NOT " + ChatColor.GREEN + "spawn: %s%s" + : "Monsters that" + ChatColor.GREEN + " CAN SPAWN: %s%s", + ChatColor.WHITE, toCommaSeparated(world.getMonsterList()))); + } + contents.add("%lf%"); - // Page 5 - contents.add(parseHeader("Animal Settings")); - contents.add(String.format("Multiverse Setting: %s%s", ChatColor.WHITE, world.canAnimalsSpawn())); - contents.add(String.format("Bukkit Setting: %s%s", ChatColor.WHITE, world.getCBWorld().getAllowAnimals())); + // Page 5 + contents.add(parseHeader("Animal Settings")); + contents.add(String.format("Multiverse Setting: %s%s", ChatColor.WHITE, world.canAnimalsSpawn())); + contents.add(String.format("Bukkit Setting: %s%s", ChatColor.WHITE, world.getCBWorld().getAllowAnimals())); - if (!world.getAnimalList().isEmpty()){ - contents.add(String.format((world.canMonstersSpawn()) - ? "Animals that" + ChatColor.RED + " CAN NOT " + ChatColor.GREEN + "spawn: %s%s" - : "Animals that" + ChatColor.GREEN + " CAN SPAWN: %s%s", - ChatColor.WHITE, toCommaSeparated(world.getAnimalList()))); - } + if (!world.getAnimalList().isEmpty()){ + contents.add(String.format((world.canMonstersSpawn()) + ? "Animals that" + ChatColor.RED + " CAN NOT " + ChatColor.GREEN + "spawn: %s%s" + : "Animals that" + ChatColor.GREEN + " CAN SPAWN: %s%s", + ChatColor.WHITE, toCommaSeparated(world.getAnimalList()))); + } - return contents; + return contents; + }; } private String parseHeader(String header) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java index c822506a..c5ceee90 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java +++ b/src/main/java/com/onarandombox/MultiverseCore/commands/ListCommand.java @@ -15,7 +15,9 @@ import co.aikar.commands.annotation.Subcommand; import co.aikar.commands.annotation.Syntax; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; -import com.onarandombox.MultiverseCore.commandTools.display.PageDisplay; +import com.onarandombox.MultiverseCore.commandTools.display.ColourAlternator; +import com.onarandombox.MultiverseCore.commandTools.display.ContentCreator; +import com.onarandombox.MultiverseCore.commandTools.display.page.PageDisplay; import com.onarandombox.MultiverseCore.commandTools.PageFilter; import org.bukkit.ChatColor; import org.bukkit.World; @@ -42,28 +44,39 @@ public class ListCommand extends MultiverseCommand { @Nullable @Optional Player player, @NotNull PageFilter pageFilter) { - List worldList = new ArrayList<>(); - this.plugin.getMVWorldManager().getMVWorlds().stream() - .filter(world -> player == null || this.plugin.getMVPerms().canEnterWorld(player, world)) - .filter(world -> canSeeHidden(player, world)) - .map(world -> hiddenText(world) + world.getColoredWorldString() + " - " + parseColouredEnvironment(world.getEnvironment())) - .sorted() - .forEach(worldList::add); - - this.plugin.getMVWorldManager().getUnloadedWorlds().stream() - .filter(world -> this.plugin.getMVPerms().hasPermission(sender, "multiverse.access." + world, true)) - .map(world -> ChatColor.GRAY + world + " - UNLOADED") - .sorted() - .forEach(worldList::add); - PageDisplay pageDisplay = new PageDisplay( + this.plugin, sender, ChatColor.LIGHT_PURPLE + "====[ Multiverse World List ]====", - worldList, - pageFilter + getListContents(sender, player), + pageFilter.getFilter(), + new ColourAlternator(ChatColor.WHITE, ChatColor.WHITE), + pageFilter.getPage(), + 8 ); - pageDisplay.showContentAsync(this.plugin); + pageDisplay.showContentAsync(); + } + + private ContentCreator> getListContents(@NotNull CommandSender sender, + @Nullable @Optional Player player) { + return () -> { + List worldList = new ArrayList<>(); + plugin.getMVWorldManager().getMVWorlds().stream() + .filter(world -> player == null || plugin.getMVPerms().canEnterWorld(player, world)) + .filter(world -> canSeeHidden(player, world)) + .map(world -> hiddenText(world) + world.getColoredWorldString() + " - " + parseColouredEnvironment(world.getEnvironment())) + .sorted() + .forEach(worldList::add); + + plugin.getMVWorldManager().getUnloadedWorlds().stream() + .filter(world -> plugin.getMVPerms().hasPermission(sender, "multiverse.access." + world, true)) + .map(world -> ChatColor.GRAY + world + " - UNLOADED") + .sorted() + .forEach(worldList::add); + + return worldList; + }; } private boolean canSeeHidden(Player player, MultiverseWorld world) {