From 50596953c3eca090fc90cc4721086cc4e8d123da Mon Sep 17 00:00:00 2001 From: HappyAreaBean Date: Sun, 22 Dec 2024 15:26:38 +0800 Subject: [PATCH] feat: new paginated help menu - lamp v4 changes Closes #5 --- .../happyareabean/sjm/SimpleJoinMessage.java | 24 +--- .../sjm/commands/SJMCommand.java | 33 ++---- .../happyareabean/sjm/utils/CommandUtils.java | 105 ++++++++++++++++++ .../cc/happyareabean/sjm/utils/Constants.java | 3 + 4 files changed, 124 insertions(+), 41 deletions(-) create mode 100644 src/main/java/cc/happyareabean/sjm/utils/CommandUtils.java diff --git a/src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java b/src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java index 06495fc..d13386e 100644 --- a/src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java +++ b/src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java @@ -6,7 +6,6 @@ import cc.happyareabean.sjm.listener.PlayerJoinListener; import cc.happyareabean.sjm.listener.UpdateNotifyListener; import cc.happyareabean.sjm.utils.AdventureWebEditorAPI; -import cc.happyareabean.sjm.utils.Constants; import lombok.Getter; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.minimessage.MiniMessage; @@ -17,7 +16,10 @@ import org.inventivetalent.update.spiget.SpigetUpdate; import org.inventivetalent.update.spiget.UpdateCallback; import org.inventivetalent.update.spiget.comparator.VersionComparator; -import revxrsal.commands.bukkit.BukkitCommandHandler; +import revxrsal.commands.Lamp; +import revxrsal.commands.bukkit.BukkitLamp; +import revxrsal.commands.bukkit.BukkitLampConfig; +import revxrsal.commands.bukkit.actor.BukkitCommandActor; import java.io.File; import java.net.URI; @@ -37,7 +39,7 @@ public class SimpleJoinMessage extends JavaPlugin { @Getter public static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacy('&'); @Getter private static BukkitAudiences adventure; @Getter private AdventureWebEditorAPI adventureWebEditorAPI; - @Getter private BukkitCommandHandler commandHandler; + @Getter private Lamp commandHandler; @Getter private SJMConfig SJMConfig; @Getter private SJMMisc miscConfig; @@ -55,21 +57,7 @@ public void onEnable() { getServer().getPluginManager().registerEvents(new UpdateNotifyListener(), this); getLogger().info("Registering commands..."); - commandHandler = BukkitCommandHandler.create(this); - commandHandler.enableAdventure(adventure); - commandHandler.setMessagePrefix(LEGACY_SERIALIZER.serialize(Constants.PREFIX)); - commandHandler.setHelpWriter((command, actor) -> { - StringBuilder sb = new StringBuilder(); - sb.append("&8• &e/"); - sb.append(command.getPath().toRealString()); - if (!command.getUsage().isEmpty()) { - sb.append(" "); - sb.append(command.getUsage()); - } - sb.append(" &7- &f"); - sb.append(command.getDescription()); - return sb.toString(); - }); + commandHandler = BukkitLamp.builder(BukkitLampConfig.builder(this).audiences(adventure).build()).build(); commandHandler.register(new SJMCommand()); adventureWebEditorAPI = new AdventureWebEditorAPI(URI.create(this.SJMConfig.getAdventureWebURL())); diff --git a/src/main/java/cc/happyareabean/sjm/commands/SJMCommand.java b/src/main/java/cc/happyareabean/sjm/commands/SJMCommand.java index 7039a3e..3459645 100644 --- a/src/main/java/cc/happyareabean/sjm/commands/SJMCommand.java +++ b/src/main/java/cc/happyareabean/sjm/commands/SJMCommand.java @@ -3,6 +3,7 @@ import cc.happyareabean.sjm.SimpleJoinMessage; import cc.happyareabean.sjm.config.SJMConfig; import cc.happyareabean.sjm.utils.AdventureWebEditorAPI; +import cc.happyareabean.sjm.utils.CommandUtils; import cc.happyareabean.sjm.utils.Constants; import cc.happyareabean.sjm.utils.MessageUtil; import net.kyori.adventure.text.Component; @@ -10,21 +11,19 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; import revxrsal.commands.annotation.Command; +import revxrsal.commands.annotation.CommandPlaceholder; import revxrsal.commands.annotation.Default; -import revxrsal.commands.annotation.DefaultFor; import revxrsal.commands.annotation.Description; import revxrsal.commands.annotation.Named; +import revxrsal.commands.annotation.Range; import revxrsal.commands.annotation.Subcommand; -import revxrsal.commands.bukkit.BukkitCommandActor; +import revxrsal.commands.bukkit.actor.BukkitCommandActor; import revxrsal.commands.bukkit.annotation.CommandPermission; -import revxrsal.commands.help.CommandHelp; +import revxrsal.commands.help.Help; -import java.util.ArrayList; import java.util.Arrays; -import java.util.List; import java.util.stream.Collectors; -import static cc.happyareabean.sjm.SimpleJoinMessage.LEGACY_SERIALIZER; import static cc.happyareabean.sjm.SimpleJoinMessage.MINIMESSAGE; import static net.kyori.adventure.text.Component.newline; import static net.kyori.adventure.text.Component.space; @@ -36,22 +35,10 @@ @CommandPermission("sjm.admin") public class SJMCommand { - @DefaultFor({"~"}) + @CommandPlaceholder @Description("SimpleJoinMessage commands list") - public void help(BukkitCommandActor actor, CommandHelp helpEntries, @Default("1") int page) { - List list = new ArrayList<>(); - list.add(Constants.HEADER); - list.add(MINIMESSAGE.deserialize(String.format("SimpleJoinMessage %s", Constants.VERSION)) - .clickEvent(openUrl("https://go.happyareabean.cc/sjm")) - .hoverEvent(MINIMESSAGE.deserialize("click me!"))); - list.add(text() - .content("By ") - .color(NamedTextColor.GRAY) - .append(text("HappyAreaBean", NamedTextColor.GREEN)).build()); - list.add(Constants.HEADER); - helpEntries.paginate(page, 10).forEach(s -> list.add(LEGACY_SERIALIZER.deserialize(s))); - list.add(Constants.HEADER); - list.forEach(actor::reply); + public void help(BukkitCommandActor actor, @Range(min = 1) @Default("1") int page, Help.RelatedCommands helpEntries) { + CommandUtils.handleHelpMenu(actor, page, helpEntries, 5, "sjm"); } @Subcommand("reload") @@ -69,14 +56,14 @@ public void reloadShow(BukkitCommandActor actor) { actor.reply(Constants.PREFIX.append(text("The settings have been reloaded, but you're in console mode, so the message won't be sent.", NamedTextColor.RED))); return; } - MessageUtil.sendMessageDelay(actor.getAsPlayer(), SimpleJoinMessage.getInstance().getSJMConfig().getJoinMessage(), 0); + MessageUtil.sendMessageDelay(actor.asPlayer(), SimpleJoinMessage.getInstance().getSJMConfig().getJoinMessage(), 0); } @Subcommand({"show"}) @Description("Send a join message in your chat") public void show(BukkitCommandActor actor) { actor.requirePlayer(); - MessageUtil.sendMessageDelay(actor.getAsPlayer(), SimpleJoinMessage.getInstance().getSJMConfig().getJoinMessage(), 0); + MessageUtil.sendMessageDelay(actor.asPlayer(), SimpleJoinMessage.getInstance().getSJMConfig().getJoinMessage(), 0); } @Subcommand({"editor"}) diff --git a/src/main/java/cc/happyareabean/sjm/utils/CommandUtils.java b/src/main/java/cc/happyareabean/sjm/utils/CommandUtils.java new file mode 100644 index 0000000..f28953b --- /dev/null +++ b/src/main/java/cc/happyareabean/sjm/utils/CommandUtils.java @@ -0,0 +1,105 @@ +package cc.happyareabean.sjm.utils; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; +import org.jetbrains.annotations.Nullable; +import revxrsal.commands.bukkit.actor.BukkitCommandActor; +import revxrsal.commands.command.ExecutableCommand; +import revxrsal.commands.help.Help; + +import java.util.ArrayList; +import java.util.List; + +import static cc.happyareabean.sjm.SimpleJoinMessage.LEGACY_SERIALIZER; +import static cc.happyareabean.sjm.SimpleJoinMessage.MINIMESSAGE; +import static cc.happyareabean.sjm.utils.Constants.PAGE_TEXT; +import static net.kyori.adventure.text.Component.text; +import static net.kyori.adventure.text.event.ClickEvent.openUrl; + +public class CommandUtils { + + public static void handleHelpMenu(BukkitCommandActor actor, + int page, + Help.CommandList commands, + int elementsPerPage, + @Nullable String filterFor) { + List> list = new ArrayList<>(); + + if (filterFor != null) { + for (ExecutableCommand command : commands.all()) { + String usage = command.usage(); + if (!usage.startsWith(filterFor)) continue; + list.add(command); + } + } else { + list = commands.all(); + } + + List> commandList = Help.paginate(list, page, elementsPerPage); + + TextComponent.Builder builder = Component.text(); + + builder.append(Constants.HEADER); + builder.appendNewline(); + builder.append(MINIMESSAGE.deserialize(String.format("SimpleJoinMessage %s", + Constants.VERSION)) + .clickEvent(openUrl("https://go.happyareabean.cc/sjm")) + .hoverEvent(MINIMESSAGE.deserialize("click me!"))); + builder.appendNewline(); + builder.append(text() + .content("By ") + .color(NamedTextColor.GRAY) + .append(text("HappyAreaBean", NamedTextColor.GREEN)).build()); + builder.appendNewline(); + builder.append(Constants.HEADER); + builder.appendNewline(); + + commandList.forEach(command -> { + builder.append(LEGACY_SERIALIZER.deserialize(String.format(" &8• &e/%s %s", command.usage(), (command.description() != null ? "&7- &f" + command.description() : "")))); + builder.appendNewline(); + }); + + int numberOfPages = Help.numberOfPages(list.size(), elementsPerPage); + if (numberOfPages > 1) { + builder.appendNewline(); + builder.append(paginateNavigation(page, numberOfPages, Constants.HELP_COMMAND_FORMAT)); + builder.appendNewline(); + } + + builder.append(Constants.HEADER); + + actor.reply(builder.build()); + } + + public static void handleHelpMenu(BukkitCommandActor actor, int page, Help.CommandList commands, int elementsPerPage) { + handleHelpMenu(actor, page, commands, elementsPerPage, null); + } + + public static Component paginateNavigation(int currentPage, int maxPage, String commandFormat) { + int previousPage = currentPage - 1; + int nextPage = currentPage + 1; + + boolean havePreviousPage = previousPage != 0; + boolean haveNextPage = maxPage != currentPage; + + TextComponent.Builder pageText = text() + .decorate(TextDecoration.BOLD) + .color(NamedTextColor.YELLOW); + + pageText.append(text("«", !havePreviousPage ? NamedTextColor.DARK_GRAY : null) + .clickEvent(havePreviousPage ? ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, String.format(commandFormat, previousPage)) : null) + .hoverEvent(havePreviousPage ? text(String.format(PAGE_TEXT, previousPage)).color(NamedTextColor.GOLD) : null)); + + pageText.append(text(" ▍ ")); + + pageText.append(text("»", !haveNextPage ? NamedTextColor.DARK_GRAY : null) + .clickEvent(haveNextPage ? ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, String.format(commandFormat, nextPage)) : null) + .hoverEvent(haveNextPage ? text(String.format(PAGE_TEXT, nextPage)).color(NamedTextColor.GOLD) : null)); + + return pageText.build(); + } + +} diff --git a/src/main/java/cc/happyareabean/sjm/utils/Constants.java b/src/main/java/cc/happyareabean/sjm/utils/Constants.java index 257de52..88d15be 100644 --- a/src/main/java/cc/happyareabean/sjm/utils/Constants.java +++ b/src/main/java/cc/happyareabean/sjm/utils/Constants.java @@ -13,4 +13,7 @@ public class Constants { public static final String VERSION = SimpleJoinMessage.getInstance().getDescription().getVersion(); public static final String USER_AGENT = "SimpleJoinMessage/" + VERSION; + + public static final String HELP_COMMAND_FORMAT = "/sjm %s"; + public static final String PAGE_TEXT = "Page %s"; }