From ea2cb47c07628abeace3b2d4ac4b3f16b88e382a Mon Sep 17 00:00:00 2001 From: Brady Date: Sun, 26 May 2024 10:44:08 -0230 Subject: Improvement: Cleanup help command (#1720) --- .../hannibal2/skyhanni/config/commands/Commands.kt | 63 +------------ .../skyhanni/features/commands/HelpCommand.kt | 105 +++++++++++++++++++++ 2 files changed, 107 insertions(+), 61 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/features/commands/HelpCommand.kt (limited to 'src/main/java/at/hannibal2/skyhanni') diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt index ddadb8177..d0fd99dd0 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt @@ -17,6 +17,7 @@ import at.hannibal2.skyhanni.features.bingo.card.nextstephelper.BingoNextStepHel import at.hannibal2.skyhanni.features.chat.Translator import at.hannibal2.skyhanni.features.combat.endernodetracker.EnderNodeTracker import at.hannibal2.skyhanni.features.combat.ghostcounter.GhostUtil +import at.hannibal2.skyhanni.features.commands.HelpCommand import at.hannibal2.skyhanni.features.commands.PartyChatCommands import at.hannibal2.skyhanni.features.commands.PartyCommands import at.hannibal2.skyhanni.features.commands.WikiManager @@ -80,16 +81,11 @@ import at.hannibal2.skyhanni.utils.APIUtil import at.hannibal2.skyhanni.utils.ChatUtils import at.hannibal2.skyhanni.utils.LorenzUtils import at.hannibal2.skyhanni.utils.SoundUtils -import at.hannibal2.skyhanni.utils.StringUtils.splitLines import at.hannibal2.skyhanni.utils.TabListData import at.hannibal2.skyhanni.utils.chat.ChatClickActionManager -import at.hannibal2.skyhanni.utils.chat.Text -import at.hannibal2.skyhanni.utils.chat.Text.hover -import at.hannibal2.skyhanni.utils.chat.Text.suggest import at.hannibal2.skyhanni.utils.repopatterns.RepoPatternGui import net.minecraft.command.ICommandSender import net.minecraft.util.BlockPos -import net.minecraft.util.ChatComponentText import net.minecraftforge.client.ClientCommandHandler object Commands { @@ -156,7 +152,7 @@ object Commands { registerCommand("sh", "Opens the main SkyHanni config", openMainMenu) registerCommand("skyhanni", "Opens the main SkyHanni config", openMainMenu) registerCommand("ff", "Opens the Farming Fortune Guide") { openFortuneGuide() } - registerCommand("shcommands", "Shows this list") { commandHelp(it) } + registerCommand("shcommands", "Shows this list") { HelpCommand.onCommand(it, commands) } registerCommand0("shdefaultoptions", "Select default options", { DefaultConfigFeatures.onCommand( it.getOrNull(0) ?: "null", it.getOrNull(1) ?: "null" @@ -551,61 +547,6 @@ object Commands { registerCommand("pd", "Disbands the party") { PartyCommands.disband() } } - private fun commandHelp(args: Array) { - var filter: (String) -> Boolean = { true } - val title: String - if (args.size == 1) { - val searchTerm = args[0].lowercase() - filter = { it.lowercase().contains(searchTerm) } - title = "SkyHanni commands with '§e$searchTerm§7'" - } else { - title = "All SkyHanni commands" - } - - val components = mutableListOf() - components.add(ChatComponentText(" \n§7$title:\n")) - - for (command in commands) { - if (!filter(command.name) && !filter(command.description)) continue - val category = command.category - val name = command.name - val color = category.color - - val hoverText = buildList { - add("§e/$name") - if (command.description.isNotEmpty()) { - addDescription(command.description) - } - add("") - add("$color${category.categoryName}") - addDescription(category.description) - } - - components.add(Text.text("$color/$name") { - this.hover = Text.multiline(hoverText) - this.suggest = "/$name" - }) - components.add(ChatComponentText("§7, ")) - } - components.add(ChatComponentText("\n ")) - ChatUtils.multiComponentMessage(components) - } - - private fun MutableList.addDescription(description: String) { - val lines = description.splitLines(200).removeSuffix("§r").replace("§r", "§7").addOptionalDot() - for (line in lines.split("\n")) { - add(" §7${line}") - } - } - - private fun String.addOptionalDot(): String { - if (endsWith(".")) return this - if (endsWith("?")) return this - if (endsWith("!")) return this - - return "$this." - } - @JvmStatic fun openFortuneGuide() { if (!LorenzUtils.inSkyBlock) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/commands/HelpCommand.kt b/src/main/java/at/hannibal2/skyhanni/features/commands/HelpCommand.kt new file mode 100644 index 000000000..3500b0025 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/commands/HelpCommand.kt @@ -0,0 +1,105 @@ +package at.hannibal2.skyhanni.features.commands + +import at.hannibal2.skyhanni.config.commands.Commands +import at.hannibal2.skyhanni.utils.StringUtils.splitLines +import at.hannibal2.skyhanni.utils.chat.Text +import at.hannibal2.skyhanni.utils.chat.Text.asComponent +import at.hannibal2.skyhanni.utils.chat.Text.center +import at.hannibal2.skyhanni.utils.chat.Text.fitToChat +import at.hannibal2.skyhanni.utils.chat.Text.hover +import at.hannibal2.skyhanni.utils.chat.Text.onClick +import at.hannibal2.skyhanni.utils.chat.Text.send +import at.hannibal2.skyhanni.utils.chat.Text.style +import at.hannibal2.skyhanni.utils.chat.Text.suggest +import net.minecraft.util.EnumChatFormatting +import net.minecraft.util.IChatComponent + +object HelpCommand { + + private const val COMMANDS_PER_PAGE = 15 + private const val HELP_ID = -6457563 + + private fun createDivider() = Text.HYPHEN.fitToChat().style { + strikethrough = true + color = EnumChatFormatting.BLUE + } + + private fun createCommandEntry(command: Commands.CommandInfo): IChatComponent { + val category = command.category + val color = category.color + val description = command.description.splitLines(200).replace("§r", "§7") + val categoryDescription = category.description.splitLines(200).replace("§r", "§7") + + return Text.text("§7 - $color${command.name}") { + this.hover = Text.multiline( + "§e/${command.name}", + if (description.isNotEmpty()) description.prependIndent(" ") else null, + "", + "$color§l${category.categoryName}", + categoryDescription.prependIndent(" ") + ) + this.suggest = "/${command.name}" + } + } + + private fun showPage( + page: Int, + search: String, + commands: List + ) { + val filtered = commands.filter { + it.name.contains(search, ignoreCase = true) || it.description.contains(search, ignoreCase = true) + } + val maxPage = filtered.size / COMMANDS_PER_PAGE + 1 + val page = page.coerceIn(1, maxPage) + val title = if (search.isEmpty()) "§6SkyHanni Commands" else "§6SkyHanni Commands matching '$search'" + + val text = mutableListOf() + + text.add(createDivider()) + text.add(title.asComponent().center()) + text.add(Text.join( + if (page > 1) "§6§l<<".asComponent { + this.hover = "§eClick to view page ${page - 1}".asComponent() + this.onClick { showPage(page - 1, search, commands) } + } else null, + " ", + "§6(Page $page of $maxPage)", + " ", + if (page < maxPage) "§6§l>>".asComponent { + this.hover = "§eClick to view page ${page + 1}".asComponent() + this.onClick { showPage(page + 1, search, commands) } + } else null + ).center()) + text.add(createDivider()) + + if (filtered.isEmpty()) { + text.add(Text.EMPTY) + text.add("§cNo reminders found.".asComponent().center()) + text.add(Text.EMPTY) + } else { + val start = (page - 1) * COMMANDS_PER_PAGE + val end = (page * COMMANDS_PER_PAGE).coerceAtMost(filtered.size) + for (i in start until end) { + text.add(createCommandEntry(filtered[i])) + } + } + + text.add(createDivider()) + + Text.multiline(text).send(HELP_ID) + } + + fun onCommand(args: Array, commands: List) { + val page: Int + val search: String + if (args.firstOrNull() == "-p") { + page = args.getOrNull(1)?.toIntOrNull() ?: 1 + search = args.drop(2).joinToString(" ") + } else { + page = 1 + search = args.joinToString(" ") + } + showPage(page, search, commands) + } +} -- cgit