Skip to content

Commit

Permalink
Remove duplicate registration checks, and some minor code cleanup (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaintNinja authored Oct 4, 2024
1 parent f29ef3d commit e010fb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ASMEventHandler implements IEventListener {
private final IEventListenerFactory factory;
private final IEventListener handler;
private final SubscribeEvent subInfo;
private String readable;
private final String readable;
private Type filter = null;

public ASMEventHandler(IEventListenerFactory factory, Object target, Method method, boolean isGeneric) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/net/minecraftforge/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
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 AtomicInteger maxID = new AtomicInteger(0);
private static final AtomicInteger maxID = new AtomicInteger(0);
private final boolean trackPhases;

private ConcurrentHashMap<Object, List<IEventListener>> listeners = new ConcurrentHashMap<>();
private final ConcurrentHashMap<Object, List<IEventListener>> listeners = new ConcurrentHashMap<>();
private final int busID = maxID.getAndIncrement();
private final IEventExceptionHandler exceptionHandler;
private volatile boolean shutdown = false;
Expand Down Expand Up @@ -161,34 +161,32 @@ private void registerListener(final Object target, final Method method, final Me

private static final Predicate<Event> checkCancelled = e -> !e.isCanceled();
@SuppressWarnings("unchecked")
private <T extends Event> Predicate<T> passCancelled(boolean ignored) {
private static <T extends Event> Predicate<T> passCancelled(boolean ignored) {
return ignored ? null : (Predicate<T>)checkCancelled;
}

private <T extends GenericEvent<? extends F>, F> Predicate<T> passGenericFilter(Class<F> type, boolean ignored) {
private static <T extends GenericEvent<? extends F>, F> Predicate<T> passGenericFilter(Class<F> type, boolean ignored) {
if (ignored)
return e -> e.getGenericType() == type;
return e -> e.getGenericType() == type && !e.isCanceled();
}

private void checkNotGeneric(final Consumer<? extends Event> consumer) {
private static void checkNotGeneric(final Consumer<? extends Event> consumer) {
checkNotGeneric(getEventClass(consumer));
}

private void checkNotGeneric(final Class<? extends Event> eventType) {
private static void checkNotGeneric(final Class<? extends Event> eventType) {
if (GenericEvent.class.isAssignableFrom(eventType))
throw new IllegalArgumentException("Cannot register a generic event listener with addListener, use addGenericListener");
}

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

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

Expand Down Expand Up @@ -225,7 +223,7 @@ public <T extends GenericEvent<? extends F>, F> void addGenericListener(final Cl
}

@SuppressWarnings("unchecked")
private <T extends Event> Class<T> getEventClass(Consumer<T> consumer) {
private static <T extends Event> Class<T> getEventClass(Consumer<T> consumer) {
final Class<T> eventClass = (Class<T>) TypeResolver.resolveRawArgument(Consumer.class, consumer.getClass());
if ((Class<?>)eventClass == TypeResolver.Unknown.class) {
LOGGER.error(EVENTBUS, "Failed to resolve handler for \"{}\"", consumer.toString());
Expand All @@ -252,7 +250,7 @@ private <T extends Event> void addListener(final EventPriority priority, final P
}

@SuppressWarnings("unchecked")
private <T extends Event> void doCastFilter(final Predicate<? super T> filter, final Class<T> eventClass, final Consumer<T> consumer, final Event e) {
private static <T extends Event> void doCastFilter(final Predicate<? super T> filter, final Class<T> eventClass, final Consumer<T> consumer, final Event e) {
T cast = (T)e;
if (filter == null || filter.test(cast))
consumer.accept(cast);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/minecraftforge/eventbus/ListenerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@


public class ListenerList {
private static List<ListenerList> allLists = new ArrayList<>();
private static final List<ListenerList> allLists = new ArrayList<>();
private static int maxSize = 0;

@Nullable
private ListenerList parent;
private final ListenerList parent;
private ListenerListInst[] lists = new ListenerListInst[0];

public ListenerList() {
Expand Down Expand Up @@ -97,13 +97,13 @@ public static synchronized void unregisterAll(int id, IEventListener listener) {
list.unregister(id, listener);
}

private class ListenerListInst {
private static class ListenerListInst {
private boolean rebuild = true;
private AtomicReference<IEventListener[]> listeners = new AtomicReference<>();
private ArrayList<ArrayList<IEventListener>> priorities;
private final ArrayList<ArrayList<IEventListener>> priorities;
private ListenerListInst parent;
private List<ListenerListInst> children;
private Semaphore writeLock = new Semaphore(1, true);
private final Semaphore writeLock = new Semaphore(1, true);

private ListenerListInst() {
int count = EventPriority.values().length;
Expand Down

0 comments on commit e010fb3

Please sign in to comment.