Java Code Examples for net.minecraft.block.Block#isReplaceable()

The following examples show how to use net.minecraft.block.Block#isReplaceable() . 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: BlockCraftingGrid.java    From Translocators with MIT License 6 votes vote down vote up
public boolean placeBlock(World world, EntityPlayer player, int x, int y, int z, int side) {
    if(disableCraftingGridKey)
        return false;

    Block block = world.getBlock(x, y, z);
    if(side != 1 && block != Blocks.snow_layer)
        return false;

    if (block != Blocks.vine && block != Blocks.tallgrass && block != Blocks.deadbush
            && !block.isReplaceable(world, x, y, z))
        y++;

    if (!world.isSideSolid(x, y-1, z, ForgeDirection.UP))
        return false;

    if (!world.canPlaceEntityOnSide(this, x, y, z, false, 1, null, null))
        return false;

    player.swingItem();
    if(!world.setBlock(x, y, z, this))
        return false;

    onBlockPlacedBy(world, x, y, z, player, null);
    return true;
}
 
Example 2
Source File: BlockMagiciansWorktable.java    From Wizardry with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Nonnull
@Override
public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
	EnumFacing placerFacing = placer.getHorizontalFacing();
	EnumFacing offsetDir = placerFacing.rotateY();
	BlockPos part2Pos = pos.offset(offsetDir);
	Block block = worldIn.getBlockState(part2Pos).getBlock();
	if (block.isReplaceable(worldIn, part2Pos)) {
		worldIn.setBlockState(part2Pos, getDefaultState().withProperty(FACING, placerFacing.getOpposite()).withProperty(ISLEFTSIDE, false));
		return getDefaultState().withProperty(FACING, placerFacing.getOpposite()).withProperty(ISLEFTSIDE, true);
	} else {
		block = worldIn.getBlockState(part2Pos.offset(offsetDir.getOpposite(), 2)).getBlock();
		part2Pos = part2Pos.offset(offsetDir.getOpposite(), 2);
		if (block.isReplaceable(worldIn, part2Pos)) {
			worldIn.setBlockState(part2Pos, getDefaultState().withProperty(FACING, placerFacing.getOpposite()).withProperty(ISLEFTSIDE, true));
		} else {
			return Blocks.AIR.getDefaultState();
		}
		return getDefaultState().withProperty(FACING, placerFacing.getOpposite()).withProperty(ISLEFTSIDE, false);
	}
}
 
Example 3
Source File: BlockUtils.java    From enderutilities with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Check if the given block can be placed in the given position.
 * Note: This method is a functional copy of ItemBlock.func_150936_a() which is client side only.
 */
public static boolean checkCanPlaceBlockAt(World world, BlockPos pos, EnumFacing side, Block blockNew)
{
    Block blockExisting = world.getBlockState(pos).getBlock();

    if (blockExisting == Blocks.SNOW_LAYER && blockExisting.isReplaceable(world, pos))
    {
        side = EnumFacing.UP;
    }
    else if (blockExisting.isReplaceable(world, pos) == false)
    {
        pos = pos.offset(side);
    }

    return world.mayPlace(blockNew, pos, false, side, (Entity) null);
}
 
Example 4
Source File: ItemTraverseWoodDoor.java    From CommunityMod with GNU Lesser General Public License v2.1 5 votes vote down vote up
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (facing != EnumFacing.UP) {
        return EnumActionResult.FAIL;
    }
    else {
        IBlockState iblockstate = worldIn.getBlockState(pos);
        Block block = iblockstate.getBlock();

        if (!block.isReplaceable(worldIn, pos)) {
            pos = pos.offset(facing);
        }

        ItemStack itemstack = player.getHeldItem(hand);

        if (player.canPlayerEdit(pos, facing, itemstack) && this.block.canPlaceBlockAt(worldIn, pos)) {
            EnumFacing enumfacing = EnumFacing.fromAngle((double) player.rotationYaw);
            int i = enumfacing.getXOffset();
            int j = enumfacing.getYOffset();
            boolean flag = i < 0 && hitZ < 0.5F || i > 0 && hitZ > 0.5F || j < 0 && hitX > 0.5F || j > 0 && hitX < 0.5F;
            placeDoor(worldIn, pos, enumfacing, this.block, flag);
            SoundType soundtype = worldIn.getBlockState(pos).getBlock().getSoundType(worldIn.getBlockState(pos), worldIn, pos, player);
            worldIn.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
            itemstack.shrink(1);
            return EnumActionResult.SUCCESS;
        }
        else {
            return EnumActionResult.FAIL;
        }
    }
}
 
Example 5
Source File: ItemOpenBlock.java    From OpenModsLib with MIT License 5 votes vote down vote up
/**
 * Replicates the super method, but with our own hooks
 */
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
	final IBlockState clickedBlockState = world.getBlockState(pos);
	final Block clickedBlock = clickedBlockState.getBlock();

	if (!clickedBlock.isReplaceable(world, pos)) pos = pos.offset(facing);

	ItemStack stack = player.getHeldItem(hand);

	if (!isStackValid(stack, player)) return EnumActionResult.FAIL;
	if (!player.canPlayerEdit(pos, facing, stack)) return EnumActionResult.FAIL;
	if (!world.mayPlace(this.block, pos, false, facing, (Entity)null)) return EnumActionResult.FAIL;

	final int itemMetadata = getMetadata(stack.getMetadata());

	if (this.block instanceof OpenBlock) {
		final OpenBlock openBlock = (OpenBlock)this.block;
		if (!openBlock.canBlockBePlaced(world, pos, hand, facing, hitX, hitY, hitZ, itemMetadata, player)) return EnumActionResult.FAIL;
	}

	final IBlockState newBlockState = this.block.getStateForPlacement(world, pos, facing, hitX, hitY, hitZ, itemMetadata, player, hand);

	if (placeBlockAt(stack, player, world, pos, facing, hitX, hitY, hitZ, newBlockState)) {
		final SoundType soundType = this.block.getSoundType(newBlockState, world, pos, player);
		world.playSound(player, pos, soundType.getPlaceSound(), SoundCategory.BLOCKS, (soundType.getVolume() + 1.0F) / 2.0F, soundType.getPitch() * 0.8F);
		afterBlockPlaced(stack, player, world, pos);
	}

	return EnumActionResult.SUCCESS;
}
 
Example 6
Source File: ItemTranslocator.java    From Translocators with MIT License 5 votes vote down vote up
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) {
    Block block = world.getBlock(x, y, z);

    if (block == Blocks.snow_layer && (world.getBlockMetadata(x, y, z) & 7) < 1) {
        side = 1;
    } else if (block != Blocks.vine && block != Blocks.tallgrass && block != Blocks.deadbush && !block.isReplaceable(world, x, y, z)) {
        if (side == 0)
            --y;
        if (side == 1)
            ++y;
        if (side == 2)
            --z;
        if (side == 3)
            ++z;
        if (side == 4)
            --x;
        if (side == 5)
            ++x;
    }

    if (stack.stackSize == 0 || !player.canPlayerEdit(x, y, z, side, stack))
        return false;

    if (placeBlockAt(stack, player, world, x, y, z, side, hitX, hitY, hitZ, stack.getItemDamage())) {
        world.playSoundEffect(x + 0.5, y + 0.5, z + 0.5, field_150939_a.stepSound.func_150496_b(), (field_150939_a.stepSound.getVolume() + 1.0F) / 2.0F, field_150939_a.stepSound.getPitch() * 0.8F);
        --stack.stackSize;
        return true;
    }

    return false;
}
 
Example 7
Source File: ItemBlockChiselTorchPart.java    From Chisel-2 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) {
	Block block = world.getBlock(x, y, z);
	BlockCoord pos = new BlockCoord(x, y, z);
	if (!block.isReplaceable(world, x, y, z)) {
		pos = new BlockCoord(x, y, z).offset(side);
	}
	if (placePart(world, pos, stack, side, !world.isRemote)) {
		world.playSoundEffect(pos.x + 0.5F, pos.y + 0.5F, pos.z + 0.5F, field_150939_a.stepSound.func_150496_b(), (block.stepSound.getVolume() + 1.0F) / 2.0F, block.stepSound.getPitch() * 0.8F);
		stack.stackSize -= 1;
		return true;
	}
	return super.onItemUse(stack, player, world, x, y, z, side, hitX, hitY, hitZ);
}
 
Example 8
Source File: BlockGravity.java    From TFC2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean canFallInto(World worldIn, BlockPos pos)
{
	if (worldIn.isAirBlock(pos)) return true;
	Block block = worldIn.getBlockState(pos).getBlock();
	Material material = block.getMaterial(worldIn.getBlockState(pos));
	return (block == Blocks.FIRE) || (material == Material.AIR) || (material == Material.WATER) || (material == Material.LAVA) || block.isReplaceable(worldIn, pos);
}
 
Example 9
Source File: WorldGenAlienTree.java    From AdvancedRocketry with MIT License 5 votes vote down vote up
private boolean replaceBlockWithWood(World world, int x, int y, int z, EnumFacing direction) {
	BlockPos pos = new BlockPos(x,y,z);
	IBlockState state = world.getBlockState(pos);
	
	Block block = state.getBlock();

	if( block.isReplaceable(world, pos) ||  block.isLeaves(state, world, pos) || block == AdvancedRocketryBlocks.blockAlienWood) {
		this.setBlockAndNotifyAdequately(world, pos, AdvancedRocketryBlocks.blockAlienWood.getDefaultState().withProperty(BlockLog.LOG_AXIS, BlockLog.EnumAxis.fromFacingAxis(direction.getAxis())));
		return true;
	}
	else
		return false;
}
 
Example 10
Source File: BuildBattleDecoratorImplementation.java    From malmo with MIT License 5 votes vote down vote up
@SubscribeEvent
public void onPlayerInteract(PlayerInteractEvent event)
{
    // Disallow creating or destroying events in the player structure:
    if (event instanceof PlayerInteractEvent.LeftClickBlock)
    {
        // Destroy block
        if (blockInBounds(event.getPos(), this.sourceBounds))
            event.setCanceled(true);
    }
    else if (event instanceof PlayerInteractEvent.RightClickBlock)
    {
        // Place block - need to work out *where* the block would be placed.
        // This code was cribbed from ItemBlock.onItemUse()
        IBlockState iblockstate = event.getWorld().getBlockState(event.getPos());
        Block block = iblockstate.getBlock();
        EnumFacing side = event.getFace();
        BlockPos pos = event.getPos();
        if (block == Blocks.SNOW_LAYER && ((Integer)iblockstate.getValue(BlockSnow.LAYERS)).intValue() < 1)
        {
            side = EnumFacing.UP;
        }
        else if (!block.isReplaceable(event.getWorld(), pos))
        {
            pos = pos.offset(side);
        }
        if (blockInBounds(pos, this.sourceBounds))
            event.setCanceled(true);
    }
}
 
Example 11
Source File: ItemEngineeringTable.java    From Cyberware with MIT License 5 votes vote down vote up
/**
 * Called when a Block is right-clicked with this Item
 */
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
	if (facing != EnumFacing.UP)
	{
		return EnumActionResult.FAIL;
	}
	else
	{
		IBlockState iblockstate = worldIn.getBlockState(pos);
		Block block = iblockstate.getBlock();

		if (!block.isReplaceable(worldIn, pos))
		{
			pos = pos.offset(facing);
		}

		if (playerIn.canPlayerEdit(pos, facing, stack) && this.block.canPlaceBlockAt(worldIn, pos))
		{
			EnumFacing enumfacing = EnumFacing.fromAngle((double)playerIn.rotationYaw);
			int i = enumfacing.getFrontOffsetX();
			int j = enumfacing.getFrontOffsetZ();
			placeDoor(worldIn, pos, enumfacing, this.block);
			SoundType soundtype = this.block.getSoundType();
			worldIn.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
			--stack.stackSize;
			return EnumActionResult.SUCCESS;
		}
		else
		{
			return EnumActionResult.FAIL;
		}
	}
}
 
Example 12
Source File: ItemSurgeryChamber.java    From Cyberware with MIT License 5 votes vote down vote up
/**
 * Called when a Block is right-clicked with this Item
 */
public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
	if (facing != EnumFacing.UP)
	{
		return EnumActionResult.FAIL;
	}
	else
	{
		IBlockState iblockstate = worldIn.getBlockState(pos);
		Block block = iblockstate.getBlock();

		if (!block.isReplaceable(worldIn, pos))
		{
			pos = pos.offset(facing);
		}

		if (playerIn.canPlayerEdit(pos, facing, stack) && this.block.canPlaceBlockAt(worldIn, pos))
		{
			EnumFacing enumfacing = EnumFacing.fromAngle((double)playerIn.rotationYaw);
			int i = enumfacing.getFrontOffsetX();
			int j = enumfacing.getFrontOffsetZ();
			placeDoor(worldIn, pos, enumfacing, this.block);
			SoundType soundtype = this.block.getSoundType();
			worldIn.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
			--stack.stackSize;
			return EnumActionResult.SUCCESS;
		}
		else
		{
			return EnumActionResult.FAIL;
		}
	}
}
 
Example 13
Source File: ItemTraverseWoodDoor.java    From Traverse-Legacy-1-12-2 with MIT License 5 votes vote down vote up
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
    if (facing != EnumFacing.UP) {
        return EnumActionResult.FAIL;
    } else {
        IBlockState iblockstate = worldIn.getBlockState(pos);
        Block block = iblockstate.getBlock();

        if (!block.isReplaceable(worldIn, pos)) {
            pos = pos.offset(facing);
        }

        ItemStack itemstack = player.getHeldItem(hand);

        if (player.canPlayerEdit(pos, facing, itemstack) && this.block.canPlaceBlockAt(worldIn, pos)) {
            EnumFacing enumfacing = EnumFacing.fromAngle((double) player.rotationYaw);
            int i = enumfacing.getFrontOffsetX();
            int j = enumfacing.getFrontOffsetZ();
            boolean flag = i < 0 && hitZ < 0.5F || i > 0 && hitZ > 0.5F || j < 0 && hitX > 0.5F || j > 0 && hitX < 0.5F;
            placeDoor(worldIn, pos, enumfacing, this.block, flag);
            SoundType soundtype = worldIn.getBlockState(pos).getBlock().getSoundType(worldIn.getBlockState(pos), worldIn, pos, player);
            worldIn.playSound(player, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
            itemstack.shrink(1);
            return EnumActionResult.SUCCESS;
        } else {
            return EnumActionResult.FAIL;
        }
    }
}
 
Example 14
Source File: WorldHelper.java    From CommunityMod with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns true if block is replaceable and has no collider
 */
public static boolean isAirLikeBlock(World world, BlockPos pos)
{
	IBlockState state = world.getBlockState(pos);
	Block block = state.getBlock();
	boolean replaceable = block.isReplaceable(world, pos);
	boolean noCollider = (state.getCollisionBoundingBox(world, pos) == Block.NULL_AABB);
	return (replaceable || noCollider);
}
 
Example 15
Source File: ItemExtendedNodeJar.java    From Gadomancy with GNU Lesser General Public License v3.0 4 votes vote down vote up
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float par8, float par9, float par10) {
    Block var11 = world.getBlock(x, y, z);
    if (var11 == Blocks.snow_layer) {
        side = 1;
    } else if ((var11 != Blocks.vine) && (var11 != Blocks.tallgrass) && (var11 != Blocks.deadbush) && ((var11.isAir(world, x, y, z)) || (!var11.isReplaceable(world, x, y, z)))) {
        if (side == 0) {
            y--;
        }
        if (side == 1) {
            y++;
        }
        if (side == 2) {
            z--;
        }
        if (side == 3) {
            z++;
        }
        if (side == 4) {
            x--;
        }
        if (side == 5) {
            x++;
        }
    }
    if (stack.stackSize == 0) {
        return false;
    }
    if (!player.canPlayerEdit(x, y, z, side, stack)) {
        return false;
    }
    if ((y == 255) && (RegisteredBlocks.blockExtendedNodeJar.getMaterial().isSolid())) {
        return false;
    }
    if (world.canPlaceEntityOnSide(RegisteredBlocks.blockExtendedNodeJar, x, y, z, false, side, player, stack)) {
        Block var12 = RegisteredBlocks.blockExtendedNodeJar;
        int var13 = 2;
        int var14 = RegisteredBlocks.blockExtendedNodeJar.onBlockPlaced(world, x, y, z, side, par8, par9, par10, var13);
        if (placeBlockAt(stack, player, world, x, y, z, side, par8, par9, par10, var14)) {
            TileEntity te = world.getTileEntity(x, y, z);

            if ((te != null) && ((te instanceof TileExtendedNodeJar))) {
                if (stack.hasTagCompound()) {
                    AspectList aspects = getAspects(stack);
                    if (aspects != null) {
                        ((TileExtendedNodeJar) te).setAspects(aspects);
                        ((TileExtendedNodeJar) te).setNodeType(getNodeType(stack));
                        ((TileExtendedNodeJar) te).setNodeModifier(getNodeModifier(stack));
                        ((TileExtendedNodeJar) te).setExtendedNodeType(getExtendedNodeType(stack));
                        ((TileExtendedNodeJar) te).setId(getNodeId(stack));
                        ((TileExtendedNodeJar) te).setBehaviorSnapshot(getBehaviorSnapshot(stack));
                    }
                }
            }


            world.playSoundEffect(x + 0.5F, y + 0.5F, z + 0.5F, var12.stepSound.getStepResourcePath(), (var12.stepSound.getVolume() + 1.0F) / 2.0F, var12.stepSound.getPitch() * 0.8F);
            stack.stackSize -= 1;
        }
        return true;
    }
    return false;
}
 
Example 16
Source File: ItemAstroBed.java    From AdvancedRocketry with MIT License 4 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn,
		BlockPos pos, EnumHand hand, EnumFacing facing, float hitX,
		float hitY, float hitZ)
{
	if (worldIn.isRemote)
	{
		return EnumActionResult.SUCCESS;
	}
	else if (facing != EnumFacing.UP)
	{
		return EnumActionResult.FAIL;
	}
	else
	{
		ItemStack stack = playerIn.getHeldItem(hand);
		IBlockState iblockstate = worldIn.getBlockState(pos);
		Block block = iblockstate.getBlock();
		boolean flag = block.isReplaceable(worldIn, pos);

		if (!flag)
		{
			pos = pos.up();
		}

		int i = MathHelper.floor((double)(playerIn.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
		EnumFacing enumfacing = EnumFacing.getHorizontal(i);
		BlockPos blockpos = pos.offset(enumfacing);

		if (playerIn.canPlayerEdit(pos, facing, stack) && playerIn.canPlayerEdit(blockpos, facing, stack))
		{
			boolean flag1 = worldIn.getBlockState(blockpos).getBlock().isReplaceable(worldIn, blockpos);
			boolean flag2 = flag || worldIn.isAirBlock(pos);
			boolean flag3 = flag1 || worldIn.isAirBlock(blockpos);

			if (flag2 && flag3 && worldIn.getBlockState(pos.down()).isSideSolid(worldIn, pos.down(), EnumFacing.UP) && worldIn.getBlockState(blockpos.down()).isSideSolid(worldIn, blockpos.down(), EnumFacing.UP))
			{
				IBlockState iblockstate1 = AdvancedRocketryBlocks.blockAstroBed.getDefaultState().withProperty(BlockBed.OCCUPIED, Boolean.valueOf(false)).withProperty(BlockBed.FACING, enumfacing).withProperty(BlockBed.PART, BlockBed.EnumPartType.FOOT);

				if (worldIn.setBlockState(pos, iblockstate1, 11))
				{
					IBlockState iblockstate2 = iblockstate1.withProperty(BlockBed.PART, BlockBed.EnumPartType.HEAD);
					worldIn.setBlockState(blockpos, iblockstate2, 11);
				}

				SoundType soundtype = iblockstate1.getBlock().getSoundType();
				worldIn.playSound((EntityPlayer)null, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
				stack.setCount(stack.getCount() - 1);
				return EnumActionResult.SUCCESS;
			}
			else
			{
				return EnumActionResult.FAIL;
			}
		}
		else
		{
			return EnumActionResult.FAIL;
		}
	}
}
 
Example 17
Source File: ItemBlockPlacementProperty.java    From enderutilities with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos,
        EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
    ItemStack stack = player.getHeldItem(hand);

    if (this.hasPlacementProperty(stack))
    {
        ItemType type = new ItemType(stack, this.getPlacementProperty(stack).isNBTSensitive());
        stack = stack.copy();

        EnumActionResult result = super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);

        if (result == EnumActionResult.SUCCESS)
        {
            NBTTagCompound tag = PlacementProperties.getInstance().getPropertyTag(player.getUniqueID(), type);

            if (tag != null)
            {
                Block block = worldIn.getBlockState(pos).getBlock();

                if (block.isReplaceable(worldIn, pos) == false)
                {
                    pos = pos.offset(facing);
                    block = worldIn.getBlockState(pos).getBlock();
                }

                if (block instanceof BlockEnderUtilities)
                {
                    ((BlockEnderUtilities) block).setPlacementProperties(worldIn, pos, stack, tag);
                }
            }
        }

        return result;
    }
    else
    {
        return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
    }
}
 
Example 18
Source File: ItemSnow.java    From customstuff4 with GNU General Public License v3.0 4 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
    ItemStack itemstack = player.getHeldItem(hand);

    if (!itemstack.isEmpty() && player.canPlayerEdit(pos, facing, itemstack))
    {
        IBlockState iblockstate = worldIn.getBlockState(pos);
        Block block = iblockstate.getBlock();
        BlockPos blockpos = pos;

        if ((facing != EnumFacing.UP || block != this.block) && !block.isReplaceable(worldIn, pos))
        {
            blockpos = pos.offset(facing);
            iblockstate = worldIn.getBlockState(blockpos);
            block = iblockstate.getBlock();
        }

        if (block == this.block)
        {
            int i = iblockstate.getValue(BlockSnow.LAYERS);

            if (i < 8)
            {
                IBlockState iblockstate1 = iblockstate.withProperty(BlockSnow.LAYERS, i + 1);
                AxisAlignedBB axisalignedbb = iblockstate1.getCollisionBoundingBox(worldIn, blockpos);

                if (axisalignedbb != Block.NULL_AABB && worldIn.checkNoEntityCollision(axisalignedbb.offset(blockpos)) && worldIn.setBlockState(blockpos, iblockstate1, 10))
                {
                    SoundType soundtype = this.block.getSoundType(iblockstate1, worldIn, pos, player);
                    worldIn.playSound(player, blockpos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);

                    if (player instanceof EntityPlayerMP)
                    {
                        CriteriaTriggers.PLACED_BLOCK.trigger((EntityPlayerMP)player, pos, itemstack);
                    }

                    itemstack.shrink(1);
                    return EnumActionResult.SUCCESS;
                }
            }
        }

        return super.onItemUse(player, worldIn, pos, hand, facing, hitX, hitY, hitZ);
    }
    else
    {
        return EnumActionResult.FAIL;
    }
}
 
Example 19
Source File: ItemFuton.java    From Sakura_mod with MIT License 4 votes vote down vote up
@Override
public EnumActionResult onItemUse(EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand,
		EnumFacing facing, float hitX, float hitY, float hitZ) {
	if (worldIn.isRemote) {
		return EnumActionResult.SUCCESS;
	} else if (facing != EnumFacing.UP) {
		return EnumActionResult.FAIL;
	} else {
		IBlockState iblockstate = worldIn.getBlockState(pos);
		Block block = iblockstate.getBlock();
		boolean flag = block.isReplaceable(worldIn, pos);

		if (!flag) {
			pos = pos.up();
		}

		int i = MathHelper.floor(playerIn.rotationYaw * 4.0F / 360.0F + 0.5D) & 3;
		EnumFacing enumfacing = EnumFacing.getHorizontal(i);
		BlockPos blockpos = pos.offset(enumfacing);
		ItemStack itemstack = playerIn.getHeldItem(hand);

		if (playerIn.canPlayerEdit(pos, facing, itemstack) && playerIn.canPlayerEdit(blockpos, facing, itemstack)) {
			boolean flag1 = worldIn.getBlockState(blockpos).getBlock().isReplaceable(worldIn, blockpos);
			boolean flag2 = flag || worldIn.isAirBlock(pos);
			boolean flag3 = flag1 || worldIn.isAirBlock(blockpos);

			if (flag2 && flag3 && worldIn.getBlockState(pos.down()).isTopSolid()
					&& worldIn.getBlockState(blockpos.down()).isFullCube()) {
				IBlockState iblockstate1 = BlockLoader.FUTON.getDefaultState()
						.withProperty(BlockFuton.OCCUPIED, Boolean.valueOf(false))
						.withProperty(BlockHorizontal.FACING, enumfacing)
						.withProperty(BlockFuton.PART, BlockFuton.EnumPartType.FOOT);

				if (worldIn.setBlockState(pos, iblockstate1, 11)) {
					IBlockState iblockstate2 = iblockstate1.withProperty(BlockFuton.PART,
							BlockFuton.EnumPartType.HEAD);
					worldIn.setBlockState(blockpos, iblockstate2, 11);
				}

				SoundType soundtype = iblockstate1.getBlock().getSoundType(iblockstate1, worldIn, pos, playerIn);
				worldIn.playSound(null, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS,
						(soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F);
				itemstack.shrink(1);
				return EnumActionResult.SUCCESS;
			}
			return EnumActionResult.FAIL;
		}
		return EnumActionResult.FAIL;
	}
}