mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Introduce JPluginConfig for Java callers, update docs
This commit is contained in:
parent
c821f9e9e9
commit
91e30377fc
@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2020 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.console.data
|
||||
|
||||
/**
|
||||
* 一个插件的配置数据, 用于和用户交互.
|
||||
*
|
||||
* 用户可通过 mirai-console 前端 (如在 Android 中可视化实现) 修改这些配置, 修改会自动写入这个对象中.
|
||||
*
|
||||
* **提示**:
|
||||
* 插件内部的数据应用 [PluginData] 存储, 而不能使用 [PluginConfig].
|
||||
*
|
||||
* ### 实现
|
||||
*
|
||||
* 在 [JPluginData] 的示例基础上, 修改类定义
|
||||
* ```java
|
||||
* // 原
|
||||
* public class AccountPluginData extends JPluginData {
|
||||
* // 修改为
|
||||
* public class AccountPluginConfig extends JPluginConfig {
|
||||
* ```
|
||||
* 即可将一个 [PluginData] 变更为 [PluginConfig].
|
||||
*
|
||||
* @see JPluginData
|
||||
* @see PluginConfig
|
||||
*/
|
||||
public abstract class JPluginConfig(delegate: PluginData) : JPluginData(delegate), PluginConfig
|
@ -19,6 +19,24 @@ import kotlin.annotation.AnnotationTarget.*
|
||||
*
|
||||
* **提示**:
|
||||
* 插件内部的数据应用 [PluginData] 存储, 而不能使用 [PluginConfig].
|
||||
*
|
||||
* ## 实现
|
||||
* 对使用者来说, [PluginConfig] 与 [PluginData] 实现几乎相同. 目前仅需在 [PluginData] 使用的基础上添加接口实现即可.
|
||||
*
|
||||
* ### Kotlin
|
||||
* 在 [PluginData] 的示例基础上, 修改对象定义
|
||||
* ```
|
||||
* // 原
|
||||
* object MyPluginData : PluginData by PluginMain.loadPluginData()
|
||||
* // 修改为
|
||||
* object MyPluginConfig : PluginConfig by PluginMain.loadPluginConfig()
|
||||
* ```
|
||||
* 即可将一个 [PluginData] 变更为 [PluginConfig].
|
||||
*
|
||||
* ### Java
|
||||
* 见 [JPluginConfig]
|
||||
*
|
||||
* @see PluginData
|
||||
*/
|
||||
@ExperimentalPluginConfig
|
||||
public interface PluginConfig : PluginData
|
||||
|
@ -47,10 +47,11 @@ import kotlin.reflect.KType
|
||||
* ```
|
||||
* object PluginMain : KotlinPlugin()
|
||||
*
|
||||
* object AccountPluginData : PluginData by PluginMain.loadPluginData() {
|
||||
* object MyPluginData : PluginData by PluginMain.loadPluginData() {
|
||||
* val list: MutableList<String> by value(mutableListOf("a", "b")) // mutableListOf("a", "b") 是初始值, 可以省略
|
||||
* val custom: Map<Long, CustomData> by value() // 使用 kotlinx-serialization 序列化的类型. (目前还不支持)
|
||||
* var custom2: CustomData by value() // 允许 var
|
||||
* var long: Long by value(0) // 允许 var
|
||||
* var int by value(0) // 可以使用类型推断, 但更推荐使用 `var long: Long by value(0)` 这种定义方式.
|
||||
* }
|
||||
*
|
||||
* @Serializable
|
||||
|
@ -16,9 +16,9 @@ import net.mamoe.mirai.console.data.PluginData
|
||||
import net.mamoe.mirai.console.data.SerializableValue.Companion.serializableValueWith
|
||||
import net.mamoe.mirai.console.data.SerializerAwareValue
|
||||
import net.mamoe.mirai.console.data.valueFromKType
|
||||
import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KType
|
||||
import kotlin.reflect.full.createInstance as createInstanceKotlin
|
||||
|
||||
private val primitiveCollectionsImplemented by lazy {
|
||||
false
|
||||
@ -101,24 +101,36 @@ internal fun PluginData.valueFromKTypeImpl(type: KType): SerializerAwareValue<*>
|
||||
|
||||
internal fun KClass<*>.createInstanceSmart(): Any? {
|
||||
return when (this) {
|
||||
Byte::class -> 0.toByte()
|
||||
Short::class -> 0.toShort()
|
||||
Int::class -> 0
|
||||
Long::class -> 0L
|
||||
Float::class -> 0.toFloat()
|
||||
Double::class -> 0.0
|
||||
|
||||
Boolean::class -> false
|
||||
|
||||
String::class -> ""
|
||||
|
||||
MutableMap::class,
|
||||
Map::class,
|
||||
LinkedHashMap::class,
|
||||
HashMap::class
|
||||
-> mutableMapOf<Any?, Any?>()
|
||||
-> LinkedHashMap<Any?, Any?>()
|
||||
|
||||
MutableList::class,
|
||||
List::class,
|
||||
ArrayList::class
|
||||
-> mutableListOf<Any?>()
|
||||
-> ArrayList<Any?>()
|
||||
|
||||
MutableSet::class,
|
||||
Set::class,
|
||||
LinkedHashSet::class,
|
||||
HashSet::class
|
||||
-> mutableSetOf<Any?>()
|
||||
-> LinkedHashSet<Any?>()
|
||||
|
||||
else -> createInstanceKotlin()
|
||||
else -> createInstanceOrNull()
|
||||
?: error("Cannot create instance or find a initial value for ${this.qualifiedNameOrTip}")
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user