Search in sources :

Example 1 with CollideBlockEvent

use of org.spongepowered.api.event.block.CollideBlockEvent in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method handleCollideBlockEvent.

public static boolean handleCollideBlockEvent(Block block, net.minecraft.world.World world, BlockPos pos, IBlockState state, net.minecraft.entity.Entity entity, Direction direction) {
    if (pos.getY() <= 0) {
        return false;
    }
    final PhaseTracker phaseTracker = PhaseTracker.getInstance();
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(entity);
        if (!(entity instanceof EntityPlayer)) {
            IMixinEntity spongeEntity = (IMixinEntity) entity;
            Optional<User> user = spongeEntity.getCreatorUser();
            if (user.isPresent()) {
                Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, user.get());
            }
        }
        // TODO: Add target side support
        CollideBlockEvent event = SpongeEventFactory.createCollideBlockEvent(Sponge.getCauseStackManager().getCurrentCause(), (BlockState) state, new Location<>((World) world, VecHelper.toVector3d(pos)), direction);
        boolean cancelled = SpongeImpl.postEvent(event);
        if (!cancelled) {
            IMixinEntity spongeEntity = (IMixinEntity) entity;
            if (!pos.equals(spongeEntity.getLastCollidedBlockPos())) {
                final PhaseData peek = phaseTracker.getCurrentPhaseData();
                final Optional<User> notifier = peek.context.getNotifier();
                if (notifier.isPresent()) {
                    IMixinChunk spongeChunk = (IMixinChunk) world.getChunkFromBlockCoords(pos);
                    spongeChunk.addTrackedBlockPosition(block, pos, notifier.get(), PlayerTracker.Type.NOTIFIER);
                }
            }
        }
        return cancelled;
    }
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) User(org.spongepowered.api.entity.living.player.User) PhaseData(org.spongepowered.common.event.tracking.PhaseData) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) EntityPlayer(net.minecraft.entity.player.EntityPlayer) IMixinEntity(org.spongepowered.common.interfaces.entity.IMixinEntity) World(org.spongepowered.api.world.World) CollideBlockEvent(org.spongepowered.api.event.block.CollideBlockEvent)

Aggregations

EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 User (org.spongepowered.api.entity.living.player.User)1 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)1 CollideBlockEvent (org.spongepowered.api.event.block.CollideBlockEvent)1 World (org.spongepowered.api.world.World)1 PhaseData (org.spongepowered.common.event.tracking.PhaseData)1 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)1 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)1 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)1