Java Code Examples for net.minecraft.block.state.IBlockState#getBlockHardness()

The following examples show how to use net.minecraft.block.state.IBlockState#getBlockHardness() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemTofuForceSword.java    From TofuCraftReload with MIT License 6 votes vote down vote up
/**
 * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.
 */
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving) {
    if ((double) state.getBlockHardness(worldIn, pos) != 0.0D) {
        if (isUsable(stack)) {

            if (getEnergy(stack) >= 10) {
                drain(stack, 10, false);

            } else {
                stack.damageItem(1, entityLiving);
            }
        }
    }

    return true;
}
 
Example 2
Source File: BlockUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Checks if the player is allowed to change this block. Creative mode players are always allowed to change blocks.
 */
public static boolean canChangeBlock(World world, BlockPos pos, EntityPlayer player, boolean allowTileEntities, float maxHardness)
{
    if (player.capabilities.isCreativeMode)
    {
        return true;
    }

    IBlockState state = world.getBlockState(pos);

    if (state.getBlock().isAir(state, world, pos))
    {
        return true;
    }

    float hardness = state.getBlockHardness(world, pos);

    return world.isBlockModifiable(player, pos) && hardness >= 0 && (hardness <= maxHardness || state.getMaterial().isLiquid()) &&
           (allowTileEntities || world.getTileEntity(pos) == null);
}
 
Example 3
Source File: ItemEnderSword.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos, EntityLivingBase livingBase)
{
    if (state.getBlockHardness(world, pos) != 0.0f && this.isToolBroken(stack) == false)
    {
        int amount = Math.min(2, this.getMaxDamage(stack) - stack.getItemDamage());
        stack.damageItem(amount, livingBase);

        // Tool just broke
        if (this.isToolBroken(stack))
        {
            livingBase.renderBrokenItemStack(stack);
        }

        return true;
    }

    return false;
}
 
Example 4
Source File: ItemVoidPickaxe.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos pos, EntityPlayer player)
{
    //System.out.printf("onBlockStartBreak() @ %s\n", player.getEntityWorld().isRemote ? "client" : "server");
    World world = player.getEntityWorld();
    IBlockState state = world.getBlockState(pos);

    if (state.getBlockHardness(world, pos) >= 0f)
    {
        if (world.isRemote == false)
        {
            BlockUtils.setBlockToAirWithoutSpillingContents(world, pos);
        }

        if (player.capabilities.isCreativeMode == false)
        {
            this.addToolDamage(stack, world, pos, state, player);
        }

        return world.isRemote == false;
    }

    return false;
}
 
Example 5
Source File: ItemBasicLaserGun.java    From AdvancedRocketry with MIT License 6 votes vote down vote up
@Nullable
@Override
public ItemStack onItemUseFinish(ItemStack stack, World world, EntityLivingBase entityLiving)
{
	RayTraceResult rayTrace = rayTrace(world, (EntityPlayer) entityLiving, false);

	if(rayTrace != null && rayTrace.typeOfHit == Type.BLOCK) {
		IBlockState state = world.getBlockState(rayTrace.getBlockPos());
		if(state.getBlockHardness(world, rayTrace.getBlockPos()) != -1) {

			//
			if(!world.isRemote) {
				((EntityPlayerMP)entityLiving).interactionManager.tryHarvestBlock(rayTrace.getBlockPos());
				//world.destroyBlock(rayTrace.getBlockPos(), true);
			}

			//state.getPlayerRelativeBlockHardness((EntityPlayer)player, world, rayTrace.getBlockPos());
		}
	}

	posMap.remove(entityLiving);

	return stack;
}
 
Example 6
Source File: UpgradeBreaker.java    From BetterChests with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * see PlayerInteractionManager.tryHarvestBlock
 */
public boolean breakBlock(EntityPlayerMP player, BlockPos pos) {
	World world = player.world;
	IBlockState state = world.getBlockState(pos);
	TileEntity te = world.getTileEntity(pos);

	if (state.getBlockHardness(world, pos) < 0 || !state.getBlock().canHarvestBlock(world, pos, player))
	{
		return false;
	}
	else {
		ItemStack stack = player.getHeldItemMainhand();
		if (!stack.isEmpty() && stack.getItem().onBlockStartBreak(stack, pos, player)) return false;

		world.playEvent(player, 2001, pos, Block.getStateId(state));
		ItemStack itemHand = player.getHeldItemMainhand();
		ItemStack itemHandCopy = itemHand.isEmpty() ? ItemStack.EMPTY : itemHand.copy();
		boolean canHarvest = state.getBlock().canHarvestBlock(world, pos, player);

		if (!itemHand.isEmpty()) {
			itemHand.onBlockDestroyed(world, state, pos, player);
			if (itemHand.isEmpty()) {
				ForgeEventFactory.onPlayerDestroyItem(player, itemHandCopy, EnumHand.MAIN_HAND);
			}
		}

		boolean didRemove = removeBlock(player, pos, canHarvest);
		if (didRemove && canHarvest) {
			state.getBlock().harvestBlock(world, player, pos, state, te, itemHandCopy);
		}
		return didRemove;
	}
}
 
Example 7
Source File: ItemEnderTool.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public boolean onBlockDestroyed(ItemStack stack, World world, IBlockState state, BlockPos pos, EntityLivingBase livingBase)
{
    //System.out.println("onBlockDestroyed(): living: " + living + " remote: " + living.worldObj.isRemote);

    if ((livingBase instanceof EntityPlayer) && this.isCreativeLikeBreakingEnabled(stack))
    {
        ((EntityPlayer) livingBase).getCooldownTracker().setCooldown(this, 6);
    }

    // Don't use durability for breaking leaves with an axe
    if (state.getMaterial() == Material.LEAVES && ToolType.fromStack(stack).equals(ToolType.AXE))
    {
        return false;
    }

    // Don't use durability on instant-minable blocks (hardness == 0.0f), or if the tool is already broken
    if (this.isToolBroken(stack) == false && state.getBlockHardness(world, pos) > 0.0f)
    {
        // Fast mode uses double the durability, but not while using the Creative Breaking upgrade
        int dmg = (PowerStatus.fromStack(stack) == PowerStatus.POWERED && this.isCreativeLikeBreakingEnabled(stack) == false ? 2 : 1);
        this.addToolDamage(stack, dmg, livingBase, livingBase);
        return true;
    }

    return false;
}
 
Example 8
Source File: ItemVoidPickaxe.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean addToolDamage(ItemStack stack, World world, BlockPos pos, IBlockState state, EntityLivingBase livingBase)
{
    // Don't use durability on instant-minable blocks (hardness == 0.0f), or if the tool is already broken
    if (this.isToolBroken(stack) == false && state.getBlockHardness(world, pos) > 0.0f)
    {
        // Fast mode uses double the durability
        int dmg = (this.isFastMode(stack) ? 2 : 1);

        this.addToolDamage(stack, dmg, livingBase, livingBase);
        return true;
    }

    return false;
}
 
Example 9
Source File: ItemDolly.java    From enderutilities with GNU Lesser General Public License v3.0 5 votes vote down vote up
private boolean shouldTryToPickUpBlock(World world, BlockPos pos, EnumFacing side, EntityPlayer player)
{
    if (world.isBlockModifiable(player, pos) == false)
    {
        return false;
    }

    if (player.capabilities.isCreativeMode)
    {
        return true;
    }

    IBlockState state = world.getBlockState(pos);

    if (BlackLists.isBlockAllowedForDolly(state) == false)
    {
        return false;
    }

    TileEntity te = world.getTileEntity(pos);

    // Unbreakable block, player not in creative mode
    if (state.getBlockHardness(world, pos) < 0)
    {
        if ((te instanceof TileEntityEnderUtilitiesInventory) == false)
        {
            return false;
        }

        TileEntityEnderUtilitiesInventory teinv = (TileEntityEnderUtilitiesInventory) te;
        // From the unbreakable blocks in this mod, only allow moving non-Creative mode storage blocks.
        return teinv.isCreative() == false && teinv.isMovableBy(player);
    }

    return te instanceof TileEntityEnderUtilitiesInventory || te instanceof IInventory ||
           (te != null && te.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side));
}
 
Example 10
Source File: ItemMetalSword.java    From BaseMetals with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public boolean onBlockDestroyed(final ItemStack item, final World world, final IBlockState block, final BlockPos coord,
                                final EntityLivingBase entity) {
    if (block.getBlockHardness( world, coord) != 0.0) {
        item.damageItem(2, entity);
    }
    return true;
}
 
Example 11
Source File: AutoTool.java    From ForgeHax with MIT License 5 votes vote down vote up
private double getDigSpeed(InvItem item, IBlockState state, BlockPos pos) {
  double str = item.getItemStack().getDestroySpeed(state);
  int eff = getEnchantmentLevel(EFFICIENCY, item);
  return state.getBlockHardness(getWorld(), pos) > 0.D
      ? Math.max(str + (str > 1.D ? (eff * eff + 1.D) : 0.D), 0.D)
      : 1.D;
}
 
Example 12
Source File: ItemKatana.java    From Sakura_mod with MIT License 5 votes vote down vote up
/**
 * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.
 */
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving) {
    if (state.getBlockHardness(worldIn, pos) != 0.0D) {
        stack.damageItem(2, entityLiving);
    }

    return true;
}
 
Example 13
Source File: ItemKotachi.java    From Sakura_mod with MIT License 5 votes vote down vote up
/**
 * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.
 */
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving) {
    if (state.getBlockHardness(worldIn, pos) != 0.0D) {
        stack.damageItem(2, entityLiving);
    }

    return true;
}
 
Example 14
Source File: ItemSheathKatana.java    From Sakura_mod with MIT License 5 votes vote down vote up
/**
 * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.
 */
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving) {
    if (state.getBlockHardness(worldIn, pos) != 0.0D) {
        stack.damageItem(2, entityLiving);
    }
    return true;
}
 
Example 15
Source File: ItemSheath.java    From Sakura_mod with MIT License 5 votes vote down vote up
/**
 * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.
 */
public boolean onBlockDestroyed(ItemStack stack, World worldIn, IBlockState state, BlockPos pos, EntityLivingBase entityLiving) {
    if (state.getBlockHardness(worldIn, pos) != 0.0D) {
        stack.damageItem(2, entityLiving);
    }

    return true;
}
 
Example 16
Source File: BlockBarrel.java    From TofuCraftReload with MIT License 5 votes vote down vote up
public boolean isUnderWeight(World world, BlockPos pos) {
    IBlockState weightBlock = world.getBlockState(pos.up());
    IBlockState baseBlock = world.getBlockState(pos.down());

    boolean isWeightValid = weightBlock != null
            && (weightBlock.getMaterial() == Material.ROCK || weightBlock.getMaterial() == Material.IRON);

    float baseHardness = baseBlock.getBlockHardness(world, pos.down());
    boolean isBaseValid = baseBlock.isNormalCube() &&
            (baseBlock.getMaterial() == Material.ROCK || baseBlock.getMaterial() == Material.IRON || baseHardness >= 1.0F || baseHardness < 0.0F);

    return isWeightValid && isBaseValid;
}
 
Example 17
Source File: BlockTofu.java    From TofuCraftReload with MIT License 5 votes vote down vote up
public boolean isUnderWeight(World world, BlockPos pos) {
    IBlockState weightBlock = world.getBlockState(pos.up());
    IBlockState baseBlock = world.getBlockState(pos.down());

    boolean isWeightValid = weightBlock != null
            && (weightBlock.getMaterial() == Material.ROCK || weightBlock.getMaterial() == Material.IRON) && weightBlock != BlockLoader.ISHITOFU.getDefaultState();

    float baseHardness = baseBlock.getBlockHardness(world, pos.down());
    boolean isBaseValid = baseBlock.isNormalCube() &&
            (baseBlock.getMaterial() == Material.ROCK || baseBlock.getMaterial() == Material.IRON || baseHardness >= 1.0F || baseHardness < 0.0F);

    return isWeightValid && isBaseValid;
}
 
Example 18
Source File: BlockHelper.java    From ForgeHax with MIT License 4 votes vote down vote up
public static float getBlockHardness(BlockPos pos) {
  IBlockState state = getWorld().getBlockState(pos);
  return state.getBlockHardness(getWorld(), pos);
}
 
Example 19
Source File: TileEntityDrawbridge.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
private boolean retractOneBlock(int position, FakePlayer player, boolean takeNonPlaced, boolean playPistonSound)
{
    World world = this.getWorld();
    BlockPos pos = this.getPos().offset(this.getFacing(), position + 1);

    if (world.isBlockLoaded(pos, world.isRemote == false) == false)
    {
        return false;
    }

    IBlockState state = world.getBlockState(pos);

    if ((takeNonPlaced || state == this.blockStatesPlaced[position]) &&
        state.getBlock().isAir(state, world, pos) == false &&
        state.getBlockHardness(world, pos) >= 0f)
    {
        ItemStack stack = BlockUtils.getPickBlockItemStack(world, pos, player, EnumFacing.UP);

        if (stack.isEmpty() == false)
        {
            NBTTagCompound nbt = null;
            final int invPosition = this.isAdvanced() ? position : 0;

            if (state.getBlock().hasTileEntity(state))
            {
                TileEntity te = world.getTileEntity(pos);

                if (te != null)
                {
                    TileUtils.storeTileEntityInStack(stack, te, false);
                    nbt = te.writeToNBT(new NBTTagCompound());
                    NBTUtils.removePositionFromTileEntityNBT(nbt);
                }
            }

            if (this.itemHandlerDrawbridge.insertItem(invPosition, stack, false).isEmpty())
            {
                this.blockInfoTaken[position] = new BlockInfo(state, nbt);
                this.numTaken++;

                if (playPistonSound)
                {
                    world.playSound(null, pos, SoundEvents.BLOCK_PISTON_CONTRACT, SoundCategory.BLOCKS, 0.5f, 0.7f);
                }
                else
                {
                    BlockUtils.playBlockBreakSound(world, pos);
                }

                BlockUtils.setBlockToAirWithoutSpillingContents(world, pos);

                this.blockStatesPlaced[position] = null;

                return true;
            }
        }
    }

    return false;
}