Search in sources :

Example 1 with IMixinASMEventHandler

use of org.spongepowered.mod.interfaces.IMixinASMEventHandler in project SpongeForge by SpongePowered.

the class SpongeModEventManager method post.

// Uses Forge mixins
public boolean post(Event spongeEvent, net.minecraftforge.fml.common.eventhandler.Event forgeEvent, IEventListener[] listeners, boolean useCauseStackManager) {
    checkNotNull(forgeEvent, "forgeEvent");
    if (spongeEvent == null) {
        // Fired by Forge
        spongeEvent = ((IMixinEvent) forgeEvent).createSpongeEvent();
    }
    boolean isNotSameEvent = spongeEvent != forgeEvent;
    RegisteredListener.Cache listenerCache = getHandlerCache(spongeEvent);
    // Fire events to plugins before modifications
    for (Order order : Order.values()) {
        post(spongeEvent, listenerCache.getListenersByOrder(order), true, false, useCauseStackManager);
    }
    if (isNotSameEvent) {
        syncToForge(forgeEvent, spongeEvent);
    }
    // If plugin cancelled event before modifications, ignore mods
    if (listeners.length > 0 && !forgeEvent.isCanceled()) {
        for (IEventListener listener : listeners) {
            try {
                if (listener instanceof IMixinASMEventHandler) {
                    IMixinASMEventHandler modListener = (IMixinASMEventHandler) listener;
                    modListener.getTimingsHandler().startTimingIfSync();
                    listener.invoke(forgeEvent);
                    modListener.getTimingsHandler().stopTimingIfSync();
                } else {
                    listener.invoke(forgeEvent);
                }
            } catch (Throwable throwable) {
                this.logger.error("Encountered an exception while processing a Forge event listener", throwable);
            }
        }
        if (isNotSameEvent) {
            syncToSponge(forgeEvent, spongeEvent);
        }
    }
    // Fire events to plugins after modifications (default)
    for (Order order : Order.values()) {
        post(spongeEvent, listenerCache.getListenersByOrder(order), false, false, useCauseStackManager);
    }
    if (isNotSameEvent) {
        syncToForge(forgeEvent, spongeEvent);
    }
    return forgeEvent.isCancelable() && forgeEvent.isCanceled();
}
Also used : Order(org.spongepowered.api.event.Order) IEventListener(net.minecraftforge.fml.common.eventhandler.IEventListener) IMixinASMEventHandler(org.spongepowered.mod.interfaces.IMixinASMEventHandler) RegisteredListener(org.spongepowered.common.event.RegisteredListener)

Example 2 with IMixinASMEventHandler

use of org.spongepowered.mod.interfaces.IMixinASMEventHandler 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)

Aggregations

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