Commit 68a9d492 authored by Him188's avatar Him188

Add a java way to broadcast an event

parent f7533e3c
...@@ -12,9 +12,12 @@ ...@@ -12,9 +12,12 @@
package net.mamoe.mirai.event package net.mamoe.mirai.event
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import net.mamoe.mirai.JavaFriendlyAPI
import net.mamoe.mirai.event.internal.broadcastInternal import net.mamoe.mirai.event.internal.broadcastInternal
import net.mamoe.mirai.utils.MiraiExperimentalAPI import net.mamoe.mirai.utils.MiraiExperimentalAPI
import net.mamoe.mirai.utils.SinceMirai import net.mamoe.mirai.utils.SinceMirai
import net.mamoe.mirai.utils.internal.runBlocking
import kotlin.jvm.JvmName
import kotlin.jvm.JvmSynthetic import kotlin.jvm.JvmSynthetic
import kotlin.jvm.Volatile import kotlin.jvm.Volatile
...@@ -140,7 +143,9 @@ interface CancellableEvent : Event { ...@@ -140,7 +143,9 @@ interface CancellableEvent : Event {
/** /**
* 广播一个事件的唯一途径. * 广播一个事件的唯一途径.
* @see __broadcastJava
*/ */
@JvmSynthetic
suspend fun <E : Event> E.broadcast(): E = apply { suspend fun <E : Event> E.broadcast(): E = apply {
if (this is BroadcastControllable && !this.shouldBroadcast) { if (this is BroadcastControllable && !this.shouldBroadcast) {
return@apply return@apply
...@@ -148,6 +153,21 @@ suspend fun <E : Event> E.broadcast(): E = apply { ...@@ -148,6 +153,21 @@ suspend fun <E : Event> E.broadcast(): E = apply {
this@broadcast.broadcastInternal() // inline, no extra cost 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` 以关闭事件. * 设置为 `true` 以关闭事件.
* 所有的 `subscribe` 都能正常添加到监听器列表, 但所有的广播都会直接返回. * 所有的 `subscribe` 都能正常添加到监听器列表, 但所有的广播都会直接返回.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment