-
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.
- Loading branch information
Showing
8 changed files
with
202 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
15 changes: 15 additions & 0 deletions
15
api/src/main/kotlin/com/mattmx/ktgui/scheduling/IteratingTask.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 | ||
|
||
open class IteratingTask( | ||
val task: BukkitTask | ||
) { | ||
/** | ||
* How many times the task has repeated. | ||
* Do not increment yourself. | ||
*/ | ||
var iterations = 0 | ||
|
||
open fun cancel() = task.cancel() | ||
} |
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
36 changes: 36 additions & 0 deletions
36
api/src/main/kotlin/com/mattmx/ktgui/scheduling/builder/LaterTaskBuilder.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,36 @@ | ||
package com.mattmx.ktgui.scheduling.builder | ||
|
||
import com.mattmx.ktgui.scheduling.* | ||
import org.bukkit.scheduler.BukkitTask | ||
|
||
class LaterTaskBuilder( | ||
val isAsync: Boolean | ||
) { | ||
var delay = 0L | ||
private set | ||
lateinit var block: (IteratingTask) -> Unit | ||
private set | ||
|
||
infix fun delay(ticks: Long) = apply { | ||
this.delay = ticks | ||
} | ||
|
||
|
||
infix fun runs(block: IteratingTask.() -> Unit) = apply { | ||
this.block = block | ||
} | ||
|
||
fun run(): IteratingTask { | ||
var task: IteratingTask? = null | ||
|
||
val block: BukkitTask.() -> Unit = task@{ | ||
block.invoke(task!!) | ||
task!!.iterations++ | ||
} | ||
|
||
task = IteratingTask(if (isAsync) asyncDelayed(delay, block) else syncDelayed(delay, block)) | ||
|
||
return task | ||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
api/src/main/kotlin/com/mattmx/ktgui/scheduling/builder/RepeatingTaskBuilder.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,53 @@ | ||
package com.mattmx.ktgui.scheduling.builder | ||
|
||
import com.mattmx.ktgui.scheduling.IteratingTask | ||
import com.mattmx.ktgui.scheduling.asyncRepeat | ||
import com.mattmx.ktgui.scheduling.syncRepeat | ||
import org.bukkit.scheduler.BukkitTask | ||
|
||
class RepeatingTaskBuilder( | ||
val isAsync: Boolean | ||
) { | ||
var max = -1L | ||
private set | ||
var delay = 0L | ||
private set | ||
var period = 0L | ||
private set | ||
lateinit var block: (IteratingTask) -> Unit | ||
private set | ||
|
||
infix fun repeat(times: Long) = apply { | ||
this.max = times | ||
} | ||
|
||
infix fun delay(ticks: Long) = apply { | ||
this.delay = ticks | ||
} | ||
|
||
infix fun period(ticks: Long) = apply { | ||
this.period = ticks | ||
} | ||
|
||
infix fun runs(block: IteratingTask.() -> Unit) = apply { | ||
this.block = block | ||
} | ||
|
||
fun run(): IteratingTask { | ||
var task: IteratingTask? = null | ||
|
||
val block: BukkitTask.() -> Unit = task@{ | ||
if (task!!.iterations > max) { | ||
cancel() | ||
return@task | ||
} | ||
|
||
block.invoke(task!!) | ||
task!!.iterations++ | ||
} | ||
|
||
task = IteratingTask(if (isAsync) asyncRepeat(period, delay, block) else syncRepeat(period, delay, block)) | ||
|
||
return task | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
api/src/main/kotlin/com/mattmx/ktgui/scheduling/builder/Task.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,37 @@ | ||
package com.mattmx.ktgui.scheduling.builder | ||
|
||
import com.mattmx.ktgui.scheduling.asyncDelayed | ||
import com.mattmx.ktgui.scheduling.asyncRepeat | ||
import com.mattmx.ktgui.utils.seconds | ||
|
||
object Task { | ||
|
||
@JvmStatic | ||
fun async() = AsyncTaskBuilder() | ||
|
||
@JvmStatic | ||
fun sync() = SyncTaskBuilder() | ||
} | ||
|
||
fun main() { | ||
val repeating = Task | ||
.async() | ||
.repeating() | ||
.repeat(20) | ||
.delay(2.seconds) | ||
.period(1) | ||
.runs { | ||
println("Task repeating $iterations") | ||
}.run() | ||
|
||
val later = Task | ||
.sync() | ||
.later() | ||
.delay(1) | ||
.runs { | ||
println(iterations) | ||
}.run() | ||
|
||
val dsl = Task.async() repeating { | ||
} period 2 delay 2 repeat 20 | ||
} |
34 changes: 34 additions & 0 deletions
34
api/src/main/kotlin/com/mattmx/ktgui/scheduling/builder/TaskBuilder.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,34 @@ | ||
package com.mattmx.ktgui.scheduling.builder | ||
|
||
import com.mattmx.ktgui.scheduling.IteratingTask | ||
import java.util.function.Consumer | ||
|
||
interface TaskBuilder { | ||
|
||
fun repeating() = RepeatingTaskBuilder(isAsync()) | ||
|
||
infix fun repeating(block: IteratingTask.() -> Unit) = | ||
repeating().apply { runs(block) } | ||
|
||
infix fun repeating(block: Consumer<IteratingTask>) = | ||
repeating().apply { runs { block(this) } } | ||
|
||
fun later() = LaterTaskBuilder(isAsync()) | ||
|
||
infix fun later(block: IteratingTask.() -> Unit) = | ||
later().apply { runs(block) } | ||
|
||
infix fun later(block: Consumer<IteratingTask>) = | ||
later().apply { runs { block(this) } } | ||
|
||
fun isAsync() : Boolean | ||
|
||
} | ||
|
||
class AsyncTaskBuilder : TaskBuilder { | ||
override fun isAsync() = true | ||
} | ||
|
||
class SyncTaskBuilder : TaskBuilder { | ||
override fun isAsync() = false | ||
} |
22 changes: 22 additions & 0 deletions
22
plugin/src/main/java/com/mattmx/ktgui/examples/JavaScheduling.java
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,22 @@ | ||
package com.mattmx.ktgui.examples; | ||
|
||
import com.mattmx.ktgui.scheduling.builder.Task; | ||
|
||
public class JavaScheduling { | ||
public void test() { | ||
var repeating = Task.async() | ||
.repeating((task) -> { | ||
System.out.println("Ran this task " + task.getIterations() + " times."); | ||
}).delay(2) | ||
.period(2) | ||
.repeat(20) | ||
.run(); | ||
|
||
var later = Task.sync() | ||
.later((task) -> { | ||
System.out.println("1s later"); | ||
}) | ||
.delay(20) | ||
.run(); | ||
} | ||
} |