Search in sources :

Example 1 with AbstractEvent

use of org.spongepowered.api.event.impl.AbstractEvent in project SpongeForge by SpongePowered.

the class SpongeModEventManager method post.

@SuppressWarnings("unchecked")
private boolean post(Event event, List<RegisteredListener<?>> listeners, boolean beforeModifications, boolean forced, boolean useCauseStackManager) {
    ModContainer oldContainer = ((IMixinLoadController) SpongeMod.instance.getController()).getActiveModContainer();
    for (@SuppressWarnings("rawtypes") RegisteredListener listener : listeners) {
        ((IMixinLoadController) SpongeMod.instance.getController()).setActiveModContainer((ModContainer) listener.getPlugin());
        try {
            if (forced || (!listener.isBeforeModifications() && !beforeModifications) || (listener.isBeforeModifications() && beforeModifications)) {
                listener.getTimingsHandler().startTimingIfSync();
                if (event instanceof AbstractEvent) {
                    ((AbstractEvent) event).currentOrder = listener.getOrder();
                }
                if (useCauseStackManager) {
                    Sponge.getCauseStackManager().pushCause(listener.getPlugin());
                    try (CauseStackManager.StackFrame ignored = Sponge.getCauseStackManager().pushCauseFrame()) {
                        listener.handle(event);
                    }
                    Sponge.getCauseStackManager().popCause();
                } else {
                    listener.handle(event);
                }
            }
        } catch (Throwable e) {
            this.logger.error("Could not pass {} to {}", event.getClass().getSimpleName(), listener.getPlugin(), e);
        } finally {
            listener.getTimingsHandler().stopTimingIfSync();
        }
    }
    if (event instanceof AbstractEvent) {
        ((AbstractEvent) event).currentOrder = null;
    }
    ((IMixinLoadController) SpongeMod.instance.getController()).setActiveModContainer(oldContainer);
    return event instanceof Cancellable && ((Cancellable) event).isCancelled();
}
Also used : ModContainer(net.minecraftforge.fml.common.ModContainer) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Cancellable(org.spongepowered.api.event.Cancellable) AbstractEvent(org.spongepowered.api.event.impl.AbstractEvent) IMixinLoadController(org.spongepowered.mod.interfaces.IMixinLoadController) RegisteredListener(org.spongepowered.common.event.RegisteredListener)

Aggregations

ModContainer (net.minecraftforge.fml.common.ModContainer)1 Cancellable (org.spongepowered.api.event.Cancellable)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 AbstractEvent (org.spongepowered.api.event.impl.AbstractEvent)1 RegisteredListener (org.spongepowered.common.event.RegisteredListener)1 IMixinLoadController (org.spongepowered.mod.interfaces.IMixinLoadController)1