A teenager who used a 3D printer in an attempt to build a semi-automatic gun has been spared jail.