mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-08 17:20:11 +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 Versions {
|
||||||
object Mirai {
|
object Mirai {
|
||||||
const val core = "0.32.0"
|
const val core = "0.32.0"
|
||||||
const val console = "0.4.2"
|
const val console = "0.4.3"
|
||||||
const val consoleGraphical = "0.0.5"
|
const val consoleGraphical = "0.0.6"
|
||||||
const val consoleWrapper = "0.2.0"
|
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 {
|
public final class Events {
|
||||||
|
|
||||||
/**
|
private static void printDeprecated(){
|
||||||
* 监听一个事件, 当 {@code onEvent} 返回 {@link ListeningStatus#STOPPED} 时停止监听.
|
System.err.println("Events.subscribe is deprecated, it will be remove soon");
|
||||||
* 机器人离线后不会停止监听.
|
System.err.println("Please use PluginBase.getEventListener");
|
||||||
*
|
System.err.println("Events.subscribe 即将在下个版本移除");
|
||||||
* @param eventClass 事件类
|
System.err.println("请更换为PluginBase.getEventListener");
|
||||||
* @param onEvent 事件处理. 返回 {@link ListeningStatus#LISTENING} 时继续监听.
|
System.err.println("Events.subscribe is deprecated, it will be remove soon");
|
||||||
* @param <E> 事件类型
|
System.err.println("Please use PluginBase.getEventListener");
|
||||||
* @return 事件监听器. 可调用 {@link Listener#complete()} 或 {@link Listener#completeExceptionally(Throwable)} 让监听正常停止或异常停止.
|
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
|
@NotNull
|
||||||
|
@Deprecated()
|
||||||
public static <E extends Event> Listener<E> subscribe(@NotNull Class<E> eventClass, @NotNull Function<E, ListeningStatus> onEvent) {
|
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);
|
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
|
@NotNull
|
||||||
|
@Deprecated()
|
||||||
public static <E extends Event> Listener<E> subscribeAlways(@NotNull Class<E> eventClass, @NotNull Consumer<E> onEvent) {
|
public static <E extends Event> Listener<E> subscribeAlways(@NotNull Class<E> eventClass, @NotNull Consumer<E> onEvent) {
|
||||||
|
printDeprecated();
|
||||||
return EventsImplKt.subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent);
|
return EventsImplKt.subscribeEventForJaptOnly(eventClass, GlobalScope.INSTANCE, onEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ public class Utils {
|
|||||||
}catch(Exception e){last=e;}
|
}catch(Exception e){last=e;}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(result != null){
|
if(result != null){
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -29,7 +29,7 @@ inline fun <R> retryCatching(n: Int, block: () -> R): Result<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(ExperimentalContracts::class)
|
@OptIn(ExperimentalContracts::class)
|
||||||
inline fun <T> tryNTimes(n: Int, block: () -> T):T {
|
inline fun <T> tryNTimes(n: Int = 2, block: () -> T):T {
|
||||||
contract {
|
contract {
|
||||||
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
|
callsInPlace(block, InvocationKind.AT_LEAST_ONCE)
|
||||||
}
|
}
|
||||||
@ -43,6 +43,8 @@ inline fun <T> tryNTimes(n: Int, block: () -> T):T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//给我编译
|
||||||
|
|
||||||
throw last!!
|
throw last!!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user