diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/Events.java b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/Events.java new file mode 100644 index 000000000..287d1c3a9 --- /dev/null +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/Events.java @@ -0,0 +1,66 @@ +/* + * 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.events; + +import kotlinx.coroutines.GlobalScope; +import net.mamoe.mirai.event.Event; +import net.mamoe.mirai.event.Listener; +import net.mamoe.mirai.event.ListeningStatus; +import net.mamoe.mirai.event.internal.EventInternalJvmKt; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * 事件处理 + */ +public final class Events { + + /** + * 监听一个事件, 当 {@code onEvent} 返回 {@link ListeningStatus#STOPPED} 时停止监听. + * 机器人离线后不会停止监听. + * + * @param eventClass 事件类 + * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. + * @param 事件类型 + * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. + */ + @NotNull + public static Listener subscribe(@NotNull Class eventClass, @NotNull Function onEvent) { + return EventInternalJvmKt._subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent); + } + + /** + * 监听一个事件, 直到手动停止. + * 机器人离线后不会停止监听. + * + * @param eventClass 事件类 + * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. + * @param 事件类型 + * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. + */ + @NotNull + public static Listener subscribeAlways(@NotNull Class eventClass, @NotNull Consumer onEvent) { + return EventInternalJvmKt._subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent); + } + + /** + * 阻塞地广播一个事件. + * + * @param event 事件 + * @param 事件类型 + * @return {@code event} 本身 + */ + @NotNull + public static E broadcast(@NotNull E event) { + return EventsImplKt.broadcast(event); + } +} \ No newline at end of file diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/EventsImpl.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/EventsImpl.kt new file mode 100644 index 000000000..baf44f72e --- /dev/null +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/events/EventsImpl.kt @@ -0,0 +1,16 @@ +/* + * 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.events; + +import kotlinx.coroutines.runBlocking +import net.mamoe.mirai.event.Event +import net.mamoe.mirai.event.broadcast + +internal fun broadcast(e: E): E = runBlocking { e.broadcast() } \ No newline at end of file diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/scheduler/SchedulerTask.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/scheduler/SchedulerTask.kt new file mode 100644 index 000000000..ac145af57 --- /dev/null +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/scheduler/SchedulerTask.kt @@ -0,0 +1,69 @@ +package net.mamoe.mirai.console.scheduler + +import net.mamoe.mirai.console.plugins.PluginBase + +interface SchedulerTask{ + abstract fun onTick(i:Long) + abstract fun onRun() +} + +abstract class RepeatTask( + val intervalInMs: Int +):SchedulerTask{ + + override fun onTick(i: Long) { + if(i%intervalInMs == 0L){ + onRun() + } + } + + companion object{ + fun of( + intervalInMs: Int, runnable: () -> Unit + ):RepeatTask{ + return AnonymousRepeatTask( + intervalInMs, runnable + ) + } + } +} + +internal class AnonymousRepeatTask( + intervalInMs: Int, private val runnable: () -> Unit +): RepeatTask(intervalInMs){ + override fun onRun() { + runnable.invoke() + } +} + +fun T.repeatTask( + intervalInMs: Int, runnable: () -> Unit +):RepeatTask{ + return AnonymousRepeatTask( + intervalInMs, runnable + ) +} + + +fun repeatTask(){ + +} + +class X: PluginBase() { + override fun onLoad() { + //kotlin + this.repeatTask(5){ + + } + //java1 + RepeatTask.of(5){ + + } + //java2 + class Xtask:RepeatTask(5){ + override fun onRun() { + + } + } + } +} \ No newline at end of file