Example usage for net.minecraftforge.common ForgeHooks onBlockBreakEvent

List of usage examples for net.minecraftforge.common ForgeHooks onBlockBreakEvent

Introduction

In this page you can find the example usage for net.minecraftforge.common ForgeHooks onBlockBreakEvent.

Prototype

public static int onBlockBreakEvent(World world, GameType gameType, ServerPlayerEntity entityPlayer,
            BlockPos pos) 

Source Link

Usage

From source file:blusunrize.immersiveengineering.common.items.ItemDrill.java

@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos iPos, EntityPlayer player) {
    World world = player.world;/*from w w  w  .ja  v  a  2 s. c om*/
    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.isEmpty() || 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.onPlayerDestroy(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.onPlayerDestroy(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;
}

From source file:com.buuz135.industrial.item.infinity.ItemInfinityDrill.java

License:Open Source License

@Override
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos,
        EntityLivingBase entityLiving) {
    if (entityLiving instanceof EntityPlayer) {
        RayTraceResult rayTraceResult = RayTraceUtils.rayTraceSimple(worldIn, entityLiving, 16, 0);
        EnumFacing facing = rayTraceResult.sideHit;
        DrillTier currentTier = getSelectedDrillTier(stack);
        Pair<BlockPos, BlockPos> area = getArea(pos, facing, currentTier, true);
        BlockPos.getAllInBox(area.getLeft(), area.getRight()).forEach(blockPos -> {
            if (enoughFuel(stack) && worldIn.getTileEntity(blockPos) == null
                    && entityLiving instanceof EntityPlayerMP && !worldIn.isAirBlock(blockPos)) {
                IBlockState tempState = worldIn.getBlockState(blockPos);
                Block block = tempState.getBlock();
                if (block.getBlockHardness(tempState, worldIn, blockPos) < 0)
                    return;
                int xp = ForgeHooks.onBlockBreakEvent(worldIn,
                        ((EntityPlayerMP) entityLiving).interactionManager.getGameType(),
                        (EntityPlayerMP) entityLiving, blockPos);
                if (xp >= 0 && block.removedByPlayer(tempState, worldIn, blockPos, (EntityPlayer) entityLiving,
                        true)) {//from ww  w . java 2  s. c  o  m
                    block.onPlayerDestroy(worldIn, blockPos, tempState);
                    block.harvestBlock(worldIn, (EntityPlayer) entityLiving, blockPos, tempState, null, stack);
                    block.dropXpOnBlockBreak(worldIn, blockPos, xp);
                    consumeFuel(stack);
                }
            }
        });
        worldIn.getEntitiesWithinAABB(EntityItem.class,
                new AxisAlignedBB(area.getLeft(), area.getRight()).grow(1)).forEach(entityItem -> {
                    entityItem.setPositionAndUpdate(entityLiving.posX, entityLiving.posY, entityLiving.posZ);
                    entityItem.setPickupDelay(0);
                });
        worldIn.getEntitiesWithinAABB(EntityXPOrb.class,
                new AxisAlignedBB(area.getLeft(), area.getRight()).grow(1))
                .forEach(entityXPOrb -> entityXPOrb.setPositionAndUpdate(entityLiving.posX, entityLiving.posY,
                        entityLiving.posZ));
    }
    return super.onBlockDestroyed(stack, worldIn, state, pos, entityLiving);
}

From source file:com.lothrazar.cyclicmagic.item.ItemMattock.java

License:Open Source License

/**
 * <<<< made with some help from Tinkers Construct and Spark's Hammers
 * https://github.com/thebrightspark/SparksHammers/blob/b84bd178fe2bbe47b13a89ef9435b20f09e429a4/src/main/java/com/brightspark/sparkshammers/util/CommonUtils.java and
 * https://github.com/SlimeKnights/TinkersConstruct
 *//*from   www  .j a v  a 2 s  .  c o m*/
@SuppressWarnings("deprecation")
@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos posHit, EntityPlayer player) {
    RayTraceResult ray = rayTrace(player.getEntityWorld(), player, false);
    if (ray == null) {
        return super.onBlockStartBreak(stack, posHit, player);
    }
    EnumFacing sideHit = ray.sideHit;
    World world = player.getEntityWorld();
    //use the shape builder to get region
    List<BlockPos> shape;
    if (sideHit == EnumFacing.UP || sideHit == EnumFacing.DOWN) {
        shape = UtilShape.squareHorizontalHollow(posHit, RADIUS);
    } else if (sideHit == EnumFacing.EAST || sideHit == EnumFacing.WEST) {
        shape = UtilShape.squareVerticalZ(posHit, RADIUS);
    } else {//has to be NORTHSOUTH
        shape = UtilShape.squareVerticalX(posHit, RADIUS);
    }
    for (BlockPos posCurrent : shape) {
        //first we validate
        if (posHit.equals(posCurrent)) {
            continue;
        }
        if (super.onBlockStartBreak(stack, new BlockPos(posCurrent), player)) {
            continue;
        }
        IBlockState bsCurrent = world.getBlockState(posCurrent);
        if (world.isAirBlock(posCurrent)) {
            continue;
        }
        if (!materials.contains(bsCurrent.getMaterial())) {
            continue;
        }
        Block blockCurrent = bsCurrent.getBlock();
        if (!ForgeHooks.canHarvestBlock(blockCurrent, player, world, posCurrent)
                || bsCurrent.getBlock().canEntityDestroy(bsCurrent, world, posCurrent, player) == false
                || bsCurrent.getBlock().getBlockHardness(bsCurrent, world, posCurrent) < 0) {
            continue;
        }
        //then we destroy
        stack.onBlockDestroyed(world, bsCurrent, posCurrent, player);
        if (world.isRemote) {//C
            world.playEvent(2001, posCurrent, Block.getStateId(bsCurrent));
            if (blockCurrent.removedByPlayer(bsCurrent, world, posCurrent, player, true)) {
                blockCurrent.onPlayerDestroy(world, posCurrent, bsCurrent);
            }
            stack.onBlockDestroyed(world, bsCurrent, posCurrent, player);//update tool damage
            if (stack.getCount() == 0 && stack == player.getHeldItemMainhand()) {
                ForgeEventFactory.onPlayerDestroyItem(player, stack, EnumHand.MAIN_HAND);
                player.setHeldItem(EnumHand.MAIN_HAND, ItemStack.EMPTY);
            }
            Minecraft.getMinecraft().getConnection()
                    .sendPacket(new CPacketPlayerDigging(CPacketPlayerDigging.Action.STOP_DESTROY_BLOCK,
                            posCurrent, Minecraft.getMinecraft().objectMouseOver.sideHit));
        } else if (player instanceof EntityPlayerMP) {//Server side, so this works
            EntityPlayerMP mp = (EntityPlayerMP) player;
            int xpGivenOnDrop = ForgeHooks.onBlockBreakEvent(world,
                    ((EntityPlayerMP) player).interactionManager.getGameType(), (EntityPlayerMP) player,
                    posCurrent);
            if (xpGivenOnDrop >= 0) {
                if (blockCurrent.removedByPlayer(bsCurrent, world, posCurrent, player, true)) {
                    TileEntity tile = world.getTileEntity(posCurrent);
                    blockCurrent.onPlayerDestroy(world, posCurrent, bsCurrent);
                    blockCurrent.harvestBlock(world, player, posCurrent, bsCurrent, tile, stack);
                    blockCurrent.dropXpOnBlockBreak(world, posCurrent, xpGivenOnDrop);
                }
                mp.connection.sendPacket(new SPacketBlockChange(world, posCurrent));
            }
        }
    }
    return super.onBlockStartBreak(stack, posHit, player);
}

From source file:de.ellpeck.actuallyadditions.mod.util.WorldUtil.java

public static boolean playerHarvestBlock(ItemStack stack, World world, EntityPlayer player, BlockPos pos) {
    if (world.isAirBlock(pos)) {
        return false;
    }//from  ww  w .j  a v  a2  s . c  o  m

    IBlockState state = world.getBlockState(pos);
    Block block = state.getBlock();

    if (!world.isRemote) {
        world.playEvent(player, 2001, pos, Block.getStateId(state));
    } else {
        world.playEvent(2001, pos, Block.getStateId(state));
    }

    if (player.capabilities.isCreativeMode) {

        block.onBlockHarvested(world, pos, state, player);
        if (block.removedByPlayer(state, world, pos, player, false)) {
            block.onBlockDestroyedByPlayer(world, pos, state);
        }

        if (!world.isRemote) {
            if (player instanceof EntityPlayerMP) {
                ((EntityPlayerMP) player).connection.sendPacket(new SPacketBlockChange(world, pos));
            }
        }

        return true;
    }

    stack.onBlockDestroyed(world, state, pos, player);

    if (!world.isRemote) {
        if (player instanceof EntityPlayerMP) {
            EntityPlayerMP playerMp = (EntityPlayerMP) player;

            int xp = ForgeHooks.onBlockBreakEvent(world, playerMp.interactionManager.getGameType(), playerMp,
                    pos);
            if (xp == -1) {
                return false;
            }

            TileEntity tileEntity = world.getTileEntity(pos);
            if (block.removedByPlayer(state, world, pos, player, true)) {
                block.onBlockDestroyedByPlayer(world, pos, state);
                block.harvestBlock(world, player, pos, state, tileEntity, stack);
                block.dropXpOnBlockBreak(world, pos, xp);
            }

            playerMp.connection.sendPacket(new SPacketBlockChange(world, pos));
            return true;
        }
    } else {
        if (block.removedByPlayer(state, world, pos, player, true)) {
            block.onBlockDestroyedByPlayer(world, pos, state);
        }

        if (StackUtil.getStackSize(stack) <= 0 && stack == player.getHeldItemMainhand()) {
            ForgeEventFactory.onPlayerDestroyItem(player, stack, EnumHand.MAIN_HAND);
            player.setHeldItem(EnumHand.MAIN_HAND, null);
        }

        Minecraft mc = Minecraft.getMinecraft();
        mc.getConnection().sendPacket(new CPacketPlayerDigging(CPacketPlayerDigging.Action.STOP_DESTROY_BLOCK,
                pos, mc.objectMouseOver.sideHit));

        return true;
    }
    return false;
}

From source file:vazkii.botania.common.item.equipment.tool.ToolCommons.java

License:Open Source License

public static void removeBlockWithDrops(EntityPlayer player, ItemStack stack, World world, BlockPos pos,
        Predicate<IBlockState> filter, boolean dispose, boolean particles) {
    if (!world.isBlockLoaded(pos))
        return;//from w ww. java 2s .  c  om

    IBlockState state = world.getBlockState(pos);
    Block block = state.getBlock();

    if (!world.isRemote && filter.test(state) && !block.isAir(state, world, pos)
            && state.getPlayerRelativeBlockHardness(player, world, pos) > 0
            && block.canHarvestBlock(player.world, pos, player)) {
        int exp = ForgeHooks.onBlockBreakEvent(world,
                ((EntityPlayerMP) player).interactionManager.getGameType(), (EntityPlayerMP) player, pos);
        if (exp == -1)
            return;

        if (!player.capabilities.isCreativeMode) {
            TileEntity tile = world.getTileEntity(pos);

            if (block.removedByPlayer(state, world, pos, player, true)) {
                block.onPlayerDestroy(world, pos, state);

                if (!dispose || !ItemElementiumPick.isDisposable(block)) {
                    block.harvestBlock(world, player, pos, state, tile, stack);
                    block.dropXpOnBlockBreak(world, pos, exp);
                }
            }

            damageItem(stack, 1, player, 80);
        } else
            world.setBlockToAir(pos);

        if (particles && ConfigHandler.blockBreakParticles && ConfigHandler.blockBreakParticlesTool)
            world.playEvent(2001, pos, Block.getStateId(state));
    }
}