Last active
August 22, 2020 09:21
-
-
Save realvictorprm/87f99256d571d94ac93f3378772cb537 to your computer and use it in GitHub Desktop.
Attempt to create an sync IO for bukkit
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
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