Skip to content

Commit

Permalink
Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Jul 11, 2024
1 parent 6e4cd72 commit 16fb041
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kr.toxicity.healthbar.util.*
import org.bukkit.Location
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.util.Vector
import java.util.ArrayList
import java.util.Collections
import java.util.EnumSet
import java.util.UUID
Expand Down Expand Up @@ -109,7 +110,7 @@ class HealthBarImpl(
}.toMutableList()

override fun work(): Boolean {
if (!hasNext()) {
if (!hasNext() || displays.isEmpty()) {
displays.forEach {
it.remove()
}
Expand All @@ -118,10 +119,20 @@ class HealthBarImpl(
indexes.values.forEach {
it.clear()
}
displays.removeIf {
!it.update()
var result = false
var max = 0
val pool = ArrayList<RenderedLayout.RenderedEntityPool>()
displays.forEach {
if (it.update()) {
result = true
if (max < it.max) max = it.max
pool.add(it)
}
}
pool.forEach {
it.create(max)
}
return displays.isNotEmpty()
return result
}
}

Expand Down Expand Up @@ -186,10 +197,9 @@ class HealthBarImpl(
r.canRender()
}
if (imageRender.isEmpty() && textRender.isEmpty()) return@forEach
val index = it.group?.let { s ->
val next = it.group?.let { s ->
indexes[s]
}
val next = index?.next() ?: 0
}?.next() ?: 0
imageRender.forEach { image ->
val render = image.render(next)
val length = render.pixel + render.component.width
Expand Down
110 changes: 63 additions & 47 deletions dist/src/main/kotlin/kr/toxicity/healthbar/healthbar/RenderedLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kr.toxicity.healthbar.api.healthbar.GroupIndex
import kr.toxicity.healthbar.api.healthbar.HealthBarData
import kr.toxicity.healthbar.api.layout.LayoutGroup
import kr.toxicity.healthbar.api.nms.VirtualTextDisplay
import kr.toxicity.healthbar.api.renderer.ImageRenderer
import kr.toxicity.healthbar.api.renderer.PixelRenderer
import kr.toxicity.healthbar.util.*
import org.bukkit.Location
Expand All @@ -25,10 +26,10 @@ class RenderedLayout(group: LayoutGroup, pair: HealthBarData) {
) {
private val imagesEntity = images.map {
RenderedEntity(it)
}
private val textsEntity = images.map {
}.toMutableList()
private val textsEntity = texts.map {
RenderedEntity(it)
}
}.toMutableList()

fun displays() = ArrayList<VirtualTextDisplay>().apply {
imagesEntity.forEach {
Expand All @@ -43,42 +44,51 @@ class RenderedLayout(group: LayoutGroup, pair: HealthBarData) {
}
}

private fun count(): Int {
val index = group?.let { s ->
indexes[s]
}
return index?.next() ?: 0
}

var max = 0

fun update(): Boolean {
val count = count()
val loc = data.toEntityLocation()
var result = false
imagesEntity.removeIf {
!it.has()
}
textsEntity.removeIf {
!it.has()
}
val imageMap = imagesEntity.filter {
it.can()
}
val textMap = textsEntity.filter {
it.can()
}
if (imageMap.isEmpty() && textMap.isEmpty()) return false
val count = group?.let { s ->
indexes[s]
}?.next() ?: 0
max = 0
val array = ArrayList<() -> Unit>()
imagesEntity.forEach {
if (it.update(count)) {
array.add {
it.create(loc)
}
result = true
}
imageMap.forEach {
it.update(count)
}
textsEntity.forEach {
if (it.update(count)) {
array.add {
it.create(loc)
}
result = true
}
textMap.forEach {
it.update(count)
}
array.forEach {
it()
return true
}

fun create(max: Int) {
val imageMap = imagesEntity.filter {
it.can()
}
val textMap = textsEntity.filter {
it.can()
}
val loc = data.toEntityLocation()
imageMap.forEach {
it.create(max, loc)
}
textMap.forEach {
it.create(max, loc)
}
return result
}

fun remove() {
imagesEntity.forEach {
it.remove()
Expand All @@ -92,30 +102,36 @@ class RenderedLayout(group: LayoutGroup, pair: HealthBarData) {
private val renderer: PixelRenderer
) {
var entity: VirtualTextDisplay? = null
var comp = EMPTY_WIDTH_COMPONENT
var comp = EMPTY_PIXEL_COMPONENT

fun remove() {
entity?.remove()
}

fun create(loc: Location) {
val finalComp = EMPTY_WIDTH_COMPONENT + comp + NEW_LAYER + (-comp.width + max).toSpaceComponent()
entity = (entity?.apply {
text(finalComp.component.build())
} ?: data.createEntity(finalComp, renderer.layer())).apply {
teleport(loc)
fun can(): Boolean {
val result = renderer.canRender()
if (!result) {
entity?.remove()
entity = null
}
return result
}

fun update(count: Int): Boolean {
return if (renderer.canRender()) {
comp = renderer.render(count).component
if (max < comp.width) max = comp.width
true
} else {
entity?.remove()
false
}
fun has() = renderer.hasNext()

fun create(max: Int, loc: Location) {
val length = comp.pixel + comp.component.width
val finalComp = comp.pixel.toSpaceComponent() + comp.component + (-length + max).toSpaceComponent() + NEW_LAYER
entity = entity?.apply {
teleport(loc)
text(finalComp.component.build())
} ?: data.createEntity(finalComp, renderer.layer())
}

fun update(count: Int) {
comp = renderer.render(count)
val length = comp.pixel + comp.component.width
if (renderer is ImageRenderer && renderer.isBackground && max < length) max = length
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fun HealthBarData.createEntity(component: WidthComponent, layer: Int = 0): Virtu
return PLUGIN.nms().createTextDisplay(player.player(), toEntityLocation(), component.component.build()).apply {
val scale = healthBar.scale()
transformation(
Vector(0.0, if (ConfigManagerImpl.useCoreShaders()) -(1 - scale.y) * 8192 / 40 else 0.0, (layer.toDouble()) / 400),
Vector(0.0, if (ConfigManagerImpl.useCoreShaders()) -(1 - scale.y) * 8192 / 40 else 0.0, (layer.toDouble()) / 4000),
scale
)
}
Expand Down

0 comments on commit 16fb041

Please sign in to comment.