Last active
January 23, 2024 12:03
-
-
Save entrypointkr/00c1914857dab7ecf4added036710b0d to your computer and use it in GitHub Desktop.
Bukkit boss bar immutable util
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
import org.bukkit.Bukkit | |
import org.bukkit.boss.BarColor | |
import org.bukkit.boss.BarStyle | |
import org.bukkit.boss.BossBar | |
import org.bukkit.entity.Player | |
import org.bukkit.event.EventHandler | |
import org.bukkit.event.Listener | |
import org.bukkit.event.player.PlayerQuitEvent | |
import org.bukkit.plugin.Plugin | |
import org.bukkit.scheduler.BukkitRunnable | |
import java.time.Duration | |
import java.time.LocalDateTime | |
import java.util.* | |
data class BossBarDisplay( | |
val id: UUID, | |
val title: String, | |
val color: BarColor, | |
val style: BarStyle, | |
val progress: Double, | |
val stayDuration: Duration? | |
) { | |
fun update(x: BossBar) { | |
x.title = title | |
x.color = color | |
x.style = style | |
x.progress = progress | |
} | |
} | |
data class BarPlayer( | |
val id: UUID, | |
val barId: UUID, | |
val expireTime: LocalDateTime? | |
) | |
data class BukkitBossBar( | |
val id: UUID, | |
val bar: BossBar, | |
val viewers: MutableMap<UUID, BarPlayer> | |
) | |
class BossBarService { | |
val bukkitBars: MutableMap<UUID, BukkitBossBar> = mutableMapOf() | |
fun getPlayerBars(playerId: UUID): List<Pair<BukkitBossBar, BarPlayer>> = | |
bukkitBars.values.flatMap { | |
val barPlayer = it.viewers[playerId] | |
if (barPlayer != null) { | |
listOf(it to barPlayer) | |
} else emptyList() | |
} | |
} | |
val bossBarService: BossBarService | |
get() = Bukkit.getServicesManager().load(BossBarService::class.java) | |
fun registerBukkitBossBar(plugin: BattlePlugin) { | |
val service = BossBarService() | |
Bukkit.getServicesManager().register(BossBarService::class.java, service, plugin, ServicePriority.Normal) | |
object : BukkitRunnable() { | |
override fun run() { | |
val nowTime = LocalDateTime.now() | |
for (p in Bukkit.getOnlinePlayers()) { | |
for ((bukkitBar, barPlayer) in service.getPlayerBars(p.uniqueId)) { | |
val diff = if (barPlayer.expireTime != null) { | |
Duration.between(nowTime, barPlayer.expireTime) | |
} else null | |
if (diff != null && diff.seconds <= 0) { | |
p.removeBossBar(bukkitBar.id) | |
} | |
} | |
} | |
} | |
}.runTaskTimer(plugin, 20L, 20L) | |
Bukkit.getPluginManager().registerEvents( | |
object : Listener { | |
@EventHandler | |
fun onQuit(e: PlayerQuitEvent) { | |
for ((bukkitBar, _) in service.getPlayerBars(e.player.uniqueId)) { | |
bukkitBar.viewers.remove(e.player.uniqueId) | |
bukkitBar.bar.removePlayer(e.player) | |
} | |
} | |
}, | |
plugin | |
) | |
} | |
fun Player.showBossBar(display: BossBarDisplay) { | |
val bukkitBar = bossBarService.bukkitBars.getOrPut(display.id) { | |
val bar = Bukkit.createBossBar( | |
display.title, | |
display.color, | |
display.style | |
) | |
BukkitBossBar(display.id, bar, mutableMapOf()) | |
} | |
bukkitBar.viewers[uniqueId] = BarPlayer( | |
uniqueId, | |
display.id, | |
if (display.stayDuration != null) LocalDateTime.now().plus(display.stayDuration) else null | |
) | |
display.update(bukkitBar.bar) | |
bukkitBar.bar.addPlayer(this) | |
} | |
fun Player.removeBossBar(id: UUID) { | |
val bukkitBar = bossBarService.bukkitBars[id] ?: return | |
if (bukkitBar.viewers.remove(uniqueId) != null) { | |
bukkitBar.bar.removePlayer(this) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment