Skip to content

Instantly share code, notes, and snippets.

@riking
Created July 2, 2013 19:52
Show Gist options
  • Save riking/5912537 to your computer and use it in GitHub Desktop.
Save riking/5912537 to your computer and use it in GitHub Desktop.
/////////////////////////////
// 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