Search in sources :

Example 1 with BlockEventTickContext

use of org.spongepowered.common.event.tracking.phase.tick.BlockEventTickContext in project SpongeCommon by SpongePowered.

the class TrackingUtil method fireMinecraftBlockEvent.

public static boolean fireMinecraftBlockEvent(final ServerLevel worldIn, final BlockEventData event, final net.minecraft.world.level.block.state.BlockState currentState) {
    final TrackableBlockEventDataBridge blockEvent = (TrackableBlockEventDataBridge) event;
    @Nullable final Object source = blockEvent.bridge$getTileEntity() != null ? blockEvent.bridge$getTileEntity() : blockEvent.bridge$getTickingLocatable();
    if (source == null) {
        // No source present which means we are ignoring the phase state
        return currentState.triggerEvent(worldIn, event.getPos(), event.getParamA(), event.getParamB());
    }
    final BlockEventTickContext phaseContext = TickPhase.Tick.BLOCK_EVENT.createPhaseContext(PhaseTracker.SERVER);
    phaseContext.source(source);
    final UUID user = ((TrackableBlockEventDataBridge) event).bridge$getSourceUserUUID();
    if (user != null) {
        phaseContext.creator = user;
        phaseContext.notifier = user;
    }
    boolean result = true;
    try (final BlockEventTickContext o = phaseContext) {
        o.buildAndSwitch();
        phaseContext.setEventSucceeded(currentState.triggerEvent(worldIn, event.getPos(), event.getParamA(), event.getParamB()));
        // We need to grab the result here as the phase context close will trigger a reset
        result = phaseContext.wasNotCancelled();
    }
    // at which point we want to keep track of the return value from the target, and from the block events.
    return result;
}
Also used : TrackableBlockEventDataBridge(org.spongepowered.common.bridge.world.level.TrackableBlockEventDataBridge) BlockEventTickContext(org.spongepowered.common.event.tracking.phase.tick.BlockEventTickContext) UUID(java.util.UUID) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

UUID (java.util.UUID)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 TrackableBlockEventDataBridge (org.spongepowered.common.bridge.world.level.TrackableBlockEventDataBridge)1 BlockEventTickContext (org.spongepowered.common.event.tracking.phase.tick.BlockEventTickContext)1