From e3194ba15ace7950a921679d66d823f3384ce35c Mon Sep 17 00:00:00 2001 From: William Date: Tue, 23 Aug 2022 23:37:26 +0100 Subject: [PATCH] Add about menu via new AboutMenu builder, start /huskhomes --- .../huskhomes/command/BukkitCommand.java | 2 +- .../huskhomes/command/BukkitCommandType.java | 3 +- bukkit/src/main/resources/plugin.yml | 4 +- .../huskhomes/command/HuskHomesCommand.java | 97 +++++++++ .../william278/huskhomes/util/AboutMenu.java | 194 ++++++++++++++++++ .../william278/huskhomes/util/Permission.java | 8 + 6 files changed, 305 insertions(+), 3 deletions(-) create mode 100644 common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java create mode 100644 common/src/main/java/net/william278/huskhomes/util/AboutMenu.java diff --git a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java index 2c1c49d5..443a7aa4 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java +++ b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommand.java @@ -52,7 +52,7 @@ public class BukkitCommand implements CommandExecutor, TabExecutor { if (sender instanceof Player player) { this.command.onExecute(BukkitPlayer.adapt(player), args); } else { - if (command instanceof ConsoleExecutable consoleExecutable) { + if (this.command instanceof ConsoleExecutable consoleExecutable) { consoleExecutable.onConsoleExecute(args); } else { plugin.getLocales().getLocale("error_in_game_only"). diff --git a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java index 94202b5c..3907c2cb 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java +++ b/bukkit/src/main/java/net/william278/huskhomes/command/BukkitCommandType.java @@ -37,7 +37,8 @@ public enum BukkitCommandType { TPA_ALL_COMMAND(new TpaAllCommand(BukkitHuskHomes.getInstance())), SPAWN_COMMAND(new SpawnCommand(BukkitHuskHomes.getInstance())), SET_SPAWN_COMMAND(new SetSpawnCommand(BukkitHuskHomes.getInstance())), - BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance())); + BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance())), + HUSKHOMES_COMMAND(new HuskHomesCommand(BukkitHuskHomes.getInstance())); public final CommandBase commandBase; diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 161d57ec..b41f03c1 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -70,4 +70,6 @@ commands: setspawn: usage: /setspawn back: - usage: /back \ No newline at end of file + usage: /back + huskhomes: + usage: /huskhomes [about|help|reload|update] \ No newline at end of file diff --git a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java new file mode 100644 index 00000000..39eaf9ff --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java @@ -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 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(); + } +} diff --git a/common/src/main/java/net/william278/huskhomes/util/AboutMenu.java b/common/src/main/java/net/william278/huskhomes/util/AboutMenu.java new file mode 100644 index 00000000..1b4f3dd1 --- /dev/null +++ b/common/src/main/java/net/william278/huskhomes/util/AboutMenu.java @@ -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> attributions; + + @NotNull + private final List 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 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> 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> 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; + } + + } + +} diff --git a/common/src/main/java/net/william278/huskhomes/util/Permission.java b/common/src/main/java/net/william278/huskhomes/util/Permission.java index e44bcc0e..f27c4902 100644 --- a/common/src/main/java/net/william278/huskhomes/util/Permission.java +++ b/common/src/main/java/net/william278/huskhomes/util/Permission.java @@ -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 */