Search in sources :

Example 1 with SPacketBlockChange

use of net.minecraft.network.play.server.SPacketBlockChange in project MinecraftForge by MinecraftForge.

the class ForgeHooks method onBlockBreakEvent.

public static int onBlockBreakEvent(World world, GameType gameType, EntityPlayerMP entityPlayer, BlockPos pos) {
    // Logic from tryHarvestBlock for pre-canceling the event
    boolean preCancelEvent = false;
    if (gameType.isCreative() && !entityPlayer.getHeldItemMainhand().isEmpty() && entityPlayer.getHeldItemMainhand().getItem() instanceof ItemSword)
        preCancelEvent = true;
    if (gameType.isAdventure()) {
        if (gameType == GameType.SPECTATOR)
            preCancelEvent = true;
        if (!entityPlayer.isAllowEdit()) {
            ItemStack itemstack = entityPlayer.getHeldItemMainhand();
            if (itemstack.isEmpty() || !itemstack.canDestroy(world.getBlockState(pos).getBlock()))
                preCancelEvent = true;
        }
    }
    // Tell client the block is gone immediately then process events
    if (world.getTileEntity(pos) == null) {
        SPacketBlockChange packet = new SPacketBlockChange(world, pos);
        packet.blockState = Blocks.AIR.getDefaultState();
        entityPlayer.connection.sendPacket(packet);
    }
    // Post the block break event
    IBlockState state = world.getBlockState(pos);
    BlockEvent.BreakEvent event = new BlockEvent.BreakEvent(world, pos, state, entityPlayer);
    event.setCanceled(preCancelEvent);
    MinecraftForge.EVENT_BUS.post(event);
    // Handle if the event is canceled
    if (event.isCanceled()) {
        // Let the client know the block still exists
        entityPlayer.connection.sendPacket(new SPacketBlockChange(world, pos));
        // Update any tile entity data for this block
        TileEntity tileentity = world.getTileEntity(pos);
        if (tileentity != null) {
            Packet<?> pkt = tileentity.getUpdatePacket();
            if (pkt != null) {
                entityPlayer.connection.sendPacket(pkt);
            }
        }
    }
    return event.isCanceled() ? -1 : event.getExpToDrop();
}
Also used : ItemSword(net.minecraft.item.ItemSword) TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) ItemStack(net.minecraft.item.ItemStack) NoteBlockEvent(net.minecraftforge.event.world.NoteBlockEvent) BlockEvent(net.minecraftforge.event.world.BlockEvent) SPacketBlockChange(net.minecraft.network.play.server.SPacketBlockChange)

Example 2 with SPacketBlockChange

use of net.minecraft.network.play.server.SPacketBlockChange in project Overloaded by CJ-MC-Mods.

the class PlayerInteractionUtil method tryHarvestBlock.

public static boolean tryHarvestBlock(EntityPlayerMP player, World world, BlockPos pos) {
    int exp = net.minecraftforge.common.ForgeHooks.onBlockBreakEvent(world, player.interactionManager.getGameType(), player, pos);
    if (exp == -1) {
        return false;
    } else {
        IBlockState iblockstate = world.getBlockState(pos);
        TileEntity tileentity = world.getTileEntity(pos);
        Block block = iblockstate.getBlock();
        if ((block instanceof BlockCommandBlock || block instanceof BlockStructure) && !player.canUseCommandBlock()) {
            world.notifyBlockUpdate(pos, iblockstate, iblockstate, 3);
            return false;
        } else {
            world.playEvent(null, 2001, pos, Block.getStateId(iblockstate));
            boolean flag1;
            if (player.capabilities.isCreativeMode) {
                flag1 = removeBlock(world, pos, player, false);
                player.connection.sendPacket(new SPacketBlockChange(world, pos));
            } else {
                ItemStack itemstack1 = player.getHeldItemMainhand();
                ItemStack itemstack2 = itemstack1.isEmpty() ? ItemStack.EMPTY : itemstack1.copy();
                boolean flag = iblockstate.getBlock().canHarvestBlock(world, pos, player);
                itemstack1.onBlockDestroyed(world, iblockstate, pos, player);
                flag1 = removeBlock(world, pos, player, flag);
                if (flag1 && flag) {
                    iblockstate.getBlock().harvestBlock(world, player, pos, iblockstate, tileentity, itemstack2);
                }
            }
            // Drop experience
            if (!player.isCreative() && flag1 && exp > 0) {
                iblockstate.getBlock().dropXpOnBlockBreak(world, player.getPosition(), exp);
            }
            return flag1;
        }
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) BlockCommandBlock(net.minecraft.block.BlockCommandBlock) BlockStructure(net.minecraft.block.BlockStructure) BlockCommandBlock(net.minecraft.block.BlockCommandBlock) Block(net.minecraft.block.Block) ItemBlock(net.minecraft.item.ItemBlock) ItemStack(net.minecraft.item.ItemStack) SPacketBlockChange(net.minecraft.network.play.server.SPacketBlockChange)

Example 3 with SPacketBlockChange

use of net.minecraft.network.play.server.SPacketBlockChange in project ImmersiveEngineering by BluSunrize.

the class ItemDrill method onBlockStartBreak.

@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos iPos, EntityPlayer player) {
    World world = player.worldObj;
    if (player.isSneaking() || world.isRemote || !(player instanceof EntityPlayerMP))
        return false;
    RayTraceResult mop = this.rayTrace(world, player, true);
    ItemStack head = getHead(stack);
    if (mop == null || head == null || this.isDrillBroken(stack))
        return false;
    //		EnumFacing side = mop.sideHit;
    //		int diameter = ((IDrillHead)head.getItem()).getMiningSize(head)+getUpgrades(stack).getInteger("size");
    //		int depth = ((IDrillHead)head.getItem()).getMiningDepth(head)+getUpgrades(stack).getInteger("depth");
    //
    //		BlockPos startPos=iPos;
    //		if(diameter%2==0)//even numbers
    //		{
    //			float hx = (float)mop.hitVec.xCoord-iPos.getX();
    //			float hy = (float)mop.hitVec.yCoord-iPos.getY();
    //			float hz = (float)mop.hitVec.zCoord-iPos.getZ();
    //			if((side.getAxis()==Axis.Y&&hx<.5)||(side.getAxis()==Axis.Z&&hx<.5))
    //				startPos.add(-diameter/2,0,0);
    //			if(side.getAxis()!=Axis.Y&&hy<.5)
    //				startPos.add(0,-diameter/2,0);
    //			if((side.getAxis()==Axis.Y&&hz<.5)||(side.getAxis()==Axis.X&&hz<.5))
    //				startPos.add(0,0,-diameter/2);
    //		}
    //		else//odd numbers
    //		{
    //			startPos.add(-(side.getAxis()==Axis.X?0: diameter/2), -(side.getAxis()==Axis.Y?0: diameter/2), -(side.getAxis()==Axis.Z?0: diameter/2));
    //		}
    //
    //		for(int dd=0; dd<depth; dd++)
    //			for(int dw=0; dw<diameter; dw++)
    //				for(int dh=0; dh<diameter; dh++)
    //				{
    //					BlockPos pos = startPos.add((side.getAxis()==Axis.X?dd: dw), (side.getAxis()==Axis.Y?dd: dh), (side.getAxis()==Axis.Y?dh: side.getAxis()==Axis.X?dw: dd));
    //					if(pos.equals(iPos))
    //						continue;
    ImmutableList<BlockPos> additional = ((IDrillHead) head.getItem()).getExtraBlocksDug(head, world, player, mop);
    for (BlockPos pos : additional) {
        if (!world.isBlockLoaded(pos))
            continue;
        IBlockState state = world.getBlockState(pos);
        Block block = state.getBlock();
        if (block != null && !block.isAir(state, world, pos) && state.getPlayerRelativeBlockHardness(player, world, pos) != 0) {
            if (!this.canBreakExtraBlock(world, block, pos, state, player, stack, head, true))
                continue;
            int xpDropEvent = ForgeHooks.onBlockBreakEvent(world, ((EntityPlayerMP) player).interactionManager.getGameType(), (EntityPlayerMP) player, pos);
            if (xpDropEvent < 0)
                continue;
            if (player.capabilities.isCreativeMode) {
                block.onBlockHarvested(world, pos, state, player);
                if (block.removedByPlayer(state, world, pos, player, false))
                    block.onBlockDestroyedByPlayer(world, pos, state);
            } else {
                block.onBlockHarvested(world, pos, state, player);
                TileEntity te = world.getTileEntity(pos);
                //implicitly damages head
                stack.onBlockDestroyed(world, state, pos, player);
                if (block.removedByPlayer(state, world, pos, player, true)) {
                    block.onBlockDestroyedByPlayer(world, pos, state);
                    block.harvestBlock(world, player, pos, state, te, stack);
                    block.dropXpOnBlockBreak(world, pos, xpDropEvent);
                }
            }
            world.playEvent(2001, pos, Block.getStateId(state));
            ((EntityPlayerMP) player).connection.sendPacket(new SPacketBlockChange(world, pos));
        }
    }
    return false;
}
Also used : IDrillHead(blusunrize.immersiveengineering.api.tool.IDrillHead) TileEntity(net.minecraft.tileentity.TileEntity) IBlockState(net.minecraft.block.state.IBlockState) RayTraceResult(net.minecraft.util.math.RayTraceResult) Block(net.minecraft.block.Block) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) BlockPos(net.minecraft.util.math.BlockPos) World(net.minecraft.world.World) ItemStack(net.minecraft.item.ItemStack) SPacketBlockChange(net.minecraft.network.play.server.SPacketBlockChange)

Aggregations

IBlockState (net.minecraft.block.state.IBlockState)3 ItemStack (net.minecraft.item.ItemStack)3 SPacketBlockChange (net.minecraft.network.play.server.SPacketBlockChange)3 TileEntity (net.minecraft.tileentity.TileEntity)3 Block (net.minecraft.block.Block)2 IDrillHead (blusunrize.immersiveengineering.api.tool.IDrillHead)1 BlockCommandBlock (net.minecraft.block.BlockCommandBlock)1 BlockStructure (net.minecraft.block.BlockStructure)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 ItemBlock (net.minecraft.item.ItemBlock)1 ItemSword (net.minecraft.item.ItemSword)1 BlockPos (net.minecraft.util.math.BlockPos)1 RayTraceResult (net.minecraft.util.math.RayTraceResult)1 World (net.minecraft.world.World)1 BlockEvent (net.minecraftforge.event.world.BlockEvent)1 NoteBlockEvent (net.minecraftforge.event.world.NoteBlockEvent)1