add usage

This commit is contained in:
jiahua.liu 2020-02-25 17:52:25 +08:00
parent 6cf563c8e5
commit e42d7ff7a1
3 changed files with 40 additions and 13 deletions

View File

@ -156,6 +156,8 @@ interface Command {
val name: String
val alias: List<String>
val description: String
val usage: String
suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean
fun register()
}
@ -163,7 +165,8 @@ interface Command {
abstract class BlockingCommand(
override val name: String,
override val alias: List<String> = listOf(),
override val description: String = ""
override val description: String = "",
override val usage: String = ""
) : Command {
/**
* 最高优先级监听器
@ -186,6 +189,7 @@ class AnonymousCommand internal constructor(
override val name: String,
override val alias: List<String>,
override val description: String,
override val usage: String = "",
val onCommand: suspend CommandSender.(args: List<String>) -> Boolean
) : Command {
override suspend fun onCommand(sender: CommandSender, args: List<String>): Boolean {
@ -201,6 +205,7 @@ class CommandBuilder internal constructor() {
var name: String? = null
var alias: List<String>? = null
var description: String = ""
var usage: String = "use /help for help"
var onCommand: (suspend CommandSender.(args: List<String>) -> Boolean)? = null
fun onCommand(commandProcess: suspend CommandSender.(args: List<String>) -> Boolean) {
@ -218,6 +223,7 @@ class CommandBuilder internal constructor() {
name!!,
alias!!,
description,
usage,
onCommand!!
).also { it.register() }
}

View File

@ -31,6 +31,9 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
private val supervisorJob = SupervisorJob()
final override val coroutineContext: CoroutineContext = coroutineContext + supervisorJob
/**
* 插件被分配的data folder 如果插件改名了 data folder 也会变 请注意
*/
val dataFolder: File by lazy {
File(PluginManager.pluginsPath + pluginDescription.name).also { it.mkdir() }
}
@ -68,12 +71,14 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
this.onEnable()
}
/**
* 加载一个data folder中的Config
* 这个config是read-write的
*/
fun loadConfig(fileName: String): Config {
return Config.load(File(fileName))
return Config.load(dataFolder.absolutePath + fileName)
}
@JvmOverloads
internal fun disable(throwable: CancellationException? = null) {
this.coroutineContext[Job]!!.cancelChildren(throwable)
@ -87,7 +92,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
this.onLoad()
}
fun getPluginManager() = PluginManager
val pluginManager = PluginManager
val logger: MiraiLogger by lazy {
SimpleLogger("Plugin ${pluginDescription.name}") { _, message, e ->
@ -99,15 +104,31 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
}
}
/**
* 加载一个插件jar, resources中的东西
*/
fun getResources(fileName: String): InputStream? {
return PluginManager.getFileInJarByName(
this.pluginDescription.name,
fileName
)
return try {
this.javaClass.classLoader.getResourceAsStream(fileName)
} catch (e: Exception) {
PluginManager.getFileInJarByName(
this.pluginDescription.name,
fileName
)
}
}
/**
* 加载一个插件jar, resources中的Config
* 这个Config是read-only的
*/
fun getResourcesConfig(fileName: String): Config {
if (fileName.contains(".")) {
error("Unknown Config Type")
}
return Config.load(getResources(fileName) ?: error("Config Not Found"), fileName.split(".")[1])
}
//fun getResourcesConfig()
}
class PluginDescription(
@ -370,7 +391,7 @@ object PluginManager {
/**
* 根据插件名字找Jar resources中的文件
* 根据插件名字找Jar中的文件
* null => 没找到
*/
fun getFileInJarByName(pluginName: String, toFind: String): InputStream? {

View File

@ -134,7 +134,7 @@ class ImageSenderMain : PluginBase() {
logger.info("loading local image data")
try {
images = Config.load(getResources(fileName = "data.yml")!!, "yml")
images = getResourcesConfig("data.yml")
} catch (e: Exception) {
e.printStackTrace()
logger.info("无法加载本地图片")