Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package yesman.epicfight.api.event;

import com.google.common.collect.Lists;
import yesman.epicfight.api.event.subscription.ContextAwareEventSubscription;
import yesman.epicfight.api.event.subscription.DefaultEventSubscription;
import yesman.epicfight.api.utils.side.LogicalSide;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;

/// EventHook definition for [CancelableEvent]
Expand All @@ -19,25 +22,28 @@ protected CancelableEventHook(LogicalSide logicalSide) {
public T post(T event) {
EventContext eventContext = event.getEventContext();

for (EventListener<T> subscriber : this.subscribers.values()) {
eventContext.subscriptionStart(subscriber.identifier());

if (subscriber.subscriptionType() instanceof DefaultEventSubscription<T> defaultSubscription) {
if (!event.isCanceled()) {
defaultSubscription.fire(event);
eventContext.onCalled();
}
} else if (subscriber.subscriptionType() instanceof ContextAwareEventSubscription<T> contextAwareSubscription) {
contextAwareSubscription.fire(event, eventContext);
eventContext.onCalled();
}
for (var subscriber : subscribers.values()) {
subscriber.forEach(subs -> processSub(event, subs, eventContext));
}

eventContext.subscriptionEnd();

return event;
}

private void processSub(T event, EventListener<T> subscriber, EventContext eventContext)
{
eventContext.subscriptionStart(subscriber.identifier());
if (subscriber.subscriptionType() instanceof DefaultEventSubscription<T> defaultSubscription) {
if (!event.isCanceled()) {
defaultSubscription.fire(event);
eventContext.onCalled();
}
} else if (subscriber.subscriptionType() instanceof ContextAwareEventSubscription<T> contextAwareSubscription) {
contextAwareSubscription.fire(event, eventContext);
eventContext.onCalled();
}
}

/// Post the event to subscribers including from [EntityEventListener], and execute tasks by their priority in descending order
@Override
public T postWithListener(T event, EntityEventListener eventListener) {
Expand All @@ -47,21 +53,11 @@ public T postWithListener(T event, EntityEventListener eventListener) {

EventContext eventContext = event.getEventContext();

Stream.concat(this.subscribers.values().stream(), eventListener.getListenersFor(this))
.sorted((o1, o2) -> Integer.compare(o2.priority(), o1.priority()))
.forEach(subscriber -> {
eventContext.subscriptionStart(subscriber.identifier());

if (subscriber.subscriptionType() instanceof DefaultEventSubscription<T> defaultSubscription) {
if (!event.isCanceled()) {
defaultSubscription.fire(event);
eventContext.onCalled();
}
} else if (subscriber.subscriptionType() instanceof ContextAwareEventSubscription<T> contextAwareSubscription) {
contextAwareSubscription.fire(event, eventContext);
eventContext.onCalled();
}
});
List<EventListener<T>> buffer = Lists.newArrayList();
this.subscribers.values().forEach(buffer::addAll);
Stream.concat(buffer.stream(), eventListener.getListenersFor(this))
.sorted(EventHook::reverseOrder)
.forEach(subs -> processSub(event, subs, eventContext));

eventContext.subscriptionEnd();

Expand All @@ -87,7 +83,7 @@ public void registerContextAwareEvent(ContextAwareEventSubscription<T> subscript

/// Registers an event with full parameters
public void registerContextAwareEvent(ContextAwareEventSubscription<T> subscription, String name, int priority) {
this.subscribers.put(priority, new EventListener<> (name, priority, subscription));
this.subscribers.computeIfAbsent(priority, sub -> new CopyOnWriteArrayList<>()).add(new EventListener<>(name, priority, subscription));
}

/// Defines a cancelable event hook
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

import java.util.Comparator;
import java.util.List;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Stream;

/// An event bus that is dedicated to one [Event] type
Expand All @@ -20,7 +22,7 @@
/// and [EpicFightClientEventHooks] for client-side only events
public class EventHook<T extends Event> {
/// Treemap to order subscribers in descending order
final TreeMap<Integer, List<EventListener<T>>> subscribers = new TreeMap<> ((i1, i2) -> Integer.compare(i2, i1));
final ConcurrentMap<Integer, List<EventListener<T>>> subscribers = new ConcurrentSkipListMap<>(Comparator.reverseOrder());

/// Determines if the event is only called in logical side
protected final LogicalSide logicalSide;
Expand All @@ -31,10 +33,9 @@ protected EventHook(LogicalSide logicalSide) {

/// Post the event to subscribers and execute tasks by their priority in descending order
public T post(T event) {

for (var subscriberList : this.subscribers.values()) {
for (var subscriber : subscriberList) {
if (subscriber.subscriptionType() instanceof DefaultEventSubscription<T> passiveSubscription) {
for (var subscriber : subscribers.values()) {
for (var listener : subscriber) {
if (listener.subscriptionType() instanceof DefaultEventSubscription<T> passiveSubscription) {
passiveSubscription.fire(event);
}
}
Expand All @@ -51,7 +52,7 @@ public T postWithListener(T event, EntityEventListener eventListener) {
List<EventListener<T>> buffer = Lists.newArrayList();
this.subscribers.values().forEach(buffer::addAll);
Stream.concat(buffer.stream(), eventListener.getListenersFor(this))
.sorted(Comparator.comparingInt(EventListener::priority))
.sorted(EventHook::reverseOrder)
.forEach(subs -> {
if (subs.subscriptionType() instanceof DefaultEventSubscription<T> passiveSubscription) {
passiveSubscription.fire(event);
Expand All @@ -60,6 +61,11 @@ public T postWithListener(T event, EntityEventListener eventListener) {
return event;
}

public static int reverseOrder(EventListener<? extends Event> A, EventListener<? extends Event> B)
{
return Integer.compare(B.priority(), A.priority());
}

/// Registers an event with default identifier and priority
public void registerEvent(DefaultEventSubscription<T> subscription) {
this.registerEvent(subscription, getDefaultSubscriberName(), 0);
Expand All @@ -79,7 +85,7 @@ public void registerEvent(DefaultEventSubscription<T> subscription, String name)

/// Registers an event with full parameters
public void registerEvent(DefaultEventSubscription<T> subscription, String name, int priority) {
this.subscribers.computeIfAbsent(priority, sub -> Lists.newArrayList()).add(new EventListener<>(name,priority,subscription));
this.subscribers.computeIfAbsent(priority, sub -> new CopyOnWriteArrayList<>()).add(new EventListener<>(name,priority,subscription));
}

public final LogicalSide logicalSide() {
Expand Down