use of org.spongepowered.common.event.tracking.context.transaction.type.TransactionType 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.common.event.tracking.context.transaction.type.TransactionType in project SpongeCommon by SpongePowered.
the class TransactionalCaptureSupplier method processTransactions.
@SuppressWarnings("unchecked")
public boolean processTransactions(final PhaseContext<@NonNull ?> context) {
if (this.head == null) {
return false;
}
final ImmutableMultimap.Builder<TransactionType, ? extends Event> builder = ImmutableMultimap.builder();
final ImmutableList<EventByTransaction<@NonNull ?>> batched = TransactionalCaptureSupplier.batchTransactions(this.head, null, context, builder);
boolean cancelledAny = false;
for (final EventByTransaction<@NonNull ?> eventWithTransactions : batched) {
final Event event = eventWithTransactions.event;
if (eventWithTransactions.isParentOrDeciderCancelled()) {
cancelledAny = true;
eventWithTransactions.markCancelled();
continue;
}
Sponge.eventManager().post(event);
if (event instanceof Cancellable && ((Cancellable) event).isCancelled()) {
eventWithTransactions.markCancelled();
cancelledAny = true;
}
if (((GameTransaction) eventWithTransactions.decider).markCancelledTransactions(event, eventWithTransactions.transactions)) {
cancelledAny = true;
}
for (final GameTransaction<@NonNull ?> transaction : eventWithTransactions.transactions) {
if (transaction.cancelled) {
((GameTransaction) transaction).markEventAsCancelledIfNecessary(eventWithTransactions.event);
}
if (!transaction.cancelled) {
((GameTransaction) transaction).postProcessEvent(context, event);
}
}
}
if (cancelledAny) {
for (final EventByTransaction<@NonNull ?> eventByTransaction : batched.reverse()) {
if (eventByTransaction.decider.cancelled) {
((GameTransaction) eventByTransaction.decider).markEventAsCancelledIfNecessary(eventByTransaction.event);
}
for (final GameTransaction<@NonNull ?> gameTransaction : eventByTransaction.transactions.reverse()) {
if (gameTransaction.cancelled) {
((GameTransaction) gameTransaction).restore(context, eventByTransaction.event);
}
}
}
}
builder.build().asMap().forEach((transactionType, events) -> transactionType.createAndProcessPostEvents(context, events));
return !cancelledAny;
}
Aggregations