From 4d8753d8e2ff63649ffc764f06d18d1f9def40a0 Mon Sep 17 00:00:00 2001 From: "jiahua.liu" Date: Mon, 6 Apr 2020 13:16:18 +0800 Subject: [PATCH] J Event Listener --- buildSrc/src/main/kotlin/versions.kt | 4 +- .../mirai/console/events/EventListener.java | 53 +++++++++++++++++++ .../mamoe/mirai/console/events/Events.java | 37 ++++++------- .../net/mamoe/mirai/console/utils/Utils.java | 1 - .../net/mamoe/mirai/console/utils/Utils.kt | 4 +- 5 files changed, 77 insertions(+), 22 deletions(-) create mode 100644 mirai-console/src/main/java/net/mamoe/mirai/console/events/EventListener.java diff --git a/buildSrc/src/main/kotlin/versions.kt b/buildSrc/src/main/kotlin/versions.kt index 7fb2952a3..b7843fcc6 100644 --- a/buildSrc/src/main/kotlin/versions.kt +++ b/buildSrc/src/main/kotlin/versions.kt @@ -12,8 +12,8 @@ import org.gradle.kotlin.dsl.DependencyHandlerScope object Versions { object Mirai { const val core = "0.32.0" - const val console = "0.4.2" - const val consoleGraphical = "0.0.5" + const val console = "0.4.3" + const val consoleGraphical = "0.0.6" const val consoleWrapper = "0.2.0" } diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/events/EventListener.java b/mirai-console/src/main/java/net/mamoe/mirai/console/events/EventListener.java new file mode 100644 index 000000000..59bfb1ff7 --- /dev/null +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/events/EventListener.java @@ -0,0 +1,53 @@ +package net.mamoe.mirai.console.events; + +import kotlinx.coroutines.GlobalScope; +import net.mamoe.mirai.console.plugins.PluginBase; +import net.mamoe.mirai.event.Event; +import net.mamoe.mirai.event.Listener; +import net.mamoe.mirai.event.ListeningStatus; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Consumer; +import java.util.function.Function; + +public class EventListener { + + PluginBase base; + + protected EventListener( + PluginBase base + ){ + this.base = base; + } + + + /** + * 监听一个事件, 当 {@code onEvent} 返回 {@link ListeningStatus#STOPPED} 时停止监听. + * 机器人离线后不会停止监听. + * + * @param eventClass 事件类 + * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. + * @param 事件类型 + * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. + */ + @NotNull + public Listener subscribe(@NotNull Class eventClass, @NotNull Function onEvent) { + return EventsImplKt.subscribeEventForJaptOnly(eventClass, base, onEvent); + } + + + /** + * 监听一个事件, 直到手动停止. + * 机器人离线后不会停止监听. + * + * @param eventClass 事件类 + * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. + * @param 事件类型 + * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. + */ + @NotNull + public Listener subscribeAlways(@NotNull Class eventClass, @NotNull Consumer onEvent) { + return EventsImplKt.subscribeEventForJaptOnly(eventClass, base, onEvent); + } + +} diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/events/Events.java b/mirai-console/src/main/java/net/mamoe/mirai/console/events/Events.java index 2679f38ab..e9445449d 100644 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/events/Events.java +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/events/Events.java @@ -23,31 +23,32 @@ 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)} 让监听正常停止或异常停止. - */ + private static void printDeprecated(){ + System.err.println("Events.subscribe is deprecated, it will be remove soon"); + System.err.println("Please use PluginBase.getEventListener"); + System.err.println("Events.subscribe 即将在下个版本移除"); + System.err.println("请更换为PluginBase.getEventListener"); + System.err.println("Events.subscribe is deprecated, it will be remove soon"); + System.err.println("Please use PluginBase.getEventListener"); + System.err.println("Events.subscribe 即将在下个版本移除"); + System.err.println("请更换为PluginBase.getEventListener"); + System.err.println("Events.subscribe is deprecated, it will be remove soon"); + System.err.println("Please use PluginBase.getEventListener"); + System.err.println("Events.subscribe 即将在下个版本移除"); + System.err.println("请更换为PluginBase.getEventListener"); + } + @NotNull + @Deprecated() public static Listener subscribe(@NotNull Class eventClass, @NotNull Function onEvent) { + printDeprecated(); return EventsImplKt.subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent); } - /** - * 监听一个事件, 直到手动停止. - * 机器人离线后不会停止监听. - * - * @param eventClass 事件类 - * @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听. - * @param 事件类型 - * @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止. - */ @NotNull + @Deprecated() public static Listener subscribeAlways(@NotNull Class eventClass, @NotNull Consumer onEvent) { + printDeprecated(); return EventsImplKt.subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent); } diff --git a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java index bf61a7b59..47455af79 100644 --- a/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java +++ b/mirai-console/src/main/java/net/mamoe/mirai/console/utils/Utils.java @@ -24,7 +24,6 @@ public class Utils { }catch(Exception e){last=e;} } - if(result != null){ return result; } diff --git a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/Utils.kt b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/Utils.kt index 4e69c9474..f5bd46b98 100644 --- a/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/Utils.kt +++ b/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/Utils.kt @@ -29,7 +29,7 @@ inline fun retryCatching(n: Int, block: () -> R): Result { } @OptIn(ExperimentalContracts::class) -inline fun tryNTimes(n: Int, block: () -> T):T { +inline fun tryNTimes(n: Int = 2, block: () -> T):T { contract { callsInPlace(block, InvocationKind.AT_LEAST_ONCE) } @@ -43,6 +43,8 @@ inline fun tryNTimes(n: Int, block: () -> T):T { } } + //给我编译 + throw last!! }