-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring + scheduling + introduced Task Tracker
- Loading branch information
Showing
25 changed files
with
287 additions
and
45 deletions.
There are no files selected for viewing
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
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
2 changes: 1 addition & 1 deletion
2
api/src/main/kotlin/com/mattmx/ktgui/commands/DummyCommandExecutor.kt
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
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
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
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
66 changes: 66 additions & 0 deletions
66
api/src/main/kotlin/com/mattmx/ktgui/scheduling/TaskTracker.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.mattmx.ktgui.scheduling | ||
|
||
import org.bukkit.scheduler.BukkitTask | ||
import java.util.* | ||
|
||
class TaskTracker { | ||
private val list = Collections.synchronizedList(arrayListOf<BukkitTask>()) | ||
|
||
fun runAsync(block: TaskTrackerTask.() -> Unit) { | ||
var task: TaskTrackerTask? = null | ||
task = TaskTrackerTask(this, async { | ||
block(task!!) | ||
list.remove(this) | ||
}.apply { list.add(this) }) | ||
} | ||
|
||
fun runSync(block: TaskTrackerTask.() -> Unit) { | ||
var task: TaskTrackerTask? = null | ||
task = TaskTrackerTask(this, sync { | ||
block(task!!) | ||
list.remove(this) | ||
}.apply { list.add(this) }) | ||
} | ||
|
||
fun runAsyncLater(period: Long, block: TaskTrackerTask.() -> Unit) { | ||
var task: TaskTrackerTask? = null | ||
task = TaskTrackerTask(this, asyncDelayed(period) { | ||
block(task!!) | ||
list.remove(this) | ||
}.apply { list.add(this) }) | ||
} | ||
|
||
fun runSyncLater(period: Long, block: TaskTrackerTask.() -> Unit) { | ||
var task: TaskTrackerTask? = null | ||
task = TaskTrackerTask(this, syncDelayed(period) { | ||
block(task!!) | ||
list.remove(this) | ||
}.apply { list.add(this) }) | ||
} | ||
|
||
fun runAsyncRepeat(delay: Long, period: Long = 0L, block: TaskTrackerTask.() -> Unit) { | ||
var task: TaskTrackerTask? = null | ||
task = TaskTrackerTask(this, asyncRepeat(delay, period) { | ||
block(task!!) | ||
task!!.iterations++ | ||
}.apply { list.add(this) }) | ||
} | ||
|
||
fun runSyncRepeat(delay: Long, period: Long = 0L, block: TaskTrackerTask.() -> Unit) { | ||
var task: TaskTrackerTask? = null | ||
task = TaskTrackerTask(this, syncRepeat(delay, period) { | ||
block(task!!) | ||
task!!.iterations++ | ||
}.apply { list.add(this) }) | ||
} | ||
|
||
fun cancelAll() = list.apply { | ||
forEach { it.cancel() } | ||
list.clear() | ||
} | ||
|
||
fun removeTask(task: BukkitTask) { | ||
list.remove(task) | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
api/src/main/kotlin/com/mattmx/ktgui/scheduling/TaskTrackerTask.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.mattmx.ktgui.scheduling | ||
|
||
import org.bukkit.scheduler.BukkitTask | ||
|
||
class TaskTrackerTask( | ||
private val owner: TaskTracker, | ||
private val task: BukkitTask | ||
) { | ||
var iterations = 0 | ||
|
||
fun cancel() { | ||
owner.removeTask(task) | ||
task.cancel() | ||
} | ||
} |
181 changes: 181 additions & 0 deletions
181
api/src/main/kotlin/com/mattmx/ktgui/scheduling/scheduling.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package com.mattmx.ktgui.scheduling | ||
|
||
import org.bukkit.Bukkit | ||
import org.bukkit.plugin.java.JavaPlugin | ||
import org.bukkit.scheduler.BukkitTask | ||
import java.util.concurrent.CompletableFuture | ||
import java.util.concurrent.Future | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Dummy object to store the plugin instance in, so we don't have to provide it for | ||
* every single method call. | ||
* | ||
* You must initialize this before calling any other methods | ||
* | ||
* @author MattMX | ||
*/ | ||
object Scheduling { | ||
lateinit var plugin: JavaPlugin | ||
} | ||
|
||
/** | ||
* Forces code to run in an async context. | ||
* | ||
* @param block to run | ||
*/ | ||
fun forceAsync(block: () -> Unit) { | ||
if (!isAsync()) async { block() } | ||
else block() | ||
} | ||
|
||
/** | ||
* Forces code to run on the main thread. | ||
* | ||
* @param block to run | ||
*/ | ||
fun forceMainThread(block: () -> Unit) { | ||
if (isAsync()) sync { block() } | ||
else block() | ||
} | ||
|
||
/** | ||
* Returns true if the context of the code is async. | ||
* | ||
* @return if code is running async | ||
*/ | ||
fun isAsync() = !Bukkit.isPrimaryThread() | ||
|
||
/** | ||
* Schedules a method to run synchronously (blocking) on the main thread. | ||
* This will execute next tick. | ||
* | ||
* @author MattMX | ||
* @param task to execute | ||
*/ | ||
fun sync(task: BukkitTask.() -> Unit): BukkitTask { | ||
var delayedInit: BukkitTask? = null | ||
delayedInit = Bukkit.getScheduler().runTask(Scheduling.plugin) { -> task(delayedInit!!) } | ||
return delayedInit | ||
} | ||
|
||
/** | ||
* Schedules a repeating task to execute the next tick. You can specify the initial delay, period and | ||
* max iterations. Keep in mind the iteration is not actually accepted in the code block. | ||
* | ||
* @author MattMX | ||
* @param period between each execution | ||
* @param delay initial delay before we start the loop | ||
* @param maxIterations how many times we want to do this before cancelling automatically | ||
* @param task to execute | ||
*/ | ||
fun syncRepeat(period: Long, delay: Long = 0, task: BukkitTask.() -> Unit): BukkitTask { | ||
var delayedInit: BukkitTask? = null | ||
delayedInit = Bukkit.getScheduler().runTaskTimer(Scheduling.plugin, {-> task(delayedInit!!) }, delay, period) | ||
return delayedInit | ||
} | ||
|
||
/** | ||
* Schedule a task to run in the future. | ||
* | ||
* @author MattMX | ||
* @param delay the delay of when this will execute, must be +ve (in ticks) | ||
* @param task to execute | ||
*/ | ||
fun syncDelayed(delay: Long, task: BukkitTask.() -> Unit): BukkitTask { | ||
var delayedInit: BukkitTask? = null | ||
delayedInit = Bukkit.getScheduler().runTaskLater(Scheduling.plugin, {-> task(delayedInit!!) }, delay) | ||
return delayedInit | ||
} | ||
|
||
/** | ||
* Schedule an async function to run next tick. | ||
* | ||
* @author MattMX | ||
* @param task to execute asynchronously | ||
*/ | ||
fun async(task: BukkitTask.() -> Unit): BukkitTask { | ||
var delayedInit: BukkitTask? = null | ||
delayedInit = Bukkit.getScheduler().runTaskAsynchronously(Scheduling.plugin) { -> task(delayedInit!!) } | ||
return delayedInit | ||
} | ||
|
||
/** | ||
* Schedules a repeating task to execute asynchronously the next tick. You can specify the initial delay, period and | ||
* max iterations. Keep in mind the iteration is not actually accepted in the code block. | ||
* | ||
* @author MattMX | ||
* @param period between each execution | ||
* @param delay initial delay before we start the loop | ||
* @param maxIterations how many times we want to do this before cancelling automatically | ||
* @param task to execute | ||
*/ | ||
fun asyncRepeat(period: Long, delay: Long = 0, task: BukkitTask.() -> Unit): BukkitTask { | ||
var delayedInit: BukkitTask? = null | ||
delayedInit = Bukkit.getScheduler().runTaskTimerAsynchronously(Scheduling.plugin, {-> task(delayedInit!!) }, delay, period) | ||
return delayedInit | ||
} | ||
|
||
/** | ||
* Schedule a task to run in the future to execute asynchronously. | ||
* | ||
* @author MattMX | ||
* @param delay the delay of when this will execute, must be +ve (in ticks) | ||
* @param task to execute | ||
*/ | ||
fun asyncDelayed(delay: Long, task: BukkitTask.() -> Unit): BukkitTask { | ||
var delayedInit: BukkitTask? = null | ||
delayedInit = Bukkit.getScheduler().runTaskLaterAsynchronously(Scheduling.plugin, {-> task(delayedInit!!) }, delay) | ||
return delayedInit | ||
} | ||
|
||
/** | ||
* Returns a [CompletableFuture] with the type you specify. | ||
* We can use this to get values asynchronously. | ||
* | ||
* @author MattMX | ||
* @param block that returns our value | ||
*/ | ||
fun <T> future(block: () -> T) : CompletableFuture<T> { | ||
val future = CompletableFuture<T>() | ||
async { | ||
val result = block() | ||
future.complete(result) | ||
} | ||
return future | ||
} | ||
|
||
/** | ||
* Similar to [future], will return a [Future] with the type you want. | ||
* This method is Synchronous. | ||
* | ||
* @author MattMX | ||
* @param block that returns our value | ||
*/ | ||
fun <T> call(block: () -> T) : Future<T> { | ||
return Bukkit.getScheduler().callSyncMethod(Scheduling.plugin, block) | ||
} | ||
|
||
/** | ||
* Utility function that runs blocking to get a value from a future, with a default | ||
* timeout of 30 seconds. | ||
* | ||
* This is useful if you have a function that returns a [Future], like [future], since we can | ||
* call the function and then [await] it. | ||
* | ||
* @author MattMX | ||
*/ | ||
inline val <reified T> Future<T>.await : T | ||
get() { | ||
return this.get(30, TimeUnit.SECONDS) | ||
} | ||
|
||
/** | ||
* Smaller notation of the [await] function. This could be confusing since it uses the `!` operator so | ||
* keep that in mind to keep your code readable. Although it does look nicer in my opinion. | ||
* | ||
* !foo() as apposed to foo().await | ||
* | ||
* @author MattMX | ||
*/ | ||
inline operator fun <reified T> Future<T>.not() = await |
4 changes: 1 addition & 3 deletions
4
api/src/main/kotlin/com/mattmx/ktgui/scoreboards/ScoreboardBuilder.kt
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
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.