Add about menu via new AboutMenu builder, start /huskhomes

This commit is contained in:
William committed 2022-08-23 23:37:26 +01:00
1 parent 1b0514ae07
commit e3194ba15a
6 files changed
+305 -3

No files matched your search

@@ -0,0 +1,97 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.util.AboutMenu;
import net.william278.huskhomes.util.Permission;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.stream.Collectors;
public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, TabCompletable {
private final String[] SUB_COMMANDS = {"about", "help", "reload", "update"};
private final AboutMenu aboutMenu;
protected HuskHomesCommand(@NotNull HuskHomes implementor) {
super("huskhomes", Permission.COMMAND_HUSKHOMES, implementor);
aboutMenu = AboutMenu.create("HuskHomes")
.withDescription("A powerful, intuitive and flexible teleportation suite")
.withVersion(implementor.getPluginVersion())
.addAttribution("Author",
AboutMenu.Credit.of("William278").withDescription("Click to visit website").withUrl("https://william278.net"))
.addAttribution("Contributors",
AboutMenu.Credit.of("imDaniX").withDescription("Code, refactoring"),
AboutMenu.Credit.of("Log1x").withDescription("Code"))
.addButtons(
AboutMenu.Link.of("https://william278.net/docs/huskhomes").withText("Documentation").withIcon("⛏"),
AboutMenu.Link.of("https://github.com/WiIIiam278/HuskHomes2/issues").withText("Issues").withIcon("❌"),
AboutMenu.Link.of("https://discord.gg/tVYhJfyDWG").withText("Discord").withIcon("⭐"));
}
@Override
public void onExecute(@NotNull OnlineUser onlineUser, @NotNull String[] args) {
if (args.length == 0) {
sendAboutMenu(onlineUser);
return;
}
switch (args[0].toLowerCase()) {
case "about" -> sendAboutMenu(onlineUser);
case "help" -> {
// todo sendHelpMenu(onlineUser);
}
case "reload" -> {
}
case "update" -> {
}
}
}
@Override
public void onConsoleExecute(@NotNull String[] args) {
if (args.length == 0) {
Arrays.stream(aboutMenu.toConsoleString().split("\n")).forEach(message ->
plugin.getLoggingAdapter().log(Level.INFO, message));
return;
}
switch (args[0].toLowerCase()) {
case "about" -> Arrays.stream(aboutMenu.toConsoleString().split("\n")).forEach(message ->
plugin.getLoggingAdapter().log(Level.INFO, message));
case "help" -> {
}
case "reload" -> {
}
case "update" -> {
}
}
}
private void sendAboutMenu(@NotNull OnlineUser onlineUser) {
if (!onlineUser.hasPermission(Permission.COMMAND_HUSKHOMES_ABOUT.node)) {
plugin.getLocales().getLocale("error_no_permission")
.ifPresent(onlineUser::sendMessage);
return;
}
onlineUser.sendMessage(aboutMenu.toMineDown());
}
@Override
public @NotNull List<String> onTabComplete(@NotNull String[] args, @Nullable OnlineUser user) {
if (args.length == 0 || args.length == 1) {
return Arrays.stream(SUB_COMMANDS)
.filter(s -> s.toLowerCase().startsWith(args.length == 1 ? args[0].toLowerCase() : ""))
.sorted().collect(Collectors.toList());
}
return Collections.emptyList();
}
}
@@ -0,0 +1,194 @@
package net.william278.huskhomes.util;
import de.themoep.minedown.MineDown;
import net.william278.huskhomes.config.Locales;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class AboutMenu {
@NotNull
private final String title;
@Nullable
private Version version;
@Nullable
private String description;
@NotNull
private final Map<String, List<Credit>> attributions;
@NotNull
private final List<Link> buttons;
private AboutMenu(@NotNull String title) {
this.title = title;
this.buttons = new ArrayList<>();
this.attributions = new HashMap<>();
}
@NotNull
public static AboutMenu create(@NotNull String title) {
return new AboutMenu(title);
}
@NotNull
public AboutMenu withDescription(@Nullable String description) {
this.description = description;
return this;
}
@NotNull
public AboutMenu withVersion(@NotNull Version version) {
this.version = version;
return this;
}
@NotNull
public AboutMenu addAttribution(@NotNull String category, @NotNull Credit... credits) {
final List<Credit> creditList = new ArrayList<>(Arrays.asList(credits));
attributions.putIfAbsent(category, new ArrayList<>());
attributions.get(category).addAll(creditList);
return this;
}
@NotNull
public AboutMenu addButtons(@NotNull Link... links) {
buttons.addAll(Arrays.asList(links));
return this;
}
@NotNull
public MineDown toMineDown() {
final StringJoiner menu = new StringJoiner("\n")
.add("[" + title + "](#00fb9a bold)" + (version != null ? " [| Version " + version + "](#00fb9a)" : ""));
if (description != null) {
menu.add("[" + Locales.escapeMineDown(description) + "](gray)");
}
if (!attributions.isEmpty()) {
menu.add("");
}
for (Map.Entry<String, List<Credit>> entry : attributions.entrySet()) {
final StringJoiner creditJoiner = new StringJoiner(", ");
for (final Credit credit : entry.getValue()) {
creditJoiner.add("[" + credit.name + "](gray" +
(credit.description != null ? " show_text=&7" + credit.description : "") +
(credit.url != null ? " open_url=" + MineDown.escape(credit.url) : "") + ")");
}
if (!creditJoiner.toString().isBlank()) {
menu.add("[• " + entry.getKey() + ":](white) " + creditJoiner);
}
}
if (!buttons.isEmpty()) {
final StringJoiner buttonsJoiner = new StringJoiner(" ");
for (final Link link : buttons) {
buttonsJoiner.add("[[" + (link.icon != null ? link.icon + " " : "")
+ link.text + "…]](#00fb9a show_text=&7Click to open link open_url="
+ MineDown.escape(link.url) + ")");
}
menu.add("").add("[Links:](gray) " + buttonsJoiner);
}
return new MineDown(menu.toString()).replace();
}
@NotNull
public String toConsoleString() {
final StringJoiner menu = new StringJoiner("\n")
.add(title + (version != null ? " | Version " + version : ""));
if (description != null) {
menu.add(description);
}
if (!attributions.entrySet().isEmpty()) {
menu.add("━━━━━━━━━━━━━━━━━━━━━━━━");
}
for (final Map.Entry<String, List<Credit>> entry : attributions.entrySet()) {
final StringJoiner creditJoiner = new StringJoiner(", ");
for (final Credit credit : entry.getValue()) {
creditJoiner.add(credit.name + (credit.description != null ? " (" + credit.description + ")" : ""));
}
if (!creditJoiner.toString().isBlank()) {
menu.add("- " + entry.getKey() + ": " + creditJoiner);
}
}
if (!buttons.isEmpty()) {
menu.add("━━━━━━━━━━━━━━━━━━━━━━━━");
for (final Link link : buttons) {
menu.add("- " + link.text + ": " + link.url);
}
}
return menu.toString();
}
public static class Link {
@NotNull
private String text = "Link";
@Nullable
private String icon;
@NotNull
private final String url;
private Link(@NotNull String url) {
this.url = url;
}
public static Link of(@NotNull String url) {
return new Link(url);
}
public Link withText(@NotNull String text) {
this.text = text;
return this;
}
public Link withIcon(@NotNull String icon) {
this.icon = icon;
return this;
}
}
public static class Credit {
@NotNull
private final String name;
@Nullable
private String description;
@Nullable
private String url;
private Credit(@NotNull String name) {
this.name = name;
}
public static Credit of(@NotNull String name) {
return new Credit(name);
}
public Credit withDescription(@Nullable String description) {
this.description = description;
return this;
}
public Credit withUrl(@Nullable String url) {
this.url = url;
return this;
}
}
}
@@ -149,6 +149,14 @@ public enum Permission {
* Lets the user use {@code /back} to return to where they died
*/
COMMAND_BACK_RETURN_BY_DEATH("huskhomes.command.back.death", DefaultAccess.EVERYONE),
/**
* Lets the user access /huskhomes subcommands
*/
COMMAND_HUSKHOMES("huskhomes.command.huskhomes", DefaultAccess.EVERYONE),
/**
* Lets the user view the command list
*/
COMMAND_HUSKHOMES_HELP("huskhomes.command.huskhomes.help", DefaultAccess.EVERYONE),
/**
* Lets the user view plugin information
*/