@SuppressWarnings("unchecked") public static List<IEventListener> getEventListeners(World world){ if(eventlisteners.containsKey(world)) return eventlisteners.get(world); try{ List<IEventListener> list = null; for(Field field : World.class.getDeclaredFields()){ if(field.getType() == World.class){ field.setAccessible(true); list = (List<IEventListener>) field.get(world); break; } } eventlisteners.put(world, list); return list; }catch(Exception e){ e.printStackTrace(); } return null; }
public static void fix() throws Exception { Field f = MinecraftForge.EVENT_BUS.getClass().getDeclaredField("listeners"); f.setAccessible(true); ConcurrentHashMap<Object, ArrayList<IEventListener>> listeners = (ConcurrentHashMap<Object, ArrayList<IEventListener>>) f.get(MinecraftForge.EVENT_BUS); for(Map.Entry<Object, ArrayList<IEventListener>> entry : listeners.entrySet()) { if (entry.getKey().getClass().equals(SensitiveCode.class)) { MinecraftForge.EVENT_BUS.unregister(entry.getKey()); Pixelmon.EVENT_BUS.unregister(entry.getKey()); SpawnerBase.SPAWN_DENIAL_BUS.unregister(entry.getKey()); return; } } }
@Override public void OnPreInit(FMLPreInitializationEvent event) { checkNotNull(event, "event"); this.configuration.Initialize(event.getSuggestedConfigurationFile()); // Register all the new items // (do this before registering the things that may depend on them) ItemConfiguration itemConfiguration = this.configuration.GetItemConfiguration(); this.itemRegistrar.RegisterNewItems(itemConfiguration, this.itemRegistry); // Register all the event handlers. this.eventSubscriber.Initialize(); EventConfiguration eventConfiguration = this.configuration.GetEventConfiguration(); for (EventType eventType : eventConfiguration.GetRecognizedEventTypes()) { for (IEventListener handler : eventConfiguration.GetEventHandlers(eventType)) { this.eventSubscriber.SubscribeToEvent(eventType, handler); } } }
@Override public void SubscribeToEvent(EventType eventType, IEventListener handler) { checkNotNull(handler, "handler"); synchronized(this.eventListenerMap) { this.eventListenerMap.put(eventType, handler); } }
@SubscribeEvent public void OnLivingDropsEvent(LivingDropsEvent event) { checkNotNull(event, "event"); for (IEventListener handler : eventListenerMap.get(EventType.LivingDrops)) { handler.invoke(event); } }
@SubscribeEvent public void OnLivingUpdateEvent(LivingUpdateEvent event) { checkNotNull(event, "event"); for (IEventListener handler : eventListenerMap.get(EventType.LivingUpdate)) { handler.invoke(event); } }
@SubscribeEvent public void OnEntityConstructingEvent(EntityConstructing event) { checkNotNull(event, "event"); for (IEventListener handler : eventListenerMap.get(EventType.EntityConstructing)) { handler.invoke(event); } }
static IEventListener[] getAllHandler(Class<? extends Event> clazz, int busId) { ListenerList list = ReflectionHelper.allocateInstance(clazz).getListenerList(); return list == null ? new IEventListener[0] : list.lists[busId].getListeners(); }
@Override public Iterable<IEventListener> GetEventHandlers(EventType eventType) { return ImmutableList.of(); }
Iterable<IEventListener> GetEventHandlers(EventType eventType);
void SubscribeToEvent(EventType eventType, IEventListener handler);