Add support for generic filtering of events.

Please DO NOT use this in performance sensitive environments where you will have tons of things added to the listener list.
If that's the case define sub-classes as they have completely separate lists and will thus be more efficient when firing the event.
This commit is contained in:
LexManos committed 2016-09-17 15:08:23 -07:00
1 parent c58a66a816
commit 1107088acb
6 files changed
+126 -16

No files matched your search

@@ -23,6 +23,7 @@ import static org.objectweb.asm.Opcodes.*;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.HashMap;
import net.minecraftforge.fml.common.ModContainer;
@@ -34,7 +35,6 @@ import org.objectweb.asm.Type;
import com.google.common.collect.Maps;
public class ASMEventHandler implements IEventListener
{
private static int IDs = 0;
@@ -48,8 +48,15 @@ public class ASMEventHandler implements IEventListener
private final SubscribeEvent subInfo;
private ModContainer owner;
private String readable;
private java.lang.reflect.Type filter = null;
@Deprecated
public ASMEventHandler(Object target, Method method, ModContainer owner) throws Exception
{
this(target, method, owner, false);
}
public ASMEventHandler(Object target, Method method, ModContainer owner, boolean isGeneric) throws Exception
{
this.owner = owner;
if (Modifier.isStatic(method.getModifiers()))
@@ -58,8 +65,19 @@ public class ASMEventHandler implements IEventListener
handler = (IEventListener)createWrapper(method).getConstructor(Object.class).newInstance(target);
subInfo = method.getAnnotation(SubscribeEvent.class);
readable = "ASM: " + target + " " + method.getName() + Type.getMethodDescriptor(method);
if (isGeneric)
{
java.lang.reflect.Type type = method.getGenericParameterTypes()[0];
System.currentTimeMillis();
if (type instanceof ParameterizedType)
{
filter = ((ParameterizedType)type).getActualTypeArguments()[0];
System.currentTimeMillis();
}
}
}
@SuppressWarnings("rawtypes")
@Override
public void invoke(Event event)
{
@@ -69,7 +87,10 @@ public class ASMEventHandler implements IEventListener
{
if (!event.isCancelable() || !event.isCanceled() || subInfo.receiveCanceled())
{
handler.invoke(event);
if (filter == null || filter == ((IGenericEvent)event).getGenericType())
{
handler.invoke(event);
}
}
}
if (GETCONTEXT)
@@ -129,7 +129,7 @@ public class EventBus implements IEventExceptionHandler
Constructor<?> ctr = eventType.getConstructor();
ctr.setAccessible(true);
Event event = (Event)ctr.newInstance();
ASMEventHandler listener = new ASMEventHandler(target, method, owner);
ASMEventHandler listener = new ASMEventHandler(target, method, owner, IGenericEvent.class.isAssignableFrom(eventType));
event.getListenerList().register(busID, listener.getPriority(), listener);
ArrayList<IEventListener> others = listeners.get(target);
@@ -0,0 +1,36 @@
/*
* Minecraft Forge
* Copyright (c) 2016.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common.eventhandler;
import java.lang.reflect.Type;
public class GenericEvent<T> extends Event implements IGenericEvent<T>
{
private Class<T> type;
protected GenericEvent(Class<T> type)
{
this.type = type;
}
@Override
public Type getGenericType()
{
return type;
}
}
@@ -0,0 +1,26 @@
/*
* Minecraft Forge
* Copyright (c) 2016.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.common.eventhandler;
import java.lang.reflect.Type;
public interface IGenericEvent<T>
{
Type getGenericType();
}