Search in sources :

Example 16 with LocatableBlock

use of org.spongepowered.api.world.LocatableBlock in project SpongeCommon by SpongePowered.

the class LocationBasedTickPhaseState method associateNeighborStateNotifier.

@Override
public void associateNeighborStateNotifier(T context, @Nullable BlockPos sourcePos, Block block, BlockPos notifyPos, WorldServer minecraftWorld, PlayerTracker.Type notifier) {
    if (sourcePos == null) {
        LocatableBlock locatableBlock = this.getLocatableBlockSourceFromContext(context);
        sourcePos = ((IMixinLocation) (Object) locatableBlock.getLocation()).getBlockPos();
    }
    User user = context.getNotifier().orElse(TrackingUtil.getNotifierOrOwnerFromBlock(minecraftWorld, sourcePos));
    if (user != null) {
        final IMixinChunk mixinChunk = (IMixinChunk) minecraftWorld.getChunkFromBlockCoords(notifyPos);
        mixinChunk.addTrackedBlockPosition(block, notifyPos, user, PlayerTracker.Type.NOTIFIER);
    }
}
Also used : User(org.spongepowered.api.entity.living.player.User) IMixinChunk(org.spongepowered.common.interfaces.IMixinChunk) LocatableBlock(org.spongepowered.api.world.LocatableBlock)

Example 17 with LocatableBlock

use of org.spongepowered.api.world.LocatableBlock in project SpongeForge by SpongePowered.

the class MixinBlockLeaves method onBreakBlock.

@Redirect(method = "breakBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;beginLeavesDecay(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)V", remap = false))
public void onBreakBlock(Block block, IBlockState state, net.minecraft.world.World worldIn, BlockPos pos) {
    if (!worldIn.isRemote) {
        final PhaseTracker phaseTracker = PhaseTracker.getInstance();
        final IPhaseState currentState = phaseTracker.getCurrentState();
        final boolean isBlockAlready = currentState.getPhase() != TrackingPhases.BLOCK;
        @Nullable PhaseContext<?> blockDecay = null;
        final boolean isWorldGen = currentState.isWorldGeneration();
        if (isBlockAlready && !isWorldGen) {
            final LocatableBlock locatable = LocatableBlock.builder().location(new Location<World>((World) worldIn, pos.getX(), pos.getY(), pos.getZ())).state((BlockState) state).build();
            blockDecay = BlockPhase.State.BLOCK_DECAY.createPhaseContext().source(locatable);
        }
        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame();
            PhaseContext<?> context = blockDecay != null ? blockDecay.buildAndSwitch() : null) {
            frame.addContext(EventContextKeys.LEAVES_DECAY, (World) worldIn);
            if (SpongeCommonEventFactory.callChangeBlockEventPre((IMixinWorldServer) worldIn, pos).isCancelled()) {
                return;
            }
            block.beginLeavesDecay(state, worldIn, pos);
        }
    } else {
        block.beginLeavesDecay(state, worldIn, pos);
    }
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) CauseStackManager(org.spongepowered.api.event.CauseStackManager) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) LocatableBlock(org.spongepowered.api.world.LocatableBlock) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) World(org.spongepowered.api.world.World) Nullable(javax.annotation.Nullable) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 18 with LocatableBlock

use of org.spongepowered.api.world.LocatableBlock in project SpongeForge by SpongePowered.

the class MixinBlockLog method onBreakBlock.

@Redirect(method = "breakBlock", at = @At(value = "INVOKE", target = "Lnet/minecraft/block/Block;beginLeavesDecay(Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)V", remap = false))
public void onBreakBlock(Block block, IBlockState state, net.minecraft.world.World worldIn, BlockPos pos) {
    if (!worldIn.isRemote) {
        if (SpongeCommonEventFactory.callChangeBlockEventPre((IMixinWorldServer) worldIn, pos).isCancelled()) {
            return;
        }
        final PhaseTracker phaseTracker = PhaseTracker.getInstance();
        final IPhaseState currentState = phaseTracker.getCurrentState();
        final boolean isBlockAlready = currentState.getPhase() != TrackingPhases.BLOCK;
        final boolean isWorldGen = currentState.isWorldGeneration();
        if (isBlockAlready && !isWorldGen) {
            final LocatableBlock locatable = LocatableBlock.builder().location(new Location<World>((World) worldIn, pos.getX(), pos.getY(), pos.getZ())).state((BlockState) state).build();
            BlockPhase.State.BLOCK_DECAY.createPhaseContext().source(locatable).buildAndSwitch();
        }
        block.beginLeavesDecay(state, worldIn, pos);
        if (isBlockAlready && !isWorldGen) {
            phaseTracker.completePhase(BlockPhase.State.BLOCK_DECAY);
        }
    } else {
        block.beginLeavesDecay(state, worldIn, pos);
    }
}
Also used : PhaseTracker(org.spongepowered.common.event.tracking.PhaseTracker) BlockState(org.spongepowered.api.block.BlockState) IBlockState(net.minecraft.block.state.IBlockState) IPhaseState(org.spongepowered.common.event.tracking.IPhaseState) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) LocatableBlock(org.spongepowered.api.world.LocatableBlock) World(org.spongepowered.api.world.World) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

LocatableBlock (org.spongepowered.api.world.LocatableBlock)18 IBlockState (net.minecraft.block.state.IBlockState)8 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)8 BlockState (org.spongepowered.api.block.BlockState)7 World (org.spongepowered.api.world.World)7 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)7 CauseStackManager (org.spongepowered.api.event.CauseStackManager)6 Entity (org.spongepowered.api.entity.Entity)5 User (org.spongepowered.api.entity.living.player.User)5 Location (org.spongepowered.api.world.Location)5 PhaseTracker (org.spongepowered.common.event.tracking.PhaseTracker)5 ArrayList (java.util.ArrayList)4 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)4 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)4 IMixinChunk (org.spongepowered.common.interfaces.IMixinChunk)4 List (java.util.List)3 Nullable (javax.annotation.Nullable)3 EntityItem (net.minecraft.entity.item.EntityItem)3 EnumFacing (net.minecraft.util.EnumFacing)3 BlockPos (net.minecraft.util.math.BlockPos)3