Added new PluginMain

Added PluginConfig
This commit is contained in:
2022-06-18 14:21:39 +08:00
parent afd06d7319
commit 202a80ce45
5 changed files with 137 additions and 40 deletions

View File

@ -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<Event>,
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<Mod>): Message {
private suspend fun MessageEvent.handleModsSearchResult(pagedList: PagedList<Mod>): 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<File>): Message {
private suspend fun handleModFileList(pagedList: PagedList<File>): 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消息")
}
}

View File

@ -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<Mod> =
doSearch(CLASS_ID_WORLDS, filter)
/**
* 搜索资源包材质包光影之类的
*/
suspend fun searchResourcePacks(filter: String): PagedList<Mod> =
doSearch(CLASS_ID_RESOURCE_PACKS, filter)
/**
* 搜索整合包
*/
suspend fun searchModPacks(filter: String): PagedList<Mod> =
doSearch(CLASS_ID_MODPACKS, filter)
/**
* 搜索mod
*/
suspend fun searchMods(filter: String): PagedList<Mod> =
doSearch(CLASS_ID_MODS, filter)
/**
* 根据分类与过滤器进行搜索返回分页的列表
* @param classId 类别ID
* @param modClass mod分类
* @param filter 过滤器
* @return 模组分页列表
*/
private suspend fun doSearch(classId: Int, filter: String): PagedList<Mod> =
fun search(modClass: ModClass, filter: String): PagedList<Mod> =
PagedList(DEFAULT_PAGE_SIZE) { index ->
val response = api.searchMods(
GAME_ID_MINECRAFT,
classId,
modClass.classId,
searchFilter = filter,
sortField = DEFAULT_SORT_FIELD,
sortOrder = SortOrder.DESC,

View File

@ -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<MinecraftService.ModClass, String> 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 ",
)
)
}

View File

@ -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" }
}
}

View File

@ -1 +1 @@
me.jie65535.jcf.JCurseforge
top.jie65535.jcf.PluginMain