mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-07 16:40:43 +08:00
J Event Listener
This commit is contained in:
parent
88b1576417
commit
4d8753d8e2
@ -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"
|
||||
}
|
||||
|
||||
|
@ -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 <E> 事件类型
|
||||
* @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止.
|
||||
*/
|
||||
@NotNull
|
||||
public <E extends Event> Listener<E> subscribe(@NotNull Class<E> eventClass, @NotNull Function<E, ListeningStatus> onEvent) {
|
||||
return EventsImplKt.subscribeEventForJaptOnly(eventClass, base, onEvent);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 监听一个事件, 直到手动停止.
|
||||
* 机器人离线后不会停止监听.
|
||||
*
|
||||
* @param eventClass 事件类
|
||||
* @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听.
|
||||
* @param <E> 事件类型
|
||||
* @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止.
|
||||
*/
|
||||
@NotNull
|
||||
public <E extends Event> Listener<E> subscribeAlways(@NotNull Class<E> eventClass, @NotNull Consumer<E> onEvent) {
|
||||
return EventsImplKt.subscribeEventForJaptOnly(eventClass, base, onEvent);
|
||||
}
|
||||
|
||||
}
|
@ -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 <E> 事件类型
|
||||
* @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 <E extends Event> Listener<E> subscribe(@NotNull Class<E> eventClass, @NotNull Function<E, ListeningStatus> onEvent) {
|
||||
printDeprecated();
|
||||
return EventsImplKt.subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听一个事件, 直到手动停止.
|
||||
* 机器人离线后不会停止监听.
|
||||
*
|
||||
* @param eventClass 事件类
|
||||
* @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听.
|
||||
* @param <E> 事件类型
|
||||
* @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止.
|
||||
*/
|
||||
@NotNull
|
||||
@Deprecated()
|
||||
public static <E extends Event> Listener<E> subscribeAlways(@NotNull Class<E> eventClass, @NotNull Consumer<E> onEvent) {
|
||||
printDeprecated();
|
||||
return EventsImplKt.subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent);
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,6 @@ public class Utils {
|
||||
}catch(Exception e){last=e;}
|
||||
}
|
||||
|
||||
|
||||
if(result != null){
|
||||
return result;
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalContracts::class)
|
||||
inline fun <T> tryNTimes(n: Int, block: () -> T):T {
|
||||
inline fun <T> tryNTimes(n: Int = 2, block: () -> T):T {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
|
||||
}
|
||||
@ -43,6 +43,8 @@ inline fun <T> tryNTimes(n: Int, block: () -> T):T {
|
||||
}
|
||||
}
|
||||
|
||||
//给我编译
|
||||
|
||||
throw last!!
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user