Skip to content

Commit

Permalink
feat: allow for duration specified and infinite actionbars through Tw…
Browse files Browse the repository at this point in the history
…ilightRunnable
  • Loading branch information
joshbker committed Oct 8, 2024
1 parent 9d967f1 commit 7ca737d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 65 deletions.
56 changes: 47 additions & 9 deletions src/main/kotlin/gg/flyte/twilight/extension/Player.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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())
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -92,6 +129,7 @@ fun Player.freeze() {
walkSpeed = 0.0f
flySpeed = 0.0f
}

/**
* Unfreezes the player.
*/
Expand Down
98 changes: 42 additions & 56 deletions src/main/kotlin/gg/flyte/twilight/scheduler/Scheduler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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())
)
}
}
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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.
*
Expand All @@ -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())
)
}
}
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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()
}
}
}

0 comments on commit 7ca737d

Please sign in to comment.