mcMMO-Dev/mcMMO — pull request #200 by bm01 (2012-04-24 00:40:39+00:00)
Title: Added Player to addXP arguments, avoids getPlayer call

Not sure if you wish to keep the gainXp task (which is still useful actually, talked to GJ about it). Anyway, this task triggers an error:

2012-04-13 12:22:17 [INFO] Glucas007 lost connection: disconnect.endOfStream
2012-04-13 12:22:17 [WARNING] Task of 'mcMMO' generated an exception
java.lang.NullPointerException
at com.gmail.nossr50.datatypes.PlayerProfile.addXP(PlayerProfile.java:1047)
at com.gmail.nossr50.runnables.GainXp.run(GainXp.java:42)
at org.bukkit.craftbukkit.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:126)
at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:517)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)

It happens because Bukkit.getPlayer (in PlayerProfile.addXP) returns null if the player is offline, which is possible because .addXP is called at the end of the server tick (I guess).

I think the most efficient and cleaner way to avoid this, is not to do a null check or even to call player.isOnline in gainXp.run, but to directly pass the player to PlayerProfile.addXP. Every method calling addXP already have a reference to the player.

I though about doing the same for the PlayerProfile, not needed but it would avoid a few Users.getProfile call.