add convenience event constructors

This commit is contained in:
moehreag 2024-06-05 12:09:35 +02:00
parent f2cf3b501c
commit 9069cc271f
2 changed files with 14 additions and 4 deletions

View file

@ -1,6 +1,8 @@
package dev.frogmc.froglib.events.api; package dev.frogmc.froglib.events.api;
import java.util.List; import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import dev.frogmc.froglib.events.impl.EventImpl; import dev.frogmc.froglib.events.impl.EventImpl;
@ -19,6 +21,18 @@ public interface Event<T> {
return EventImpl.of(invokerFactory); return EventImpl.of(invokerFactory);
} }
static Event<Runnable> runnable(){
return of(l -> () -> l.forEach(Runnable::run));
}
static <T> Event<Consumer<T>> consumer(){
return of(l -> t -> l.forEach(c -> c.accept(t)));
}
static <T, U> Event<BiConsumer<T, U>> biConsumer(){
return of(l -> (t, u) -> l.forEach(c -> c.accept(t, u)));
}
void register(T listener); void register(T listener);
T invoker(); T invoker();

View file

@ -47,10 +47,6 @@ public class EventImpl<T> implements Event<T> {
return of(list -> generateInvoker(list, (Class<B>) list.getFirst().getClass().getInterfaces()[0])); return of(list -> generateInvoker(list, (Class<B>) list.getFirst().getClass().getInterfaces()[0]));
} }
public static EventImpl<Runnable> simple() {
return of(l -> () -> l.forEach(Runnable::run));
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static <B> B generateInvoker(List<B> list, Class<B> clazz) { private static <B> B generateInvoker(List<B> list, Class<B> clazz) {
try { try {