Search in sources :

Example 1 with SpongeModEventManager

use of org.spongepowered.mod.event.SpongeModEventManager in project SpongeForge by SpongePowered.

the class MixinEventBus method onUnregisterListener.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Redirect(method = "unregister", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/eventhandler/ListenerList;unregisterAll(ILnet/minecraftforge/fml/common/eventhandler/IEventListener;)V", remap = false))
public void onUnregisterListener(int id, IEventListener listener) {
    ListenerList.unregisterAll(id, listener);
    SpongeModEventManager manager = ((SpongeModEventManager) SpongeImpl.getGame().getEventManager());
    for (Class clazz : TypeToken.of(checkNotNull(this.forgeListenerRegistry.remove(listener))).getTypes().rawTypes()) {
        Collection<Class<? extends org.spongepowered.api.event.Event>> spongeEvents = manager.forgeToSpongeEventMapping.get(clazz);
        if (spongeEvents != null) {
            for (Class<? extends org.spongepowered.api.event.Event> event : spongeEvents) {
                manager.checker.unregisterListenerFor(event);
            }
        }
    }
}
Also used : SpongeModEventManager(org.spongepowered.mod.event.SpongeModEventManager) Event(net.minecraftforge.fml.common.eventhandler.Event) WorldEvent(net.minecraftforge.event.world.WorldEvent) LivingDropsEvent(net.minecraftforge.event.entity.living.LivingDropsEvent) AttackEntityEvent(net.minecraftforge.event.entity.player.AttackEntityEvent) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 2 with SpongeModEventManager

use of org.spongepowered.mod.event.SpongeModEventManager in project SpongeForge by SpongePowered.

the class MixinEventBus method post.

@Override
public boolean post(Event event, boolean forced) {
    org.spongepowered.api.event.Event spongeEvent = null;
    if (!isSpongeSetUp) {
        try {
            Sponge.getCauseStackManager();
        } catch (Exception e) {
            return false;
        }
    }
    isSpongeSetUp = true;
    // TODO verify the frame is necessary here or if it can be placed elsewhere
    final boolean isMainThread = Sponge.isServerAvailable() && Sponge.getServer().isMainThread();
    try (final CauseStackManager.StackFrame frame = isMainThread ? Sponge.getCauseStackManager().pushCauseFrame() : null) {
        if (!forced) {
            if (!isEventAllowed(event)) {
                return false;
            }
            spongeEvent = SpongeForgeEventFactory.createSpongeEvent(event);
        }
        IEventListener[] listeners = event.getListenerList().getListeners(this.busID);
        if (!forced && (event instanceof org.spongepowered.api.event.Event || spongeEvent != null) && !Sponge.getGame().getPlatform().getExecutionType().isClient()) {
            boolean cancelled = ((SpongeModEventManager) SpongeImpl.getGame().getEventManager()).post(spongeEvent, event, listeners, SpongeModEventManager.shouldUseCauseStackManager(false));
            if (!cancelled) {
                SpongeForgeEventFactory.onForgePost(event);
            }
            return cancelled;
        }
        listeners = event.getListenerList().getListeners(this.busID);
        int index = 0;
        IMixinASMEventHandler modListener = null;
        try {
            for (; index < listeners.length; index++) {
                final IEventListener listener = listeners[index];
                if (listener instanceof IMixinASMEventHandler) {
                    modListener = (IMixinASMEventHandler) listener;
                    modListener.getTimingsHandler().startTimingIfSync();
                    try (PhaseContext<?> context = SpongeForgeEventHooks.preEventPhaseCheck(listener, event)) {
                        listener.invoke(event);
                    }
                    modListener.getTimingsHandler().stopTimingIfSync();
                } else {
                    listener.invoke(event);
                }
            }
        } catch (Throwable throwable) {
            if (modListener != null) {
                modListener.getTimingsHandler().stopTimingIfSync();
            }
            this.exceptionHandler.handleException((EventBus) (Object) this, event, listeners, index, throwable);
            throw new RuntimeException(throwable);
        }
        return (event.isCancelable() ? event.isCanceled() : false);
    }
}
Also used : IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus) EventBus(net.minecraftforge.fml.common.eventhandler.EventBus) IEventListener(net.minecraftforge.fml.common.eventhandler.IEventListener) SpongeModEventManager(org.spongepowered.mod.event.SpongeModEventManager) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Event(net.minecraftforge.fml.common.eventhandler.Event) WorldEvent(net.minecraftforge.event.world.WorldEvent) LivingDropsEvent(net.minecraftforge.event.entity.living.LivingDropsEvent) AttackEntityEvent(net.minecraftforge.event.entity.player.AttackEntityEvent) IMixinASMEventHandler(org.spongepowered.mod.interfaces.IMixinASMEventHandler)

Example 3 with SpongeModEventManager

use of org.spongepowered.mod.event.SpongeModEventManager in project SpongeForge by SpongePowered.

the class MixinEventBus method onRegister.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Redirect(method = "register(Ljava/lang/Class;Ljava/lang/Object;Ljava/lang/reflect/Method;Lnet/minecraftforge/fml/common/ModContainer;)V", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/eventhandler/ListenerList;register(ILnet/minecraftforge/fml/common/eventhandler/EventPriority;Lnet/minecraftforge/fml/common/eventhandler/IEventListener;)V", remap = false))
public void onRegister(ListenerList list, int id, EventPriority priority, IEventListener listener, Class<? extends Event> eventType, Object target, Method method, ModContainer owner) {
    list.register(id, priority, listener);
    SpongeModEventManager manager = ((SpongeModEventManager) SpongeImpl.getGame().getEventManager());
    for (Class clazz : TypeToken.of(eventType).getTypes().rawTypes()) {
        Collection<Class<? extends org.spongepowered.api.event.Event>> spongeEvents = manager.forgeToSpongeEventMapping.get(clazz);
        if (spongeEvents != null) {
            for (Class<? extends org.spongepowered.api.event.Event> event : spongeEvents) {
                manager.checker.registerListenerFor(event);
            }
        }
    }
    this.forgeListenerRegistry.put(listener, eventType);
}
Also used : SpongeModEventManager(org.spongepowered.mod.event.SpongeModEventManager) Event(net.minecraftforge.fml.common.eventhandler.Event) WorldEvent(net.minecraftforge.event.world.WorldEvent) LivingDropsEvent(net.minecraftforge.event.entity.living.LivingDropsEvent) AttackEntityEvent(net.minecraftforge.event.entity.player.AttackEntityEvent) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

LivingDropsEvent (net.minecraftforge.event.entity.living.LivingDropsEvent)3 AttackEntityEvent (net.minecraftforge.event.entity.player.AttackEntityEvent)3 WorldEvent (net.minecraftforge.event.world.WorldEvent)3 Event (net.minecraftforge.fml.common.eventhandler.Event)3 SpongeModEventManager (org.spongepowered.mod.event.SpongeModEventManager)3 Redirect (org.spongepowered.asm.mixin.injection.Redirect)2 EventBus (net.minecraftforge.fml.common.eventhandler.EventBus)1 IEventListener (net.minecraftforge.fml.common.eventhandler.IEventListener)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 IMixinASMEventHandler (org.spongepowered.mod.interfaces.IMixinASMEventHandler)1 IMixinEventBus (org.spongepowered.mod.interfaces.IMixinEventBus)1