Search in sources :

Example 1 with IMixinBlockEventData

use of org.spongepowered.common.interfaces.block.IMixinBlockEventData in project SpongeCommon by SpongePowered.

the class MixinWorldServer method onAddBlockEvent.

@Redirect(method = "addBlockEvent", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/WorldServer$ServerBlockEventList;add(Ljava/lang/Object;)Z", remap = false))
private boolean onAddBlockEvent(WorldServer.ServerBlockEventList list, Object obj, BlockPos pos, Block blockIn, int eventId, int eventParam) {
    final BlockEventData blockEventData = (BlockEventData) obj;
    IMixinBlockEventData blockEvent = (IMixinBlockEventData) blockEventData;
    // This is very common with pistons as they add block events while blocks are being notified.
    if (blockIn instanceof BlockPistonBase) {
        // We only fire pre events for pistons
        if (SpongeCommonEventFactory.handlePistonEvent(this, list, obj, pos, blockIn, eventId, eventParam)) {
            return false;
        }
        blockEvent.setCaptureBlocks(false);
    // TODO BLOCK_EVENT flag
    } else if (SpongeCommonEventFactory.callChangeBlockEventPre(this, pos).isCancelled()) {
        return false;
    }
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    final PhaseData currentPhase = phaseTracker.getCurrentPhaseData();
    final IPhaseState phaseState = currentPhase.state;
    if (phaseState.ignoresBlockEvent()) {
        return list.add((BlockEventData) obj);
    }
    final PhaseContext<?> context = currentPhase.context;
    final LocatableBlock locatable = LocatableBlock.builder().location(new Location<>(this, pos.getX(), pos.getY(), pos.getZ())).state(this.getBlock(pos.getX(), pos.getY(), pos.getZ())).build();
    blockEvent.setTickBlock(locatable);
    phaseState.addNotifierToBlockEvent(context, this, pos, blockEvent);
    return list.add((BlockEventData) obj);
}
Also used : IMixinBlockEventData(org.spongepowered.common.interfaces.block.IMixinBlockEventData) PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) PhaseData(org.spongepowered.common.event.tracking.PhaseData) BlockPistonBase(net.minecraft.block.BlockPistonBase) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) LocatableBlock(org.spongepowered.api.world.LocatableBlock) IMixinBlockEventData(org.spongepowered.common.interfaces.block.IMixinBlockEventData) BlockEventData(net.minecraft.block.BlockEventData) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 2 with IMixinBlockEventData

use of org.spongepowered.common.interfaces.block.IMixinBlockEventData in project SpongeCommon by SpongePowered.

the class TrackingUtil method fireMinecraftBlockEvent.

public static boolean fireMinecraftBlockEvent(WorldServer worldIn, BlockEventData event) {
    IBlockState currentState = worldIn.getBlockState(event.getPosition());
    final IMixinBlockEventData blockEvent = (IMixinBlockEventData) event;
    IPhaseState<?> phase = blockEvent.getCaptureBlocks() ? TickPhase.Tick.BLOCK_EVENT : TickPhase.Tick.NO_CAPTURE_BLOCK;
    final PhaseContext<?> phaseContext = phase.createPhaseContext();
    Object source = blockEvent.getTickBlock() != null ? blockEvent.getTickBlock() : blockEvent.getTickTileEntity();
    if (source != null) {
        phaseContext.source(source);
    } else {
        // No source present which means we are ignoring the phase state
        boolean result = currentState.onBlockEventReceived(worldIn, event.getPosition(), event.getEventID(), event.getEventParameter());
        return result;
    }
    if (blockEvent.getSourceUser() != null) {
        phaseContext.notifier(blockEvent.getSourceUser());
    }
    try (PhaseContext<?> o = phaseContext.buildAndSwitch()) {
        return currentState.onBlockEventReceived(worldIn, event.getPosition(), event.getEventID(), event.getEventParameter());
    }
}
Also used : IMixinBlockEventData(org.spongepowered.common.interfaces.block.IMixinBlockEventData) IBlockState(net.minecraft.block.state.IBlockState)

Aggregations

IMixinBlockEventData (org.spongepowered.common.interfaces.block.IMixinBlockEventData)2 BlockEventData (net.minecraft.block.BlockEventData)1 BlockPistonBase (net.minecraft.block.BlockPistonBase)1 IBlockState (net.minecraft.block.state.IBlockState)1 LocatableBlock (org.spongepowered.api.world.LocatableBlock)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1 IPhaseState (org.spongepowered.common.event.tracking.IPhaseState)1 PhaseData (org.spongepowered.common.event.tracking.PhaseData)1 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)1