diff --git a/src/main/kotlin/gg/flyte/twilight/extension/Player.kt b/src/main/kotlin/gg/flyte/twilight/extension/Player.kt index 5726ffa..d059161 100644 --- a/src/main/kotlin/gg/flyte/twilight/extension/Player.kt +++ b/src/main/kotlin/gg/flyte/twilight/extension/Player.kt @@ -1,8 +1,12 @@ package gg.flyte.twilight.extension import gg.flyte.twilight.Twilight -import net.md_5.bungee.api.ChatMessageType -import net.md_5.bungee.api.chat.TextComponent +import gg.flyte.twilight.scheduler.TwilightRunnable +import gg.flyte.twilight.scheduler.async +import gg.flyte.twilight.scheduler.delay +import gg.flyte.twilight.scheduler.repeat +import gg.flyte.twilight.time.TimeUnit +import net.kyori.adventure.text.Component import org.bukkit.Bukkit import org.bukkit.Sound import org.bukkit.entity.Player @@ -26,15 +30,40 @@ fun Player.playSound(sound: Sound) { * * @param message The message to be displayed on the action bar. */ -fun Player.sendActionBar(message: String) { - spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent(message)) +fun Player.sendActionBar( + message: String, + duration: Int = 40, + timeUnit: TimeUnit = TimeUnit.TICKS, + infinite: Boolean = false +): TwilightRunnable = sendActionBar(message.toComponent(), duration, timeUnit, infinite) + +/** + * Sends a message to the player's action bar. + * + * @param message The message to be displayed on the action bar. + */ +fun Player.sendActionBar( + message: Component, + duration: Int = 40, + timeUnit: TimeUnit = TimeUnit.TICKS, + infinite: Boolean = false +): TwilightRunnable { + if (infinite) return repeat(40, true) { sendActionBar(message) } + if (duration <= 40) return async { sendActionBar(message) } + return repeat(40) { + sendActionBar(message) + }.also { + delay(duration, timeUnit, true) { + cancel() + } + } } /** - * Removes any existing action bar for the player. + * Removes any existing action bar for the player, unless an infinite one has been scheduled */ fun Player.clearActionBar() { - spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent("")) + sendActionBar(Component.empty()) } /** @@ -44,9 +73,17 @@ fun Player.clearActionBar() { * filling the player's hunger bar completely. The player will be fully fed after * calling this method. */ -fun Player.feed() { foodLevel = 20 } -fun Player.resetWalkSpeed() { walkSpeed = 0.2F } -fun Player.resetFlySpeed() { flySpeed = 0.1F} +fun Player.feed() { + foodLevel = 20 +} + +fun Player.resetWalkSpeed() { + walkSpeed = 0.2F +} + +fun Player.resetFlySpeed() { + flySpeed = 0.1F +} /** * Adds the specified [itemStack] to the player's inventory and/or drops remaining on the ground once inventory is full. @@ -92,6 +129,7 @@ fun Player.freeze() { walkSpeed = 0.0f flySpeed = 0.0f } + /** * Unfreezes the player. */ diff --git a/src/main/kotlin/gg/flyte/twilight/scheduler/Scheduler.kt b/src/main/kotlin/gg/flyte/twilight/scheduler/Scheduler.kt index f2ef835..248ba71 100644 --- a/src/main/kotlin/gg/flyte/twilight/scheduler/Scheduler.kt +++ b/src/main/kotlin/gg/flyte/twilight/scheduler/Scheduler.kt @@ -6,7 +6,6 @@ package gg.flyte.twilight.scheduler import gg.flyte.twilight.Twilight import gg.flyte.twilight.time.TimeUnit import org.bukkit.scheduler.BukkitRunnable -import org.bukkit.scheduler.BukkitTask /** * Schedules a synchronous task to be executed by the Bukkit scheduler. @@ -90,19 +89,23 @@ fun repeat( unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, runnable: BukkitRunnable.() -> Unit -): BukkitTask { +): TwilightRunnable { return if (async) { - createBukkitRunnable(runnable).runTaskTimerAsynchronously( - Twilight.plugin, - unit.toTicks(delay.toLong()), - unit.toTicks(period.toLong()) - ) + TwilightRunnable(runnable, true, 0).also { + it.runTaskTimerAsynchronously( + Twilight.plugin, + unit.toTicks(delay.toLong()), + unit.toTicks(period.toLong()) + ) + } } else { - createBukkitRunnable(runnable).runTaskTimer( - Twilight.plugin, - unit.toTicks(delay.toLong()), - unit.toTicks(period.toLong()) - ) + TwilightRunnable(runnable, false, 0).also { + it.runTaskTimer( + Twilight.plugin, + unit.toTicks(delay.toLong()), + unit.toTicks(period.toLong()) + ) + } } } @@ -115,7 +118,7 @@ fun repeat( * @return The BukkitTask representing the scheduled task. * @see repeat */ -fun repeat(periodTicks: Int = 1, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeat(periodTicks: Int = 1, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeat(periodTicks, periodTicks, TimeUnit.TICKS, false, runnable) } @@ -130,7 +133,7 @@ fun repeat(periodTicks: Int = 1, runnable: BukkitRunnable.() -> Unit): BukkitTas * @return The BukkitTask representing the scheduled task. * @see repeat */ -fun repeat(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeat(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeat(periodTicks, periodTicks, TimeUnit.TICKS, async, runnable) } @@ -144,7 +147,7 @@ fun repeat(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> * @return The BukkitTask representing the scheduled task. * @see repeat */ -fun repeat(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeat(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeat(period, period, unit, false, runnable) } @@ -160,7 +163,7 @@ fun repeat(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): Bu * @return The BukkitTask representing the scheduled task. * @see repeat */ -fun repeat(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeat(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeat(period, period, unit, async, runnable) } @@ -176,7 +179,7 @@ fun repeat(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitRunnable * @return The BukkitTask representing the scheduled task. * @see repeat */ -fun repeat(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeat(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeat(delayTicks, periodTicks, TimeUnit.TICKS, async, runnable) } @@ -191,17 +194,10 @@ fun repeat(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: BukkitRu * @return The BukkitTask representing the scheduled task. * @see repeat */ -fun repeat(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeat(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeat(delay, period, unit, false, runnable) } - - - - - - - /** * Schedules a repeating task to be executed by the Bukkit scheduler. * @@ -218,19 +214,23 @@ fun repeatingTask( unit: TimeUnit = TimeUnit.TICKS, async: Boolean = false, runnable: BukkitRunnable.() -> Unit -): BukkitTask { +): TwilightRunnable { return if (async) { - createBukkitRunnable(runnable).runTaskTimerAsynchronously( - Twilight.plugin, - unit.toTicks(delay.toLong()), - unit.toTicks(period.toLong()) - ) + TwilightRunnable(runnable, true, 0).also { + it.runTaskTimerAsynchronously( + Twilight.plugin, + unit.toTicks(delay.toLong()), + unit.toTicks(period.toLong()) + ) + } } else { - createBukkitRunnable(runnable).runTaskTimer( - Twilight.plugin, - unit.toTicks(delay.toLong()), - unit.toTicks(period.toLong()) - ) + TwilightRunnable(runnable, false, 0).also { + it.runTaskTimer( + Twilight.plugin, + unit.toTicks(delay.toLong()), + unit.toTicks(period.toLong()) + ) + } } } @@ -243,7 +243,7 @@ fun repeatingTask( * @return The BukkitTask representing the scheduled task. * @see repeatingTask */ -fun repeatingTask(periodTicks: Int = 1, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeatingTask(periodTicks: Int = 1, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeatingTask(periodTicks, periodTicks, TimeUnit.TICKS, false, runnable) } @@ -258,7 +258,7 @@ fun repeatingTask(periodTicks: Int = 1, runnable: BukkitRunnable.() -> Unit): Bu * @return The BukkitTask representing the scheduled task. * @see repeatingTask */ -fun repeatingTask(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeatingTask(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeatingTask(periodTicks, periodTicks, TimeUnit.TICKS, async, runnable) } @@ -272,7 +272,7 @@ fun repeatingTask(periodTicks: Int = 1, async: Boolean, runnable: BukkitRunnable * @return The BukkitTask representing the scheduled task. * @see repeatingTask */ -fun repeatingTask(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeatingTask(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeatingTask(period, period, unit, false, runnable) } @@ -288,7 +288,7 @@ fun repeatingTask(period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Un * @return The BukkitTask representing the scheduled task. * @see repeatingTask */ -fun repeatingTask(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeatingTask(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeatingTask(period, period, unit, async, runnable) } @@ -304,7 +304,7 @@ fun repeatingTask(period: Int, unit: TimeUnit, async: Boolean, runnable: BukkitR * @return The BukkitTask representing the scheduled task. * @see repeatingTask */ -fun repeatingTask(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeatingTask(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeatingTask(delayTicks, periodTicks, TimeUnit.TICKS, async, runnable) } @@ -319,7 +319,7 @@ fun repeatingTask(delayTicks: Int, periodTicks: Int, async: Boolean, runnable: B * @return The BukkitTask representing the scheduled task. * @see repeatingTask */ -fun repeatingTask(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): BukkitTask { +fun repeatingTask(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunnable.() -> Unit): TwilightRunnable { return repeatingTask(delay, period, unit, false, runnable) } @@ -338,17 +338,3 @@ fun repeatingTask(delay: Int, period: Int, unit: TimeUnit, runnable: BukkitRunna // initialContext, // block //) - -/** - * Converts to a BukkitRunnable instance based on the provided [runnable] function. - * - * @param runnable The function representing the task to be executed. - * @return The created BukkitRunnable instance. - */ -private fun createBukkitRunnable(runnable: BukkitRunnable.() -> Unit): BukkitRunnable { - return object : BukkitRunnable() { - override fun run() { - this.runnable() - } - } -} \ No newline at end of file