Skip to content

Commit

Permalink
Add config reload command, fix #25
Browse files Browse the repository at this point in the history
  • Loading branch information
Under-estimate committed Jun 12, 2024
1 parent dc0c4f7 commit 3b98a6d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@
获取MC服务器信息: `org.zrnq.mcmotd:command.mcp`
绑定服务器到群聊: `org.zrnq.mcmotd:command.mcadd`
删除群聊绑定的服务器: `org.zrnq.mcmotd:command.mcdel`
启动/停止服务器的在线人数记录功能: `org.zrnq.mcmotd:command.mcrec`
获取http API访问计数: `org.zrnq.mcmotd:command.mcapi`
启动/停止服务器的在线人数记录功能: `org.zrnq.mcmotd:command.mcrec`
获取http API访问计数: `org.zrnq.mcmotd:command.mcapi`
重载插件配置: `org.zrnq.mcmotd:command.mcreload`
## 插件命令
> /mcp (服务器地址/服务器名称) : 查询指定地址或绑定到指定名称上的服务器信息,当本群仅绑定了一个服务器时可省略参数。
其中,服务器地址可以仅有域名,如`mc.example.com`,也可以带有端口号,如`mc.example.com:12345`
> /mcp (服务器地址/服务器名称) : 查询指定地址或绑定到指定名称上的服务器信息,当本群仅绑定了一个服务器时可省略参数。
>
> 其中,服务器地址可以仅有域名,如`mc.example.com`,也可以带有端口号,如`mc.example.com:12345`
>
> 若服务器的玩家列表信息需要通过[Query协议](https://wiki.vg/Query)获取,则需要同时指定服务器连接端口和Query端口,如`mc.example.com:12345:23456`
> /mcadd <服务器名称> <服务器地址> : 将指定地址的服务器绑定到指定名称上。各个群聊绑定的服务器相互独立。
> /mcdel <服务器名称> : 删除指定名称的服务器
> /mcrec <服务器地址> (true/false) : 启动/停止对于指定服务器的在线人数记录,仅有启用了在线人数记录的服务器才会在查询结果图片中附加历史在线人数信息
> /mcapi : 获取http API访问计数信息(需要启用http API访问计数功能)
> /mcapi : 获取http API访问计数信息(需要启用http API访问计数功能)
> /mcreload : 重载插件配置
## 插件配置
插件的配置文件位于`/config/org.zrnq.mcmotd/mcmotd.yml`

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "org.zrnq"
version = "1.1.19"
version = "1.1.20"
val ktor_version = "2.3.1"

repositories {
Expand Down
19 changes: 7 additions & 12 deletions src/main/kotlin/org/zrnq/mcmotd/McMotd.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.alibaba.fastjson.parser.ParserConfig
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import net.mamoe.mirai.console.command.Command
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregister
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
Expand All @@ -20,40 +21,34 @@ object McMotd : KotlinPlugin(
JvmPluginDescription(
id = "org.zrnq.mcmotd",
name = "Minecraft MOTD Fetcher",
version = "1.1.19",
version = "1.1.20",
) {
author("ZRnQ")
info("""以图片的形式获取指定Minecraft服务器的基本信息""")
}
) {
private lateinit var commandList : List<Command>
override fun onEnable() {
miraiLogger = logger
logger.info { "McMotd is loading" }
ParserConfig.getGlobalInstance().isSafeMode = true
PluginConfig.reload()
PluginData.reload()
QueryCommand.register()
BindCommand.register()
DelCommand.register()
RecordCommand.register()
HttpServerCommand.register()
commandList = listOf(QueryCommand, BindCommand, DelCommand, RecordCommand, HttpServerCommand, ConfigReloadCommand)
commandList.forEach { it.register() }
MClientOptions.loadPluginConfig()
startRecord()
configureHttpServer()
}

override fun onDisable() {
logger.info { "McMotd is unloading" }
QueryCommand.unregister()
BindCommand.unregister()
DelCommand.unregister()
RecordCommand.unregister()
HttpServerCommand.unregister()
commandList.forEach { it.unregister() }
stopRecord()
stopHttpServer()
}

private lateinit var timer : Timer
lateinit var timer : Timer

private fun startRecord() {
timer = Timer()
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/org/zrnq/mcmotd/QueryCommand.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package org.zrnq.mcmotd

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import net.mamoe.mirai.console.command.CommandSender
import net.mamoe.mirai.console.command.MemberCommandSender
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.util.sendAnsiMessage
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.utils.ExternalResource.Companion.toExternalResource
import org.zrnq.mclient.MClientOptions
import org.zrnq.mclient.output.APIOutputHandler
import org.zrnq.mclient.parseAddress
import org.zrnq.mclient.renderBasicInfoImage
import org.zrnq.mclient.secondToReadableTime
import org.zrnq.mcmotd.ImageUtil.appendPlayerHistory
import org.zrnq.mcmotd.McMotd.reload
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
Expand Down Expand Up @@ -152,6 +155,23 @@ object HttpServerCommand : SimpleCommand(McMotd, "mcapi", description = "获取H
}
}

@Suppress("unused")
object ConfigReloadCommand : SimpleCommand(McMotd, "mcreload", description = "重载插件配置") {
@Handler
suspend fun CommandSender.handle() {
// run inside McMotd.timer to avoid ConcurrentModification with player history recording.
McMotd.timer.schedule(object : TimerTask() {
override fun run() {
PluginConfig.reload()
MClientOptions.loadPluginConfig()
runBlocking {
this@handle.reply("配置重载完成")
}
}
}, 0)
}
}

private suspend fun CommandSender.reply(message : String) {
if(user == null) sendAnsiMessage { lightPurple().append(message) }
else sendMessage(At(user!!.id) + message)
Expand Down

0 comments on commit 3b98a6d

Please sign in to comment.