diff --git a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java index 2ff86acb..2f4a1ac1 100644 --- a/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java +++ b/bukkit/src/main/java/net/william278/huskhomes/BukkitHuskHomes.java @@ -4,6 +4,7 @@ import io.papermc.lib.PaperLib; import net.william278.annotaml.Annotaml; import net.william278.huskhomes.command.BukkitCommand; import net.william278.huskhomes.command.BukkitCommandType; +import net.william278.huskhomes.command.CommandBase; import net.william278.huskhomes.command.DisabledCommand; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; @@ -64,6 +65,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { private RtpEngine rtpEngine; private Spawn serverSpawn; private Set pluginHooks; + private List registeredCommands; @Nullable private NetworkMessenger networkMessenger; @@ -175,6 +177,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { }))); // Register commands + this.registeredCommands = new ArrayList<>(); Arrays.stream(BukkitCommandType.values()).forEach(commandType -> { final PluginCommand pluginCommand = getCommand(commandType.commandBase.command); if (pluginCommand == null) { @@ -193,7 +196,9 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { } // Otherwise, register the command - new BukkitCommand(commandType.commandBase, this).register(pluginCommand); + final CommandBase commandBase = commandType.commandBase; + this.registeredCommands.add(commandBase); + new BukkitCommand(commandBase, this).register(pluginCommand); }); getLoggingAdapter().log(Level.INFO, "Successfully registered permissions & commands."); @@ -351,6 +356,11 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes { return Version.fromString(getDescription().getVersion()); } + @Override + public @NotNull List getCommands() { + return registeredCommands; + } + /** * Returns the {@link Server} the plugin is on * diff --git a/common/src/main/java/net/william278/huskhomes/Cache.java b/common/src/main/java/net/william278/huskhomes/Cache.java index b6b46f56..9a85e5f3 100644 --- a/common/src/main/java/net/william278/huskhomes/Cache.java +++ b/common/src/main/java/net/william278/huskhomes/Cache.java @@ -1,6 +1,7 @@ package net.william278.huskhomes; import de.themoep.minedown.MineDown; +import net.william278.huskhomes.command.CommandBase; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.database.Database; import net.william278.huskhomes.player.OnlineUser; @@ -105,6 +106,7 @@ public class Cache { } } + @NotNull private ListOptions.Builder getBaseList(@NotNull Locales locales, int itemsPerPage) { return new ListOptions.Builder().setFooterFormat(locales.getRawLocale("list_footer", "%previous_page_button%", "%current_page%", @@ -126,6 +128,7 @@ public class Cache { .setEscapeItemsMineDown(false); } + @NotNull public MineDown getHomeList(@NotNull OnlineUser onlineUser, @NotNull User listOwner, @NotNull Locales locales, @NotNull List homes, final int itemsPerPage, final int page) { final PaginatedList homeList = PaginatedList.of(homes.stream().map(home -> @@ -142,6 +145,23 @@ public class Cache { return homeList.getNearestValidPage(page); } + @NotNull + public MineDown getCommandList(@NotNull Locales locales, @NotNull List commands, + final int itemsPerPage, final int page) { + return PaginatedList.of(commands.stream().map(command -> locales.getRawLocale("command_list_item", + Locales.escapeMineDown(command.command), + Locales.escapeMineDown(command.getDescription().length() > 50 + ? command.getDescription().substring(0, 49).trim() + "…" + : command.getDescription()), + Locales.escapeMineDown(locales.formatDescription(command.getDescription()))) + .orElse(command.command)) + .collect(Collectors.toList()), + getBaseList(locales, itemsPerPage) + .setHeaderFormat(locales.getRawLocale("command_list_title").orElse("")) + .setItemSeparator("\n").setCommand("/huskhomes:huskhomes help").build()) + .getNearestValidPage(page); + } + @NotNull public MineDown getPublicHomeList(@NotNull OnlineUser onlineUser, @NotNull Locales locales, @NotNull List publicHomes, final int itemsPerPage, final int page) { diff --git a/common/src/main/java/net/william278/huskhomes/HuskHomes.java b/common/src/main/java/net/william278/huskhomes/HuskHomes.java index ac0d235c..b7058bb3 100644 --- a/common/src/main/java/net/william278/huskhomes/HuskHomes.java +++ b/common/src/main/java/net/william278/huskhomes/HuskHomes.java @@ -1,5 +1,6 @@ package net.william278.huskhomes; +import net.william278.huskhomes.command.CommandBase; import net.william278.huskhomes.config.Locales; import net.william278.huskhomes.config.Settings; import net.william278.huskhomes.config.Spawn; @@ -228,10 +229,19 @@ public interface HuskHomes { @NotNull Version getPluginVersion(); + /** + * Returns a list of enabled commands + * + * @return A list of registered and enabled {@link CommandBase}s + */ + @NotNull + List getCommands(); + /** * Reloads the {@link Settings} and {@link Locales} from their respective config files * * @return a {@link CompletableFuture} that will be completed when the plugin reload is complete and if it was successful */ CompletableFuture reload(); + } diff --git a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java index a5299d13..a69cbd52 100644 --- a/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java +++ b/common/src/main/java/net/william278/huskhomes/command/HuskHomesCommand.java @@ -20,7 +20,7 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, protected HuskHomesCommand(@NotNull HuskHomes implementor) { super("huskhomes", Permission.COMMAND_HUSKHOMES, implementor); - aboutMenu = AboutMenu.create("HuskHomes") + this.aboutMenu = AboutMenu.create("HuskHomes") .withDescription("A powerful, intuitive and flexible teleportation suite") .withVersion(implementor.getPluginVersion()) .addAttribution("Author", @@ -48,10 +48,26 @@ public class HuskHomesCommand extends CommandBase implements ConsoleExecutable, sendAboutMenu(onlineUser); return; } + if (args.length > 2) { + plugin.getLocales().getLocale("error_invalid_syntax", "/huskhomes [about|help|reload|update]") + .ifPresent(onlineUser::sendMessage); + return; + } switch (args[0].toLowerCase()) { case "about" -> sendAboutMenu(onlineUser); case "help" -> { - // todo sendHelpMenu(onlineUser); + int page = 1; + if (args.length == 2) { + try { + page = Integer.parseInt(args[1]); + } catch (NumberFormatException ignored) { + plugin.getLocales().getLocale("error_invalid_syntax", "/huskhomes help ") + .ifPresent(onlineUser::sendMessage); + return; + } + } + onlineUser.sendMessage(plugin.getCache().getCommandList(plugin.getLocales(), plugin.getCommands(), + plugin.getSettings().listItemsPerPage, page)); } case "reload" -> { diff --git a/common/src/main/resources/locales/en-gb.yml b/common/src/main/resources/locales/en-gb.yml index 1b939cc8..ec4e6018 100644 --- a/common/src/main/resources/locales/en-gb.yml +++ b/common/src/main/resources/locales/en-gb.yml @@ -55,7 +55,7 @@ command_list_title: '[HuskHomes](#00fb9a bold) [| Command List](#00fb9a)' home_list_item: '[[%1%]](show_text=�fb9a&%1%\n&7ℹ %3% run_command=/huskhomes:edithome %2%)' public_home_list_item: '[[%1%]](show_text=�fb9a&%1%\n&7☻ %3%\n&7ℹ %4% run_command=/huskhomes:publichome %2%)' warp_list_item: '[[%1%]](show_text=�fb9a&%1%\n&7ℹ %2% run_command=/huskhomes:warp %1%)' -command_list_item: '[/%1%](#00fb9a show_text=�fb9a&%2% suggest_command=/%1%) [•](white) [%3%](gray)' +command_list_item: '[/%1%](#00fb9a show_text=&7Click to suggest command suggest_command=/%1%) [%2%](gray show_text=�fb9a&/%1%\n&7%3%)' list_item_divider: ' [•](gray) ' list_footer: '%1%[Page](#00fb9a) [%2%](#00fb9a)/[%3%](#00fb9a)%4% %5%'