Created
July 2, 2013 19:52
-
-
Save riking/5912537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///////////////////////////// | |
// An API method /////////// | |
///////////////////////////// | |
/** | |
* Reads the player's rank in each Skill from the database and runs the | |
* callback on the main thread. | |
*/ | |
public void readRankAsync(String playerName, Function<Map<SkillType, Integer>, Void> callback); | |
///////////////////////////// | |
// API implementation ////// | |
///////////////////////////// | |
public void readRankAsync(String playerName, Function<Map<SkillType, Integer>, Void> callback) { | |
Bukkit.getScheduler().runTaskAsynchronously(new ReadRankAsyncTask(playerName, database, callback)); | |
} | |
public class ReadRankAsyncTask implements Runnable { | |
public ReadRankAsyncTask(String player, Database database, Function<Map<SkillType, Integer>, Void> callback) { | |
// (assign those to instance vars) | |
} | |
public void run() { | |
Map<SkillType, Integer> ret = database.slowRankReadingOperation(player); | |
Bukkit.getScheduler().runCallback(MyPlugin.getInstance(), callback, ret); | |
} | |
} | |
///////////////////////////// | |
// API Usage //////////////// | |
///////////////////////////// | |
public void onCommand(....) { | |
final CommandSener fSender = sender; | |
readRankAsync(targetPlayer, new Function<Map<SkillType, Integer>, Void>() { | |
@Override | |
public Void apply(Map<SkillType, Integer> rankData) { | |
fSender.sendMessage("Your Ranks:"); | |
// blah blah loop the data and send the message | |
} | |
}); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment