Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit aa05c51

Browse files
committed
Added Scheduler Class
1 parent 8009545 commit aa05c51

File tree

6 files changed

+151
-35
lines changed

6 files changed

+151
-35
lines changed

.idea/compiler.xml

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
UndefinedAPI is a papermc api to make the life of developers easier. This is a multi use library from small util classes to a GUI manager.
77

8-
# Imports
8+
## Imports
99

1010
Maven:
1111
```<repositories>

build.gradle

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
21

32
plugins {
43
id 'java'
@@ -31,30 +30,7 @@ dependencies {
3130
implementation "org.reflections:reflections:0.9.11"
3231
}
3332

34-
def targetJavaVersion = 17
35-
java {
36-
def javaVersion = JavaVersion.toVersion(targetJavaVersion)
37-
if (JavaVersion.current() < javaVersion) {
38-
toolchain.languageVersion = JavaLanguageVersion.of(targetJavaVersion)
39-
}
40-
}
41-
42-
tasks.withType(JavaCompile).configureEach {
43-
options.encoding = 'UTF-8'
4433

45-
if (targetJavaVersion >= 10 || JavaVersion.current().isJava10Compatible()) {
46-
options.release.set(targetJavaVersion)
47-
}
48-
}
49-
50-
processResources {
51-
def props = [version: version]
52-
inputs.properties props
53-
filteringCharset 'UTF-8'
54-
filesMatching('plugin.yml') {
55-
expand props
56-
}
57-
}
5834
kotlin {
5935
jvmToolchain(17)
6036
}
@@ -66,3 +42,4 @@ tasks{
6642
}
6743

6844

45+

src/main/java/com/redmagic/undefinedapi/UndefinedAPI.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.redmagic.undefinedapi
22

33
import com.redmagic.undefinedapi.event.EventManager
44
import com.redmagic.undefinedapi.menu.MenuManager
5+
import com.redmagic.undefinedapi.scheduler.repeatingTask
56
import org.bukkit.Bukkit
67
import org.bukkit.plugin.java.JavaPlugin
78
import java.util.concurrent.TimeUnit
@@ -18,7 +19,6 @@ class UndefinedAPI(javaPlugin: JavaPlugin) {
1819
companion object{
1920
lateinit var plugin: JavaPlugin
2021
}
21-
2222
}
2323

2424

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.redmagic.undefinedapi.scheduler
2+
3+
import com.redmagic.undefinedapi.UndefinedAPI
4+
import org.bukkit.scheduler.BukkitRunnable
5+
import org.bukkit.scheduler.BukkitTask
6+
7+
fun sync(runnable: BukkitRunnable.() -> Unit): BukkitTask = createRunnable(runnable).runTask(UndefinedAPI.plugin)
8+
fun async(runnable: BukkitRunnable.() -> Unit): BukkitTask = createRunnable(runnable).runTaskAsynchronously(UndefinedAPI.plugin)
9+
10+
fun delay(ticks: Int, unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, runnable: BukkitRunnable.() -> Unit):BukkitTask{
11+
return if (async){
12+
createRunnable(runnable).runTaskLater(UndefinedAPI.plugin, unit.toTicks(ticks.toLong()))
13+
}else{
14+
createRunnable(runnable).runTaskLater(UndefinedAPI.plugin, unit.toTicks(ticks.toLong()))
15+
}
16+
}
17+
18+
fun delay(ticks: Int = 1, runnable: BukkitRunnable.() -> Unit): BukkitTask = delay(ticks, TimeUnit.TICKS, false, runnable)
19+
fun delay(ticks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = delay(ticks, TimeUnit.TICKS, async, runnable)
20+
21+
22+
fun repeatingTask(delay: Int, period: Int, times: Int = -1, unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, runnable: BukkitRunnable.() -> Unit): BukkitTask{
23+
return if (async){
24+
createRunnable(times, runnable).runTaskTimerAsynchronously(UndefinedAPI.plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
25+
}else{
26+
createRunnable(times ,runnable).runTaskTimer(UndefinedAPI.plugin, unit.toTicks(delay.toLong()), unit.toTicks(period.toLong()))
27+
}
28+
}
29+
fun repeatingTask(ticks: Int = 1, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(0, ticks, -1, TimeUnit.TICKS, false, runnable)
30+
fun repeatingTask(ticks: Int = 1, times: Int = -1, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(0, ticks, times, TimeUnit.TICKS, false, runnable)
31+
fun repeatingTask(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(periodTicks, periodTicks, -1, TimeUnit.TICKS, async, runnable)
32+
fun repeatingTask(periodTicks: Int = 1, async: Boolean, times: Int = -1, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(periodTicks, periodTicks, times, TimeUnit.TICKS, async, runnable)
33+
fun repeatingTask(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(period, period, -1, unit, false, runnable)
34+
fun repeatingTask(period: Int, unit: TimeUnit, times: Int = -1, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(period, period, times, unit, false, runnable)
35+
fun repeatingTask(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(period, period, -1, unit, async, runnable)
36+
fun repeatingTask(period: Int, unit: TimeUnit, times: Int = -1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(period, period, times, unit, async, runnable)
37+
fun repeatingTask(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(delayTicks, periodTicks, -1, TimeUnit.TICKS, async, runnable)
38+
fun repeatingTask(delayTicks: Int, periodTicks: Int, times: Int = -1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(delayTicks, periodTicks, times, TimeUnit.TICKS, async, runnable)
39+
fun repeatingTask(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(delay, period, -1, unit, false, runnable)
40+
fun repeatingTask(delay: Int, period: Int, times: Int = -1, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask = repeatingTask(delay, period, times, unit, false, runnable)
41+
42+
private fun createRunnable(runnable: BukkitRunnable.() -> Unit): BukkitRunnable{
43+
return object : BukkitRunnable(){
44+
override fun run() {
45+
runnable()
46+
}
47+
}
48+
}
49+
50+
private fun createRunnable(times: Int = -1, runnable: BukkitRunnable.() -> Unit): BukkitRunnable{
51+
var amount = 0
52+
return object : BukkitRunnable(){
53+
override fun run() {
54+
runnable()
55+
if (times == -1) return
56+
amount++
57+
if (amount >= times) {
58+
cancel()
59+
}
60+
}
61+
}
62+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.redmagic.undefinedapi.scheduler
2+
3+
/**
4+
* Provides an abstraction for representing various time units, including custom support for Minecraft ticks.
5+
* It allows conversion between standard time units like DAYS, HOURS, MINUTES, SECONDS, and MILLISECONDS using the
6+
* Java `java.util.concurrent.TimeUnit` along with custom conversions for Minecraft ticks.
7+
*
8+
* @author twilight
9+
*/
10+
enum class TimeUnit(private val javaTimeUnit: java.util.concurrent.TimeUnit?) {
11+
12+
DAYS(java.util.concurrent.TimeUnit.DAYS),
13+
HOURS(java.util.concurrent.TimeUnit.HOURS),
14+
MINUTES(java.util.concurrent.TimeUnit.MINUTES),
15+
SECONDS(java.util.concurrent.TimeUnit.SECONDS),
16+
TICKS(null),
17+
MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS);
18+
19+
companion object {
20+
private const val TICKS_PER_SECOND = 20
21+
private const val TICKS_PER_MINUTE = TICKS_PER_SECOND * 60
22+
private const val TICKS_PER_HOUR = TICKS_PER_MINUTE * 60
23+
private const val TICKS_PER_DAY = TICKS_PER_HOUR * 24
24+
}
25+
26+
/**
27+
* Convert the given `duration` to days.
28+
*
29+
* @param duration The duration to be converted.
30+
* @return The equivalent number of days.
31+
*/
32+
fun toDays(duration: Long): Long {
33+
return javaTimeUnit?.toDays(duration) ?: (duration / TICKS_PER_DAY)
34+
}
35+
36+
/**
37+
* Convert the given `duration` to hours.
38+
*
39+
* @param duration The duration to be converted.
40+
* @return The equivalent number of hours.
41+
*/
42+
fun toHours(duration: Long): Long {
43+
return javaTimeUnit?.toHours(duration) ?: (duration / TICKS_PER_HOUR)
44+
}
45+
46+
/**
47+
* Convert the given `duration` to minutes.
48+
*
49+
* @param duration The duration to be converted.
50+
* @return The equivalent number of minutes.
51+
*/
52+
fun toMinutes(duration: Long): Long {
53+
return javaTimeUnit?.toMinutes(duration) ?: (duration / TICKS_PER_MINUTE)
54+
}
55+
56+
/**
57+
* Convert the given `duration` to seconds.
58+
*
59+
* @param duration The duration to be converted.
60+
* @return The equivalent number of seconds.
61+
*/
62+
fun toSeconds(duration: Long): Long {
63+
return javaTimeUnit?.toSeconds(duration) ?: (duration / TICKS_PER_SECOND)
64+
}
65+
66+
/**
67+
* Convert the given `duration` to Minecraft ticks.
68+
*
69+
* @param duration The duration to be converted.
70+
* @return The equivalent number of Minecraft ticks.
71+
*/
72+
fun toTicks(duration: Long): Long {
73+
return (javaTimeUnit?.toMillis(duration)?.div(50)) ?: duration
74+
}
75+
76+
/**
77+
* Convert the given `duration` to milliseconds.
78+
*
79+
* @param duration The duration to be converted.
80+
* @return The equivalent number of milliseconds.
81+
*/
82+
fun toMillis(duration: Long): Long {
83+
return javaTimeUnit?.toMillis(duration) ?: (duration * 50)
84+
}
85+
86+
}

0 commit comments

Comments
 (0)