diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Event.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Event.kt index 9f5c91f52..9ef4971cd 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Event.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Event.kt @@ -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.broadcast(): E = apply { if (this is BroadcastControllable && !this.shouldBroadcast) { return@apply @@ -148,6 +153,21 @@ suspend fun E.broadcast(): E = apply { this@broadcast.broadcastInternal() // inline, no extra cost } +/** + * 在 Java 广播一个事件的唯一途径. + * + * 调用方法: `EventKt.broadcast(event)` + */ +@Suppress("FunctionName") +@JvmName("broadcast") +@JavaFriendlyAPI +fun E.__broadcastJava(): E = apply { + if (this is BroadcastControllable && !this.shouldBroadcast) { + return@apply + } + runBlocking { this@__broadcastJava.broadcastInternal() } +} + /** * 设置为 `true` 以关闭事件. * 所有的 `subscribe` 都能正常添加到监听器列表, 但所有的广播都会直接返回.