mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-23 16:41:35 +01:00
Create EventListener class
This commit is contained in:
parent
4012b6d0bd
commit
3cb7cdaea8
@ -1,14 +1,6 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Object which can be listened to by an {@link EventHandler}.
|
||||
@ -16,79 +8,11 @@ import java.util.function.Predicate;
|
||||
* Called using {@link EventHandler#callEvent(Class, Event)}.
|
||||
*/
|
||||
public class Event {
|
||||
|
||||
public static <T extends EntityEvent> Listener.Builder<T> entity(Class<T> eventType) {
|
||||
return new Listener.Builder<>(eventType);
|
||||
public static <T extends EntityEvent> EventListener.Builder<T> entity(Class<T> eventType) {
|
||||
return new EventListener.Builder<>(eventType);
|
||||
}
|
||||
|
||||
public static <T extends PlayerEvent> Listener.Builder<T> player(Class<T> eventType) {
|
||||
return new Listener.Builder<>(eventType);
|
||||
}
|
||||
|
||||
|
||||
public static class Listener<T extends Event> {
|
||||
|
||||
private final Class<T> eventType;
|
||||
private final Set<EventHandler> attach = new CopyOnWriteArraySet<>();
|
||||
private final Consumer<T> combined;
|
||||
|
||||
private Listener(Class<T> eventType, @NotNull Consumer<T> combined) {
|
||||
this.eventType = eventType;
|
||||
this.combined = combined;
|
||||
}
|
||||
|
||||
public void attachTo(@NotNull EventHandler handler) {
|
||||
final boolean success = this.attach.add(handler);
|
||||
if (success) {
|
||||
handler.addEventCallback(eventType, combined::accept);
|
||||
public static <T extends PlayerEvent> EventListener.Builder<T> player(Class<T> eventType) {
|
||||
return new EventListener.Builder<>(eventType);
|
||||
}
|
||||
}
|
||||
|
||||
public void detachFrom(@NotNull EventHandler handler) {
|
||||
final boolean success = this.attach.remove(handler);
|
||||
if (success) {
|
||||
handler.removeEventCallback(eventType, combined::accept);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Builder<T extends Event> {
|
||||
|
||||
private final Class<T> eventType;
|
||||
|
||||
private List<Predicate<T>> filters = new ArrayList<>();
|
||||
private Consumer<T> handler;
|
||||
|
||||
private Builder(Class<T> eventType) {
|
||||
this.eventType = eventType;
|
||||
}
|
||||
|
||||
public Builder<T> filter(Predicate<T> filter) {
|
||||
this.filters.add(filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder<T> handler(Consumer<T> handler) {
|
||||
this.handler = handler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Listener<T> build() {
|
||||
return new Listener<>(eventType, event -> {
|
||||
// Filtering
|
||||
if (!filters.isEmpty()) {
|
||||
if (filters.stream().anyMatch(filter -> !filter.test(event))) {
|
||||
// Cancelled
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Handler
|
||||
if (handler != null) {
|
||||
handler.accept(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
76
src/main/java/net/minestom/server/event/EventListener.java
Normal file
76
src/main/java/net/minestom/server/event/EventListener.java
Normal file
@ -0,0 +1,76 @@
|
||||
package net.minestom.server.event;
|
||||
|
||||
import net.minestom.server.event.handler.EventHandler;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class EventListener<T extends Event> {
|
||||
|
||||
private final Class<T> eventType;
|
||||
private final Set<EventHandler> attach = new CopyOnWriteArraySet<>();
|
||||
private final Consumer<T> combined;
|
||||
|
||||
private EventListener(Class<T> eventType, @NotNull Consumer<T> combined) {
|
||||
this.eventType = eventType;
|
||||
this.combined = combined;
|
||||
}
|
||||
|
||||
public void attachTo(@NotNull EventHandler handler) {
|
||||
final boolean success = this.attach.add(handler);
|
||||
if (success) {
|
||||
handler.addEventCallback(eventType, combined::accept);
|
||||
}
|
||||
}
|
||||
|
||||
public void detachFrom(@NotNull EventHandler handler) {
|
||||
final boolean success = this.attach.remove(handler);
|
||||
if (success) {
|
||||
handler.removeEventCallback(eventType, combined::accept);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Builder<T extends Event> {
|
||||
|
||||
private final Class<T> eventType;
|
||||
|
||||
private List<Predicate<T>> filters = new ArrayList<>();
|
||||
private Consumer<T> handler;
|
||||
|
||||
protected Builder(Class<T> eventType) {
|
||||
this.eventType = eventType;
|
||||
}
|
||||
|
||||
public EventListener.Builder<T> filter(Predicate<T> filter) {
|
||||
this.filters.add(filter);
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventListener.Builder<T> handler(Consumer<T> handler) {
|
||||
this.handler = handler;
|
||||
return this;
|
||||
}
|
||||
|
||||
public EventListener<T> build() {
|
||||
return new EventListener<>(eventType, event -> {
|
||||
// Filtering
|
||||
if (!filters.isEmpty()) {
|
||||
if (filters.stream().anyMatch(filter -> !filter.test(event))) {
|
||||
// Cancelled
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Handler
|
||||
if (handler != null) {
|
||||
handler.accept(event);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user