Why is having pericarditis painful?

Nerves. The pericardium is inflamed when there is pericarditis. There are nerves that connect to the pericardium. So just as you might get pain in an inflamed joint, the inflammation of the pericaridum causes pain. Medications that help such joint pain are also useful for the pain of pericarditis.
Inflammation. There are nerves in the area of the heart and the heart sac(pericardium) when there is inflammation, or infection, or ischemia the nerves send the pain signals to the brain.