Bukkit/CraftBukkit — comment on commit 8a8e4f43927c9b12c0610ff342965ce138282c4a
File: src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java line 499
By Sleaker:

as snowleo said, this is wong. setTotalExperience is supposed to pull down and adjust the player's level if the calculations would adjust the level. The issue is that messing with them seperately causes the API to be able to set non-synchronized values for level/total exp - this means that the next time a player gains exp or dies, the amounts are all wrong.

The only way around this is to manually calculate what the values should internally be, and constantly check to make sure they are properly synced up with the actual values for that level/totalExp.