Implement paged info command with fancy colour.

This commit is contained in:
benwoo1110 2020-12-21 21:25:17 +08:00
parent a76c61301e
commit 904e29ed1b
3 changed files with 180 additions and 16 deletions

View File

@ -10,9 +10,18 @@ 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.commands_helper.ColourAlternator;
import com.onarandombox.MultiverseCore.commands_helper.PageDisplay;
import com.onarandombox.MultiverseCore.utils.FancyMessage;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@CommandAlias("mv")
public class InfoCommand extends MultiverseCommand {
@ -29,8 +38,110 @@ public class InfoCommand extends MultiverseCommand {
@NotNull @Flags("other,defaultself,fallbackself") MultiverseWorld world,
@Default("1") int page) {
//TODO: The actual paged info
sender.sendMessage(world.toString());
sender.sendMessage("Page of " + page);
PageDisplay pageDisplay = new PageDisplay(
sender,
buildWorldInfoContent(world),
page,
10,
new ColourAlternator(ChatColor.YELLOW, ChatColor.AQUA)
);
pageDisplay.showPageAsync(this.plugin);
}
private List<String> buildWorldInfoContent(MultiverseWorld world) {
List<String> 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()));
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());
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!!";
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 Type: %s%s", ChatColor.WHITE, world.getWorldType().toString()));
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(" ");
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("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%");
// Page 4
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())));
}
return contents;
}
private String parseHeader(String header) {
return String.format("%s--- %s %s%s %s---", ChatColor.AQUA, header, ChatColor.DARK_PURPLE, "%page%", ChatColor.AQUA);
}
private static String toCommaSeparated(List<String> list) {
if (list == null || list.size() == 0) {
return "";
}
if (list.size() == 1) {
return list.get(0);
}
StringBuilder result = new StringBuilder(list.get(0));
for (int i = 1; i < list.size() - 1; i++) {
result.append(", ").append(list.get(i));
}
result.append(" and ").append(list.get(list.size() - 1));
return result.toString();
}
}

View File

@ -0,0 +1,32 @@
package com.onarandombox.MultiverseCore.commands_helper;
import net.milkbowl.vault.chat.Chat;
import org.bukkit.ChatColor;
public class ColourAlternator {
private boolean switcher;
private final ChatColor colorThis;
private final ChatColor colorThat;
public ColourAlternator(ChatColor colorThis, ChatColor colorThat) {
this.colorThis = colorThis;
this.colorThat = colorThat;
}
public ChatColor get() {
return (switcher ^= true) ? colorThis : colorThat;
}
public void reset() {
switcher = false;
}
public ChatColor getColorThis() {
return colorThis;
}
public ChatColor getColorThat() {
return colorThat;
}
}

View File

@ -8,7 +8,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
@ -19,8 +18,9 @@ public class PageDisplay {
private final String header;
private final List<String> contents;
private final int pageToShow;
private final int contentLinesPerPage; // excludes header
private int contentLinesPerPage; // excludes header
private final Pattern filter;
private final ColourAlternator colours;
private static final int DEFAULT_PAGE = 1;
private static final int DEFAULT_LINES_PER_PAGE = 8;
@ -28,17 +28,27 @@ public class PageDisplay {
private static final String LINE_BREAK_PLACEHOLDER = "%lf%";
private static final Pattern REGEX_SPECIAL_CHARS = Pattern.compile("[.+*?\\[^\\]$(){}=!<>|:-\\\\]");
//TODO: Cleanup messy constructors.
public PageDisplay(@NotNull CommandSender sender,
@NotNull List<String> contents,
int currentPage,
int linesPerPage,
@Nullable ColourAlternator colours) {
this(sender, null, contents, currentPage, linesPerPage, null, colours);
}
public PageDisplay(@NotNull CommandSender sender,
@NotNull List<String> contents) {
this(sender, null, contents, DEFAULT_PAGE, DEFAULT_LINES_PER_PAGE, null);
this(sender, null, contents, DEFAULT_PAGE, DEFAULT_LINES_PER_PAGE, null, null);
}
public PageDisplay(@NotNull CommandSender sender,
@Nullable String header,
@NotNull List<String> contents) {
this(sender, header, contents, DEFAULT_PAGE, DEFAULT_LINES_PER_PAGE, null);
this(sender, header, contents, DEFAULT_PAGE, DEFAULT_LINES_PER_PAGE, null, null);
}
public PageDisplay(@NotNull CommandSender sender,
@ -46,7 +56,7 @@ public class PageDisplay {
@NotNull List<String> contents,
int currentPage) {
this(sender, header, contents, currentPage, DEFAULT_LINES_PER_PAGE, null);
this(sender, header, contents, currentPage, DEFAULT_LINES_PER_PAGE, null, null);
}
public PageDisplay(@NotNull CommandSender sender,
@ -55,7 +65,7 @@ public class PageDisplay {
int currentPage,
int linesPerPage) {
this(sender, header, contents, currentPage, linesPerPage, null);
this(sender, header, contents, currentPage, linesPerPage, null, null);
}
public PageDisplay(@NotNull CommandSender sender,
@ -64,7 +74,7 @@ public class PageDisplay {
int currentPage,
@Nullable String filter) {
this(sender, header, contents, currentPage, DEFAULT_LINES_PER_PAGE, filter);
this(sender, header, contents, currentPage, DEFAULT_LINES_PER_PAGE, filter, null);
}
public PageDisplay(@NotNull CommandSender sender,
@ -72,7 +82,8 @@ public class PageDisplay {
@NotNull List<String> contents,
int currentPage,
int linesPerPage,
@Nullable String filter) {
@Nullable String filter,
@Nullable ColourAlternator colours) {
this.sender = sender;
this.header = header;
@ -80,6 +91,7 @@ public class PageDisplay {
this.pageToShow = currentPage;
this.contentLinesPerPage = linesPerPage;
this.filter = parseFilter(filter);
this.colours = colours;
}
private Pattern parseFilter(@Nullable String filter) {
@ -113,6 +125,7 @@ public class PageDisplay {
public ShowRunnable() {
this.contentToShowIndex = new ArrayList<>();
colours.reset();
}
@Override
@ -130,7 +143,10 @@ public class PageDisplay {
public void showContent() {
contentToShowIndex.stream()
.map(contents::get)
.map(line -> line.equals(LINE_BREAK_PLACEHOLDER) ? " " : line)
.map(line -> line.equals(LINE_BREAK_PLACEHOLDER)
? " "
: line.replace(PAGE_PLACEHOLDER, ""))
.map(line -> ((colours == null) ? "" : colours.get()) + line)
.forEach(sender::sendMessage);
}
}
@ -201,7 +217,7 @@ public class PageDisplay {
}
if (++lineCount > contentLinesPerPage) {
totalPages++;
lineCount = 0;
lineCount = 1;
}
if (pageToShow == totalPages) {
contentToShowIndex.add(index);
@ -211,9 +227,14 @@ public class PageDisplay {
@Override
public void showHeader() {
String theHeader = (header == null)
? contents.get(contentToShowIndex.remove(0))
: header;
String theHeader;
if (header == null) {
theHeader = contents.get(contentToShowIndex.remove(0));
contentLinesPerPage--;
}
else {
theHeader = header;
}
if (theHeader.contains(PAGE_PLACEHOLDER)) {
sender.sendMessage(theHeader.replace(PAGE_PLACEHOLDER, parsePaging()));