Split DisplaySettings into separate classes.

This commit is contained in:
Jeremy Wood 2021-07-06 22:19:19 -04:00
parent ee95aa8462
commit 62705f4d36
11 changed files with 63 additions and 42 deletions

View File

@ -10,8 +10,7 @@ package com.onarandombox.MultiverseCore.commands;
import com.onarandombox.MultiverseCore.MultiverseCore;
import com.onarandombox.MultiverseCore.display.ColorAlternator;
import com.onarandombox.MultiverseCore.display.ContentDisplay;
import com.onarandombox.MultiverseCore.display.DisplayHandlers;
import com.onarandombox.MultiverseCore.display.DisplaySettings;
import com.onarandombox.MultiverseCore.display.settings.MapDisplaySettings;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameRule;
@ -77,7 +76,7 @@ public class GamerulesCommand extends MultiverseCommand {
ContentDisplay.forContent(getGameRuleMap(world))
.header("=== Gamerules for %s%s%s ===", ChatColor.AQUA, world.getName(), ChatColor.WHITE)
.colorTool(ColorAlternator.with(ChatColor.GREEN, ChatColor.GOLD))
.setting(DisplaySettings.OPERATOR, ": ")
.setting(MapDisplaySettings.OPERATOR, ": ")
.show(sender);
}

View File

@ -13,7 +13,7 @@ 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.DisplaySettings;
import com.onarandombox.MultiverseCore.display.settings.PagedDisplaySettings;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
@ -70,7 +70,7 @@ public class ListCommand extends MultiverseCommand {
.displayHandler(DisplayHandlers.PAGE_LIST)
.colorTool(ColorAlternator.with(ChatColor.AQUA, ChatColor.GOLD))
.filter(filter)
.setting(DisplaySettings.SHOW_PAGE, page)
.setting(PagedDisplaySettings.SHOW_PAGE, page)
.show(sender);
}

View File

@ -1,5 +1,6 @@
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;

View File

@ -4,10 +4,12 @@ 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;
import java.util.stream.Collectors;
/**
* Various implementations of {@link DisplayHandler}.
@ -24,22 +26,22 @@ public class DisplayHandlers {
/**
* 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}.
* 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 DisplaySettings#SEPARATOR}.
* Supported settings: {@link InlineDisplaySettings#SEPARATOR}.
*/
public static final DisplayHandler<Collection<String>> INLINE_LIST = new InlineListDisplayHandler();
/**
* Display key value pair inline.
*
* Supported settings: {@link DisplaySettings#SEPARATOR}, {@link DisplaySettings#OPERATOR}.
* Supported settings: {@link InlineDisplaySettings#SEPARATOR}, {@link MapDisplaySettings#OPERATOR}.
*/
public static final DisplayHandler<Map<String, Object>> INLINE_MAP = new InlineMapDisplayHandler();
}

View File

@ -3,7 +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 com.onarandombox.MultiverseCore.display.DisplaySettings;
import com.onarandombox.MultiverseCore.display.settings.InlineDisplaySettings;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@ -17,7 +17,7 @@ public class InlineListDisplayHandler implements DisplayHandler<Collection<Strin
public Collection<String> format(@NotNull CommandSender sender, @NotNull ContentDisplay<Collection<String>> display)
throws DisplayFormatException {
StringBuilder builder = new StringBuilder();
String separator = display.getSetting(DisplaySettings.SEPARATOR);
String separator = display.getSetting(InlineDisplaySettings.SEPARATOR);
for (Iterator<String> iterator = display.getContents().iterator(); iterator.hasNext(); ) {
String content = iterator.next();

View File

@ -3,7 +3,8 @@ 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.DisplaySettings;
import com.onarandombox.MultiverseCore.display.settings.InlineDisplaySettings;
import com.onarandombox.MultiverseCore.display.settings.MapDisplaySettings;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
@ -19,8 +20,8 @@ public class InlineMapDisplayHandler implements DisplayHandler<Map<String, Objec
@NotNull ContentDisplay<Map<String, Object>> display)
throws DisplayFormatException {
StringBuilder builder = new StringBuilder();
String separator = display.getSetting(DisplaySettings.SEPARATOR);
String operator = display.getSetting(DisplaySettings.OPERATOR);
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();

View File

@ -2,7 +2,7 @@ package com.onarandombox.MultiverseCore.display.handlers;
import com.onarandombox.MultiverseCore.display.ContentDisplay;
import com.onarandombox.MultiverseCore.display.DisplayFormatException;
import com.onarandombox.MultiverseCore.display.DisplaySettings;
import com.onarandombox.MultiverseCore.display.settings.PagedDisplaySettings;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
@ -24,8 +24,8 @@ public class PagedListDisplayHandler extends ListDisplayHandler {
int pages = 1;
int currentLength = 0;
int targetPage = display.getSetting(DisplaySettings.SHOW_PAGE);
int linesPerPage = display.getSetting(DisplaySettings.LINES_PER_PAGE);
int targetPage = display.getSetting(PagedDisplaySettings.SHOW_PAGE);
int linesPerPage = display.getSetting(PagedDisplaySettings.LINES_PER_PAGE);
List<String> content = new ArrayList<>(linesPerPage);
// Calculate the paging.
@ -67,10 +67,10 @@ public class PagedListDisplayHandler extends ListDisplayHandler {
}
// Add empty lines to make output length consistent.
if (display.getSetting(DisplaySettings.DO_END_PADDING)) {
if (display.getSetting(PagedDisplaySettings.DO_END_PADDING)) {
IntStream.range(0, linesPerPage - content.size()).forEach(i -> content.add(""));
}
display.setSetting(DisplaySettings.TOTAL_PAGE, pages);
display.setSetting(PagedDisplaySettings.TOTAL_PAGE, pages);
return content;
}
@ -85,21 +85,21 @@ public class PagedListDisplayHandler extends ListDisplayHandler {
if (display.getFilter().hasFilter()) {
sender.sendMessage(String.format("%s[ Page %s of %s, %s ]",
ChatColor.GRAY,
display.getSetting(DisplaySettings.SHOW_PAGE),
display.getSetting(DisplaySettings.TOTAL_PAGE),
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(DisplaySettings.SHOW_PAGE),
display.getSetting(DisplaySettings.TOTAL_PAGE))
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(DisplaySettings.PAGE_IN_CONSOLE);
&& !display.getSetting(PagedDisplaySettings.PAGE_IN_CONSOLE);
}
}

View File

@ -1,4 +1,6 @@
package com.onarandombox.MultiverseCore.display;
package com.onarandombox.MultiverseCore.display.settings;
import com.onarandombox.MultiverseCore.display.DisplayHandler;
/**
* Represents a setting option that can be used by {@link DisplayHandler}.

View File

@ -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 + ", ";
}

View File

@ -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 + " = ";
}

View File

@ -1,11 +1,6 @@
package com.onarandombox.MultiverseCore.display;
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 + " = ";
}