Search in sources :

Example 1 with Cancellable

use of org.spongepowered.api.event.Cancellable 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)

Example 2 with Cancellable

use of org.spongepowered.api.event.Cancellable in project LanternServer by LanternPowered.

the class CancellationEventFilterDelegate method write.

@Override
public int write(String name, ClassWriter cw, MethodVisitor mv, Method method, int locals) {
    if (this.state == Tristate.UNDEFINED) {
        return locals;
    }
    if (!Cancellable.class.isAssignableFrom(method.getParameters()[0].getType())) {
        throw new IllegalStateException("Attempted to filter a non-cancellable event type by its cancellation status");
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Cancellable.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Cancellable.class), "isCancelled", "()Z", true);
    Label success = new Label();
    if (this.state == Tristate.TRUE) {
        mv.visitJumpInsn(IFNE, success);
    } else {
        mv.visitJumpInsn(IFEQ, success);
    }
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(success);
    return locals;
}
Also used : Cancellable(org.spongepowered.api.event.Cancellable) Label(org.objectweb.asm.Label)

Example 3 with Cancellable

use of org.spongepowered.api.event.Cancellable in project SpongeCommon by SpongePowered.

the class CancellationEventFilterDelegate method write.

@Override
public int write(final String name, final ClassWriter cw, final MethodVisitor mv, final ListenerClassVisitor.DiscoveredMethod method, final int locals) throws ClassNotFoundException {
    if (this.state == Tristate.UNDEFINED) {
        return locals;
    }
    if (!Cancellable.class.isAssignableFrom(method.parameterTypes()[0].clazz())) {
        throw new IllegalStateException("Attempted to filter a non-cancellable event type by its cancellation status");
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Cancellable.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Cancellable.class), "isCancelled", "()Z", true);
    final Label success = new Label();
    if (this.state == Tristate.TRUE) {
        mv.visitJumpInsn(IFNE, success);
    } else {
        mv.visitJumpInsn(IFEQ, success);
    }
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(success);
    return locals;
}
Also used : Cancellable(org.spongepowered.api.event.Cancellable) Label(org.objectweb.asm.Label)

Example 4 with Cancellable

use of org.spongepowered.api.event.Cancellable in project SpongeCommon by SpongePowered.

the class TransactionalCaptureSupplier method generateEventForTransaction.

@SuppressWarnings("unchecked")
private static <E extends Event & Cancellable> void generateEventForTransaction(@NonNull final GameTransaction<E> pointer, @Nullable final GameTransaction<@NonNull ?> parent, final PhaseContext<@NonNull ?> context, final ImmutableList.Builder<EventByTransaction<@NonNull ?>> builder, final ImmutableList<GameTransaction<E>> transactions, final ImmutableMultimap.Builder<TransactionType, ? extends Event> transactionPostEventBuilder) {
    final Optional<BiConsumer<PhaseContext<@NonNull ?>, CauseStackManager.StackFrame>> frameMutator = pointer.getFrameMutator(parent);
    final PhaseTracker instance = PhaseTracker.getInstance();
    try (final CauseStackManager.StackFrame frame = frameMutator.map(mutator -> {
        final CauseStackManager.StackFrame transactionFrame = instance.pushCauseFrame();
        mutator.accept(context, transactionFrame);
        return transactionFrame;
    }).orElseGet(instance::pushCauseFrame)) {
        final Optional<E> generatedEvent = pointer.generateEvent(context, parent, transactions, instance.currentCause());
        generatedEvent.ifPresent(e -> {
            final EventByTransaction<E> element = new EventByTransaction<>(e, transactions, parent, pointer);
            builder.add(element);
            ((ImmutableMultimap.Builder) transactionPostEventBuilder).put(pointer.getTransactionType(), e);
        });
        for (final GameTransaction<E> transaction : transactions) {
            if (transaction.sideEffects == null || transaction.sideEffects.isEmpty()) {
                continue;
            }
            generatedEvent.ifPresent(frame::pushCause);
            for (final ResultingTransactionBySideEffect sideEffect : transaction.sideEffects) {
                if (sideEffect.head == null) {
                    continue;
                }
                builder.addAll(TransactionalCaptureSupplier.batchTransactions(sideEffect.head, pointer, context, transactionPostEventBuilder));
            }
        }
    }
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) Iterator(java.util.Iterator) Event(org.spongepowered.api.event.Event) Spliterators(java.util.Spliterators) Sponge(org.spongepowered.api.Sponge) SpongeCommon(org.spongepowered.common.SpongeCommon) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) MonotonicNonNull(org.checkerframework.checker.nullness.qual.MonotonicNonNull) PrepareBlockDrops(org.spongepowered.common.event.tracking.context.transaction.effect.PrepareBlockDrops) Objects(java.util.Objects) ImmutableList(com.google.common.collect.ImmutableList) Cancellable(org.spongepowered.api.event.Cancellable) ICaptureSupplier(org.spongepowered.common.event.tracking.context.ICaptureSupplier) PhaseContext(org.spongepowered.common.event.tracking.PhaseContext) StringJoiner(java.util.StringJoiner) BiConsumer(java.util.function.BiConsumer) Optional(java.util.Optional) TransactionType(org.spongepowered.common.event.tracking.context.transaction.type.TransactionType) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Collections(java.util.Collections) Spliterator(java.util.Spliterator) Nullable(org.checkerframework.checker.nullness.qual.Nullable) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BiConsumer(java.util.function.BiConsumer)

Example 5 with Cancellable

use of org.spongepowered.api.event.Cancellable in project SpongeCommon by SpongePowered.

the class CancellationEventFilterDelegate method write.

@Override
public int write(String name, ClassWriter cw, MethodVisitor mv, Method method, int locals) {
    if (this.state == Tristate.UNDEFINED) {
        return locals;
    }
    if (!Cancellable.class.isAssignableFrom(method.getParameters()[0].getType())) {
        throw new IllegalStateException("Attempted to filter a non-cancellable event type by its cancellation status");
    }
    mv.visitVarInsn(ALOAD, 1);
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Cancellable.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Cancellable.class), "isCancelled", "()Z", true);
    Label success = new Label();
    if (this.state == Tristate.TRUE) {
        mv.visitJumpInsn(IFNE, success);
    } else {
        mv.visitJumpInsn(IFEQ, success);
    }
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(success);
    return locals;
}
Also used : Cancellable(org.spongepowered.api.event.Cancellable) Label(org.objectweb.asm.Label)

Aggregations

Cancellable (org.spongepowered.api.event.Cancellable)8 Label (org.objectweb.asm.Label)3 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)2 MonotonicNonNull (org.checkerframework.checker.nullness.qual.MonotonicNonNull)2 NonNull (org.checkerframework.checker.nullness.qual.NonNull)2 CauseStackManager (org.spongepowered.api.event.CauseStackManager)2 Event (org.spongepowered.api.event.Event)2 RegisteredListener (org.spongepowered.common.event.RegisteredListener)2 TransactionType (org.spongepowered.common.event.tracking.context.transaction.type.TransactionType)2 ImmutableList (com.google.common.collect.ImmutableList)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Spliterator (java.util.Spliterator)1 Spliterators (java.util.Spliterators)1 StringJoiner (java.util.StringJoiner)1 BiConsumer (java.util.function.BiConsumer)1 ClientboundBlockBreakAckPacket (net.minecraft.network.protocol.game.ClientboundBlockBreakAckPacket)1 ModContainer (net.minecraftforge.fml.common.ModContainer)1