Skip to content

Commit

Permalink
Adds popup queue.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Mar 21, 2024
1 parent de74dad commit f709425
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ object PlaceholderManagerImpl: PlaceholderManager, BetterHudManager {
p.bukkitPlayer.getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value
}
},
"health_percentage" to HudPlaceholder.of { _, _ ->
Function { p ->
p.bukkitPlayer.health / p.bukkitPlayer.getAttribute(Attribute.GENERIC_MAX_HEALTH)!!.value * 100.0
}
},
"max_air" to HudPlaceholder.of { _, _ ->
Function { p ->
p.bukkitPlayer.maximumAir
Expand Down
3 changes: 2 additions & 1 deletion dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PopupImpl(
private val group = section.getString("group") ?: internalName
private val unique = section.getBoolean("unique")
private val dispose = section.getBoolean("dispose")
private val queue = section.getBoolean("queue")
private val default = ConfigManager.defaultPopup.contains(internalName) || section.getBoolean("default")
private val keyMapping = section.getBoolean("key-mapping")
private val index: ((UpdateEvent) -> (HudPlayer) -> Int)? = section.getString("index")?.let {
Expand Down Expand Up @@ -96,7 +97,7 @@ class PopupImpl(
PopupIteratorGroupImpl(dispose)
}
if (unique && get.contains(internalName)) return null
if (get.index >= move.locations.size) return null
if (!queue && get.index >= move.locations.size) return null
val buildCondition = conditions.build(reason)
if (!buildCondition(player)) return null
var updater = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import java.util.Collections
import java.util.Comparator
import java.util.TreeSet

class PopupIteratorGroupImpl(private val dispose: Boolean): PopupIteratorGroup {
class PopupIteratorGroupImpl(
private val dispose: Boolean,
): PopupIteratorGroup {
private val list = Collections.synchronizedSet(TreeSet<PopupIterator>(Comparator.naturalOrder()))

override fun addIterator(iterator: PopupIterator) {
Expand Down Expand Up @@ -48,19 +50,24 @@ class PopupIteratorGroupImpl(private val dispose: Boolean): PopupIteratorGroup {

override fun next(): List<WidthComponent> {
val iterator = list.iterator()
val send = ArrayList<PopupIterator>()
var i = 0
while (iterator.hasNext()) {
val next = iterator.next()
val index = next.index
if (index < 0 || index > next.maxIndex || !next.available()) {
if (index > next.maxIndex) continue
if (index < 0 || !next.available()) {
list.toList().subList(i, list.size).forEach {
it.index = (it.index - 1).coerceAtLeast(it.priority)
}
next.remove()
iterator.remove()
} else i++
} else {
i++
send.add(next)
}
}
val result = list.map {
val result = send.map {
it.next()
}.sum()
if (dispose) list.clear()
Expand Down
14 changes: 9 additions & 5 deletions dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ class PopupLayout(
it.getComponent(reason)
}
return { player, index, frame ->
val get = map[index % map.size](player)
get[when (layout.animation.type) {
LayoutAnimationType.LOOP -> frame % get.size
LayoutAnimationType.PLAY_ONCE -> frame.coerceAtMost(get.lastIndex)
}]
if (index > map.lastIndex) {
EMPTY_WIDTH_COMPONENT
} else {
val get = map[index](player)
get[when (layout.animation.type) {
LayoutAnimationType.LOOP -> frame % get.size
LayoutAnimationType.PLAY_ONCE -> frame.coerceAtMost(get.lastIndex)
}]
}
}
}

Expand Down

0 comments on commit f709425

Please sign in to comment.