From 202a80ce453e3947d36f8658107517dcba4b8d9f Mon Sep 17 00:00:00 2001 From: jie65535 Date: Sat, 18 Jun 2022 14:21:39 +0800 Subject: [PATCH] Added new PluginMain Added PluginConfig --- .../kotlin/top/jie65535/jcf/MessageHandler.kt | 42 ++++++++-- .../top/jie65535/jcf/MinecraftService.kt | 77 +++++++++++-------- .../kotlin/top/jie65535/jcf/PluginConfig.kt | 23 ++++++ .../kotlin/top/jie65535/jcf/PluginMain.kt | 33 ++++++++ ...t.mamoe.mirai.console.plugin.jvm.JvmPlugin | 2 +- 5 files changed, 137 insertions(+), 40 deletions(-) create mode 100644 src/main/kotlin/top/jie65535/jcf/PluginConfig.kt create mode 100644 src/main/kotlin/top/jie65535/jcf/PluginMain.kt diff --git a/src/main/kotlin/top/jie65535/jcf/MessageHandler.kt b/src/main/kotlin/top/jie65535/jcf/MessageHandler.kt index 02cbfee..be771d0 100644 --- a/src/main/kotlin/top/jie65535/jcf/MessageHandler.kt +++ b/src/main/kotlin/top/jie65535/jcf/MessageHandler.kt @@ -1,16 +1,48 @@ package top.jie65535.jcf +import net.mamoe.mirai.event.Event +import net.mamoe.mirai.event.EventChannel +import net.mamoe.mirai.event.events.MessageEvent +import net.mamoe.mirai.event.subscribeMessages import net.mamoe.mirai.message.data.Message +import net.mamoe.mirai.message.data.MessageSource.Key.quote +import net.mamoe.mirai.utils.MiraiLogger import top.jie65535.jcf.model.file.File import top.jie65535.jcf.model.mod.Mod import top.jie65535.jcf.util.PagedList -object MessageHandler { +class MessageHandler( + private val service: MinecraftService, + private val eventChannel: EventChannel, + private val logger: MiraiLogger +) { + + fun startListen() { + eventChannel.subscribeMessages { + for ((modClass, command) in PluginConfig.searchCommands) { + if (command.isBlank()) continue + startsWith(command) { + val filter = it.trim() + if (filter.isEmpty()) { + subject.sendMessage(message.quote() + "必须输入关键字") + } else { + try { + val pagedList = service.search(modClass, filter) + handleModsSearchResult(pagedList) + } catch (e: Throwable) { + subject.sendMessage(message.quote() + "发生内部错误,请稍后重试") + logger.error("消息\"$message\"引发异常", e) + } + } + } + } + } + } /** * 处理mod搜索结果 */ - suspend fun handleModsSearchResult(pagedList: PagedList): Message { + private suspend fun MessageEvent.handleModsSearchResult(pagedList: PagedList): Message { val list = pagedList.current() // mod list val hasNext = pagedList.hasNext // 是否有下一页 val hasPrev = pagedList.hasPrev // 是否有上一页 @@ -20,14 +52,14 @@ object MessageHandler { /** * 处理展示单个mod */ - fun handleShowMod(mod: Mod): Message { + private fun handleShowMod(mod: Mod): Message { TODO("将mod转为QQ消息") } /** * 处理模组文件列表 */ - suspend fun handleModFileList(pagedList: PagedList): Message { + private suspend fun handleModFileList(pagedList: PagedList): Message { val list = pagedList.current() // mod list val hasNext = pagedList.hasNext // 是否有下一页 val hasPrev = pagedList.hasPrev // 是否有上一页 @@ -38,7 +70,7 @@ object MessageHandler { * 处理模组文件更改日志 * @param changelog 更改日志(HTML) */ - fun handleModFileChangelog(changelog: String): Message { + private fun handleModFileChangelog(changelog: String): Message { TODO("将文件更改日志渲染为QQ消息") } } \ No newline at end of file diff --git a/src/main/kotlin/top/jie65535/jcf/MinecraftService.kt b/src/main/kotlin/top/jie65535/jcf/MinecraftService.kt index aa44b24..6a0fdce 100644 --- a/src/main/kotlin/top/jie65535/jcf/MinecraftService.kt +++ b/src/main/kotlin/top/jie65535/jcf/MinecraftService.kt @@ -9,57 +9,66 @@ import top.jie65535.jcf.util.PagedList class MinecraftService(apiKey: String) { companion object { private const val GAME_ID_MINECRAFT = 432 - private const val CLASS_ID_WORLDS = 17 - private const val CLASS_ID_BUKKIT_PLUGINS = 5 - private const val CLASS_ID_CUSTOMIZATION = 4546 - private const val CLASS_ID_MODPACKS = 4471 - private const val CLASS_ID_RESOURCE_PACKS = 12 - private const val CLASS_ID_ADDONS = 4559 - private const val CLASS_ID_MODS = 6 private const val DEFAULT_PAGE_SIZE = 20 private val DEFAULT_SORT_FIELD = ModsSearchSortField.Popularity } + /** + * mod分类 + */ + enum class ModClass(val classId: Int) { + /** + * 存档 + */ + WORLDS(17), + + /** + * 水桶服插件 + */ + BUKKIT_PLUGINS(5), + + /** + * 自定义 + */ + CUSTOMIZATION(4546), + + /** + * 整合包 + */ + MODPACKS(4471), + + /** + * 资源包 + */ + RESOURCE_PACKS(12), + + /** + * 附加 + */ + ADDONS(4559), + + /** + * 模组 + */ + MODS(6); + } + /** * api客户端实例 */ private val api = CurseforgeApi(apiKey) - /** - * 搜索存档 - */ - suspend fun searchWords(filter: String): PagedList = - doSearch(CLASS_ID_WORLDS, filter) - - /** - * 搜索资源包(材质包、光影之类的) - */ - suspend fun searchResourcePacks(filter: String): PagedList = - doSearch(CLASS_ID_RESOURCE_PACKS, filter) - - /** - * 搜索整合包 - */ - suspend fun searchModPacks(filter: String): PagedList = - doSearch(CLASS_ID_MODPACKS, filter) - - /** - * 搜索mod - */ - suspend fun searchMods(filter: String): PagedList = - doSearch(CLASS_ID_MODS, filter) - /** * 根据分类与过滤器进行搜索,返回分页的列表 - * @param classId 类别ID + * @param modClass mod分类 * @param filter 过滤器 * @return 模组分页列表 */ - private suspend fun doSearch(classId: Int, filter: String): PagedList = + fun search(modClass: ModClass, filter: String): PagedList = PagedList(DEFAULT_PAGE_SIZE) { index -> val response = api.searchMods( GAME_ID_MINECRAFT, - classId, + modClass.classId, searchFilter = filter, sortField = DEFAULT_SORT_FIELD, sortOrder = SortOrder.DESC, diff --git a/src/main/kotlin/top/jie65535/jcf/PluginConfig.kt b/src/main/kotlin/top/jie65535/jcf/PluginConfig.kt new file mode 100644 index 0000000..3ad1dfe --- /dev/null +++ b/src/main/kotlin/top/jie65535/jcf/PluginConfig.kt @@ -0,0 +1,23 @@ +package top.jie65535.jcf + +import net.mamoe.mirai.console.data.AutoSavePluginConfig +import net.mamoe.mirai.console.data.ValueDescription +import net.mamoe.mirai.console.data.value + +object PluginConfig : AutoSavePluginConfig("jcf") { + @ValueDescription("Curseforge API KEY") + val apiKey: String by value() + + @ValueDescription("搜索命令 (MODS,MODPACKS,RESOURCE_PACKS,WORLDS,BUKKIT_PLUGINS,ADDONS,CUSTOMIZATION)") + val searchCommands: MutableMap by value( + mutableMapOf( + MinecraftService.ModClass.MODS to "jcfmod ", + MinecraftService.ModClass.MODPACKS to "jcfpack ", + MinecraftService.ModClass.RESOURCE_PACKS to "jcfres ", + MinecraftService.ModClass.WORLDS to "jcfword ", + MinecraftService.ModClass.BUKKIT_PLUGINS to "jcfbukkit ", + MinecraftService.ModClass.ADDONS to "jcfaddon ", + MinecraftService.ModClass.CUSTOMIZATION to "jcfcustom ", + ) + ) +} diff --git a/src/main/kotlin/top/jie65535/jcf/PluginMain.kt b/src/main/kotlin/top/jie65535/jcf/PluginMain.kt new file mode 100644 index 0000000..0f99ccf --- /dev/null +++ b/src/main/kotlin/top/jie65535/jcf/PluginMain.kt @@ -0,0 +1,33 @@ +package top.jie65535.jcf + +import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription +import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin +import net.mamoe.mirai.event.GlobalEventChannel +import net.mamoe.mirai.utils.info + +object PluginMain: KotlinPlugin( + JvmPluginDescription( + id = "top.jie65535.jcf", + name = "J Curseforge Util", + version = "1.0.0", + ) { + author("jie65535") + info("MC Curseforge Util\n" + + "https://github.com/jie65535/mirai-console-jcf-plugin") + } +) { + override fun onEnable() { + logger.info { "Plugin loaded" } + PluginConfig.reload() + + if (PluginConfig.apiKey.isBlank()) { + logger.error("必须配置 Curseforge Api Key 才可以使用本插件!") + return + } + val service = MinecraftService(PluginConfig.apiKey) + val eventChannel = GlobalEventChannel.parentScope(this) + val messageHandler = MessageHandler(service, eventChannel, logger) + messageHandler.startListen() + logger.info { "Plugin Enabled" } + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin b/src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin index 8de3fbb..104d368 100644 --- a/src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin +++ b/src/main/resources/META-INF/services/net.mamoe.mirai.console.plugin.jvm.JvmPlugin @@ -1 +1 @@ -me.jie65535.jcf.JCurseforge \ No newline at end of file +top.jie65535.jcf.PluginMain \ No newline at end of file