package org.darkstorm.darkbot.minecraftbot.events;

import java.lang.reflect.*;
import java.util.*;

final class EventSender {
	private final Map<EventListener, List<Method>> handlers;
	private final Class<? extends Event> listenerEventClass;

	public EventSender(Class<? extends Event> listenerEventClass) {
		handlers = new HashMap<EventListener, List<Method>>();
		this.listenerEventClass = listenerEventClass;
	}

	public synchronized void addHandler(EventListener listener, Method method) {
		List<Method> methods = handlers.get(listener);
		if(methods == null) {
			methods = new ArrayList<>();
			handlers.put(listener, methods);
		}
		methods.add(method);
	}

	public synchronized void unregisterListener(EventListener listener) {
		handlers.remove(listener);
	}

	public synchronized List<EventListener> getListeners() {
		return Collections.unmodifiableList(new ArrayList<>(handlers.keySet()));
	}

	public synchronized void sendEvent(Event event) {
		Class<?> eventClass = event.getClass();
		if(!listenerEventClass.isAssignableFrom(eventClass))
			return;
		List<EventListener> listeners = new ArrayList<>(handlers.keySet());
		for(EventListener listener : listeners) {
			List<Method> methods = handlers.get(listener);
			if(methods == null)
				continue;
			for(Method method : methods) {
				try {
					method.invoke(listener, event);
				} catch(InvocationTargetException exception) {
					exception.getCause().printStackTrace();
				} catch(Throwable exception) {
					exception.printStackTrace();
				}
			}
		}
	}

	public synchronized Class<? extends Event> getListenerEventClass() {
		return listenerEventClass;
	}
}

@@ -1315,37 +1315,197 @@
 %09%09%09%09
-method.invoke(listener, event
+boolean accessible = method.isAccessible();%0A%09%09%09%09%09if(!accessible)%0A%09%09%09%09%09%09method.setAccessible(true);%0A%09%09%09%09%09method.invoke(listener, event);%0A%09%09%09%09%09if(!accessible)%0A%09%09%09%09%09%09method.setAccessible(false
 );%0A%09

Support methods in private classes.