Add a java way to broadcast an event

This commit is contained in:
Him188 2020-05-11 15:15:52 +08:00
parent f7533e3cd6
commit 68a9d49237

View File

@ -12,9 +12,12 @@
package net.mamoe.mirai.event
import kotlinx.coroutines.CoroutineScope
import net.mamoe.mirai.JavaFriendlyAPI
import net.mamoe.mirai.event.internal.broadcastInternal
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import net.mamoe.mirai.utils.SinceMirai
import net.mamoe.mirai.utils.internal.runBlocking
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic
import kotlin.jvm.Volatile
@ -140,7 +143,9 @@ interface CancellableEvent : Event {
/**
* 广播一个事件的唯一途径.
* @see __broadcastJava
*/
@JvmSynthetic
suspend fun <E : Event> E.broadcast(): E = apply {
if (this is BroadcastControllable && !this.shouldBroadcast) {
return@apply
@ -148,6 +153,21 @@ suspend fun <E : Event> E.broadcast(): E = apply {
this@broadcast.broadcastInternal() // inline, no extra cost
}
/**
* Java 广播一个事件的唯一途径.
*
* 调用方法: `EventKt.broadcast(event)`
*/
@Suppress("FunctionName")
@JvmName("broadcast")
@JavaFriendlyAPI
fun <E : Event> E.__broadcastJava(): E = apply {
if (this is BroadcastControllable && !this.shouldBroadcast) {
return@apply
}
runBlocking { this@__broadcastJava.broadcastInternal() }
}
/**
* 设置为 `true` 以关闭事件.
* 所有的 `subscribe` 都能正常添加到监听器列表, 但所有的广播都会直接返回.