Search in sources :

Example 1 with EffectResult

use of org.spongepowered.common.event.tracking.context.transaction.effect.EffectResult in project SpongeCommon by SpongePowered.

the class ChunkPipeline method processChange.

@Nullable
public BlockState processChange(final PhaseContext<?> context, final BlockState currentState, final BlockState proposedState, final BlockPos pos, final int limit) {
    if (this.chunkEffects.isEmpty()) {
        return null;
    }
    final ServerLevel serverWorld = this.serverWorld.get();
    final int oldOpacity = currentState.getLightBlock(serverWorld, pos);
    final SpongeBlockChangeFlag flag = this.transaction.getBlockChangeFlag();
    @Nullable final BlockEntity existing = this.chunkSupplier.get().getBlockEntity(pos, LevelChunk.EntityCreationType.CHECK);
    PipelineCursor formerState = new PipelineCursor(currentState, oldOpacity, pos, existing, (Entity) null, limit);
    for (final ResultingTransactionBySideEffect effect : this.chunkEffects) {
        try (final EffectTransactor ignored = context.getTransactor().pushEffect(effect)) {
            final EffectResult result = effect.effect.processSideEffect(this, formerState, proposedState, flag, limit);
            if (result.hasResult) {
                return result.resultingState;
            }
            if (formerState.drops.isEmpty() && !result.drops.isEmpty()) {
                formerState = new PipelineCursor(currentState, oldOpacity, pos, existing, null, result.drops, limit);
            }
        }
    }
    // if we've gotten here, means something is wrong, we didn't build our effects right.
    return null;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) SpongeBlockChangeFlag(org.spongepowered.common.world.SpongeBlockChangeFlag) EffectResult(org.spongepowered.common.event.tracking.context.transaction.effect.EffectResult) ResultingTransactionBySideEffect(org.spongepowered.common.event.tracking.context.transaction.ResultingTransactionBySideEffect) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with EffectResult

use of org.spongepowered.common.event.tracking.context.transaction.effect.EffectResult in project SpongeCommon by SpongePowered.

the class WorldPipeline method processEffects.

public boolean processEffects(final PhaseContext<?> context, final BlockState currentState, final BlockState newProposedState, final BlockPos pos, @Nullable final Entity destroyer, final SpongeBlockChangeFlag flag, final int limit) {
    if (this.worldEffects.isEmpty()) {
        return false;
    }
    final ServerLevel serverWorld = Objects.requireNonNull(this.serverWorld).get();
    // Keep track of the existing block entity prior to processing the chunk pipeline
    // and the reasoning is that in several cases where the block entity that is being removed
    // will no longer be available. This could be avoided by having the "previous cursor" returned
    // from ChunkPipeline, but alas.... that's a refactor for another time.
    @Nullable final BlockEntity existing = this.chunkSupplier.get().getBlockEntity(pos, LevelChunk.EntityCreationType.CHECK);
    // We have to get the "old state" from
    @Nullable final BlockState oldState = this.chunkPipeline.processChange(context, currentState, newProposedState, pos, limit);
    if (oldState == null) {
        return false;
    }
    final int oldOpacity = oldState.getLightBlock(serverWorld, pos);
    PipelineCursor formerState = new PipelineCursor(oldState, oldOpacity, pos, existing, destroyer, limit);
    for (final ResultingTransactionBySideEffect effect : this.worldEffects) {
        try (final EffectTransactor ignored = context.getTransactor().pushEffect(effect)) {
            final EffectResult result = effect.effect.processSideEffect(this, formerState, newProposedState, flag, limit);
            if (result.hasResult) {
                return result.resultingState != null;
            }
            if (formerState.drops.isEmpty() && !result.drops.isEmpty()) {
                formerState = new PipelineCursor(oldState, oldOpacity, pos, existing, formerState.destroyer, result.drops, limit);
            }
        }
    }
    // if we've gotten here, means something is wrong, we didn't build our effects right.
    return false;
}
Also used : ServerLevel(net.minecraft.server.level.ServerLevel) BlockState(net.minecraft.world.level.block.state.BlockState) EffectResult(org.spongepowered.common.event.tracking.context.transaction.effect.EffectResult) ResultingTransactionBySideEffect(org.spongepowered.common.event.tracking.context.transaction.ResultingTransactionBySideEffect) EffectTransactor(org.spongepowered.common.event.tracking.context.transaction.EffectTransactor) Nullable(org.checkerframework.checker.nullness.qual.Nullable) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity)

Aggregations

ServerLevel (net.minecraft.server.level.ServerLevel)2 BlockEntity (net.minecraft.world.level.block.entity.BlockEntity)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 EffectTransactor (org.spongepowered.common.event.tracking.context.transaction.EffectTransactor)2 ResultingTransactionBySideEffect (org.spongepowered.common.event.tracking.context.transaction.ResultingTransactionBySideEffect)2 EffectResult (org.spongepowered.common.event.tracking.context.transaction.effect.EffectResult)2 BlockState (net.minecraft.world.level.block.state.BlockState)1 SpongeBlockChangeFlag (org.spongepowered.common.world.SpongeBlockChangeFlag)1