List of usage examples for net.minecraftforge.common ForgeHooks canHarvestBlock
public static boolean canHarvestBlock(@Nonnull BlockState state, @Nonnull PlayerEntity player, @Nonnull IBlockReader world, @Nonnull BlockPos pos)
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 w w w .j a v a 2s .c om*/ @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:com.specialeffect.mods.mining.MineOne.java
License:Open Source License
@SubscribeEvent public void onLiving(LivingUpdateEvent event) { if (ModUtils.entityIsMe(event.getEntityLiving())) { if (mDestroying) { // Select the best tool from the inventory World world = Minecraft.getMinecraft().world; EntityPlayer player = (EntityPlayer) event.getEntityLiving(); // Not currently using -> separate out to another key binding? //chooseBestTool(player.inventory, mBlockToDestroy); // Check selected item can actually destroy block (only in survival) if (!player.capabilities.isCreativeMode) { Block blockIn = world.getBlockState(mBlockToDestroy).getBlock(); if (!ForgeHooks.canHarvestBlock(blockIn, player, world, mBlockToDestroy)) { System.out.println("Can't destroy this block with current item"); this.stopDestroying(); return; }/* w ww . j a va 2 s . c o m*/ } // Stop attacking if we're not pointing at the block any more // (which means either we've destroyed it, or moved away) RayTraceResult mov = Minecraft.getMinecraft().objectMouseOver; boolean blockDestroyed = (world.getBlockState(mBlockToDestroy).getBlock() instanceof BlockAir); boolean movedAway = false; BlockPos pos = this.getMouseOverBlockPos(); if (pos != null) { movedAway = mBlockToDestroy.distanceSq(pos.getX(), pos.getY(), pos.getZ()) > 0; } if (mov == null || blockDestroyed || movedAway) { this.stopDestroying(); } } } }
From source file:de.ellpeck.actuallyadditions.mod.items.ItemDrill.java
/** * Tries to harvest a certain Block//from w ww. j a v a 2 s . c o m * Breaks the Block, drops Particles etc. * Has to be called on both Server and Client * * @param world The World * @param isExtra If the Block is the Block that was looked at when breaking or an additional Block * @param stack The Drill * @param player The Player breaking the Blocks * @param use The Energy that should be extracted per Block */ private boolean tryHarvestBlock(World world, BlockPos pos, boolean isExtra, ItemStack stack, EntityPlayer player, int use) { IBlockState state = world.getBlockState(pos); Block block = state.getBlock(); float hardness = block.getBlockHardness(state, world, pos); boolean canHarvest = (ForgeHooks.canHarvestBlock(block, player, world, pos) || this.canHarvestBlock(state, stack)) && (!isExtra || this.getStrVsBlock(stack, world.getBlockState(pos)) > 1.0F); if (hardness >= 0.0F && (!isExtra || (canHarvest && !block.hasTileEntity(world.getBlockState(pos))))) { this.extractEnergyInternal(stack, use, false); //Break the Block return WorldUtil.playerHarvestBlock(stack, world, player, pos); } return false; }
From source file:vazkii.psi.common.spell.trick.block.PieceTrickBreakBlock.java
public static void removeBlockWithDrops(SpellContext context, EntityPlayer player, World world, ItemStack tool, BlockPos pos, boolean particles) { if (!world.isBlockLoaded(pos) || (context.positionBroken != null && pos.equals(context.positionBroken.getBlockPos())) || !world.isBlockModifiable(player, pos)) return;//from w w w. ja va 2 s. co m IBlockState state = world.getBlockState(pos); Block block = state.getBlock(); if (!world.isRemote && block != null && !block.isAir(state, world, pos) && !(block instanceof BlockLiquid) && !(block instanceof IFluidBlock) && block.getPlayerRelativeBlockHardness(state, player, world, pos) > 0) { if (!ForgeHooks.canHarvestBlock(block, player, world, pos)) return; BreakEvent event = new BreakEvent(world, pos, state, player); MinecraftForge.EVENT_BUS.post(event); if (!event.isCanceled()) { if (!player.capabilities.isCreativeMode) { TileEntity tile = world.getTileEntity(pos); IBlockState localState = world.getBlockState(pos); block.onBlockHarvested(world, pos, localState, player); if (block.removedByPlayer(state, world, pos, player, true)) { block.onBlockDestroyedByPlayer(world, pos, state); block.harvestBlock(world, player, pos, state, tile, tool); } } else world.setBlockToAir(pos); } if (particles) world.playEvent(2001, pos, Block.getStateId(state)); } }
From source file:vazkii.psi.common.spell.trick.block.PieceTrickCollapseBlock.java
@Override public Object execute(SpellContext context) throws SpellRuntimeException { if (context.caster.getEntityWorld().isRemote) return null; Vector3 positionVal = this.<Vector3>getParamValue(context, position); if (positionVal == null) throw new SpellRuntimeException(SpellRuntimeException.NULL_VECTOR); if (!context.isInRadius(positionVal)) throw new SpellRuntimeException(SpellRuntimeException.OUTSIDE_RADIUS); World world = context.caster.getEntityWorld(); BlockPos pos = new BlockPos(positionVal.x, positionVal.y, positionVal.z); BlockPos posDown = pos.down();//from w w w . j a v a2 s. c om IBlockState state = world.getBlockState(pos); IBlockState stateDown = world.getBlockState(posDown); Block block = state.getBlock(); Block blockBelow = stateDown.getBlock(); if (!world.isBlockModifiable(context.caster, pos)) return null; if (blockBelow.isAir(stateDown, world, posDown) && block.getBlockHardness(state, world, pos) != -1 && ForgeHooks.canHarvestBlock(block, context.caster, world, pos) && world.getTileEntity(pos) == null && block.canSilkHarvest(world, pos, state, context.caster)) { if (state.getBlock() == Blocks.LIT_REDSTONE_ORE) { state = Blocks.REDSTONE_ORE.getDefaultState(); world.setBlockState(pos, state); } EntityFallingBlock falling = new EntityFallingBlock(world, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, state); world.spawnEntity(falling); } return null; }
From source file:vazkii.psi.common.spell.trick.block.PieceTrickMoveBlock.java
@Override public Object execute(SpellContext context) throws SpellRuntimeException { if (context.caster.getEntityWorld().isRemote) return null; Vector3 positionVal = this.<Vector3>getParamValue(context, position); Vector3 targetVal = this.<Vector3>getParamValue(context, target); if (positionVal == null) throw new SpellRuntimeException(SpellRuntimeException.NULL_VECTOR); if (!context.isInRadius(positionVal)) throw new SpellRuntimeException(SpellRuntimeException.OUTSIDE_RADIUS); World world = context.caster.getEntityWorld(); BlockPos pos = new BlockPos(positionVal.x, positionVal.y, positionVal.z); IBlockState state = world.getBlockState(pos); Block block = state.getBlock();/*from w w w . j a v a 2 s .c o m*/ if (world.getTileEntity(pos) != null || block.getMobilityFlag(state) != EnumPushReaction.NORMAL || !block.canSilkHarvest(world, pos, state, context.caster) || block.getPlayerRelativeBlockHardness(state, context.caster, world, pos) <= 0 || !ForgeHooks.canHarvestBlock(block, context.caster, world, pos)) return null; if (!targetVal.isAxial() || targetVal.isZero()) return null; Vector3 axis = targetVal.normalize(); int x = pos.getX() + (int) axis.x; int y = pos.getY() + (int) axis.y; int z = pos.getZ() + (int) axis.z; BlockPos pos1 = new BlockPos(x, y, z); IBlockState state1 = world.getBlockState(pos1); if (!world.isBlockModifiable(context.caster, pos) || !world.isBlockModifiable(context.caster, pos1)) return null; if (world.isAirBlock(pos1) || state1.getBlock().isReplaceable(world, pos1)) { world.setBlockState(pos1, state, 1 | 2); world.setBlockToAir(pos); world.playEvent(2001, pos, Block.getIdFromBlock(block) + (block.getMetaFromState(state) << 12)); } return null; }