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();
}
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;
}
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;
}
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));
}
}
}
}
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;
}
Aggregations