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

Skip phase tracking notifiers at registration time rather than post time #66

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
5 changes: 2 additions & 3 deletions src/main/java/net/minecraftforge/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class EventBus implements IEventExceptionHandler, IEventBus {
private static final Logger LOGGER = LogManager.getLogger();
private static final boolean checkTypesOnDispatchProperty = Boolean.parseBoolean(System.getProperty("eventbus.checkTypesOnDispatch", "false"));
private static final AtomicInteger maxID = new AtomicInteger(0);
private final boolean trackPhases;
final boolean trackPhases;

private final ConcurrentHashMap<Object, List<IEventListener>> listeners = new ConcurrentHashMap<>();
private final int busID = maxID.getAndIncrement();
Expand Down Expand Up @@ -268,7 +268,7 @@ private void register(Class<?> eventType, Object target, Method method) {

private void addToListeners(final Object target, final Class<?> eventType, final IEventListener listener, final EventPriority priority) {
ListenerList listenerList = EventListenerHelper.getListenerList(eventType);
listenerList.register(busID, priority, listener);
listenerList.register(busID, this, priority, listener);
List<IEventListener> others = listeners.computeIfAbsent(target, k -> Collections.synchronizedList(new ArrayList<>()));
others.add(listener);
}
Expand Down Expand Up @@ -298,7 +298,6 @@ public boolean post(Event event, IEventBusInvokeDispatcher wrapper) {
int index = 0;
try {
for (; index < listeners.length; index++) {
if (!trackPhases && listeners[index].getClass() == EventPriority.class) continue;
wrapper.invoke(listeners[index], event);
}
} catch (Throwable throwable) {
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/net/minecraftforge/eventbus/ListenerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ public void register(int id, EventPriority priority, IEventListener listener) {
lists[id].register(priority, listener);
}

public void register(int id, EventBus eventBus, EventPriority priority, IEventListener listener) {
var list = lists[id];
list.phaseTracking = eventBus.trackPhases;
list.register(priority, listener);
}

public void unregister(int id, IEventListener listener) {
lists[id].unregister(listener);
}
Expand Down Expand Up @@ -114,6 +120,7 @@ private static class ListenerListInst {

private ListenerListInst parent;
private List<ListenerListInst> children;
private boolean phaseTracking = true;
private final Semaphore writeLock = new Semaphore(1, true);

private ListenerListInst() {}
Expand Down Expand Up @@ -212,7 +219,7 @@ private IEventListener[] buildCache() {
for (EventPriority value : EVENT_PRIORITY_VALUES) {
List<IEventListener> listeners = getListeners(value);
if (listeners.isEmpty()) continue;
ret.add(value); // Add the priority to notify the event of its current phase.
if (phaseTracking) ret.add(value); // Add the priority to notify the event of its current phase.
ret.addAll(listeners);
}

Expand Down