Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a hot path for avoiding typetools in non-generic events. #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Tue Sep 24 15:14:52 CEST 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/net/minecraftforge/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,18 @@ public <T extends Event> void addListener(final Consumer<T> consumer) {
addListener(EventPriority.NORMAL, consumer);
}

@Override
public <T extends Event> void addListener(final EventPriority priority, final Consumer<T> consumer) {
addListener(priority, false, consumer);
}

@Override
public <T extends Event> void addListener(final EventPriority priority, final boolean receiveCancelled, final Consumer<T> consumer) {
addListener(priority, passCancelled(receiveCancelled), consumer);
addListenerWithTypetools(priority, passCancelled(receiveCancelled), consumer);
}

@Override
public <T extends Event> void addListener(final EventPriority priority, final boolean receiveCancelled, final Class<T> eventType, final Consumer<T> consumer) {
addListener(priority, passCancelled(receiveCancelled), eventType, consumer);
addListenerWithExplicitClass(priority, passCancelled(receiveCancelled), eventType, consumer);
}

@Override
Expand All @@ -176,16 +175,16 @@ public <T extends GenericEvent<? extends F>, F> void addGenericListener(final Cl

@Override
public <T extends GenericEvent<? extends F>, F> void addGenericListener(final Class<F> genericClassFilter, final EventPriority priority, final boolean receiveCancelled, final Consumer<T> consumer) {
addListener(priority, passGenericFilter(genericClassFilter).and(passCancelled(receiveCancelled)), consumer);
addListenerWithTypetools(priority, passGenericFilter(genericClassFilter).and(passCancelled(receiveCancelled)), consumer);
}

@Override
public <T extends GenericEvent<? extends F>, F> void addGenericListener(final Class<F> genericClassFilter, final EventPriority priority, final boolean receiveCancelled, final Class<T> eventType, final Consumer<T> consumer) {
addListener(priority, passGenericFilter(genericClassFilter).and(passCancelled(receiveCancelled)), eventType, consumer);
addListenerWithExplicitClass(priority, passGenericFilter(genericClassFilter).and(passCancelled(receiveCancelled)), eventType, consumer);
}

@SuppressWarnings("unchecked")
private <T extends Event> void addListener(final EventPriority priority, final Predicate<? super T> filter, final Consumer<T> consumer) {
private <T extends Event> void addListenerWithTypetools(final EventPriority priority, final Predicate<? super T> filter, final Consumer<T> consumer) {
final Class<T> eventClass = (Class<T>) TypeResolver.resolveRawArgument(Consumer.class, consumer.getClass());

if ((Class<?>) eventClass == TypeResolver.Unknown.class) {
Expand All @@ -199,14 +198,14 @@ private <T extends Event> void addListener(final EventPriority priority, final P
+ "the generic type information is erased and cannot be recovered at runtime.");
}

addListener(priority, filter, eventClass, consumer);
addListenerWithExplicitClass(priority, filter, eventClass, consumer);
}

private <T extends Event> void addListener(final EventPriority priority, final Predicate<? super T> filter, final Class<T> eventClass, final Consumer<T> consumer) {
addListener(priority, filter, eventClass, consumer, consumer);
private <T extends Event> void addListenerWithExplicitClass(final EventPriority priority, final Predicate<? super T> filter, final Class<T> eventClass, final Consumer<T> consumer) {
addListenerWithExplicitClass(priority, filter, eventClass, consumer, consumer);
}

private <T extends Event> void addListener(final EventPriority priority, final Predicate<? super T> filter, final Class<T> eventClass, final Consumer<T> consumer, final Object context) {
private <T extends Event> void addListenerWithExplicitClass(final EventPriority priority, final Predicate<? super T> filter, final Class<T> eventClass, final Consumer<T> consumer, final Object context) {
IEventListener listener = event -> doCastFilter(filter, consumer, event);

ListenerList listenerList = EventListenerHelper.getListenerList(eventClass);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/net/minecraftforge/eventbus/api/IEventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public interface IEventBus {
*/
<T extends Event> void addListener(Consumer<T> consumer);

default <T extends Event> void addExplicitListener(Consumer<T> consumer, Class<T> eventClass) {
addListener(EventPriority.NORMAL, false, eventClass, consumer);
}

/**
* Add a consumer listener with the specified {@link EventPriority} and not receiving cancelled events.
*
Expand All @@ -45,6 +49,10 @@ public interface IEventBus {
*/
<T extends Event> void addListener(EventPriority priority, Consumer<T> consumer);

default <T extends Event> void addExplicitListener(EventPriority priority, Consumer<T> consumer, Class<T> eventClass) {
addListener(priority, false, eventClass, consumer);
}

/**
* Add a consumer listener with the specified {@link EventPriority} and potentially cancelled events.
*
Expand All @@ -68,7 +76,6 @@ public interface IEventBus {
* @param <T> The {@link Event} subclass to listen for
*/
<T extends Event> void addListener(EventPriority priority, boolean receiveCancelled, Class<T> eventType, Consumer<T> consumer);

/**
* Add a consumer listener for a {@link GenericEvent} subclass, filtered to only be called for the specified
* filter {@link Class}.
Expand Down