Skip to content

Commit

Permalink
Bug fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Nov 24, 2024
1 parent 65016c4 commit db47896
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 37 deletions.
10 changes: 7 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudImageElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class HudImageElement(parent: HudImpl, private val imageLayout: ImageLayout, gui
imageLayout.property
)
val negativeSpace = parent.getOrCreateSpace(-1)
fun HudImage.toComponent(): ImageComponent {
fun HudImage.toComponent(parentComponent: ImageComponent? = null): ImageComponent {
val list = ArrayList<PixelComponent>()
if (listener != null) {
list.add(EMPTY_PIXEL_COMPONENT)
Expand Down Expand Up @@ -68,7 +68,7 @@ class HudImageElement(parent: HudImpl, private val imageLayout: ImageLayout, gui

list.add(component.toPixelComponent(finalPixel.x + (pair.image.xOffset * scale).roundToInt()))
}
return ImageComponent(this, list, children.entries.associate {
return ImageComponent(this, parentComponent, list, children.entries.associate {
it.key to it.value.toComponent()
})
}
Expand All @@ -77,7 +77,11 @@ class HudImageElement(parent: HudImpl, private val imageLayout: ImageLayout, gui
imageLayout.space,
imageLayout.stack,
imageLayout.maxStack,
imageLayout.image.toComponent(),
try {
imageLayout.image.toComponent()
} catch (_: StackOverflowError) {
throw RuntimeException("circular reference found in ${imageLayout.image.name}")
},
imageLayout.follow,
imageLayout.cancelIfFollowerNotExists,
imageLayout.conditions and imageLayout.image.conditions
Expand Down
32 changes: 21 additions & 11 deletions dist/src/main/kotlin/kr/toxicity/hud/image/HudImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,31 @@ class HudImage(
val listener = setting["listener"]?.asObject()?.let {
ListenerManagerImpl.getListener(it)
}
val children by lazy {
fun Iterable<YamlElement>.asMap() = associate {
val str = it.asString()
if (str == name) throw RuntimeException("circular image reference: $name")
str to ImageManager.getImage(str).ifNull("This children image doesn't exist in $name: $str")

private val childrenList = when (val child = setting["children"]) {
is YamlArray -> child.map {
it.asString()
}
when (val child = setting["children"]) {
is YamlArray -> child.asMap()
is YamlElement -> if (child.asString() == "*") ImageManager.allImage.associateBy {
is YamlElement -> listOf(child.asString())
null -> emptyList()
else -> throw RuntimeException("Unsupported children section: $name")
}

val children by lazy {
fun String.toImagePair() = this to ImageManager.getImage(this).ifNull("This children image doesn't exist in $name: $this")
when {
childrenList.isEmpty() -> emptyMap()
childrenList.size == 1 -> if (childrenList[0] == "*") ImageManager.allImage.filter {
it.name != name && !it.childrenList.contains(name)
}.associateBy {
it.name
} else listOf(child).asMap()
null -> emptyMap<String, HudImage>()
else -> throw RuntimeException("Unsupported children section: $name")
} else mapOf(childrenList[0].toImagePair())
else -> childrenList.associate {
it.toImagePair()
}
}
}

val follow = setting["follow"]?.asString()?.let {
PlaceholderManagerImpl.find(it).apply {
if (!java.lang.String::class.java.isAssignableFrom(clazz)) throw RuntimeException("This placeholder is not a string in image $name: $it")
Expand Down
44 changes: 24 additions & 20 deletions dist/src/main/kotlin/kr/toxicity/hud/image/ImageComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import net.kyori.adventure.text.format.TextColor

class ImageComponent(
private val original: HudImage,
private val parent: ImageComponent?,
val images: List<PixelComponent>,
val children: Map<String, ImageComponent>,
) : Map<String, ImageComponent> by children {
Expand All @@ -23,7 +24,7 @@ class ImageComponent(
val max
get(): Int = maxOf(images.maxOf {
it.component.width
}, children.values.maxOfOrNull {
}, values.maxOfOrNull {
it.max
} ?: 0)

Expand All @@ -34,33 +35,36 @@ class ImageComponent(

fun applyColor(color: TextColor): ImageComponent = ImageComponent(
original,
parent,
images.map {
it.applyColor(color)
},
children.entries.associate {
entries.associate {
it.key to it.value.applyColor(color)
}
)

private fun interface ImageMapper : (HudPlayer) -> ImageComponent

private val childrenMapper: (UpdateEvent) -> ImageMapper = original.childrenMapper?.map {
children[it.first].ifNull("This children doesn't exist in ${original.name}: ${it.first}") to it.second
}?.let {
{ event: UpdateEvent ->
it.map { builder ->
builder.first to builder.second.build(event)
}.let { buildList ->
ImageMapper { player ->
buildList.firstOrNull { pair ->
pair.second(player)
}?.first ?: this
private val childrenMapper: (ImageComponent, UpdateEvent) -> ImageMapper = run {
original.childrenMapper?.map {
children[it.first].ifNull("This children doesn't exist in ${original.name}: ${it.first}") to it.second
}?.let {
{ root, event ->
it.map { builder ->
builder.first.imageMapper(event) to builder.second.build(event)
}.let { buildList ->
ImageMapper ret@ { player ->
buildList.firstOrNull { pair ->
pair.second(player)
}?.first?.invoke(player) ?: root
}
}
}
}
} ?: {
ImageMapper {
this
} ?: { root, _ ->
ImageMapper {
root
}
}
}

Expand All @@ -72,9 +76,9 @@ class ImageComponent(
fun imageMapper(event: UpdateEvent): (HudPlayer) -> ImageComponent {
val buildFollow = original.follow?.build(event)
val mapperTree = ImageMapperTree(
childrenMapper(event),
children.entries.associate {
it.key to it.value.childrenMapper(event)
childrenMapper(this, event),
entries.associate {
it.key to it.value.childrenMapper(this, event)
}
)
return { player ->
Expand Down
10 changes: 7 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class PopupLayout(
)
val negativeSpace = parent.getOrCreateSpace(-1)

fun HudImage.toComponent(): ImageComponent {
fun HudImage.toComponent(parentComponent: ImageComponent? = null): ImageComponent {
val list = ArrayList<PixelComponent>()
if (listener != null) list.add(EMPTY_PIXEL_COMPONENT)
image.forEach {
Expand Down Expand Up @@ -158,7 +158,7 @@ class PopupLayout(
}
list.add(component.toPixelComponent(pixel.x + xOffset))
}
return ImageComponent(this, list, children.entries.associate {
return ImageComponent(this, parentComponent, list, children.entries.associate {
it.key to it.value.toComponent()
})
}
Expand All @@ -167,7 +167,11 @@ class PopupLayout(
target.space,
target.stack,
target.maxStack,
target.image.toComponent(),
try {
target.image.toComponent()
} catch (_: StackOverflowError) {
throw RuntimeException("circular reference found in ${target.image.name}")
},
target.follow,
target.cancelIfFollowerNotExists,
hudImage.conditions and target.conditions
Expand Down

0 comments on commit db47896

Please sign in to comment.