Skip to content

Instantly share code, notes, and snippets.

@realvictorprm
Last active August 22, 2020 09:21
Show Gist options
  • Save realvictorprm/87f99256d571d94ac93f3378772cb537 to your computer and use it in GitHub Desktop.
Save realvictorprm/87f99256d571d94ac93f3378772cb537 to your computer and use it in GitHub Desktop.
Attempt to create an sync IO for bukkit
object Bukkit {
import scala.concurrent.ExecutionContext
implicit val cs: ContextShift[IO] = IO.contextShift(ExecutionContext.global)
def scheduler: BukkitScheduler = JBukkit.getScheduler
def runTask(task: IO[Unit])(implicit plugin: JavaPlugin): BukkitTask = scheduler.runTask(plugin, Utils.mkRunnable(task.unsafeRunSync()))
def runTaskAsync(task: IO[Unit])(implicit plugin: JavaPlugin): BukkitTask = scheduler.runTaskAsynchronously(plugin, Utils.mkRunnable(task.unsafeRunSync()))
def callSyncMethod[T](codeBlock: => T)(implicit plugin: JavaPlugin): IO[T] = {
def fun(deferred: Deferred[IO, T]) =
for {
res <- IO {
codeBlock
}
_ <- deferred.complete(res).attempt.void
} yield ()
for {
deferred <- Deferred[IO, T]
_ <- IO {
runTask(fun(deferred))
}
res <- deferred.get
} yield res
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment