Morality turns on whether the pleasure precedes or follows the pain. Thus, it is immoral to get drunk because the headache comes after the drinking, but if the headache came first, and the drunkenness afterwards, it would be moral to get drunk.