Skip to content

Commit 1ee5757

Browse files
committed
Ensure tick thread for heal/feed command
1 parent f5a0e26 commit 1ee5757

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/essentialcommands/Feed.kt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.github.rothes.esu.bukkit.module.essentialcommands
33
import io.github.rothes.esu.bukkit.user
44
import io.github.rothes.esu.bukkit.user.PlayerUser
55
import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.player
6+
import io.github.rothes.esu.bukkit.util.scheduler.Scheduler.syncTick
67
import io.github.rothes.esu.core.command.annotation.ShortPerm
78
import io.github.rothes.esu.core.configuration.data.MessageData
89
import io.github.rothes.esu.core.configuration.data.MessageData.Companion.message
@@ -26,12 +27,14 @@ object Feed : BaseCommand<FeatureToggle.DefaultTrue, Feed.Lang>() {
2627
@Command("feed <player>")
2728
@ShortPerm("others")
2829
fun feed(sender: User, player: Player, @Flag("silent") silent: Boolean = sender.uuid != player.uniqueId) {
29-
player.foodLevel = 20
30-
player.saturation = 20.0f // 5.0f on respawn
31-
player.exhaustion = 0.0f
32-
sender.message(lang, { fedPlayer }, player(player))
33-
if (!silent) {
34-
player.user.message(lang, { fed })
30+
player.syncTick {
31+
player.foodLevel = 20
32+
player.saturation = 20.0f // 5.0f on respawn
33+
player.exhaustion = 0.0f
34+
sender.message(lang, { fedPlayer }, player(player))
35+
if (!silent) {
36+
player.user.message(lang, { fed })
37+
}
3538
}
3639
}
3740
})

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/module/essentialcommands/Heal.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.github.rothes.esu.bukkit.module.essentialcommands
33
import io.github.rothes.esu.bukkit.user
44
import io.github.rothes.esu.bukkit.user.PlayerUser
55
import io.github.rothes.esu.bukkit.util.ComponentBukkitUtils.player
6+
import io.github.rothes.esu.bukkit.util.scheduler.Scheduler.syncTick
67
import io.github.rothes.esu.bukkit.util.version.adapter.AttributeAdapter
78
import io.github.rothes.esu.core.command.annotation.ShortPerm
89
import io.github.rothes.esu.core.configuration.data.MessageData
@@ -27,10 +28,12 @@ object Heal : BaseCommand<FeatureToggle.DefaultTrue, Heal.Lang>() {
2728
@Command("heal <player>")
2829
@ShortPerm("others")
2930
fun heal(sender: User, player: Player, @Flag("silent") silent: Boolean = sender.uuid != player.uniqueId) {
30-
player.heal(player.getAttribute(AttributeAdapter.MAX_HEALTH)!!.value)
31-
sender.message(lang, { healedPlayer }, player(player))
32-
if (!silent) {
33-
player.user.message(lang, { healed })
31+
player.syncTick {
32+
player.heal(player.getAttribute(AttributeAdapter.MAX_HEALTH)!!.value)
33+
sender.message(lang, { healedPlayer }, player(player))
34+
if (!silent) {
35+
player.user.message(lang, { healed })
36+
}
3437
}
3538
}
3639
})

bukkit/src/main/kotlin/io/github/rothes/esu/bukkit/util/scheduler/Scheduler.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.rothes.esu.bukkit.util.scheduler
22

33
import io.github.rothes.esu.bukkit.util.ServerCompatibility.isFolia
4+
import io.github.rothes.esu.bukkit.util.version.adapter.TickThreadAdapter.Companion.checkTickThread
45
import kotlinx.coroutines.CompletableDeferred
56
import org.bukkit.Bukkit
67
import org.bukkit.Location
@@ -120,6 +121,13 @@ object Scheduler {
120121
return schedule(this, plugin, func)
121122
}
122123

124+
fun Entity.syncTick(plugin: Plugin = esuPlugin, func: () -> Unit) {
125+
if (checkTickThread())
126+
func()
127+
else
128+
schedule(this, plugin, func)
129+
}
130+
123131
fun cancelTasks(plugin: Plugin = esuPlugin) {
124132
if (isFolia) {
125133
Bukkit.getGlobalRegionScheduler().cancelTasks(plugin)

0 commit comments

Comments
 (0)