Permalink
Browse files

Resolves #9 - asynchronous handling.

This is a very thin layer, I feel that trying to build too much more to protect people from theirselves will just be digging myself into a pit.
  • Loading branch information...
1 parent 1b4ef01 commit a68aa183ba57cbfd335387f5bc2c103948d15573 @CmdrDats committed Dec 21, 2012
Showing with 30 additions and 0 deletions.
  1. +30 −0 src/cljminecraft/bukkit.clj
View
30 src/cljminecraft/bukkit.clj
@@ -33,6 +33,36 @@
(defn world-by-uuid [uuid]
(.getWorld (server) (UUID/fromString uuid)))
+(defn seconds-to-ticks [s]
+ (int (* 20 s)))
+(defn ui-sync
+ "Execute a given function on the main UI thread"
+ [plugin fn]
+ (.runTask (scheduler) plugin fn))
+(defn delayed-task
+ "Execute a given function on the main UI thread after a delay in server ticks (1 tick = 1/20 second), will return a task id you can use to cancel the task - if you specify async?, take care not to directly call any Bukkit API and, by extension, and clj-minecraft functions that use the Bukkit API within this function"
+ [plugin fn delay & [async?]]
+ (if async?
+ (.runTaskAsynchronously (scheduler) plugin fn (long delay))
+ (.runTaskLater (scheduler) plugin fn (long delay))))
+(defn repeated-task
+ "Execute a given function repeatedly on the UI thread, delay and period in server ticks. If you specify async?, take care not to directly call any Bukkit API and, by extension, and clj-minecraft functions that use the Bukkit API within this function"
+ [plugin fn delay period & [async?]]
+ (if async?
+ (.runTaskTimerAsynchronously (scheduler) fn (long delay) (long period))
+ (.runTaskTimer (scheduler) fn (long delay) (long period))))
+
+(defn cancel-task
+ [task-id]
+ (.cancelTask (scheduler) task-id))
+
+(defn running-task?
+ [task-id]
+ (.isCurrentlyRunning (scheduler) task-id))
+
+(defn queued-task?
+ [task-id]
+ (.isQueued (scheduler) task-id))

0 comments on commit a68aa18

Please sign in to comment.