|
| 1 | +package io.github.crucible.api; |
| 2 | + |
| 3 | +import cpw.mods.fml.common.FMLCommonHandler; |
| 4 | +import cpw.mods.fml.common.eventhandler.*; |
| 5 | +import io.github.crucible.CrucibleModContainer; |
| 6 | +import io.github.crucible.eventfactory.PluginClassLoaderFactory; |
| 7 | +import net.minecraftforge.common.MinecraftForge; |
| 8 | +import org.bukkit.plugin.Plugin; |
| 9 | +import org.objectweb.asm.Type; |
| 10 | + |
| 11 | +import java.lang.reflect.Method; |
| 12 | +import java.lang.reflect.Modifier; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.Optional; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.concurrent.ConcurrentHashMap; |
| 18 | + |
| 19 | +public class CrucibleEventBus { |
| 20 | + |
| 21 | + /** |
| 22 | + * Register an object to the specific forge event bus! |
| 23 | + * |
| 24 | + * |
| 25 | + * @param plugin The plugin that is registering the event, can be 'null' |
| 26 | + * |
| 27 | + * @param bus The event bus to register to: |
| 28 | + * - You can use {@link MinecraftForge#EVENT_BUS} |
| 29 | + * - You can use {@link FMLCommonHandler#bus()} |
| 30 | + * - Any other bus. |
| 31 | + * |
| 32 | + * @param target Either a {@link Class} or an arbitrary object. |
| 33 | + * The object maybe have methods annotated with {@link SubscribeEvent} |
| 34 | + * The class maybe have static methods annotated with {@link SubscribeEvent} |
| 35 | + */ |
| 36 | + @SuppressWarnings("unchecked") |
| 37 | + public static void register(Plugin plugin, EventBus bus, Object target) { |
| 38 | + ConcurrentHashMap<Object, ?> listeners = bus.getListeners(); |
| 39 | + if (!listeners.containsKey(target)) { |
| 40 | + if (target.getClass() == Class.class) { |
| 41 | + registerClass(plugin, (Class<?>) target, bus); |
| 42 | + } else { |
| 43 | + registerObject(plugin, target, bus); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static void registerClass(Plugin plugin, final Class<?> clazz, EventBus bus) { |
| 49 | + Arrays.stream(clazz.getMethods()). |
| 50 | + filter(m -> Modifier.isStatic(m.getModifiers())). |
| 51 | + filter(m -> m.isAnnotationPresent(SubscribeEvent.class)). |
| 52 | + forEach(m -> registerListener(plugin, clazz, m, m, bus)); |
| 53 | + } |
| 54 | + |
| 55 | + private static void registerObject(Plugin plugin, final Object obj, EventBus bus) { |
| 56 | + final HashSet<Class<?>> classes = new HashSet<>(); |
| 57 | + typesFor(obj.getClass(), classes); |
| 58 | + Arrays.stream(obj.getClass().getMethods()). |
| 59 | + filter(m -> !Modifier.isStatic(m.getModifiers())). |
| 60 | + forEach(m -> classes.stream(). |
| 61 | + map(c -> getDeclMethod(c, m)). |
| 62 | + filter(rm -> rm.isPresent() && rm.get().isAnnotationPresent(SubscribeEvent.class)). |
| 63 | + findFirst(). |
| 64 | + ifPresent(rm -> registerListener(plugin, obj, m, rm.get(), bus))); |
| 65 | + } |
| 66 | + |
| 67 | + private static void registerListener(Plugin plugin, final Object target, final Method method, final Method real, EventBus bus) { |
| 68 | + Class<?>[] parameterTypes = method.getParameterTypes(); |
| 69 | + if (parameterTypes.length != 1) { |
| 70 | + throw new IllegalArgumentException( |
| 71 | + "Method " + method + " has @SubscribeEvent annotation. " + |
| 72 | + "It has " + parameterTypes.length + " arguments, " + |
| 73 | + "but event handler methods require a single argument only." |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + Class<?> eventType = parameterTypes[0]; |
| 78 | + |
| 79 | + if (!Event.class.isAssignableFrom(eventType)) { |
| 80 | + throw new IllegalArgumentException( |
| 81 | + "Method " + method + " has @SubscribeEvent annotation, " + |
| 82 | + "but takes an argument that is not an Event subtype : " + eventType); |
| 83 | + } |
| 84 | + |
| 85 | + register(plugin, eventType, target, real, bus); |
| 86 | + } |
| 87 | + |
| 88 | + private static void register(Plugin plugin, Class<?> eventType, Object target, Method method, EventBus bus) { |
| 89 | + String pluginName = (plugin != null ? plugin.getName() : "null"); |
| 90 | + try { |
| 91 | + PluginClassLoaderFactory factory = new PluginClassLoaderFactory(); |
| 92 | + IEventListener iEventListener = factory.create(method, target); |
| 93 | + String readable = "ASM_CRUCIBLE: (Plugin='" + pluginName + "') " + target + " " + method.getName() + Type.getMethodDescriptor(method); |
| 94 | + ASMEventHandler asm = new ASMEventHandler(iEventListener, target, method, CrucibleModContainer.instance, readable); |
| 95 | + bus.crucible_register(asm, eventType, target, method, CrucibleModContainer.instance); |
| 96 | + } catch (Throwable e) { |
| 97 | + CrucibleModContainer.logger.error("Error registering event handler for the plugin ({}): {} {}", pluginName, eventType, method, e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // ----------------------------------------------------------------------------------------------------------------- |
| 102 | + // The following methods are utilities that come from isnide the EventBus on modern forge. |
| 103 | + // ----------------------------------------------------------------------------------------------------------------- |
| 104 | + |
| 105 | + private static void typesFor(final Class<?> clz, final Set<Class<?>> visited) { |
| 106 | + if (clz.getSuperclass() == null) return; |
| 107 | + typesFor(clz.getSuperclass(),visited); |
| 108 | + Arrays.stream(clz.getInterfaces()).forEach(i->typesFor(i, visited)); |
| 109 | + visited.add(clz); |
| 110 | + } |
| 111 | + |
| 112 | + private static Optional<Method> getDeclMethod(final Class<?> clz, final Method in) { |
| 113 | + try { |
| 114 | + return Optional.of(clz.getDeclaredMethod(in.getName(), in.getParameterTypes())); |
| 115 | + } catch (NoSuchMethodException nse) { |
| 116 | + return Optional.empty(); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments