From 5c78daa091592b8ff0b5c425679b026e1ef96f1b Mon Sep 17 00:00:00 2001 From: toxicity Date: Wed, 30 Oct 2024 00:46:50 +0900 Subject: [PATCH 01/10] Bumps dependency version. --- .../hud/background/BackgroundImage.kt | 53 ++++ .../toxicity/hud/background/HudBackground.kt | 34 ++- .../hud/background/LegacyHudBackground.kt | 17 ++ .../kr/toxicity/hud/hud/HudTextElement.kt | 170 ++++++------ ...undLayout.kt => LegacyBackgroundLayout.kt} | 3 +- .../kr/toxicity/hud/layout/TextLayout.kt | 8 + .../toxicity/hud/manager/BackgroundManager.kt | 48 ++-- .../toxicity/hud/manager/TextManagerImpl.kt | 9 +- .../kr/toxicity/hud/popup/PopupLayout.kt | 63 ++--- .../hud/renderer/BackgroundRenderer.kt | 35 +++ .../kr/toxicity/hud/renderer/TextRenderer.kt | 260 +++++++++++------- .../kr/toxicity/hud/text/HudTextData.kt | 11 +- .../kr/toxicity/hud/text/HudTextFont.kt | 9 + .../kotlin/kr/toxicity/hud/util/Adventures.kt | 83 +++++- .../main/kotlin/kr/toxicity/hud/util/Files.kt | 3 + .../kr/toxicity/hud/util/ValueViewer.kt | 30 ++ dist/src/test/kotlin/AdventureTest.kt | 48 ++++ 17 files changed, 631 insertions(+), 253 deletions(-) create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/background/LegacyHudBackground.kt rename dist/src/main/kotlin/kr/toxicity/hud/layout/{BackgroundLayout.kt => LegacyBackgroundLayout.kt} (71%) create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/text/HudTextFont.kt create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/util/ValueViewer.kt create mode 100644 dist/src/test/kotlin/AdventureTest.kt diff --git a/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt b/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt new file mode 100644 index 00000000..8d49ef46 --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt @@ -0,0 +1,53 @@ +package kr.toxicity.hud.background + +import kr.toxicity.hud.image.LoadedImage +import kr.toxicity.hud.util.ifNull +import kr.toxicity.hud.util.removeEmptySide +import kr.toxicity.hud.util.removeEmptyWidth +import java.awt.image.BufferedImage + +class BackgroundImage( + val first: LoadedImage, + val second: LoadedImage, + val third: LoadedImage +) { + companion object { + fun splitOf(line: Int, image: BufferedImage): List { + if (line < 1) throw RuntimeException("line cannot be < 1") + val finalLine = line.coerceAtMost(3) + val load = image.removeEmptySide().ifNull("Empty image.").image + + val width = load.width + val height = load.height + + val widthSplit = (width.toDouble() / 3.0).toInt() + val heightSplit = (height.toDouble() / finalLine.toDouble()).toInt() + + val widthMod = width % widthSplit + val heightMod = height % heightSplit + + var bh = 0 + return (0..2).map { l -> + var h = heightSplit + if (l == 1) h += heightMod + var bw = 0 + fun loadImage(wl: Int): LoadedImage { + var w = widthSplit + if (wl == 1) w += widthMod + val target = load.getSubimage(bw, bh, w, h) + .removeEmptyWidth() + .ifNull("Unsupported image.") + bw += w + return target + } + val ground = BackgroundImage( + loadImage(0), + loadImage(1), + loadImage(2) + ) + bh += h + ground + } + } + } +} \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt b/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt index 2314428b..5e90cf7f 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt @@ -1,16 +1,36 @@ package kr.toxicity.hud.background +import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.configuration.HudConfiguration import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.image.LoadedImage +import kr.toxicity.hud.util.* +import java.io.File class HudBackground( override val path: String, - val name: String, + id: String, + yamlObject: YamlObject +) : HudConfiguration { - val left: LoadedImage, - val right: LoadedImage, - val body: LoadedImage, + val location = PixelLocation(yamlObject) + val image = BackgroundType.valueOf(yamlObject.getAsString("type", "auto").uppercase()).parse(id, yamlObject) - val location: PixelLocation, -) : HudConfiguration \ No newline at end of file + enum class BackgroundType { + AUTO { + override fun parse(id: String, yamlObject: YamlObject): List { + val image = File(DATA_FOLDER.subFolder("assets"), yamlObject.get("file") + .ifNull("value 'file' not set in $id") + .asString() + .replace('/', File.separatorChar)) + .ifNotExist { + "This file doesn't exist: $id in $name" + } + .toImage() + val line = yamlObject.getAsInt("line", 1) + return BackgroundImage.splitOf(line, image) + } + } + ; + abstract fun parse(id: String, yamlObject: YamlObject): List + } +} \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/background/LegacyHudBackground.kt b/dist/src/main/kotlin/kr/toxicity/hud/background/LegacyHudBackground.kt new file mode 100644 index 00000000..a99fa79b --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/background/LegacyHudBackground.kt @@ -0,0 +1,17 @@ +package kr.toxicity.hud.background + +import kr.toxicity.hud.configuration.HudConfiguration +import kr.toxicity.hud.location.PixelLocation +import kr.toxicity.hud.image.LoadedImage + +@Deprecated(message = "Rewrite new background.") +class LegacyHudBackground( + override val path: String, + val name: String, + + val left: LoadedImage, + val right: LoadedImage, + val body: LoadedImage, + + val location: PixelLocation, +) : HudConfiguration \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt index 015750af..ac66934a 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt @@ -6,7 +6,7 @@ import kr.toxicity.hud.api.player.HudPlayer import kr.toxicity.hud.api.update.UpdateEvent import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.image.LoadedImage -import kr.toxicity.hud.layout.BackgroundLayout +import kr.toxicity.hud.layout.LegacyBackgroundLayout import kr.toxicity.hud.layout.TextLayout import kr.toxicity.hud.manager.ConfigManagerImpl import kr.toxicity.hud.manager.MinecraftManager @@ -17,6 +17,7 @@ import kr.toxicity.hud.location.GuiLocation import kr.toxicity.hud.shader.HudShader import kr.toxicity.hud.shader.ShaderGroup import kr.toxicity.hud.text.HudTextData +import kr.toxicity.hud.text.HudTextFont import kr.toxicity.hud.util.* import net.kyori.adventure.text.Component import kotlin.math.roundToInt @@ -39,94 +40,105 @@ class HudTextElement( loc.opacity, text.property ) - val yAxis = loc.y.coerceAtLeast(-HudImpl.ADD_HEIGHT).coerceAtMost(HudImpl.ADD_HEIGHT) - val group = ShaderGroup(shader, text.text.name, text.scale, yAxis) - val key = TextManagerImpl.getKey(group) ?: run { - val index2 = ++parent.textIndex - val array = text.startJson() - text.text.array.forEach { - HudImpl.createBit(shader, yAxis) { y -> - array.add(jsonObjectOf( - "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:${it.file}", - "ascent" to y, - "height" to (it.height * text.scale).roundToInt(), - "chars" to it.chars - )) - } - } - var textIndex = 0xC0000 - val textEncoded = "hud_${parent.name}_text_${index2 + 1}".encodeKey() - val key = createAdventureKey(textEncoded) - val imageMap = HashMap() - text.text.images.forEach { - val result = textIndex++.parseChar() - val imageScale = it.value.scale * text.scale - val height = (it.value.image.image.height.toDouble() * imageScale).roundToInt() - val div = height.toDouble() / it.value.image.image.height - HudImpl.createBit(shader, loc.y + it.value.location.y) { y -> - array.add(jsonObjectOf( - "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:${"glyph_${it.key}".encodeKey()}.png", - "ascent" to y, - "height" to height, - "chars" to jsonArrayOf(result) - )) - } - imageMap[it.key] = it.value.location.x.toSpaceComponent() + WidthComponent(Component.text() - .font(key) - .content(result) - .append(NEGATIVE_ONE_SPACE_COMPONENT.component), (it.value.image.image.width.toDouble() * div).roundToInt()) - } - if (ConfigManagerImpl.loadMinecraftDefaultTextures) { - HudImpl.createBit(shader, text.emojiLocation.y) { y -> - MinecraftManager.applyAll(array, y, text.emojiScale, key) { - textIndex++ - }.forEach { - imageMap[it.key] = text.emojiLocation.x.toSpaceComponent() + it.value + val imageMap = HashMap() + val keys = (0.. + val yAxis = (loc.y + lineIndex * text.line).coerceAtLeast(-HudImpl.ADD_HEIGHT).coerceAtMost(HudImpl.ADD_HEIGHT) + val group = ShaderGroup(shader, text.text.name, text.scale, yAxis) + TextManagerImpl.getKey(group) ?: run { + val index2 = ++parent.textIndex + val array = text.startJson() + text.text.array.forEach { + HudImpl.createBit(shader, yAxis) { y -> + array.add(jsonObjectOf( + "type" to "bitmap", + "file" to "$NAME_SPACE_ENCODED:${it.file}", + "ascent" to y, + "height" to (it.height * text.scale).roundToInt(), + "chars" to it.chars + )) } } - } - val result = HudTextData( - key, - imageMap, - text.background?.let { - fun getString(image: LoadedImage, file: String): WidthComponent { - val result = textIndex++.parseChar() - val height = (image.image.height.toDouble() * text.backgroundScale).roundToInt() - val div = height.toDouble() / image.image.height - HudImpl.createBit(HudShader( - gui, - text.renderScale, - text.layer - 1, - false, - loc.opacity + it.location.opacity, - text.property - ), loc.y + it.location.y) { y -> - array.add(jsonObjectOf( + var textIndex = 0xC0000 + val textEncoded = "hud_${parent.name}_text_${index2 + 1}_${lineIndex + 1}".encodeKey() + val key = createAdventureKey(textEncoded) + text.text.images.forEach { + val result = textIndex++.parseChar() + val imageScale = it.value.scale * text.scale + val height = (it.value.image.image.height.toDouble() * imageScale).roundToInt() + val div = height.toDouble() / it.value.image.image.height + HudImpl.createBit(shader, loc.y + it.value.location.y + lineIndex * text.line) { y -> + array.add( + jsonObjectOf( "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:$file.png", + "file" to "$NAME_SPACE_ENCODED:${"glyph_${it.key}".encodeKey()}.png", "ascent" to y, "height" to height, "chars" to jsonArrayOf(result) - )) - } - return WidthComponent(Component.text() - .font(key) - .content(result) - .append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt()) + ) + ) } - BackgroundLayout( - it.location.x, - getString(it.left, "background_${it.name}_left".encodeKey()), - getString(it.right, "background_${it.name}_right".encodeKey()), - getString(it.body, "background_${it.name}_body".encodeKey()) + imageMap[it.key] = it.value.location.x.toSpaceComponent() + WidthComponent( + Component.text() + .font(key) + .content(result), (it.value.image.image.width.toDouble() * div).roundToInt() ) } - ) - PackGenerator.addTask(file + "$textEncoded.json") { - jsonObjectOf("providers" to array).toByteArray() + if (ConfigManagerImpl.loadMinecraftDefaultTextures) { + HudImpl.createBit(shader, loc.y + text.emojiLocation.y + lineIndex * text.line) { y -> + MinecraftManager.applyAll(array, y, text.emojiScale, key) { + textIndex++ + }.forEach { + imageMap[it.key] = text.emojiLocation.x.toSpaceComponent() + it.value + } + } + } + PackGenerator.addTask(file + "$textEncoded.json") { + jsonObjectOf("providers" to array).toByteArray() + } + HudTextFont(key, imageMap) } + } + + val key = TextManagerImpl.getKey(group) ?: run { + + val result = HudTextData( + key, + imageMap, + null +// text.background?.let { +// fun getString(image: LoadedImage, file: String): WidthComponent { +// val result = textIndex++.parseChar() +// val height = (image.image.height.toDouble() * text.backgroundScale).roundToInt() +// val div = height.toDouble() / image.image.height +// HudImpl.createBit(HudShader( +// gui, +// text.renderScale, +// text.layer - 1, +// false, +// loc.opacity + it.location.opacity, +// text.property +// ), loc.y + it.location.y) { y -> +// array.add(jsonObjectOf( +// "type" to "bitmap", +// "file" to "$NAME_SPACE_ENCODED:$file.png", +// "ascent" to y, +// "height" to height, +// "chars" to jsonArrayOf(result) +// )) +// } +// return WidthComponent(Component.text() +// .font(key) +// .content(result) +// .append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt()) +// } +// LegacyBackgroundLayout( +// it.location.x, +// getString(it.left, "background_${it.name}_left".encodeKey()), +// getString(it.right, "background_${it.name}_right".encodeKey()), +// getString(it.body, "background_${it.name}_body".encodeKey()) +// ) +// } + ) TextManagerImpl.setKey(group, result) result } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/BackgroundLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/LegacyBackgroundLayout.kt similarity index 71% rename from dist/src/main/kotlin/kr/toxicity/hud/layout/BackgroundLayout.kt rename to dist/src/main/kotlin/kr/toxicity/hud/layout/LegacyBackgroundLayout.kt index 8776f620..97fbd471 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/BackgroundLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/LegacyBackgroundLayout.kt @@ -2,7 +2,8 @@ package kr.toxicity.hud.layout import kr.toxicity.hud.api.component.WidthComponent -class BackgroundLayout( +@Deprecated(message = "Rewrite new background") +class LegacyBackgroundLayout( val x: Int, val left: WidthComponent, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt index 7af06b81..40e0bc1f 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt @@ -46,6 +46,14 @@ class TextLayout( val useLegacyFormat: Boolean = yamlObject.getAsBoolean("use-legacy-format", ConfigManagerImpl.useLegacyFormat) val legacySerializer: ComponentDeserializer = yamlObject.get("legacy-serializer")?.asString()?.toLegacySerializer() ?: ConfigManagerImpl.legacySerializer + val line = yamlObject.getAsInt("line", 1).apply { + if (this < 1) throw RuntimeException("line cannot be < 1: $s") + } + val splitWidth = yamlObject.getAsInt("split-with", 200).apply { + if (this < 1) throw RuntimeException("split-width cannot be < 1: $s") + } + val lineWidth = yamlObject.getAsInt("line-width", 9) + fun startJson() = jsonArrayOf( jsonObjectOf( "type" to "space", diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt index 481249d8..9a67cd9c 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt @@ -1,16 +1,17 @@ package kr.toxicity.hud.manager import kr.toxicity.hud.background.HudBackground -import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.pack.PackGenerator +import kr.toxicity.hud.renderer.BackgroundRenderer import kr.toxicity.hud.resource.GlobalResource +import kr.toxicity.hud.shader.ShaderGroup import kr.toxicity.hud.util.* import net.kyori.adventure.audience.Audience -import java.io.File +import java.util.concurrent.ConcurrentHashMap object BackgroundManager : BetterHudManager { private val backgroundMap = HashMap() + private val backgroundRendererMap = ConcurrentHashMap() override fun start() { @@ -21,36 +22,27 @@ object BackgroundManager : BetterHudManager { override fun reload(sender: Audience, resource: GlobalResource) { val folder = DATA_FOLDER.subFolder("backgrounds") backgroundMap.clear() - folder.forEach { - if (it.extension == "yml") { - runWithExceptionHandling(sender, "Unable to load this yml: ${it.name}") { - val yaml = it.toYaml() - val name = it.nameWithoutExtension - val backgroundFolder = folder.subFolder(name) - fun getImage(imageName: String) = File(backgroundFolder, "$imageName.png") - .ifNotExist("this image doesn't exist: $imageName.png in $name") - .toImage() - .removeEmptyWidth() - .ifNull("this image is empty: $imageName.png in $name").apply { - PackGenerator.addTask(resource.textures + "${"background_${name}_$imageName".encodeKey()}.png") { - image.toByteArray() - } - } - backgroundMap.putSync("background", name) { - HudBackground( - it.path, - name, - getImage("left"), - getImage("right"), - getImage("body"), - PixelLocation(yaml) - ) - } + backgroundRendererMap.clear() + folder.forEachAllYaml(sender) { file, id, yamlObject -> + runWithExceptionHandling(sender, "Unable to load this background: $id in ${file.name}") { + backgroundMap.putSync("background", id) { + HudBackground( + file.path, + id, + yamlObject + ) } } } } + @Synchronized + fun getRenderer(shaderGroup: ShaderGroup) = backgroundRendererMap[shaderGroup] + @Synchronized + fun setRenderer(shaderGroup: ShaderGroup, backgroundRenderer: BackgroundRenderer) { + backgroundRendererMap[shaderGroup] = backgroundRenderer + } + override fun end() { } } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt index 6bd66a7f..78cd5556 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt @@ -9,10 +9,7 @@ import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.placeholder.ConditionBuilder import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.shader.ShaderGroup -import kr.toxicity.hud.text.CharWidth -import kr.toxicity.hud.text.HudText -import kr.toxicity.hud.text.HudTextArray -import kr.toxicity.hud.text.HudTextData +import kr.toxicity.hud.text.* import kr.toxicity.hud.util.* import net.kyori.adventure.audience.Audience import java.awt.AlphaComposite @@ -63,7 +60,7 @@ object TextManagerImpl : BetterHudManager, TextManager { private val textCacheMap = HashMap() private val textWidthMap = HashMap() - private val textKeyMap = ConcurrentHashMap() + private val textKeyMap = ConcurrentHashMap() private val defaultBitmapImageMap = HashMap() private val translatableString = HashMap>() @@ -133,7 +130,7 @@ object TextManagerImpl : BetterHudManager, TextManager { @Synchronized fun getKey(shaderGroup: ShaderGroup) = textKeyMap[shaderGroup] @Synchronized - fun setKey(shaderGroup: ShaderGroup, key: HudTextData) { + fun setKey(shaderGroup: ShaderGroup, key: HudTextFont) { textKeyMap[shaderGroup] = key } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt index a7b8c54c..477080c2 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt @@ -10,7 +10,7 @@ import kr.toxicity.hud.hud.HudImpl import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.image.LoadedImage import kr.toxicity.hud.image.LocationGroup -import kr.toxicity.hud.layout.BackgroundLayout +import kr.toxicity.hud.layout.LegacyBackgroundLayout import kr.toxicity.hud.location.AnimationType import kr.toxicity.hud.layout.LayoutGroup import kr.toxicity.hud.manager.* @@ -240,36 +240,37 @@ class PopupLayout( val result = HudTextData( key, imageMap, - textLayout.background?.let { - fun getString(image: LoadedImage, file: String): WidthComponent { - val result = textIndex++.parseChar() - val height = (image.image.height.toDouble() * textLayout.backgroundScale).roundToInt() - val div = height.toDouble() / image.image.height - HudImpl.createBit(HudShader( - elementGui, - textLayout.renderScale, - textLayout.layer - 1, - false, - pixel.opacity + it.location.opacity, - textLayout.property - ), pixel.y + it.location.y) { y -> - array.add(jsonObjectOf( - "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:$file.png", - "ascent" to y, - "height" to height, - "chars" to jsonArrayOf(result) - )) - } - return WidthComponent(Component.text().font(key).content(result).append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt()) - } - BackgroundLayout( - it.location.x, - getString(it.left, "background_${it.name}_left".encodeKey()), - getString(it.right, "background_${it.name}_right".encodeKey()), - getString(it.body, "background_${it.name}_body".encodeKey()) - ) - } + null +// textLayout.background?.let { +// fun getString(image: LoadedImage, file: String): WidthComponent { +// val result = textIndex++.parseChar() +// val height = (image.image.height.toDouble() * textLayout.backgroundScale).roundToInt() +// val div = height.toDouble() / image.image.height +// HudImpl.createBit(HudShader( +// elementGui, +// textLayout.renderScale, +// textLayout.layer - 1, +// false, +// pixel.opacity + it.location.opacity, +// textLayout.property +// ), pixel.y + it.location.y) { y -> +// array.add(jsonObjectOf( +// "type" to "bitmap", +// "file" to "$NAME_SPACE_ENCODED:$file.png", +// "ascent" to y, +// "height" to height, +// "chars" to jsonArrayOf(result) +// )) +// } +// return WidthComponent(Component.text().font(key).content(result).append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt()) +// } +// LegacyBackgroundLayout( +// it.location.x, +// getString(it.left, "background_${it.name}_left".encodeKey()), +// getString(it.right, "background_${it.name}_right".encodeKey()), +// getString(it.body, "background_${it.name}_body".encodeKey()) +// ) +// } ) PackGenerator.addTask(file + "$textEncoded.json") { jsonObjectOf("providers" to array).toByteArray() diff --git a/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt b/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt new file mode 100644 index 00000000..1b020506 --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt @@ -0,0 +1,35 @@ +package kr.toxicity.hud.renderer + +import kr.toxicity.hud.api.component.WidthComponent +import kr.toxicity.hud.util.toSpaceComponent + +class BackgroundRenderer( + val x: Int, + private val lists: List +) { + + fun build(line: Int, width: Int): WidthComponent { + var comp = x.toSpaceComponent() + var w = 0 + for (i in 0.., @@ -57,6 +60,39 @@ class TextRenderer( private val parsedPatter = PlaceholderManagerImpl.parse(pattern) + private val widthViewer = ValueViewer, Int>() + .addFunction( + { + if (it.first.font() == null) { + if (it.second == SPACE_POINT) 4 + it.first.addNumber() + else widthMap[it.second]?.scaledWidth(scale)?.let { w -> + w + 1 + it.first.addNumber() + } + } else null + }, + { + if (it.first.font() == null && it.second == TEXT_SPACE_KEY_CODEPOINT) space + it.first.addNumber() else null + }, + data.images.map { + val comp = it.value + comp.component.content().codePointAt(0) to comp.width + 1 + }.toMap().let { + { pair: Pair -> + it[pair.second]?.let { + it + 1 + pair.first.addNumber() + } + } + }, + { + when (it.first.font()) { + SPACE_KEY -> it.second - CURRENT_CENTER_SPACE_CODEPOINT + it.first.addNumber() + LEGACY_SPACE_KEY -> it.second - LEGACY_CENTER_SPACE_CODEPOINT + it.first.addNumber() + else -> null + } + } + ) + + fun getText(reason: UpdateEvent): (HudPlayer) -> PixelComponent { val buildPattern = parsedPatter(reason) val cond = condition.build(reason) @@ -73,108 +109,148 @@ class TextRenderer( } } if (!cond(targetHudPlayer)) return@build EMPTY_PIXEL_COMPONENT - var width = 0 - val patternResult = buildPattern(targetHudPlayer) - var targetString = (if (useLegacyFormat) legacySerializer(patternResult) else Component.text(patternResult)) - .color(defaultColor) - .replaceText(TextReplacementConfig.builder() - .match(imagePattern) - .replacement { r, _ -> - when (r.group(1)) { - "image" -> data.images[r.group(3)]?.let { - width += it.width - it.component.build() - } ?: Component.empty() - "space" -> r.group(3).toIntOrNull()?.toSpaceComponent()?.let { - width += it.width - it.component - } ?: Component.empty() - else -> Component.empty() - } - } - .build()) - .replaceText(TextReplacementConfig.builder() - .match(allPattern) - .replacement { r, _ -> - MiniMessage.miniMessage().deserialize(r.group()) - } - .build()) - if (!disableNumberFormat) { - targetString = targetString.replaceText(TextReplacementConfig.builder() - .match(decimalPattern) - .replacement { r, _ -> - val g = r.group() - Component.text(runCatching { - numberPattern.format(numberEquation.evaluate(g.toDouble())) - }.getOrDefault(g)) - } - .build()) - } - fun hasDecoration(parent: Boolean, state: TextDecoration.State) = when (state) { - TextDecoration.State.TRUE -> true - TextDecoration.State.NOT_SET -> parent - TextDecoration.State.FALSE -> false - } - fun applyDecoration(component: Component, bold: Boolean, italic: Boolean): Component { - var ret = component - if (ret is TextComponent && ret.font() == null) { - val codepoint = ret.content().codePoints().toArray() - var add = 0 - if (bold) add++ - if (italic) add++ - if (space != 0) { - ret = ret.content(buildString { - codepoint.forEachIndexed { index, i -> - appendCodePoint(i) - width += if (i != SPACE_POINT) widthMap[i]?.let { width -> - width.scaledWidth(scale) + add + 1 - } ?: 0 else 4 + add - if (index < codepoint.lastIndex) { - width += space + add - appendCodePoint(TEXT_SPACE_KEY_CODEPOINT) + + var widthComp = EMPTY_WIDTH_COMPONENT + + buildPattern(targetHudPlayer) + .parseToComponent() + .split(data.splitWidth, widthViewer) + .forEachIndexed { index, comp -> + if (data.words.lastIndex < index) return@forEachIndexed + if (comp !is TextComponent) return@forEachIndexed + fun Component.search(): Int { + var i = 0 + if (this is TextComponent) { + val style = style() + for (codePoint in content().codePoints()) { + widthViewer(style to codePoint)?.let { w -> + i += w } } - }) - } else { - width += codepoint.sumOf { - if (it != SPACE_POINT) widthMap[it]?.let { width -> - width.scaledWidth(scale) + add + 1 - } ?: 0 else 4 + add + } + return i + children().sumOf { children -> + children.search() } } - ret = ret.font(data.word) + val newComp = WidthComponent(comp.toBuilder().font(data.words[index]), comp.search()) + widthComp = if (widthComp.width == 0) newComp else widthComp plusWithAlign newComp } - return ret.children(ret.children().map { - applyDecoration( - it, - hasDecoration(bold, it.decoration(TextDecoration.BOLD)), - hasDecoration(italic, it.decoration(TextDecoration.ITALIC)), - ) - }) + data.background?.let { + it.build(data.words.size, widthComp.width) + } - val finalComp = Component.text().append(applyDecoration( - targetString, - hasDecoration(false, targetString.decoration(TextDecoration.BOLD)), - hasDecoration(false, targetString.decoration(TextDecoration.ITALIC)) - )) - var comp = WidthComponent(finalComp, width) - data.background?.let { - val builder = Component.text().append(it.left.component) - var length = 0 - while (length < comp.width) { - builder.append(it.body.component) - length += it.body.width + +// data.background?.let { +// val builder = Component.text().append(it.left.component) +// var length = 0 +// while (length < comp.width) { +// builder.append(it.body.component) +// length += it.body.width +// } +// val total = it.left.width + length + it.right.width +// val minus = -total + (length - comp.width) / 2 + it.left.width - it.x +// comp = it.x.toSpaceComponent() + WidthComponent(builder.append(it.right.component), total) + minus.toSpaceComponent() + comp +// } + widthComp.toPixelComponent(when (align) { + LayoutAlign.LEFT -> x + LayoutAlign.CENTER -> x - widthComp.width / 2 + LayoutAlign.RIGHT -> x - widthComp.width + }) + } + } + + private infix fun WidthComponent.plusWithAlign(other: WidthComponent) = when (align) { + LayoutAlign.LEFT -> this + (-width).toSpaceComponent() + other + LayoutAlign.CENTER -> { + if (width > other.width) { + val div = (width - other.width).toDouble() / 2 + this + floor(-width + div).toInt().toSpaceComponent() + other + ceil(div).toInt().toSpaceComponent() + } else { + val div = floor((other.width - width).toDouble() / 2).toInt() + div.toSpaceComponent() + this + (-width - div).toSpaceComponent() + other + } + } + LayoutAlign.RIGHT -> { + if (width > other.width) { + val div = width - other.width + this + (-width + div).toSpaceComponent() + other + } else { + val div = other.width - width + div.toSpaceComponent() + this + (-width - div).toSpaceComponent() + other + } + } + } + + private fun Style.addNumber(): Int { + var i = 0 + if (hasDecoration(TextDecoration.BOLD)) i++ + if (hasDecoration(TextDecoration.ITALIC)) i++ + return i + } + + private fun String.parseToComponent(): Component { + var targetString = (if (useLegacyFormat) legacySerializer(this) else Component.text(this)) + .color(defaultColor) + .replaceText(TextReplacementConfig.builder() + .match(imagePattern) + .replacement { r, _ -> + when (r.group(1)) { + "image" -> data.images[r.group(3)]?.component?.build() ?: Component.empty() + "space" -> r.group(3).toIntOrNull()?.toSpaceComponent()?.component ?: Component.empty() + else -> Component.empty() + } + } + .build()) + .replaceText(TextReplacementConfig.builder() + .match(allPattern) + .replacement { r, _ -> + MiniMessage.miniMessage().deserialize(r.group()) + } + .build()) + if (!disableNumberFormat) { + targetString = targetString.replaceText(TextReplacementConfig.builder() + .match(decimalPattern) + .replacement { r, _ -> + val g = r.group() + Component.text(runCatching { + numberPattern.format(numberEquation.evaluate(g.toDouble())) + }.getOrDefault(g)) + } + .build()) + } + fun hasDecoration(parent: Boolean, state: TextDecoration.State) = when (state) { + TextDecoration.State.TRUE -> true + TextDecoration.State.NOT_SET -> parent + TextDecoration.State.FALSE -> false + } + fun applyDecoration(component: Component, bold: Boolean, italic: Boolean): Component { + var ret = component + if (ret is TextComponent && ret.font() == null) { + val codepoint = ret.content().codePoints().toArray() + if (space != 0) { + ret = ret.content(buildString { + codepoint.forEachIndexed { index, i -> + appendCodePoint(i) + if (index < codepoint.lastIndex) { + appendCodePoint(TEXT_SPACE_KEY_CODEPOINT) + } + } + }) } - val total = it.left.width + length + it.right.width - val minus = -total + (length - comp.width) / 2 + it.left.width - it.x - comp = it.x.toSpaceComponent() + WidthComponent(builder.append(it.right.component), total) + minus.toSpaceComponent() + comp } - comp.toPixelComponent(when (align) { - LayoutAlign.LEFT -> x - LayoutAlign.CENTER -> x - comp.width / 2 - LayoutAlign.RIGHT -> x - comp.width + return ret.children(ret.children().map { + applyDecoration( + it, + hasDecoration(bold, it.decoration(TextDecoration.BOLD)), + hasDecoration(italic, it.decoration(TextDecoration.ITALIC)), + ) }) } + return applyDecoration( + targetString, + hasDecoration(false, targetString.decoration(TextDecoration.BOLD)), + hasDecoration(false, targetString.decoration(TextDecoration.ITALIC)) + ) } } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt index b62fe014..7143bf5e 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt @@ -1,12 +1,9 @@ package kr.toxicity.hud.text -import kr.toxicity.hud.api.component.WidthComponent -import kr.toxicity.hud.layout.BackgroundLayout -import net.kyori.adventure.key.Key +import kr.toxicity.hud.renderer.BackgroundRenderer class HudTextData( - val word: Key, - - val images: Map, - val background: BackgroundLayout? + val font: List, + val splitWidth: Int, + val background: BackgroundRenderer? ) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextFont.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextFont.kt new file mode 100644 index 00000000..248530b0 --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextFont.kt @@ -0,0 +1,9 @@ +package kr.toxicity.hud.text + +import kr.toxicity.hud.api.component.WidthComponent +import net.kyori.adventure.key.Key + +class HudTextFont( + val word: Key, + val images: Map +) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt b/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt index ffcbc81f..c2f83f4c 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt @@ -5,9 +5,13 @@ import kr.toxicity.hud.api.component.WidthComponent import kr.toxicity.hud.manager.ConfigManagerImpl import net.kyori.adventure.key.Key import net.kyori.adventure.text.Component +import net.kyori.adventure.text.ComponentLike +import net.kyori.adventure.text.TextComponent import net.kyori.adventure.text.format.NamedTextColor +import net.kyori.adventure.text.format.Style import net.kyori.adventure.text.format.TextColor import net.kyori.adventure.text.format.TextDecoration +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer import kotlin.math.abs fun createAdventureKey(value: String) = Key.key(NAME_SPACE_ENCODED, value) @@ -45,10 +49,13 @@ val EMPTY_PIXEL_COMPONENT val NEW_LAYER get() = if (BOOTSTRAP.useLegacyFont()) EMPTY_WIDTH_COMPONENT else WidthComponent(Component.text().content(0xC0000.parseChar()).font(SPACE_KEY), 0) +const val LEGACY_CENTER_SPACE_CODEPOINT = 0xFFC00 +const val CURRENT_CENTER_SPACE_CODEPOINT = 0xD0000 + private val LEGACY_NEGATIVE_ONE_SPACE_COMPONENT - get() = WidthComponent(Component.text().content((0xFFC00 - 1).parseChar()).font(LEGACY_SPACE_KEY), 0) + get() = WidthComponent(Component.text().content((LEGACY_CENTER_SPACE_CODEPOINT - 1).parseChar()).font(LEGACY_SPACE_KEY), 0) private val CURRENT_NEGATIVE_ONE_SPACE_COMPONENT - get() = WidthComponent(Component.text().content((0xD0000 - 1).parseChar()).font(SPACE_KEY), 0) + get() = WidthComponent(Component.text().content((CURRENT_CENTER_SPACE_CODEPOINT - 1).parseChar()).font(SPACE_KEY), 0) val NEGATIVE_ONE_SPACE_COMPONENT get() = if (BOOTSTRAP.useLegacyFont()) { LEGACY_NEGATIVE_ONE_SPACE_COMPONENT @@ -104,4 +111,76 @@ fun Int.toSpaceComponent(width: Int) = if (BOOTSTRAP.useLegacyFont()) { .content((this + 0xD0000).parseChar()), width ) +} + +private class SplitBuilder( + private val chain: (Component) -> Unit +) { + private var builder = Component.text() + var isClean = true + private set + fun accept(block: TextComponent.Builder.() -> Unit = {}) { + val build = builder.apply(block).build() + builder = Component.text() + isClean = true + chain(build) + } + fun build(block: TextComponent.Builder.() -> Unit = {}): Component { + val build = builder.apply(block).build() + builder = Component.text() + isClean = true + return build + } + fun append(like: ComponentLike) { + builder.append(like) + isClean = false + } + fun then(firstChain: (SplitBuilder, Component) -> Unit) = SplitBuilder other@ { + firstChain(this, it) + accept() + } +} + +fun Component.split(endWidth: Int, charWidth: (Pair) -> Int?): List { + var i = 0 + val list = ArrayList() + val topBuilder = SplitBuilder { + i = 0 + list.add(it) + } + fun Component.parse(target: SplitBuilder) { + if (this is TextComponent) { + val style = style() + if (style.font() == null) { + val subBuilder = target.then { b, c -> + b.append(c) + } + val sb = StringBuilder() + for (codepoint in content().codePoints()) { + sb.appendCodePoint(codepoint) + i += if (codepoint == ' '.code) 4 else charWidth(style to codepoint) ?: continue + if (i >= endWidth) { + subBuilder.accept { + style(style).content(sb.toString()) + } + sb.setLength(0) + } + } + if (!subBuilder.isClean || sb.isNotEmpty()) target.append(subBuilder.build { + style(style).content(sb.toString()) + }) + for (child in children()) { + child.parse(subBuilder) + } + if (!subBuilder.isClean) target.append(subBuilder.build { + style(style) + }) + } else { + target.append(Component.text().content(content()).style(style)) + } + } + } + parse(topBuilder) + if (!topBuilder.isClean) list.add(topBuilder.build()) + return list } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/util/Files.kt b/dist/src/main/kotlin/kr/toxicity/hud/util/Files.kt index 611fca0e..a1cadb00 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/util/Files.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/util/Files.kt @@ -14,6 +14,9 @@ fun File.subFile(name: String) = File(this, name).apply { fun File.ifNotExist(message: String) = apply { if (!exists()) throw RuntimeException(message) } +fun File.ifNotExist(messageCreator: File.() -> String) = apply { + if (!exists()) throw RuntimeException(messageCreator()) +} fun File.forEach(block: (File) -> Unit) { listFiles()?.sortedBy { diff --git a/dist/src/main/kotlin/kr/toxicity/hud/util/ValueViewer.kt b/dist/src/main/kotlin/kr/toxicity/hud/util/ValueViewer.kt new file mode 100644 index 00000000..d441f2f2 --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/util/ValueViewer.kt @@ -0,0 +1,30 @@ +package kr.toxicity.hud.util + +import java.lang.ref.WeakReference + +class ValueViewer : (K) -> V? { + + private val refs = ArrayList<(K) -> V?>() + + fun addMap(vararg map: Map): ValueViewer { + val mapRef = map.map { + WeakReference(it) + } + for (weakReference in mapRef) { + refs.add { + weakReference.get()?.get(it) + } + } + return this + } + fun addFunction(vararg function: (K) -> V?): ValueViewer { + refs.addAll(function) + return this + } + + override fun invoke(p1: K): V? { + return refs.firstNotNullOfOrNull { + it(p1) + } + } +} \ No newline at end of file diff --git a/dist/src/test/kotlin/AdventureTest.kt b/dist/src/test/kotlin/AdventureTest.kt new file mode 100644 index 00000000..703b7972 --- /dev/null +++ b/dist/src/test/kotlin/AdventureTest.kt @@ -0,0 +1,48 @@ +import kr.toxicity.hud.util.split +import net.kyori.adventure.text.Component +import net.kyori.adventure.text.format.NamedTextColor +import net.kyori.adventure.text.minimessage.MiniMessage +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer +import org.junit.jupiter.api.Test + +class AdventureTest { + @Test + fun testAdventure() { + val serializer = GsonComponentSerializer.gson() + val message = MiniMessage.miniMessage().deserialize("Hello <#FFAC00>world!") +// val message = Component.text() +// .content("He") +// .color(NamedTextColor.GOLD) +// .append( +// Component.text() +// .content("llo") +// .color(NamedTextColor.RED) +// .append( +// Component.text() +// .content(" wor") +// .color(NamedTextColor.AQUA) +// ) +// ) +// .append( +// Component.text() +// .content("ld!") +// .color(NamedTextColor.YELLOW) +// ) +// .build() + + println("# Original") + println(serializer.serializeToTree(message)) + println("# Split") + message.split(20, mapOf( + 'H'.code to 5, + 'e'.code to 5, + 'l'.code to 1, + 'o'.code to 5, + 'w'.code to 5, + 'r'.code to 5, + 'd'.code to 5 + )).forEach { + println(serializer.serializeToTree(it)) + } + } +} \ No newline at end of file From 26c58b64c234353ed5bc58e77aa63e0fbd152b94 Mon Sep 17 00:00:00 2001 From: toxicity Date: Wed, 6 Nov 2024 03:07:42 +0900 Subject: [PATCH 02/10] Multi line text update. --- .../mythicmobs/MythicMobsCompatibility.kt | 84 ++++++++++ .../kotlin/kr/toxicity/hud/BetterHudImpl.kt | 2 +- .../hud/background/BackgroundImage.kt | 24 +-- .../toxicity/hud/background/HudBackground.kt | 92 +++++++++- .../hud/background/LegacyHudBackground.kt | 17 -- .../kotlin/kr/toxicity/hud/hud/HudElement.kt | 5 +- .../kr/toxicity/hud/hud/HudImageElement.kt | 3 +- .../kotlin/kr/toxicity/hud/hud/HudImpl.kt | 7 +- .../kr/toxicity/hud/hud/HudTextElement.kt | 103 ++++-------- .../hud/layout/LegacyBackgroundLayout.kt | 12 -- .../kr/toxicity/hud/layout/TextLayout.kt | 12 +- .../toxicity/hud/manager/BackgroundManager.kt | 13 +- .../kr/toxicity/hud/manager/CommandManager.kt | 12 +- .../kr/toxicity/hud/manager/HudManagerImpl.kt | 2 +- .../toxicity/hud/manager/PopupManagerImpl.kt | 2 +- .../toxicity/hud/manager/TextManagerImpl.kt | 26 ++- .../kotlin/kr/toxicity/hud/popup/PopupImpl.kt | 8 +- .../kr/toxicity/hud/popup/PopupLayout.kt | 157 ++++++++---------- .../hud/renderer/BackgroundRenderer.kt | 22 +-- .../kr/toxicity/hud/renderer/TextRenderer.kt | 124 ++------------ .../kr/toxicity/hud/shader/HudShader.kt | 9 + .../kr/toxicity/hud/text/BackgroundKey.kt | 9 + .../kotlin/kr/toxicity/hud/text/CharWidth.kt | 2 +- .../kotlin/kr/toxicity/hud/text/HudText.kt | 3 +- .../kr/toxicity/hud/text/HudTextData.kt | 8 +- .../kr/toxicity/hud/text/HudTextFont.kt | 9 - .../kr/toxicity/hud/text/ImageCharWidth.kt | 5 + .../kotlin/kr/toxicity/hud/util/Adventures.kt | 92 +++++----- .../kr/toxicity/hud/util/ValueViewer.kt | 2 +- 29 files changed, 422 insertions(+), 444 deletions(-) delete mode 100644 dist/src/main/kotlin/kr/toxicity/hud/background/LegacyHudBackground.kt delete mode 100644 dist/src/main/kotlin/kr/toxicity/hud/layout/LegacyBackgroundLayout.kt create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/text/BackgroundKey.kt delete mode 100644 dist/src/main/kotlin/kr/toxicity/hud/text/HudTextFont.kt create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt diff --git a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/mythicmobs/MythicMobsCompatibility.kt b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/mythicmobs/MythicMobsCompatibility.kt index 7522debe..6a4996ee 100644 --- a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/mythicmobs/MythicMobsCompatibility.kt +++ b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/mythicmobs/MythicMobsCompatibility.kt @@ -92,6 +92,78 @@ class MythicMobsCompatibility : Compatibility { } ?: 0 } } + }, + // entity + "entity_current_cooldown" to object : HudPlaceholder { + override fun getRequiredArgsLength(): Int = 1 + override fun invoke(args: MutableList, reason: UpdateEvent): Function { + return reason.unwrap { event: EntityEvent -> + val skill = MythicBukkit.inst().skillManager.getSkill(args[0]).orElseThrow { + RuntimeException("this skill doesn't exist: ${args[0]}") + } as AbstractSkill + Function { + skill.getCooldown(object : SkillCaster { + override fun getEntity(): AbstractEntity = BukkitAdapter.adapt(event.entity) + override fun setUsingDamageSkill(p0: Boolean) {} + override fun isUsingDamageSkill(): Boolean = false + }) + } + } + } + }, + "entity_aura_stack" to object : HudPlaceholder { + override fun getRequiredArgsLength(): Int = 1 + override fun invoke(args: MutableList, reason: UpdateEvent): Function { + return reason.unwrap { event: EntityEvent -> + Function { + MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { + it.getAuraStacks(args[0]) + }.orElse(-1) + } + } + } + }, + "entity_aura_max_duration" to object : HudPlaceholder { + override fun getRequiredArgsLength(): Int = 1 + override fun invoke(args: MutableList, reason: UpdateEvent): Function { + return reason.unwrap { event: EntityEvent -> + Function { + MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { + it.auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> + aura.startDuration + } ?: 0 + }.orElse(-1) + } + } + } + }, + "entity_aura_duration" to object : HudPlaceholder { + override fun getRequiredArgsLength(): Int = 1 + override fun invoke(args: MutableList, reason: UpdateEvent): Function { + return reason.unwrap { event: EntityEvent -> + Function { + MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { + it.auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> + aura.ticksRemaining + } ?: 0 + }.orElse(-1) + } + } + } + }, + "entity_aura_duration_reversed" to object : HudPlaceholder { + override fun getRequiredArgsLength(): Int = 1 + override fun invoke(args: MutableList, reason: UpdateEvent): Function { + return reason.unwrap { event: EntityEvent -> + Function { + MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { + it.auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> + aura.startDuration - aura.ticksRemaining + } ?: 0 + }.orElse(-1) + } + } + } } ) override val strings: Map> @@ -150,6 +222,18 @@ class MythicMobsCompatibility : Compatibility { MythicBukkit.inst().mobManager.isMythicMob(e.entity) } } + }, + "entity_has_aura" to object : HudPlaceholder { + override fun getRequiredArgsLength(): Int = 1 + override fun invoke(args: MutableList, reason: UpdateEvent): Function { + return reason.unwrap { event: EntityEvent -> + Function { _ -> + MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { + it.auraRegistry.hasAura(args[0]) + }.orElse(false) + } + } + } } ) diff --git a/dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt index f8473a90..865171b3 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt @@ -102,7 +102,7 @@ class BetterHudImpl(val bootstrap: BetterHudBootstrap): BetterHud { PlaceholderManagerImpl, TriggerManagerImpl, - BackgroundManager, + //BackgroundManager, //TODO Fix this ImageManager, TextManagerImpl, PlayerHeadManager, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt b/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt index 8d49ef46..f7aa8694 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt @@ -6,6 +6,7 @@ import kr.toxicity.hud.util.removeEmptySide import kr.toxicity.hud.util.removeEmptyWidth import java.awt.image.BufferedImage +//TODO Fix this class BackgroundImage( val first: LoadedImage, val second: LoadedImage, @@ -26,28 +27,7 @@ class BackgroundImage( val widthMod = width % widthSplit val heightMod = height % heightSplit - var bh = 0 - return (0..2).map { l -> - var h = heightSplit - if (l == 1) h += heightMod - var bw = 0 - fun loadImage(wl: Int): LoadedImage { - var w = widthSplit - if (wl == 1) w += widthMod - val target = load.getSubimage(bw, bh, w, h) - .removeEmptyWidth() - .ifNull("Unsupported image.") - bw += w - return target - } - val ground = BackgroundImage( - loadImage(0), - loadImage(1), - loadImage(2) - ) - bh += h - ground - } + return listOf() } } } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt b/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt index 5e90cf7f..c365466a 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt @@ -1,23 +1,102 @@ package kr.toxicity.hud.background +import kr.toxicity.hud.api.component.WidthComponent import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.configuration.HudConfiguration +import kr.toxicity.hud.hud.HudImpl +import kr.toxicity.hud.image.NamedLoadedImage import kr.toxicity.hud.location.PixelLocation +import kr.toxicity.hud.pack.PackGenerator +import kr.toxicity.hud.renderer.BackgroundRenderer +import kr.toxicity.hud.resource.GlobalResource +import kr.toxicity.hud.shader.HudShader import kr.toxicity.hud.util.* +import net.kyori.adventure.text.Component import java.io.File +import kotlin.math.roundToInt +//TODO Fix this class HudBackground( override val path: String, - id: String, - yamlObject: YamlObject + private val id: String, + private val yamlObject: YamlObject ) : HudConfiguration { - val location = PixelLocation(yamlObject) - val image = BackgroundType.valueOf(yamlObject.getAsString("type", "auto").uppercase()).parse(id, yamlObject) + private val lineMap = HashMap>() + + private val location = PixelLocation(yamlObject) + private val imageBuilder = BackgroundType.valueOf(yamlObject.getAsString("type", "auto").uppercase()) + + fun generateImage(resource: GlobalResource, line: Int, index: Int): ImageTriple { + val triple = lineMap.computeIfAbsent(line) { + val image = imageBuilder.parse(id, line, yamlObject) + val leftX = image.maxOf { + it.first.xOffset + } + val nameMap = run { + val list = ArrayList() + var i = 0 + image.forEach { + list.add(ImageTriple( + leftX, + it.first.toNamed("background_${id}_${line}_${++i}"), + it.second.toNamed("background_${id}_${line}_${++i}"), + it.third.toNamed("background_${id}_${line}_${++i}") + )) + } + list + } + nameMap.forEach { + it.forEach { image -> + PackGenerator.addTask(resource.textures + image.name) { + image.image.image.toByteArray() + } + } + } + nameMap + } + return if (index == 0) triple.first() else if (index == triple.lastIndex) triple.last() else triple[1.coerceAtMost(triple.lastIndex)] + } + + inner class ImageTriple( + private val max: Int, + private val first: NamedLoadedImage, + private val second: NamedLoadedImage, + private val third: NamedLoadedImage + ) : Iterable { + override fun iterator(): Iterator = listOf(first, second, third).iterator() +// fun toBackground(shaders: HudShader, y: Int, scale: Double): BackgroundRenderer { +// var char = start +// fun NamedLoadedImage.toWidth(): WidthComponent { +// val c = (++char).parseChar() +// val height = (image.image.height.toDouble() * scale).roundToInt() +// val div = height.toDouble() / image.image.height.toDouble() +// HudImpl.createBit(shaders, y + location.y) { +// json.add(jsonObjectOf( +// "type" to "bitmap", +// "file" to "$NAME_SPACE_ENCODED:$name", +// "ascent" to it, +// "height" to height, +// "chars" to jsonArrayOf(c) +// )) +// } +// return WidthComponent(Component.text().content(c), (image.image.width * div).roundToInt()) +// } +// return BackgroundRenderer( +// location.x, +// BackgroundRenderer.BackgroundComponent( +// max, +// first.toWidth(), +// second.toWidth(), +// third.toWidth() +// ) +// ) +// } + } enum class BackgroundType { AUTO { - override fun parse(id: String, yamlObject: YamlObject): List { + override fun parse(id: String, line: Int, yamlObject: YamlObject): List { val image = File(DATA_FOLDER.subFolder("assets"), yamlObject.get("file") .ifNull("value 'file' not set in $id") .asString() @@ -26,11 +105,10 @@ class HudBackground( "This file doesn't exist: $id in $name" } .toImage() - val line = yamlObject.getAsInt("line", 1) return BackgroundImage.splitOf(line, image) } } ; - abstract fun parse(id: String, yamlObject: YamlObject): List + abstract fun parse(id: String, line: Int, yamlObject: YamlObject): List } } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/background/LegacyHudBackground.kt b/dist/src/main/kotlin/kr/toxicity/hud/background/LegacyHudBackground.kt deleted file mode 100644 index a99fa79b..00000000 --- a/dist/src/main/kotlin/kr/toxicity/hud/background/LegacyHudBackground.kt +++ /dev/null @@ -1,17 +0,0 @@ -package kr.toxicity.hud.background - -import kr.toxicity.hud.configuration.HudConfiguration -import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.image.LoadedImage - -@Deprecated(message = "Rewrite new background.") -class LegacyHudBackground( - override val path: String, - val name: String, - - val left: LoadedImage, - val right: LoadedImage, - val body: LoadedImage, - - val location: PixelLocation, -) : HudConfiguration \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudElement.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudElement.kt index 9456509c..5477d374 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudElement.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudElement.kt @@ -6,11 +6,12 @@ import kr.toxicity.hud.component.LayoutComponentContainer import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.layout.LayoutGroup import kr.toxicity.hud.location.GuiLocation +import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.util.EMPTY_WIDTH_COMPONENT class HudElement( hud: HudImpl, - file: List, + resource: GlobalResource, private val layout: LayoutGroup, gui: GuiLocation, pixel: PixelLocation @@ -19,7 +20,7 @@ class HudElement( HudImageElement(hud, image, gui, pixel) } private val textElement = layout.text.map { textLayout -> - HudTextElement(hud, file, textLayout, gui, pixel) + HudTextElement(hud, resource, textLayout, gui, pixel) } private val headElement = layout.head.map { image -> HudHeadElement(hud, image, gui, pixel) diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImageElement.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImageElement.kt index 0fc120f2..345797a9 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImageElement.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImageElement.kt @@ -13,6 +13,7 @@ import kr.toxicity.hud.shader.HudShader import kr.toxicity.hud.shader.ShaderGroup import kr.toxicity.hud.util.* import net.kyori.adventure.text.Component +import kotlin.math.roundToInt class HudImageElement(parent: HudImpl, private val image: ImageLayout, gui: GuiLocation, pixel: PixelLocation) { @@ -45,7 +46,7 @@ class HudImageElement(parent: HudImpl, private val image: ImageLayout, gui: GuiL val finalWidth = WidthComponent(Component.text() .content(c) .font(parent.imageKey) - .append(NEGATIVE_ONE_SPACE_COMPONENT.component), Math.round(pair.image.image.width.toDouble() * scale).toInt()) + NEW_LAYER + .append(NEGATIVE_ONE_SPACE_COMPONENT.component), (pair.image.image.width.toDouble() * scale).roundToInt()) parent.jsonArray?.let { array -> HudImpl.createBit(shader, ascent) { y -> array.add(jsonObjectOf( diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt index f4bb2ad1..b52d2749 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt @@ -15,13 +15,14 @@ import kr.toxicity.hud.manager.LayoutManager import kr.toxicity.hud.manager.ShaderManagerImpl import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.location.GuiLocation +import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.shader.HudShader import kr.toxicity.hud.util.* class HudImpl( override val path: String, private val internalName: String, - file: List, + resource: GlobalResource, section: YamlObject ): Hud, HudConfiguration { companion object { @@ -65,7 +66,7 @@ class HudImpl( layout.animation.location.map { HudElement( this@HudImpl, - file, + resource, layout, gui, it + pixel @@ -83,7 +84,7 @@ class HudImpl( it.value to it.key }.toTypedArray()) )) - PackGenerator.addTask(file + "$imageEncoded.json") { + PackGenerator.addTask(resource.font + "$imageEncoded.json") { jsonObjectOf("providers" to array).toByteArray() } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt index ac66934a..bd64561c 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt @@ -4,27 +4,24 @@ import kr.toxicity.hud.api.component.PixelComponent import kr.toxicity.hud.api.component.WidthComponent import kr.toxicity.hud.api.player.HudPlayer import kr.toxicity.hud.api.update.UpdateEvent -import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.image.LoadedImage -import kr.toxicity.hud.layout.LegacyBackgroundLayout import kr.toxicity.hud.layout.TextLayout +import kr.toxicity.hud.location.GuiLocation +import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.manager.ConfigManagerImpl import kr.toxicity.hud.manager.MinecraftManager import kr.toxicity.hud.manager.TextManagerImpl import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.renderer.TextRenderer -import kr.toxicity.hud.location.GuiLocation +import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.shader.HudShader import kr.toxicity.hud.shader.ShaderGroup import kr.toxicity.hud.text.HudTextData -import kr.toxicity.hud.text.HudTextFont import kr.toxicity.hud.util.* -import net.kyori.adventure.text.Component import kotlin.math.roundToInt class HudTextElement( parent: HudImpl, - file: List, + resource: GlobalResource, private val text: TextLayout, gui: GuiLocation, pixel: PixelLocation @@ -40,12 +37,14 @@ class HudTextElement( loc.opacity, text.property ) - val imageMap = HashMap() + val imageCodepointMap = text.text.imageCharWidth.map { + it.value.name to it.key + }.toMap() + val index2 = ++parent.textIndex val keys = (0.. - val yAxis = (loc.y + lineIndex * text.line).coerceAtLeast(-HudImpl.ADD_HEIGHT).coerceAtMost(HudImpl.ADD_HEIGHT) + val yAxis = (loc.y + lineIndex * text.lineWidth).coerceAtLeast(-HudImpl.ADD_HEIGHT).coerceAtMost(HudImpl.ADD_HEIGHT) val group = ShaderGroup(shader, text.text.name, text.scale, yAxis) TextManagerImpl.getKey(group) ?: run { - val index2 = ++parent.textIndex val array = text.startJson() text.text.array.forEach { HudImpl.createBit(shader, yAxis) { y -> @@ -58,94 +57,50 @@ class HudTextElement( )) } } - var textIndex = 0xC0000 + var textIndex = TEXT_IMAGE_START_CODEPOINT + text.text.imageCharWidth.size val textEncoded = "hud_${parent.name}_text_${index2 + 1}_${lineIndex + 1}".encodeKey() + val imageMap = HashMap() val key = createAdventureKey(textEncoded) - text.text.images.forEach { - val result = textIndex++.parseChar() - val imageScale = it.value.scale * text.scale - val height = (it.value.image.image.height.toDouble() * imageScale).roundToInt() - val div = height.toDouble() / it.value.image.image.height - HudImpl.createBit(shader, loc.y + it.value.location.y + lineIndex * text.line) { y -> + text.text.imageCharWidth.forEach { + val height = (it.value.height.toDouble() * text.scale).roundToInt() + HudImpl.createBit(shader, loc.y + it.value.location.y + lineIndex * text.lineWidth) { y -> array.add( jsonObjectOf( "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:${"glyph_${it.key}".encodeKey()}.png", + "file" to "$NAME_SPACE_ENCODED:${"glyph_${it.value.name}".encodeKey()}.png", "ascent" to y, "height" to height, - "chars" to jsonArrayOf(result) + "chars" to jsonArrayOf(it.key.parseChar()) ) ) } - imageMap[it.key] = it.value.location.x.toSpaceComponent() + WidthComponent( - Component.text() - .font(key) - .content(result), (it.value.image.image.width.toDouble() * div).roundToInt() - ) } if (ConfigManagerImpl.loadMinecraftDefaultTextures) { - HudImpl.createBit(shader, loc.y + text.emojiLocation.y + lineIndex * text.line) { y -> + HudImpl.createBit(shader, loc.y + text.emojiLocation.y + lineIndex * text.lineWidth) { y -> MinecraftManager.applyAll(array, y, text.emojiScale, key) { - textIndex++ + ++textIndex }.forEach { imageMap[it.key] = text.emojiLocation.x.toSpaceComponent() + it.value } } } - PackGenerator.addTask(file + "$textEncoded.json") { + PackGenerator.addTask(resource.font + "$textEncoded.json") { jsonObjectOf("providers" to array).toByteArray() } - HudTextFont(key, imageMap) + key.apply { + TextManagerImpl.setKey(group, this) + } } } - - val key = TextManagerImpl.getKey(group) ?: run { - - val result = HudTextData( - key, - imageMap, - null -// text.background?.let { -// fun getString(image: LoadedImage, file: String): WidthComponent { -// val result = textIndex++.parseChar() -// val height = (image.image.height.toDouble() * text.backgroundScale).roundToInt() -// val div = height.toDouble() / image.image.height -// HudImpl.createBit(HudShader( -// gui, -// text.renderScale, -// text.layer - 1, -// false, -// loc.opacity + it.location.opacity, -// text.property -// ), loc.y + it.location.y) { y -> -// array.add(jsonObjectOf( -// "type" to "bitmap", -// "file" to "$NAME_SPACE_ENCODED:$file.png", -// "ascent" to y, -// "height" to height, -// "chars" to jsonArrayOf(result) -// )) -// } -// return WidthComponent(Component.text() -// .font(key) -// .content(result) -// .append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt()) -// } -// LegacyBackgroundLayout( -// it.location.x, -// getString(it.left, "background_${it.name}_left".encodeKey()), -// getString(it.right, "background_${it.name}_right".encodeKey()), -// getString(it.body, "background_${it.name}_body".encodeKey()) -// ) -// } - ) - TextManagerImpl.setKey(group, result) - result - } TextRenderer( text.text.charWidth, + text.text.imageCharWidth, text.color, - key, + HudTextData( + keys, + imageCodepointMap, + text.splitWidth, + ), text.pattern, text.align, text.scale, @@ -158,7 +113,7 @@ class HudTextElement( text.useLegacyFormat, text.legacySerializer, text.space, - text.conditions.and(text.text.conditions) + text.conditions and text.text.conditions ) }.getText(UpdateEvent.EMPTY) diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/LegacyBackgroundLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/LegacyBackgroundLayout.kt deleted file mode 100644 index 97fbd471..00000000 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/LegacyBackgroundLayout.kt +++ /dev/null @@ -1,12 +0,0 @@ -package kr.toxicity.hud.layout - -import kr.toxicity.hud.api.component.WidthComponent - -@Deprecated(message = "Rewrite new background") -class LegacyBackgroundLayout( - val x: Int, - - val left: WidthComponent, - val right: WidthComponent, - val body: WidthComponent -) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt index 40e0bc1f..5836a715 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt @@ -1,10 +1,8 @@ package kr.toxicity.hud.layout import kr.toxicity.hud.api.yaml.YamlObject -import kr.toxicity.hud.background.HudBackground import kr.toxicity.hud.equation.TEquation import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.manager.BackgroundManager import kr.toxicity.hud.manager.ConfigManagerImpl import kr.toxicity.hud.manager.TextManagerImpl import kr.toxicity.hud.text.HudText @@ -33,10 +31,10 @@ class TextLayout( DecimalFormat(it) } ?: ConfigManagerImpl.numberFormat val disableNumberFormat: Boolean = yamlObject.getAsBoolean("disable-number-format", true) - val background: HudBackground? = yamlObject.get("background")?.asString()?.let { - BackgroundManager.getBackground(it) - } - val backgroundScale: Double = yamlObject.getAsDouble("background-scale", scale) +// val background: HudBackground? = yamlObject.get("background")?.asString()?.let { +// BackgroundManager.getBackground(it) +// } +// val backgroundScale: Double = yamlObject.getAsDouble("background-scale", scale) val emojiLocation: PixelLocation = yamlObject.get("emoji-pixel")?.asObject()?.let { PixelLocation(it) } ?: PixelLocation.zero @@ -52,7 +50,7 @@ class TextLayout( val splitWidth = yamlObject.getAsInt("split-with", 200).apply { if (this < 1) throw RuntimeException("split-width cannot be < 1: $s") } - val lineWidth = yamlObject.getAsInt("line-width", 9) + val lineWidth = yamlObject.getAsInt("line-width", 10) fun startJson() = jsonArrayOf( jsonObjectOf( diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt index 9a67cd9c..406d8001 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt @@ -1,17 +1,14 @@ package kr.toxicity.hud.manager import kr.toxicity.hud.background.HudBackground -import kr.toxicity.hud.renderer.BackgroundRenderer import kr.toxicity.hud.resource.GlobalResource -import kr.toxicity.hud.shader.ShaderGroup import kr.toxicity.hud.util.* import net.kyori.adventure.audience.Audience -import java.util.concurrent.ConcurrentHashMap +//TODO Fix this object BackgroundManager : BetterHudManager { private val backgroundMap = HashMap() - private val backgroundRendererMap = ConcurrentHashMap() override fun start() { @@ -22,7 +19,6 @@ object BackgroundManager : BetterHudManager { override fun reload(sender: Audience, resource: GlobalResource) { val folder = DATA_FOLDER.subFolder("backgrounds") backgroundMap.clear() - backgroundRendererMap.clear() folder.forEachAllYaml(sender) { file, id, yamlObject -> runWithExceptionHandling(sender, "Unable to load this background: $id in ${file.name}") { backgroundMap.putSync("background", id) { @@ -36,13 +32,6 @@ object BackgroundManager : BetterHudManager { } } - @Synchronized - fun getRenderer(shaderGroup: ShaderGroup) = backgroundRendererMap[shaderGroup] - @Synchronized - fun setRenderer(shaderGroup: ShaderGroup, backgroundRenderer: BackgroundRenderer) { - backgroundRendererMap[shaderGroup] = backgroundRenderer - } - override fun end() { } } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/CommandManager.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/CommandManager.kt index 3c302ad1..947ed343 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/CommandManager.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/CommandManager.kt @@ -210,8 +210,8 @@ object CommandManager : BetterHudManager { "player" to Component.text(player.name()), "hud" to Component.text(hud.name) ) - if (success) add_success.send(me, map) - else add_failure.send(me, map) + if (success) remove_success.send(me, map) + else remove_failure.send(me, map) } } } @@ -265,8 +265,8 @@ object CommandManager : BetterHudManager { "player" to Component.text(player.name()), "compass" to Component.text(compass.name) ) - if (success) add_success.send(me, map) - else add_failure.send(me, map) + if (success) remove_success.send(me, map) + else remove_failure.send(me, map) } } } @@ -320,8 +320,8 @@ object CommandManager : BetterHudManager { "player" to Component.text(player.name()), "popup" to Component.text(popup.name) ) - if (success) add_success.send(me, map) - else add_failure.send(me, map) + if (success) remove_success.send(me, map) + else remove_failure.send(me, map) } } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/HudManagerImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/HudManagerImpl.kt index 78df70ff..ad3bb778 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/HudManagerImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/HudManagerImpl.kt @@ -23,7 +23,7 @@ object HudManagerImpl : BetterHudManager, HudManager { DATA_FOLDER.subFolder("huds").forEachAllYaml(sender) { file, s, yamlObject -> runWithExceptionHandling(sender, "Unable to load this hud: $s in ${file.name}") { hudMap.putSync("hud", s) { - HudImpl(file.path, s, resource.font, yamlObject) + HudImpl(file.path, s, resource, yamlObject) } } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/PopupManagerImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/PopupManagerImpl.kt index 28f85889..17361a56 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/PopupManagerImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/PopupManagerImpl.kt @@ -19,7 +19,7 @@ object PopupManagerImpl : BetterHudManager, PopupManager { DATA_FOLDER.subFolder("popups").forEachAllYaml(sender) { file, s, yamlObject -> runCatching { popupMap.putSync("popup", s) { - PopupImpl(file.path, resource.font, s, yamlObject) + PopupImpl(file.path, resource, s, yamlObject) } }.onFailure { e -> warn( diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt index 78cd5556..5ffef065 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt @@ -3,8 +3,8 @@ package kr.toxicity.hud.manager import com.google.gson.JsonArray import kr.toxicity.hud.api.manager.TextManager import kr.toxicity.hud.configuration.PluginConfiguration -import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.image.LocatedImage +import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.placeholder.ConditionBuilder import kr.toxicity.hud.resource.GlobalResource @@ -12,6 +12,7 @@ import kr.toxicity.hud.shader.ShaderGroup import kr.toxicity.hud.text.* import kr.toxicity.hud.util.* import net.kyori.adventure.audience.Audience +import net.kyori.adventure.key.Key import java.awt.AlphaComposite import java.awt.Font import java.awt.font.FontRenderContext @@ -20,7 +21,6 @@ import java.io.File import java.io.InputStreamReader import java.util.* import java.util.concurrent.ConcurrentHashMap -import kotlin.collections.HashMap import kotlin.math.roundToInt object TextManagerImpl : BetterHudManager, TextManager { @@ -60,7 +60,7 @@ object TextManagerImpl : BetterHudManager, TextManager { private val textCacheMap = HashMap() private val textWidthMap = HashMap() - private val textKeyMap = ConcurrentHashMap() + private val textKeyMap = ConcurrentHashMap() private val defaultBitmapImageMap = HashMap() private val translatableString = HashMap>() @@ -130,7 +130,7 @@ object TextManagerImpl : BetterHudManager, TextManager { @Synchronized fun getKey(shaderGroup: ShaderGroup) = textKeyMap[shaderGroup] @Synchronized - fun setKey(shaderGroup: ShaderGroup, key: HudTextFont) { + fun setKey(shaderGroup: ShaderGroup, key: Key) { textKeyMap[shaderGroup] = key } @@ -254,7 +254,7 @@ object TextManagerImpl : BetterHudManager, TextManager { provider, scale, resource.textures, - HashMap().apply { + TreeMap().apply { section.get("images")?.asObject() ?.forEachSubConfiguration { key, yamlObject -> put(key, LocatedImage( @@ -435,7 +435,7 @@ object TextManagerImpl : BetterHudManager, TextManager { ): HudText { return synchronized(textCacheMap) { textCacheMap[TextCache(saveName, emptySet())]?.let { old -> - HudText(path, saveName, old.array, old.images, old.charWidth, old.conditions) + HudText(path, saveName, old.array, old.charWidth, old.imageCharWidth, old.conditions) } } ?: run { val saveFontName = synchronized (this) { @@ -492,8 +492,8 @@ object TextManagerImpl : BetterHudManager, TextManager { path, saveName, textArray, - mapOf(), charWidthMap, + mapOf(), condition ) } @@ -517,7 +517,7 @@ object TextManagerImpl : BetterHudManager, TextManager { ): HudText { return synchronized(textCacheMap) { textCacheMap[TextCache(saveName, images.keys)]?.let { old -> - HudText(path, saveName, old.array, old.images, old.charWidth, old.conditions) + HudText(path, saveName, old.array, old.charWidth, old.imageCharWidth, old.conditions) } } ?: run { val saveFontName = synchronized (this) { @@ -573,7 +573,15 @@ object TextManagerImpl : BetterHudManager, TextManager { } val textList = ArrayList() var i = 0 + var imageStart = TEXT_IMAGE_START_CODEPOINT + val imageCharWidthMap = HashMap() images.forEach { + imageCharWidthMap[++imageStart] = ImageCharWidth( + it.key, + it.value.location, + it.value.image.image.width, + (it.value.image.image.height.toDouble() * it.value.scale).roundToInt() + ) PackGenerator.addTask(imageSaveFolder + "${"glyph_${it.key}".encodeKey()}.png") { it.value.image.image.toByteArray() } @@ -612,7 +620,7 @@ object TextManagerImpl : BetterHudManager, TextManager { } } } - val result = HudText(path, saveName, textList, images, charWidthMap, condition) + val result = HudText(path, saveName, textList, charWidthMap, imageCharWidthMap, condition) synchronized(textCacheMap) { textCacheMap[TextCache(saveName, images.keys)] = result } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt index 31c6cb41..17aed9e5 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt @@ -15,12 +15,13 @@ import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.manager.* import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.location.GuiLocation +import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.util.* import java.util.* class PopupImpl( override val path: String, - file: List, + resource: GlobalResource, val internalName: String, section: YamlObject ) : Popup, HudConfiguration { @@ -76,6 +77,7 @@ class PopupImpl( loc += GuiLocation(gui) } PopupLayout( + resource, json, LayoutManager.getLayout(layout).ifNull("this layout doesn't exist: $layout"), this@PopupImpl, @@ -83,7 +85,7 @@ class PopupImpl( yamlObject.get("pixel")?.asObject()?.let { pixel -> PixelLocation(pixel) } ?: PixelLocation.zero, - file, + resource.font, ) } }.ifNull("layouts configuration not set.").ifEmpty { @@ -118,7 +120,7 @@ class PopupImpl( it.value to it.key }.toTypedArray()) )) - PackGenerator.addTask(file + "$imageEncoded.json") { + PackGenerator.addTask(resource.font + "$imageEncoded.json") { jsonObjectOf("providers" to arr).toByteArray() } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt index 477080c2..59c16d48 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt @@ -7,12 +7,11 @@ import kr.toxicity.hud.api.player.HudPlayer import kr.toxicity.hud.api.update.UpdateEvent import kr.toxicity.hud.component.LayoutComponentContainer import kr.toxicity.hud.hud.HudImpl -import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.image.LoadedImage import kr.toxicity.hud.image.LocationGroup -import kr.toxicity.hud.layout.LegacyBackgroundLayout -import kr.toxicity.hud.location.AnimationType import kr.toxicity.hud.layout.LayoutGroup +import kr.toxicity.hud.location.AnimationType +import kr.toxicity.hud.location.GuiLocation +import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.manager.* import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.player.head.HeadKey @@ -21,7 +20,7 @@ import kr.toxicity.hud.player.head.HeadRenderType.STANDARD import kr.toxicity.hud.renderer.HeadRenderer import kr.toxicity.hud.renderer.ImageRenderer import kr.toxicity.hud.renderer.TextRenderer -import kr.toxicity.hud.location.GuiLocation +import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.shader.HudShader import kr.toxicity.hud.shader.ShaderGroup import kr.toxicity.hud.text.HudTextData @@ -30,6 +29,7 @@ import net.kyori.adventure.text.Component import kotlin.math.roundToInt class PopupLayout( + private val resource: GlobalResource, private val json: JsonArray, private val layout: LayoutGroup, private val parent: PopupImpl, @@ -141,8 +141,8 @@ class PopupLayout( "chars" to jsonArrayOf(char) )) } - val xWidth = Math.round(it.image.image.width.toDouble() * scale).toInt() - val comp = WidthComponent(Component.text().content(char).font(parent.imageKey), xWidth) + NEGATIVE_ONE_SPACE_COMPONENT + NEW_LAYER + val xWidth = (it.image.image.width.toDouble() * scale).roundToInt() + val comp = WidthComponent(Component.text().content(char).font(parent.imageKey), xWidth) + NEGATIVE_ONE_SPACE_COMPONENT ImageManager.setImage(shaderGroup, comp) comp } @@ -159,7 +159,7 @@ class PopupLayout( "chars" to jsonArrayOf(char) )) } - val comp = WidthComponent(Component.text().content(char).font(parent.imageKey), Math.round(it.image.image.width.toDouble() * target.scale).toInt()) + NEGATIVE_ONE_SPACE_COMPONENT + NEW_LAYER + val comp = WidthComponent(Component.text().content(char).font(parent.imageKey), Math.round(it.image.image.width.toDouble() * target.scale).toInt()) + NEGATIVE_ONE_SPACE_COMPONENT list.add(comp.toPixelComponent(pixel.x)) } @@ -191,97 +191,70 @@ class PopupLayout( textLayout.property ) val group = ShaderGroup(textShader, textLayout.text.name, textLayout.scale, pixel.y) - val textKey = TextManagerImpl.getKey(group) ?: run { - val index = ++textIndex - val array = textLayout.startJson() - HudImpl.createBit(textShader, pixel.y) { y -> - textLayout.text.array.forEach { - array.add(jsonObjectOf( - "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:${it.file}", - "ascent" to y, - "height" to (it.height * textLayout.scale).roundToInt(), - "chars" to it.chars - )) + val imageCodepointMap = textLayout.text.imageCharWidth.map { + it.value.name to it.key + }.toMap() + val index = ++textIndex + val keys = (0.. + TextManagerImpl.getKey(group) ?: run { + val array = textLayout.startJson() + HudImpl.createBit(textShader, pixel.y + lineIndex * textLayout.lineWidth) { y -> + textLayout.text.array.forEach { + array.add( + jsonObjectOf( + "type" to "bitmap", + "file" to "$NAME_SPACE_ENCODED:${it.file}", + "ascent" to y, + "height" to (it.height * textLayout.scale).roundToInt(), + "chars" to it.chars + ) + ) + } } - } - var textIndex = 0xC0000 - val imageMap = HashMap() - val textEncoded = "popup_${parent.name}_text_${index}".encodeKey() - val key = createAdventureKey(textEncoded) - textLayout.text.images.forEach { - val result = textIndex++.parseChar() - val imageScale = it.value.scale * textLayout.scale - val height = (it.value.image.image.height.toDouble() * imageScale).roundToInt() - val div = height.toDouble() / it.value.image.image.height - HudImpl.createBit(textShader, pixel.y + it.value.location.y) { y -> - array.add(jsonObjectOf( - "type" to "bitmap", - "file" to "$NAME_SPACE_ENCODED:${"glyph_${it.key}".encodeKey()}.png", - "ascent" to y, - "height" to height, - "chars" to jsonArrayOf(result) - )) + val imageMap = HashMap() + val textEncoded = "popup_${parent.name}_text_${index}".encodeKey() + val key = createAdventureKey(textEncoded) + var imageTextIndex = TEXT_IMAGE_START_CODEPOINT + textLayout.text.imageCharWidth.size + textLayout.text.imageCharWidth.forEach { + val height = (it.value.height.toDouble() * textLayout.scale).roundToInt() + HudImpl.createBit(textShader, pixel.y + it.value.location.y + lineIndex * textLayout.lineWidth) { y -> + array.add( + jsonObjectOf( + "type" to "bitmap", + "file" to "$NAME_SPACE_ENCODED:${"glyph_${it.value.name}".encodeKey()}.png", + "ascent" to y, + "height" to height, + "chars" to jsonArrayOf(it.key.parseChar()) + ) + ) + } } - imageMap[it.key] = it.value.location.x.toSpaceComponent() + WidthComponent(Component.text() - .font(key) - .content(result) - .append(NEGATIVE_ONE_SPACE_COMPONENT.component), (it.value.image.image.width.toDouble() * div).roundToInt()) - } - if (ConfigManagerImpl.loadMinecraftDefaultTextures) { - HudImpl.createBit(textShader, textLayout.emojiLocation.y) { y -> - MinecraftManager.applyAll(array, y, textLayout.emojiScale, key) { - textIndex++ - }.forEach { - imageMap[it.key] = textLayout.emojiLocation.x.toSpaceComponent() + it.value + if (ConfigManagerImpl.loadMinecraftDefaultTextures) { + HudImpl.createBit(textShader, pixel.y + textLayout.emojiLocation.y + lineIndex * textLayout.lineWidth) { y -> + MinecraftManager.applyAll(array, y, textLayout.emojiScale, key) { + ++imageTextIndex + }.forEach { + imageMap[it.key] = textLayout.emojiLocation.x.toSpaceComponent() + it.value + } } } + PackGenerator.addTask(file + "$textEncoded.json") { + jsonObjectOf("providers" to array).toByteArray() + } + key.apply { + TextManagerImpl.setKey(group, this) + } } - val result = HudTextData( - key, - imageMap, - null -// textLayout.background?.let { -// fun getString(image: LoadedImage, file: String): WidthComponent { -// val result = textIndex++.parseChar() -// val height = (image.image.height.toDouble() * textLayout.backgroundScale).roundToInt() -// val div = height.toDouble() / image.image.height -// HudImpl.createBit(HudShader( -// elementGui, -// textLayout.renderScale, -// textLayout.layer - 1, -// false, -// pixel.opacity + it.location.opacity, -// textLayout.property -// ), pixel.y + it.location.y) { y -> -// array.add(jsonObjectOf( -// "type" to "bitmap", -// "file" to "$NAME_SPACE_ENCODED:$file.png", -// "ascent" to y, -// "height" to height, -// "chars" to jsonArrayOf(result) -// )) -// } -// return WidthComponent(Component.text().font(key).content(result).append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt()) -// } -// LegacyBackgroundLayout( -// it.location.x, -// getString(it.left, "background_${it.name}_left".encodeKey()), -// getString(it.right, "background_${it.name}_right".encodeKey()), -// getString(it.body, "background_${it.name}_body".encodeKey()) -// ) -// } - ) - PackGenerator.addTask(file + "$textEncoded.json") { - jsonObjectOf("providers" to array).toByteArray() - } - TextManagerImpl.setKey(group, result) - result } TextRenderer( textLayout.text.charWidth, + textLayout.text.imageCharWidth, textLayout.color, - textKey, + HudTextData( + keys, + imageCodepointMap, + textLayout.splitWidth + ), textLayout.pattern, textLayout.align, textLayout.scale, @@ -294,7 +267,7 @@ class PopupLayout( textLayout.useLegacyFormat, textLayout.legacySerializer, textLayout.space, - textLayout.conditions.and(textLayout.text.conditions) + textLayout.conditions and textLayout.text.conditions ) } @@ -375,7 +348,7 @@ class PopupLayout( headLayout.type, headLayout.follow, headLayout.cancelIfFollowerNotExists, - headLayout.conditions.and(headLayout.head.conditions) + headLayout.conditions and headLayout.head.conditions ) } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt b/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt index 1b020506..c64c4c6a 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt @@ -3,28 +3,24 @@ package kr.toxicity.hud.renderer import kr.toxicity.hud.api.component.WidthComponent import kr.toxicity.hud.util.toSpaceComponent +//TODO Fix this class BackgroundRenderer( val x: Int, - private val lists: List + private val component: BackgroundComponent ) { - - fun build(line: Int, width: Int): WidthComponent { + fun build(width: Int): WidthComponent { var comp = x.toSpaceComponent() var w = 0 - for (i in 0.., + private val imageWidthMap: Map, private val defaultColor: TextColor, private val data: HudTextData, pattern: String, @@ -46,7 +46,6 @@ class TextRenderer( private val condition: ConditionBuilder ) { companion object { - private const val SPACE_POINT = ' '.code private val decimalPattern = Pattern.compile("([0-9]+((\\.([0-9]+))?))") private val allPattern = Pattern.compile(".+") private val imagePattern = Pattern.compile("<(?(image|space)):(?(([a-zA-Z]|[0-9]|-)+))>") @@ -62,31 +61,14 @@ class TextRenderer( private val widthViewer = ValueViewer, Int>() .addFunction( - { - if (it.first.font() == null) { - if (it.second == SPACE_POINT) 4 + it.first.addNumber() - else widthMap[it.second]?.scaledWidth(scale)?.let { w -> - w + 1 + it.first.addNumber() - } - } else null - }, - { - if (it.first.font() == null && it.second == TEXT_SPACE_KEY_CODEPOINT) space + it.first.addNumber() else null - }, - data.images.map { - val comp = it.value - comp.component.content().codePointAt(0) to comp.width + 1 - }.toMap().let { - { pair: Pair -> - it[pair.second]?.let { - it + 1 + pair.first.addNumber() - } - } - }, { when (it.first.font()) { - SPACE_KEY -> it.second - CURRENT_CENTER_SPACE_CODEPOINT + it.first.addNumber() - LEGACY_SPACE_KEY -> it.second - LEGACY_CENTER_SPACE_CODEPOINT + it.first.addNumber() + SPACE_KEY -> it.second - CURRENT_CENTER_SPACE_CODEPOINT + LEGACY_SPACE_KEY -> it.second - LEGACY_CENTER_SPACE_CODEPOINT + null -> when (it.second) { + TEXT_SPACE_KEY_CODEPOINT -> space + else -> (widthMap[it.second] ?: imageWidthMap[it.second])?.let { c -> c.scaledWidth(scale) + 1 } + } else -> null } } @@ -112,46 +94,14 @@ class TextRenderer( var widthComp = EMPTY_WIDTH_COMPONENT - buildPattern(targetHudPlayer) + val compList = buildPattern(targetHudPlayer) .parseToComponent() - .split(data.splitWidth, widthViewer) - .forEachIndexed { index, comp -> - if (data.words.lastIndex < index) return@forEachIndexed - if (comp !is TextComponent) return@forEachIndexed - fun Component.search(): Int { - var i = 0 - if (this is TextComponent) { - val style = style() - for (codePoint in content().codePoints()) { - widthViewer(style to codePoint)?.let { w -> - i += w - } - } - } - return i + children().sumOf { children -> - children.search() - } - } - val newComp = WidthComponent(comp.toBuilder().font(data.words[index]), comp.search()) - widthComp = if (widthComp.width == 0) newComp else widthComp plusWithAlign newComp - } - data.background?.let { - it.build(data.words.size, widthComp.width) - + .split(data.splitWidth, space, widthViewer) + compList.forEachIndexed { index, comp -> + if (data.font.lastIndex < index) return@forEachIndexed + comp.component.font(data.font[index]) + widthComp = if (widthComp.width == 0) comp else widthComp plusWithAlign comp } - - -// data.background?.let { -// val builder = Component.text().append(it.left.component) -// var length = 0 -// while (length < comp.width) { -// builder.append(it.body.component) -// length += it.body.width -// } -// val total = it.left.width + length + it.right.width -// val minus = -total + (length - comp.width) / 2 + it.left.width - it.x -// comp = it.x.toSpaceComponent() + WidthComponent(builder.append(it.right.component), total) + minus.toSpaceComponent() + comp -// } widthComp.toPixelComponent(when (align) { LayoutAlign.LEFT -> x LayoutAlign.CENTER -> x - widthComp.width / 2 @@ -160,7 +110,8 @@ class TextRenderer( } } - private infix fun WidthComponent.plusWithAlign(other: WidthComponent) = when (align) { + private infix fun WidthComponent.plusWithAlign(other: WidthComponent) = plusWithAlign(LayoutAlign.CENTER, other) + private fun WidthComponent.plusWithAlign(align: LayoutAlign, other: WidthComponent) = when (align) { LayoutAlign.LEFT -> this + (-width).toSpaceComponent() + other LayoutAlign.CENTER -> { if (width > other.width) { @@ -182,13 +133,6 @@ class TextRenderer( } } - private fun Style.addNumber(): Int { - var i = 0 - if (hasDecoration(TextDecoration.BOLD)) i++ - if (hasDecoration(TextDecoration.ITALIC)) i++ - return i - } - private fun String.parseToComponent(): Component { var targetString = (if (useLegacyFormat) legacySerializer(this) else Component.text(this)) .color(defaultColor) @@ -196,7 +140,7 @@ class TextRenderer( .match(imagePattern) .replacement { r, _ -> when (r.group(1)) { - "image" -> data.images[r.group(3)]?.component?.build() ?: Component.empty() + "image" -> data.imageCodepoint[r.group(3)]?.let { Component.text(it.parseChar()) } ?: Component.empty() "space" -> r.group(3).toIntOrNull()?.toSpaceComponent()?.component ?: Component.empty() else -> Component.empty() } @@ -219,38 +163,6 @@ class TextRenderer( } .build()) } - fun hasDecoration(parent: Boolean, state: TextDecoration.State) = when (state) { - TextDecoration.State.TRUE -> true - TextDecoration.State.NOT_SET -> parent - TextDecoration.State.FALSE -> false - } - fun applyDecoration(component: Component, bold: Boolean, italic: Boolean): Component { - var ret = component - if (ret is TextComponent && ret.font() == null) { - val codepoint = ret.content().codePoints().toArray() - if (space != 0) { - ret = ret.content(buildString { - codepoint.forEachIndexed { index, i -> - appendCodePoint(i) - if (index < codepoint.lastIndex) { - appendCodePoint(TEXT_SPACE_KEY_CODEPOINT) - } - } - }) - } - } - return ret.children(ret.children().map { - applyDecoration( - it, - hasDecoration(bold, it.decoration(TextDecoration.BOLD)), - hasDecoration(italic, it.decoration(TextDecoration.ITALIC)), - ) - }) - } - return applyDecoration( - targetString, - hasDecoration(false, targetString.decoration(TextDecoration.BOLD)), - hasDecoration(false, targetString.decoration(TextDecoration.ITALIC)) - ) + return targetString } } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/shader/HudShader.kt b/dist/src/main/kotlin/kr/toxicity/hud/shader/HudShader.kt index d2d197f7..54bdbdab 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/shader/HudShader.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/shader/HudShader.kt @@ -29,4 +29,13 @@ data class HudShader( override fun compareTo(other: HudShader): Int { return comparator.compare(this, other) } + + fun toBackground() = HudShader( + gui, + renderScale, + layer - 1, + false, + opacity, + property + ) } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/BackgroundKey.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/BackgroundKey.kt new file mode 100644 index 00000000..9c2f1226 --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/BackgroundKey.kt @@ -0,0 +1,9 @@ +package kr.toxicity.hud.text + +import kr.toxicity.hud.renderer.BackgroundRenderer +import net.kyori.adventure.key.Key + +data class BackgroundKey( + val key: Key, + val background: BackgroundRenderer? +) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/CharWidth.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/CharWidth.kt index 1b2382ee..972d4877 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/text/CharWidth.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/CharWidth.kt @@ -2,6 +2,6 @@ package kr.toxicity.hud.text import kotlin.math.roundToInt -data class CharWidth(val width: Int, val height: Int) { +open class CharWidth(val width: Int, val height: Int) { fun scaledWidth(scale: Double) = (width.toDouble() / height.toDouble() * (height * scale).roundToInt()).roundToInt() } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/HudText.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/HudText.kt index c1acd33c..20f85854 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/text/HudText.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/HudText.kt @@ -1,14 +1,13 @@ package kr.toxicity.hud.text import kr.toxicity.hud.configuration.HudConfiguration -import kr.toxicity.hud.image.LocatedImage import kr.toxicity.hud.placeholder.ConditionBuilder class HudText( override val path: String, val name: String, val array: List, - val images: Map, val charWidth: Map, + val imageCharWidth: Map, val conditions: ConditionBuilder ) : HudConfiguration \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt index 7143bf5e..2d6b65fa 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt @@ -1,9 +1,9 @@ package kr.toxicity.hud.text -import kr.toxicity.hud.renderer.BackgroundRenderer +import net.kyori.adventure.key.Key class HudTextData( - val font: List, - val splitWidth: Int, - val background: BackgroundRenderer? + val font: List, + val imageCodepoint: Map, + val splitWidth: Int ) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextFont.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextFont.kt deleted file mode 100644 index 248530b0..00000000 --- a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextFont.kt +++ /dev/null @@ -1,9 +0,0 @@ -package kr.toxicity.hud.text - -import kr.toxicity.hud.api.component.WidthComponent -import net.kyori.adventure.key.Key - -class HudTextFont( - val word: Key, - val images: Map -) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt new file mode 100644 index 00000000..a9e48353 --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/ImageCharWidth.kt @@ -0,0 +1,5 @@ +package kr.toxicity.hud.text + +import kr.toxicity.hud.location.PixelLocation + +class ImageCharWidth(val name: String, val location: PixelLocation, width: Int, height: Int) : CharWidth(width, height) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt b/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt index c2f83f4c..1511c169 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt @@ -11,12 +11,12 @@ import net.kyori.adventure.text.format.NamedTextColor import net.kyori.adventure.text.format.Style import net.kyori.adventure.text.format.TextColor import net.kyori.adventure.text.format.TextDecoration -import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer import kotlin.math.abs fun createAdventureKey(value: String) = Key.key(NAME_SPACE_ENCODED, value) const val TEXT_SPACE_KEY_CODEPOINT = 0xC0000 +const val TEXT_IMAGE_START_CODEPOINT = 0xC0000 val CONSOLE get() = BOOTSTRAP.console() @@ -46,8 +46,6 @@ val EMPTY_WIDTH_COMPONENT get() = WidthComponent(Component.text().color(NamedTextColor.WHITE), 0) val EMPTY_PIXEL_COMPONENT get() = PixelComponent(EMPTY_WIDTH_COMPONENT, 0) -val NEW_LAYER - get() = if (BOOTSTRAP.useLegacyFont()) EMPTY_WIDTH_COMPONENT else WidthComponent(Component.text().content(0xC0000.parseChar()).font(SPACE_KEY), 0) const val LEGACY_CENTER_SPACE_CODEPOINT = 0xFFC00 const val CURRENT_CENTER_SPACE_CODEPOINT = 0xD0000 @@ -114,19 +112,19 @@ fun Int.toSpaceComponent(width: Int) = if (BOOTSTRAP.useLegacyFont()) { } private class SplitBuilder( - private val chain: (Component) -> Unit + private val chain: (TextComponent.Builder) -> Unit ) { private var builder = Component.text() var isClean = true private set fun accept(block: TextComponent.Builder.() -> Unit = {}) { - val build = builder.apply(block).build() + val build = builder.apply(block) builder = Component.text() isClean = true chain(build) } - fun build(block: TextComponent.Builder.() -> Unit = {}): Component { - val build = builder.apply(block).build() + fun build(block: TextComponent.Builder.() -> Unit = {}): TextComponent.Builder { + val build = builder.apply(block) builder = Component.text() isClean = true return build @@ -135,52 +133,70 @@ private class SplitBuilder( builder.append(like) isClean = false } - fun then(firstChain: (SplitBuilder, Component) -> Unit) = SplitBuilder other@ { + fun then(firstChain: (SplitBuilder, TextComponent.Builder) -> Unit) = SplitBuilder other@ { firstChain(this, it) accept() } } -fun Component.split(endWidth: Int, charWidth: (Pair) -> Int?): List { +private fun Style.parseDecoration(decoration: TextDecoration, default: Boolean): Boolean = when (decoration(decoration)) { + TextDecoration.State.NOT_SET -> default + TextDecoration.State.FALSE -> false + TextDecoration.State.TRUE -> true +} + +fun Component.split(endWidth: Int, space: Int, charWidth: (Pair) -> Int?): List { var i = 0 - val list = ArrayList() + val list = ArrayList() val topBuilder = SplitBuilder { + list.add(WidthComponent(it, i)) i = 0 - list.add(it) } - fun Component.parse(target: SplitBuilder) { + fun Component.parse(target: SplitBuilder, bold: Boolean, italic: Boolean) { if (this is TextComponent) { - val style = style() - if (style.font() == null) { - val subBuilder = target.then { b, c -> - b.append(c) + var style = style() + var add = 0 + val subBold = style.parseDecoration(TextDecoration.BOLD, bold) + val subItalic = style.parseDecoration(TextDecoration.ITALIC, italic) + style = style.decorations(mapOf( + TextDecoration.BOLD to if (subBold) TextDecoration.State.TRUE else TextDecoration.State.FALSE, + TextDecoration.ITALIC to if (subItalic) TextDecoration.State.TRUE else TextDecoration.State.FALSE + )) + if (subBold) add++ + if (subItalic) add++ + val subBuilder = target.then { b, c -> + b.append(c) + } + val sb = StringBuilder() + for (codepoint in content().codePoints()) { + sb.appendCodePoint(codepoint) + i += if (codepoint == ' '.code) 4 else charWidth(style to codepoint) ?: continue + i += add + if (space > 0) { + if (BOOTSTRAP.useLegacyFont()) subBuilder.append(space.toSpaceComponent().component) + else sb.appendCodePoint(TEXT_SPACE_KEY_CODEPOINT) + i += space + add } - val sb = StringBuilder() - for (codepoint in content().codePoints()) { - sb.appendCodePoint(codepoint) - i += if (codepoint == ' '.code) 4 else charWidth(style to codepoint) ?: continue - if (i >= endWidth) { - subBuilder.accept { - style(style).content(sb.toString()) - } - sb.setLength(0) + if (i >= endWidth) { + subBuilder.accept { + style(style).content(sb.toString()) } + sb.setLength(0) } - if (!subBuilder.isClean || sb.isNotEmpty()) target.append(subBuilder.build { - style(style).content(sb.toString()) - }) - for (child in children()) { - child.parse(subBuilder) - } - if (!subBuilder.isClean) target.append(subBuilder.build { - style(style) - }) - } else { - target.append(Component.text().content(content()).style(style)) } + if (!subBuilder.isClean || sb.isNotEmpty()) target.append(subBuilder.build { + style(style).content(sb.toString()) + }) + for (child in children()) { + child.parse(subBuilder, subBold, subItalic) + } + if (!subBuilder.isClean) target.append(subBuilder.build { + style(style) + }) } } - parse(topBuilder) - if (!topBuilder.isClean) list.add(topBuilder.build()) + val style = style() + parse(topBuilder, style.parseDecoration(TextDecoration.BOLD, false), style.parseDecoration(TextDecoration.ITALIC, false)) + if (!topBuilder.isClean) list.add(WidthComponent(topBuilder.build(), i)) return list } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/util/ValueViewer.kt b/dist/src/main/kotlin/kr/toxicity/hud/util/ValueViewer.kt index d441f2f2..d7031b44 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/util/ValueViewer.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/util/ValueViewer.kt @@ -23,7 +23,7 @@ class ValueViewer : (K) -> V? { } override fun invoke(p1: K): V? { - return refs.firstNotNullOfOrNull { + return if (refs.size == 1) refs[0](p1) else refs.firstNotNullOfOrNull { it(p1) } } From 344338137febcc59a60e2b0d52c544a486a0181a Mon Sep 17 00:00:00 2001 From: toxicity Date: Wed, 6 Nov 2024 04:01:36 +0900 Subject: [PATCH 03/10] Bug fix. --- .../mythicmobs/MythicMobsCompatibility.kt | 42 +++++++------------ .../kr/toxicity/hud/hud/HudTextElement.kt | 4 +- .../kr/toxicity/hud/layout/TextLayout.kt | 1 + .../toxicity/hud/manager/TextManagerImpl.kt | 6 ++- .../kr/toxicity/hud/popup/PopupLayout.kt | 2 + .../kr/toxicity/hud/renderer/TextRenderer.kt | 13 ++++-- .../kotlin/kr/toxicity/hud/util/Adventures.kt | 19 ++++++--- 7 files changed, 48 insertions(+), 39 deletions(-) diff --git a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/mythicmobs/MythicMobsCompatibility.kt b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/mythicmobs/MythicMobsCompatibility.kt index 6a4996ee..7c14a59c 100644 --- a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/mythicmobs/MythicMobsCompatibility.kt +++ b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/mythicmobs/MythicMobsCompatibility.kt @@ -115,10 +115,8 @@ class MythicMobsCompatibility : Compatibility { override fun getRequiredArgsLength(): Int = 1 override fun invoke(args: MutableList, reason: UpdateEvent): Function { return reason.unwrap { event: EntityEvent -> - Function { - MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { - it.getAuraStacks(args[0]) - }.orElse(-1) + Function get@ { + (MythicBukkit.inst().mobManager.getMythicMobInstance(event.entity) ?: return@get -1).getAuraStacks(args[0]) } } } @@ -127,12 +125,10 @@ class MythicMobsCompatibility : Compatibility { override fun getRequiredArgsLength(): Int = 1 override fun invoke(args: MutableList, reason: UpdateEvent): Function { return reason.unwrap { event: EntityEvent -> - Function { - MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { - it.auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> - aura.startDuration - } ?: 0 - }.orElse(-1) + Function get@ { + (MythicBukkit.inst().mobManager.getMythicMobInstance(event.entity) ?: return@get -1).auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> + aura.startDuration + } ?: 0 } } } @@ -141,12 +137,10 @@ class MythicMobsCompatibility : Compatibility { override fun getRequiredArgsLength(): Int = 1 override fun invoke(args: MutableList, reason: UpdateEvent): Function { return reason.unwrap { event: EntityEvent -> - Function { - MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { - it.auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> - aura.ticksRemaining - } ?: 0 - }.orElse(-1) + Function get@ { + (MythicBukkit.inst().mobManager.getMythicMobInstance(event.entity) ?: return@get -1).auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> + aura.ticksRemaining + } ?: 0 } } } @@ -155,12 +149,10 @@ class MythicMobsCompatibility : Compatibility { override fun getRequiredArgsLength(): Int = 1 override fun invoke(args: MutableList, reason: UpdateEvent): Function { return reason.unwrap { event: EntityEvent -> - Function { - MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { - it.auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> - aura.startDuration - aura.ticksRemaining - } ?: 0 - }.orElse(-1) + Function get@ { + (MythicBukkit.inst().mobManager.getMythicMobInstance(event.entity) ?: return@get -1).auraRegistry.auras[args[0]]?.maxOfOrNull { aura -> + aura.startDuration - aura.ticksRemaining + } ?: 0 } } } @@ -227,10 +219,8 @@ class MythicMobsCompatibility : Compatibility { override fun getRequiredArgsLength(): Int = 1 override fun invoke(args: MutableList, reason: UpdateEvent): Function { return reason.unwrap { event: EntityEvent -> - Function { _ -> - MythicBukkit.inst().playerManager.getProfile(event.entity.uniqueId).map { - it.auraRegistry.hasAura(args[0]) - }.orElse(false) + Function get@ { _ -> + (MythicBukkit.inst().mobManager.getMythicMobInstance(event.entity) ?: return@get false).auraRegistry.hasAura(args[0]) } } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt index bd64561c..3152c0a8 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt @@ -62,7 +62,7 @@ class HudTextElement( val imageMap = HashMap() val key = createAdventureKey(textEncoded) text.text.imageCharWidth.forEach { - val height = (it.value.height.toDouble() * text.scale).roundToInt() + val height = (it.value.height.toDouble() * text.scale * text.emojiScale).roundToInt() HudImpl.createBit(shader, loc.y + it.value.location.y + lineIndex * text.lineWidth) { y -> array.add( jsonObjectOf( @@ -103,7 +103,9 @@ class HudTextElement( ), text.pattern, text.align, + text.lineAlign, text.scale, + text.emojiScale, loc.x, text.numberEquation, text.numberFormat, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt index 5836a715..79ab0262 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt @@ -23,6 +23,7 @@ class TextLayout( val scale: Double = yamlObject.getAsDouble("scale", 1.0) val space: Int = yamlObject.getAsInt("space", 0).coerceAtLeast(0) val align: LayoutAlign = yamlObject.get("align")?.asString().toLayoutAlign() + val lineAlign: LayoutAlign = yamlObject.get("line-align")?.asString().toLayoutAlign() val color: TextColor = yamlObject.get("color")?.asString()?.toTextColor() ?: NamedTextColor.WHITE val numberEquation: TEquation = yamlObject.get("number-equation")?.asString()?.let { TEquation(it) diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt index 5ffef065..e5cce21e 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt @@ -576,11 +576,13 @@ object TextManagerImpl : BetterHudManager, TextManager { var imageStart = TEXT_IMAGE_START_CODEPOINT val imageCharWidthMap = HashMap() images.forEach { + val h = (it.value.image.image.height.toDouble() * it.value.scale).roundToInt() + val div = h / it.value.image.image.height.toDouble() imageCharWidthMap[++imageStart] = ImageCharWidth( it.key, it.value.location, - it.value.image.image.width, - (it.value.image.image.height.toDouble() * it.value.scale).roundToInt() + (it.value.image.image.width * div).roundToInt(), + h ) PackGenerator.addTask(imageSaveFolder + "${"glyph_${it.key}".encodeKey()}.png") { it.value.image.image.toByteArray() diff --git a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt index 59c16d48..3c4ef1a4 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt @@ -257,7 +257,9 @@ class PopupLayout( ), textLayout.pattern, textLayout.align, + textLayout.lineAlign, textLayout.scale, + textLayout.emojiScale, pixel.x, textLayout.numberEquation, textLayout.numberFormat, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt b/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt index 05e97453..cc3aebce 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt @@ -30,7 +30,9 @@ class TextRenderer( private val data: HudTextData, pattern: String, private val align: LayoutAlign, + private val lineAlign: LayoutAlign, private val scale: Double, + private val emojiScale: Double, private val x: Int, private val numberEquation: TEquation, @@ -67,7 +69,7 @@ class TextRenderer( LEGACY_SPACE_KEY -> it.second - LEGACY_CENTER_SPACE_CODEPOINT null -> when (it.second) { TEXT_SPACE_KEY_CODEPOINT -> space - else -> (widthMap[it.second] ?: imageWidthMap[it.second])?.let { c -> c.scaledWidth(scale) + 1 } + else -> (widthMap[it.second]?.scaledWidth(scale) ?: imageWidthMap[it.second]?.scaledWidth(scale * emojiScale))?.let { c -> c + 1 } } else -> null } @@ -97,6 +99,9 @@ class TextRenderer( val compList = buildPattern(targetHudPlayer) .parseToComponent() .split(data.splitWidth, space, widthViewer) + val max = compList.maxOf { + it.width + } compList.forEachIndexed { index, comp -> if (data.font.lastIndex < index) return@forEachIndexed comp.component.font(data.font[index]) @@ -104,13 +109,13 @@ class TextRenderer( } widthComp.toPixelComponent(when (align) { LayoutAlign.LEFT -> x - LayoutAlign.CENTER -> x - widthComp.width / 2 - LayoutAlign.RIGHT -> x - widthComp.width + LayoutAlign.CENTER -> x - max / 2 + LayoutAlign.RIGHT -> x - max }) } } - private infix fun WidthComponent.plusWithAlign(other: WidthComponent) = plusWithAlign(LayoutAlign.CENTER, other) + private infix fun WidthComponent.plusWithAlign(other: WidthComponent) = plusWithAlign(lineAlign, other) private fun WidthComponent.plusWithAlign(align: LayoutAlign, other: WidthComponent) = when (align) { LayoutAlign.LEFT -> this + (-width).toSpaceComponent() + other LayoutAlign.CENTER -> { diff --git a/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt b/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt index 1511c169..2d64965e 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt @@ -168,7 +168,19 @@ fun Component.split(endWidth: Int, space: Int, charWidth: (Pair) -> b.append(c) } val sb = StringBuilder() + + fun end() { + subBuilder.accept { + style(style).content(sb.toString()) + } + sb.setLength(0) + } + for (codepoint in content().codePoints()) { + if ('\n'.code == codepoint) { + end() + continue + } sb.appendCodePoint(codepoint) i += if (codepoint == ' '.code) 4 else charWidth(style to codepoint) ?: continue i += add @@ -177,12 +189,7 @@ fun Component.split(endWidth: Int, space: Int, charWidth: (Pair) -> else sb.appendCodePoint(TEXT_SPACE_KEY_CODEPOINT) i += space + add } - if (i >= endWidth) { - subBuilder.accept { - style(style).content(sb.toString()) - } - sb.setLength(0) - } + if (i >= endWidth) end() } if (!subBuilder.isClean || sb.isNotEmpty()) target.append(subBuilder.build { style(style).content(sb.toString()) From 25bfa6ea59cb5c9f5337410d4a005848212ee74d Mon Sep 17 00:00:00 2001 From: toxicity Date: Wed, 6 Nov 2024 04:11:56 +0900 Subject: [PATCH 04/10] Typo fix. --- changelog/1.9.md | 9 ++++++++- .../src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/changelog/1.9.md b/changelog/1.9.md index 3aed1102..e4b2bf84 100644 --- a/changelog/1.9.md +++ b/changelog/1.9.md @@ -4,7 +4,7 @@ - Now support about 1.17, 1.17.1 and 1.18.1 is dropped. - BetterHud and my other pugin does NOT support a closed or obfuscated source mod/plugin/modified-client such as ItemsAdder, Optifine and Feather. -## Updates +## Resource pack merge - If your server has Polymer, BetterHud's resource pack will automatically be merged. (Fabric) - If your server has Oraxen, BetterHud's resource pack will automatically be merged. (Bukkit) @@ -14,6 +14,13 @@ - If your server uses Polymer, you have to use '/polymer generate-pack' instead of '/hud reload'. - If your server uses Oraxen, you have to use '/oraxen reload all' instead of '/hud reload'. +## Text layout + +- Add 'line' to use multiple line text. +- Add 'line-width' to define the y location of each line. +- Add 'split-width' to define max length of each line. +- Add 'line-align' to sort each line. + ## Config - Add 'remove-default-hotbar' to disable vanilla hotbar. diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt index 79ab0262..07fcd522 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt @@ -48,7 +48,7 @@ class TextLayout( val line = yamlObject.getAsInt("line", 1).apply { if (this < 1) throw RuntimeException("line cannot be < 1: $s") } - val splitWidth = yamlObject.getAsInt("split-with", 200).apply { + val splitWidth = yamlObject.getAsInt("split-width", 200).apply { if (this < 1) throw RuntimeException("split-width cannot be < 1: $s") } val lineWidth = yamlObject.getAsInt("line-width", 10) From b560cb9bfe91b5ef2650e7c079c1aeec30713851 Mon Sep 17 00:00:00 2001 From: toxicity Date: Thu, 7 Nov 2024 01:05:49 +0900 Subject: [PATCH 05/10] Resource pack server fix. --- .../toxicity/hud/api/manager/TextManager.java | 3 -- changelog/1.9.md | 20 +++++++- .../toxicity/hud/manager/TextManagerImpl.kt | 6 --- .../kr/toxicity/hud/pack/PackGenerator.kt | 2 + .../kotlin/kr/toxicity/hud/util/Adventures.kt | 4 +- dist/src/test/kotlin/AdventureTest.kt | 48 ------------------- 6 files changed, 23 insertions(+), 60 deletions(-) delete mode 100644 dist/src/test/kotlin/AdventureTest.kt diff --git a/api/standard-api/src/main/java/kr/toxicity/hud/api/manager/TextManager.java b/api/standard-api/src/main/java/kr/toxicity/hud/api/manager/TextManager.java index f102fec9..40292884 100644 --- a/api/standard-api/src/main/java/kr/toxicity/hud/api/manager/TextManager.java +++ b/api/standard-api/src/main/java/kr/toxicity/hud/api/manager/TextManager.java @@ -1,7 +1,4 @@ package kr.toxicity.hud.api.manager; -import org.jetbrains.annotations.NotNull; - public interface TextManager { - int getWidth(@NotNull String textName, double scale, @NotNull String text); } diff --git a/changelog/1.9.md b/changelog/1.9.md index e4b2bf84..a1b931a4 100644 --- a/changelog/1.9.md +++ b/changelog/1.9.md @@ -21,10 +21,28 @@ - Add 'split-width' to define max length of each line. - Add 'line-align' to sort each line. +## Placeholder (MythicMobs) +These placeholders are added +- mythicmobs_aura_duration_reversed:arg +- mythicmobs_entity_current_cooldown:arg +- mythicmobs_entity_aura_stack:arg +- mythicmobs_entity_aura_max_duration:arg +- mythicmobs_entity_aura_duration:arg +- mythicmobs_entity_aura_duration_reversed:arg +- mythicmobs_entity_has_aura:arg + +## Bug Fix +- Fix inappropriate space. +- Fix self-host to stop when switch to other pack type. + ## Config - Add 'remove-default-hotbar' to disable vanilla hotbar. - Add 'pack-type: none' to use other resource pack extension. ## Contribute -- These languages are included: ja_JP, vi_VN, zh_CH, zh_TW \ No newline at end of file +These languages are included +- ja_JP +- vi_VN +- zh_CH +- zh_TW \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt index e5cce21e..a1f2066b 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt @@ -636,10 +636,4 @@ object TextManagerImpl : BetterHudManager, TextManager { override fun end() { } - - override fun getWidth(textName: String, scale: Double, text: String): Int = textMap[textName]?.let { - text.codePoints().map { i -> - if (i == ' '.code) 4 else (it.charWidth[i]?.scaledWidth(scale) ?: 0) + 1 - }.sum() - } ?: 0 } \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/pack/PackGenerator.kt b/dist/src/main/kotlin/kr/toxicity/hud/pack/PackGenerator.kt index 1565cd63..1d400e1a 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/pack/PackGenerator.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/pack/PackGenerator.kt @@ -101,6 +101,7 @@ object PackGenerator { get() = Collections.unmodifiableMap(builder.byteArrayMap) override fun close() { + if (PackUploader.stop()) info("Resource pack host is stopped.") } override fun invoke(p1: PackFile) { @@ -132,6 +133,7 @@ object PackGenerator { get() = Collections.unmodifiableMap(builder.byteArrayMap) override fun close() { + if (PackUploader.stop()) info("Resource pack host is stopped.") builder.close() } override fun invoke(p1: PackFile) { diff --git a/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt b/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt index 2d64965e..1c615166 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/util/Adventures.kt @@ -12,6 +12,7 @@ import net.kyori.adventure.text.format.Style import net.kyori.adventure.text.format.TextColor import net.kyori.adventure.text.format.TextDecoration import kotlin.math.abs +import kotlin.math.roundToInt fun createAdventureKey(value: String) = Key.key(NAME_SPACE_ENCODED, value) @@ -175,7 +176,6 @@ fun Component.split(endWidth: Int, space: Int, charWidth: (Pair) -> } sb.setLength(0) } - for (codepoint in content().codePoints()) { if ('\n'.code == codepoint) { end() @@ -189,7 +189,7 @@ fun Component.split(endWidth: Int, space: Int, charWidth: (Pair) -> else sb.appendCodePoint(TEXT_SPACE_KEY_CODEPOINT) i += space + add } - if (i >= endWidth) end() + if (i >= endWidth && (i >= (1.25 * endWidth).roundToInt() || ' '.code == codepoint)) end() } if (!subBuilder.isClean || sb.isNotEmpty()) target.append(subBuilder.build { style(style).content(sb.toString()) diff --git a/dist/src/test/kotlin/AdventureTest.kt b/dist/src/test/kotlin/AdventureTest.kt deleted file mode 100644 index 703b7972..00000000 --- a/dist/src/test/kotlin/AdventureTest.kt +++ /dev/null @@ -1,48 +0,0 @@ -import kr.toxicity.hud.util.split -import net.kyori.adventure.text.Component -import net.kyori.adventure.text.format.NamedTextColor -import net.kyori.adventure.text.minimessage.MiniMessage -import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer -import org.junit.jupiter.api.Test - -class AdventureTest { - @Test - fun testAdventure() { - val serializer = GsonComponentSerializer.gson() - val message = MiniMessage.miniMessage().deserialize("Hello <#FFAC00>world!") -// val message = Component.text() -// .content("He") -// .color(NamedTextColor.GOLD) -// .append( -// Component.text() -// .content("llo") -// .color(NamedTextColor.RED) -// .append( -// Component.text() -// .content(" wor") -// .color(NamedTextColor.AQUA) -// ) -// ) -// .append( -// Component.text() -// .content("ld!") -// .color(NamedTextColor.YELLOW) -// ) -// .build() - - println("# Original") - println(serializer.serializeToTree(message)) - println("# Split") - message.split(20, mapOf( - 'H'.code to 5, - 'e'.code to 5, - 'l'.code to 1, - 'o'.code to 5, - 'w'.code to 5, - 'r'.code to 5, - 'd'.code to 5 - )).forEach { - println(serializer.serializeToTree(it)) - } - } -} \ No newline at end of file From 96ed57615987f5fa90fdb823a3bf0beba25320a5 Mon Sep 17 00:00:00 2001 From: toxicity Date: Thu, 7 Nov 2024 01:06:56 +0900 Subject: [PATCH 06/10] Simple typo fix. --- changelog/1.9.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/changelog/1.9.md b/changelog/1.9.md index a1b931a4..a85ad719 100644 --- a/changelog/1.9.md +++ b/changelog/1.9.md @@ -2,7 +2,7 @@ ## Notice - Now support about 1.17, 1.17.1 and 1.18.1 is dropped. -- BetterHud and my other pugin does NOT support a closed or obfuscated source mod/plugin/modified-client such as ItemsAdder, Optifine and Feather. +- BetterHud and my other plugin does NOT support a closed or obfuscated source mod/plugin/modified-client such as ItemsAdder, Optifine and Feather. ## Resource pack merge @@ -22,7 +22,7 @@ - Add 'line-align' to sort each line. ## Placeholder (MythicMobs) -These placeholders are added +These placeholders are added: - mythicmobs_aura_duration_reversed:arg - mythicmobs_entity_current_cooldown:arg - mythicmobs_entity_aura_stack:arg @@ -41,7 +41,7 @@ These placeholders are added - Add 'pack-type: none' to use other resource pack extension. ## Contribute -These languages are included +These languages are included: - ja_JP - vi_VN - zh_CH From 1b36b512d2a61108366298ba5be805bba1a66924 Mon Sep 17 00:00:00 2001 From: toxicity188 Date: Thu, 7 Nov 2024 13:23:38 +0900 Subject: [PATCH 07/10] add some placeholders. --- .../module/bukkit/BukkitEntityModule.kt | 28 ++++++-------- .../module/bukkit/BukkitStandardModule.kt | 37 ++++++++++++------- .../hud/bootstrap/bukkit/util/Entities.kt | 6 +++ changelog/1.9.md | 12 ++++-- .../kr/toxicity/hud/layout/TextLayout.kt | 2 +- 5 files changed, 50 insertions(+), 35 deletions(-) create mode 100644 bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/util/Entities.kt diff --git a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/module/bukkit/BukkitEntityModule.kt b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/module/bukkit/BukkitEntityModule.kt index 16a4c2bc..0b73c9bd 100644 --- a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/module/bukkit/BukkitEntityModule.kt +++ b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/module/bukkit/BukkitEntityModule.kt @@ -6,11 +6,7 @@ import kr.toxicity.hud.api.placeholder.HudPlaceholder import kr.toxicity.hud.api.update.UpdateEvent import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.bootstrap.bukkit.module.BukkitModule -import kr.toxicity.hud.bootstrap.bukkit.util.ATTRIBUTE_MAX_HEALTH -import kr.toxicity.hud.bootstrap.bukkit.util.bukkitPlayer -import kr.toxicity.hud.bootstrap.bukkit.util.createBukkitTrigger -import kr.toxicity.hud.bootstrap.bukkit.util.unwrap -import org.bukkit.Material +import kr.toxicity.hud.bootstrap.bukkit.util.* import org.bukkit.entity.LivingEntity import org.bukkit.entity.Player import org.bukkit.entity.Projectile @@ -83,7 +79,15 @@ class BukkitEntityModule : BukkitModule { "max_health" to HudPlaceholder.of { _, u -> u.unwrap { e: EntityEvent -> Function { - (e.entity as? LivingEntity)?.getAttribute(ATTRIBUTE_MAX_HEALTH)?.value ?: 0.0 + (e.entity as? LivingEntity)?.maximumHealth ?: 0.0 + } + } + }, + "health_percentage" to HudPlaceholder.of { _, u -> + u.unwrap { e: EntityEvent -> + Function get@ { + val entity = e.entity as? LivingEntity ?: return@get 0.0 + entity.health / entity.maximumHealth } } }, @@ -120,17 +124,7 @@ class BukkitEntityModule : BukkitModule { e.entity.isDead } } - }, - "has_off_hand" to HudPlaceholder.of { _, _ -> - Function { - it.bukkitPlayer.inventory.itemInOffHand.type != Material.AIR - } - }, - "has_main_hand" to HudPlaceholder.of { _, _ -> - Function { - it.bukkitPlayer.inventory.itemInMainHand.type != Material.AIR - } - }, + } ) } \ No newline at end of file diff --git a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/module/bukkit/BukkitStandardModule.kt b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/module/bukkit/BukkitStandardModule.kt index 5fe2ce19..8eb8a3be 100644 --- a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/module/bukkit/BukkitStandardModule.kt +++ b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/module/bukkit/BukkitStandardModule.kt @@ -38,7 +38,7 @@ class BukkitStandardModule : BukkitModule { "health" to { _ -> { HudListener { p -> - p.bukkitPlayer.health / p.bukkitPlayer.getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value + p.bukkitPlayer.health / p.bukkitPlayer.maximumHealth } } }, @@ -46,7 +46,7 @@ class BukkitStandardModule : BukkitModule { { HudListener { p -> (p.bukkitPlayer.vehicle as? LivingEntity)?.let { entity -> - entity.health / entity.getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value + entity.health / entity.maximumHealth } ?: 0.0 } } @@ -82,7 +82,7 @@ class BukkitStandardModule : BukkitModule { "absorption" to { _ -> { HudListener { p -> - p.bukkitPlayer.absorptionAmount / p.bukkitPlayer.getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value + p.bukkitPlayer.absorptionAmount / p.bukkitPlayer.maximumHealth } } }, @@ -121,35 +121,36 @@ class BukkitStandardModule : BukkitModule { }, "max_health" to HudPlaceholder.of { _, _ -> Function { p -> - p.bukkitPlayer.getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value + p.bukkitPlayer.maximumHealth + } + }, + "health_percentage" to HudPlaceholder.of { _, _ -> + Function { p -> + val bukkit = p.bukkitPlayer + bukkit.health / bukkit.maximumHealth } }, "vehicle_max_health" to HudPlaceholder.of { _, _ -> Function { p -> - (p.bukkitPlayer.vehicle as? LivingEntity)?.getAttribute(ATTRIBUTE_MAX_HEALTH)?.value ?: 0.0 + (p.bukkitPlayer.vehicle as? LivingEntity)?.maximumHealth ?: 0.0 } }, "max_health_with_absorption" to HudPlaceholder.of { _, _ -> Function { p -> - p.bukkitPlayer.getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value + p.bukkitPlayer.absorptionAmount + p.bukkitPlayer.maximumHealth + p.bukkitPlayer.absorptionAmount } }, "vehicle_max_health_with_absorption" to HudPlaceholder.of { _, _ -> Function { p -> (p.bukkitPlayer.vehicle as? LivingEntity)?.let { entity -> - entity.getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value + entity.absorptionAmount + entity.maximumHealth + entity.absorptionAmount } ?: 0.0 } }, - "health_percentage" to HudPlaceholder.of { _, _ -> - Function { p -> - p.bukkitPlayer.health / p.bukkitPlayer.getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value * 100.0 - } - }, "vehicle_health_percentage" to HudPlaceholder.of { _, _ -> Function { p -> (p.bukkitPlayer.vehicle as? LivingEntity)?.let { entity -> - entity.health / entity.getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value * 100.0 + entity.health / entity.maximumHealth * 100.0 } ?: 0.0 } }, @@ -254,6 +255,16 @@ class BukkitStandardModule : BukkitModule { p.bukkitPlayer.isFrozen } }, + "has_off_hand" to HudPlaceholder.of { _, _ -> + Function { + it.bukkitPlayer.inventory.itemInOffHand.type != Material.AIR + } + }, + "has_main_hand" to HudPlaceholder.of { _, _ -> + Function { + it.bukkitPlayer.inventory.itemInMainHand.type != Material.AIR + } + }, "has_permission" to object : HudPlaceholder { override fun invoke(args: MutableList, reason: UpdateEvent): Function { return Function { diff --git a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/util/Entities.kt b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/util/Entities.kt new file mode 100644 index 00000000..32fb78f1 --- /dev/null +++ b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/util/Entities.kt @@ -0,0 +1,6 @@ +package kr.toxicity.hud.bootstrap.bukkit.util + +import org.bukkit.entity.LivingEntity + +val LivingEntity.maximumHealth + get() = getAttribute(ATTRIBUTE_MAX_HEALTH)!!.value \ No newline at end of file diff --git a/changelog/1.9.md b/changelog/1.9.md index a85ad719..b4ef41e9 100644 --- a/changelog/1.9.md +++ b/changelog/1.9.md @@ -5,7 +5,6 @@ - BetterHud and my other plugin does NOT support a closed or obfuscated source mod/plugin/modified-client such as ItemsAdder, Optifine and Feather. ## Resource pack merge - - If your server has Polymer, BetterHud's resource pack will automatically be merged. (Fabric) - If your server has Oraxen, BetterHud's resource pack will automatically be merged. (Bukkit) @@ -15,12 +14,18 @@ - If your server uses Oraxen, you have to use '/oraxen reload all' instead of '/hud reload'. ## Text layout - - Add 'line' to use multiple line text. - Add 'line-width' to define the y location of each line. - Add 'split-width' to define max length of each line. - Add 'line-align' to sort each line. +## Placeholder +These placeholders are added: +- has_main_hand +- has_off_hand +- entity_health_percentage +- entity_vehicle_health_percentage + ## Placeholder (MythicMobs) These placeholders are added: - mythicmobs_aura_duration_reversed:arg @@ -31,12 +36,11 @@ These placeholders are added: - mythicmobs_entity_aura_duration_reversed:arg - mythicmobs_entity_has_aura:arg -## Bug Fix +## Bug fix - Fix inappropriate space. - Fix self-host to stop when switch to other pack type. ## Config - - Add 'remove-default-hotbar' to disable vanilla hotbar. - Add 'pack-type: none' to use other resource pack extension. diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt index 07fcd522..db0061f5 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt @@ -48,7 +48,7 @@ class TextLayout( val line = yamlObject.getAsInt("line", 1).apply { if (this < 1) throw RuntimeException("line cannot be < 1: $s") } - val splitWidth = yamlObject.getAsInt("split-width", 200).apply { + val splitWidth = if (line == 1) Int.MAX_VALUE else yamlObject.getAsInt("split-width", 200).apply { if (this < 1) throw RuntimeException("split-width cannot be < 1: $s") } val lineWidth = yamlObject.getAsInt("line-width", 10) From b2a8b67c202b0500313e74bd5d66b95d0290fb29 Mon Sep 17 00:00:00 2001 From: toxicity Date: Sat, 9 Nov 2024 11:22:35 +0900 Subject: [PATCH 08/10] Edit changelog. --- changelog/1.9.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/changelog/1.9.md b/changelog/1.9.md index b4ef41e9..8fb61a4c 100644 --- a/changelog/1.9.md +++ b/changelog/1.9.md @@ -36,9 +36,11 @@ These placeholders are added: - mythicmobs_entity_aura_duration_reversed:arg - mythicmobs_entity_has_aura:arg -## Bug fix +## Fix - Fix inappropriate space. - Fix self-host to stop when switch to other pack type. +- Fix command load problem. +- Fix inappropriate compass location fix. ## Config - Add 'remove-default-hotbar' to disable vanilla hotbar. From 696ba82052b0c4bdd8c4a6c2e9ec14d8ef99763b Mon Sep 17 00:00:00 2001 From: toxicity Date: Sat, 9 Nov 2024 11:44:46 +0900 Subject: [PATCH 09/10] Edit CI. --- .github/workflows/gradle.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 8f105e82..39e7948a 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -3,8 +3,6 @@ name: Java CI with Gradle on: push: branches: [ "master" ] - pull_request: - branches: [ "master" ] permissions: contents: read @@ -26,7 +24,7 @@ jobs: distribution: 'temurin' - name: Grant execute permission for gradlew run: chmod +x gradlew - - name: Build all file. + - name: Build all file run: ./gradlew build --stacktrace - name: Publish to Modrinth run: ./gradlew modrinthPublish --stacktrace From f3707b95ab69818fb94c79ef8b9b09d6033f7f76 Mon Sep 17 00:00:00 2001 From: toxicity Date: Sun, 10 Nov 2024 15:17:23 +0900 Subject: [PATCH 10/10] Restore background function. --- .../oraxen/OraxenCompatibility.kt | 5 +- .../PolymerResourcePackCompatibility.kt | 4 + .../module/fabric/FabricStandardModule.kt | 14 ++- build.gradle.kts | 4 +- changelog/1.9.md | 51 +++++++- .../kotlin/kr/toxicity/hud/BetterHudImpl.kt | 2 +- .../hud/background/BackgroundImage.kt | 33 ----- .../toxicity/hud/background/HudBackground.kt | 113 ++---------------- .../kr/toxicity/hud/compass/CompassType.kt | 1 + .../hud/compass/{ => type}/CircleCompass.kt | 3 +- .../hud/component/LayoutComponentContainer.kt | 4 +- .../hud/equation/EquationPairLocation.kt | 2 +- .../kotlin/kr/toxicity/hud/hud/HudImpl.kt | 2 +- .../kr/toxicity/hud/hud/HudTextElement.kt | 42 ++++++- .../kotlin/kr/toxicity/hud/image/HudImage.kt | 1 + .../kr/toxicity/hud/image/LocationGroup.kt | 9 -- .../hud/image/{ => enums}/ImageType.kt | 2 +- .../hud/image/{ => enums}/SplitType.kt | 4 +- .../toxicity/hud/layout/BackgroundLayout.kt | 12 ++ .../kr/toxicity/hud/layout/HeadLayout.kt | 1 + .../kr/toxicity/hud/layout/LayoutGroup.kt | 4 +- .../kr/toxicity/hud/layout/TextLayout.kt | 11 +- .../hud/layout/{ => enums}/LayoutAlign.kt | 2 +- .../hud/layout/{ => enums}/LayoutOffset.kt | 2 +- .../kr/toxicity/hud/location/LocationGroup.kt | 6 + .../{ => animation}/AnimationLocation.kt | 3 +- .../location/{ => animation}/AnimationType.kt | 2 +- .../toxicity/hud/manager/BackgroundManager.kt | 39 ++++-- .../kr/toxicity/hud/manager/CommandManager.kt | 8 ++ .../kr/toxicity/hud/manager/ImageManager.kt | 4 +- .../toxicity/hud/manager/TextManagerImpl.kt | 5 +- .../kotlin/kr/toxicity/hud/popup/PopupImpl.kt | 1 - .../kr/toxicity/hud/popup/PopupLayout.kt | 47 +++++++- .../hud/renderer/BackgroundRenderer.kt | 31 ----- .../kr/toxicity/hud/renderer/HeadRenderer.kt | 2 +- .../kr/toxicity/hud/renderer/TextRenderer.kt | 25 +++- .../kr/toxicity/hud/text/BackgroundKey.kt | 7 +- .../kr/toxicity/hud/text/HudTextData.kt | 4 +- .../kotlin/kr/toxicity/hud/util/Functions.kt | 2 +- 39 files changed, 275 insertions(+), 239 deletions(-) delete mode 100644 dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt rename dist/src/main/kotlin/kr/toxicity/hud/compass/{ => type}/CircleCompass.kt (99%) delete mode 100644 dist/src/main/kotlin/kr/toxicity/hud/image/LocationGroup.kt rename dist/src/main/kotlin/kr/toxicity/hud/image/{ => enums}/ImageType.kt (98%) rename dist/src/main/kotlin/kr/toxicity/hud/image/{ => enums}/SplitType.kt (97%) create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/layout/BackgroundLayout.kt rename dist/src/main/kotlin/kr/toxicity/hud/layout/{ => enums}/LayoutAlign.kt (61%) rename dist/src/main/kotlin/kr/toxicity/hud/layout/{ => enums}/LayoutOffset.kt (61%) create mode 100644 dist/src/main/kotlin/kr/toxicity/hud/location/LocationGroup.kt rename dist/src/main/kotlin/kr/toxicity/hud/location/{ => animation}/AnimationLocation.kt (92%) rename dist/src/main/kotlin/kr/toxicity/hud/location/{ => animation}/AnimationType.kt (55%) delete mode 100644 dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt diff --git a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/oraxen/OraxenCompatibility.kt b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/oraxen/OraxenCompatibility.kt index 7a5322b8..c58cc2f5 100644 --- a/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/oraxen/OraxenCompatibility.kt +++ b/bootstrap/bukkit/src/main/kotlin/kr/toxicity/hud/bootstrap/bukkit/compatibility/oraxen/OraxenCompatibility.kt @@ -45,6 +45,9 @@ class OraxenCompatibility : Compatibility { } else -> warn("Unknown Oraxen Version.") } - info("Successfully handle Oraxen $version.") + info( + "BetterHud hooks Oraxen $version.", + "Be sure to set 'pack-type' to 'none' in your config." + ) } } \ No newline at end of file diff --git a/bootstrap/fabric/src/main/kotlin/kr/toxicity/hud/bootstrap/fabric/compatibility/PolymerResourcePackCompatibility.kt b/bootstrap/fabric/src/main/kotlin/kr/toxicity/hud/bootstrap/fabric/compatibility/PolymerResourcePackCompatibility.kt index 21394527..7cfc8ed9 100644 --- a/bootstrap/fabric/src/main/kotlin/kr/toxicity/hud/bootstrap/fabric/compatibility/PolymerResourcePackCompatibility.kt +++ b/bootstrap/fabric/src/main/kotlin/kr/toxicity/hud/bootstrap/fabric/compatibility/PolymerResourcePackCompatibility.kt @@ -49,5 +49,9 @@ class PolymerResourcePackCompatibility : Compatibility { is OnReload -> warn("This mod is still on reload!") } } + info( + "BetterHud hooks Polymer resource pack.", + "Be sure to set 'pack-type' to 'none' in your config." + ) } } \ No newline at end of file diff --git a/bootstrap/fabric/src/main/kotlin/kr/toxicity/hud/bootstrap/fabric/module/fabric/FabricStandardModule.kt b/bootstrap/fabric/src/main/kotlin/kr/toxicity/hud/bootstrap/fabric/module/fabric/FabricStandardModule.kt index 5a9f7b53..018baf37 100644 --- a/bootstrap/fabric/src/main/kotlin/kr/toxicity/hud/bootstrap/fabric/module/fabric/FabricStandardModule.kt +++ b/bootstrap/fabric/src/main/kotlin/kr/toxicity/hud/bootstrap/fabric/module/fabric/FabricStandardModule.kt @@ -13,6 +13,7 @@ import kr.toxicity.hud.bootstrap.fabric.module.Module import net.minecraft.resources.ResourceLocation import net.minecraft.world.entity.LivingEntity import net.minecraft.world.entity.ai.attributes.Attributes +import net.minecraft.world.item.ItemStack import java.util.function.Function class FabricStandardModule : Module { @@ -206,8 +207,17 @@ class FabricStandardModule : Module { it.fabricPlayer.hasPermission(args[0]) } } - override fun getRequiredArgsLength(): Int = 1 - } + }, + "has_main_hand" to HudPlaceholder.of { _, _ -> + Function { p -> + !p.fabricPlayer.mainHandItem.`is`(ItemStack.EMPTY.item) + } + }, + "has_off_hand" to HudPlaceholder.of { _, _ -> + Function { p -> + !p.fabricPlayer.offhandItem.`is`(ItemStack.EMPTY.item) + } + }, ) } \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index f2ef2a46..0b6eb2a2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -31,7 +31,7 @@ val platform = "4.3.4" val targetJavaVersion = 21 val velocity = "3.4.0" val bStats = "3.1.0" -val betterCommand = "1.1" +val betterCommand = "1.2" val supportedMinecraftVersions = listOf( //1.17 @@ -95,7 +95,7 @@ allprojects { apply(plugin = "org.jetbrains.dokka") group = "kr.toxicity.hud" - version = "1.8" + (System.getenv("BUILD_NUMBER")?.let { ".DEV-$it" } ?: "") + version = "1.9" + (System.getenv("BUILD_NUMBER")?.let { ".DEV-$it" } ?: "") repositories { mavenCentral() diff --git a/changelog/1.9.md b/changelog/1.9.md index 8fb61a4c..96cde10d 100644 --- a/changelog/1.9.md +++ b/changelog/1.9.md @@ -14,18 +14,55 @@ - If your server uses Oraxen, you have to use '/oraxen reload all' instead of '/hud reload'. ## Text layout +![1](https://github.com/user-attachments/assets/2201c5ee-093f-4790-ab55-e0c40bf6a32f) +```yaml +test_text: + texts: + 1: + name: unifont + pattern: Minecraft is a 3D sandbox adventure game developed by Mojang Studios where players can interact with a fully customizable three-dimensional world made of blocks and entities. Its diverse gameplay options allow players to choose the way they play, creating countless possibilities. + align: center + line-align: left + scale: 0.5 + line: 5 + split-width: 300 + y: 32 +``` +![2](https://github.com/user-attachments/assets/ff220742-ecf0-4239-bc2b-be172ac2cc24) +```yaml +test_text: + texts: + 1: + name: unifont + pattern: | + Background test + Background test2 3 34 23423 + background: test + align: center + line-align: center + scale: 0.5 + line: 5 + split-width: 300 + y: 32 +``` - Add 'line' to use multiple line text. - Add 'line-width' to define the y location of each line. - Add 'split-width' to define max length of each line. - Add 'line-align' to sort each line. +- BetterHud uses '\n' to split text line. -## Placeholder +## Placeholder (Bukkit) These placeholders are added: - has_main_hand - has_off_hand - entity_health_percentage - entity_vehicle_health_percentage +## Placeholder (Fabric) +These placeholders are added: +- has_main_hand +- has_off_hand + ## Placeholder (MythicMobs) These placeholders are added: - mythicmobs_aura_duration_reversed:arg @@ -37,15 +74,23 @@ These placeholders are added: - mythicmobs_entity_has_aura:arg ## Fix -- Fix inappropriate space. +- Fix inappropriate text space. - Fix self-host to stop when switch to other pack type. - Fix command load problem. -- Fix inappropriate compass location fix. +- Fix inappropriate compass location. +- Fix no mysql connector library found in Fabric and Velocity. + +## Improve +- Improve compass movement. ## Config - Add 'remove-default-hotbar' to disable vanilla hotbar. - Add 'pack-type: none' to use other resource pack extension. +## Library +- Now BetterHud uses BetterCommand 1.2. +- Clean jar file to use runtime library injector. + ## Contribute These languages are included: - ja_JP diff --git a/dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt index 355ffbca..eb542680 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt @@ -49,7 +49,7 @@ class BetterHudImpl(val bootstrap: BetterHudBootstrap): BetterHud { PlaceholderManagerImpl, TriggerManagerImpl, - //BackgroundManager, //TODO Fix this + BackgroundManager, ImageManager, TextManagerImpl, PlayerHeadManager, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt b/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt deleted file mode 100644 index f7aa8694..00000000 --- a/dist/src/main/kotlin/kr/toxicity/hud/background/BackgroundImage.kt +++ /dev/null @@ -1,33 +0,0 @@ -package kr.toxicity.hud.background - -import kr.toxicity.hud.image.LoadedImage -import kr.toxicity.hud.util.ifNull -import kr.toxicity.hud.util.removeEmptySide -import kr.toxicity.hud.util.removeEmptyWidth -import java.awt.image.BufferedImage - -//TODO Fix this -class BackgroundImage( - val first: LoadedImage, - val second: LoadedImage, - val third: LoadedImage -) { - companion object { - fun splitOf(line: Int, image: BufferedImage): List { - if (line < 1) throw RuntimeException("line cannot be < 1") - val finalLine = line.coerceAtMost(3) - val load = image.removeEmptySide().ifNull("Empty image.").image - - val width = load.width - val height = load.height - - val widthSplit = (width.toDouble() / 3.0).toInt() - val heightSplit = (height.toDouble() / finalLine.toDouble()).toInt() - - val widthMod = width % widthSplit - val heightMod = height % heightSplit - - return listOf() - } - } -} \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt b/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt index c365466a..36c7a9b9 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/background/HudBackground.kt @@ -1,114 +1,17 @@ package kr.toxicity.hud.background -import kr.toxicity.hud.api.component.WidthComponent -import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.configuration.HudConfiguration -import kr.toxicity.hud.hud.HudImpl -import kr.toxicity.hud.image.NamedLoadedImage import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.pack.PackGenerator -import kr.toxicity.hud.renderer.BackgroundRenderer -import kr.toxicity.hud.resource.GlobalResource -import kr.toxicity.hud.shader.HudShader -import kr.toxicity.hud.util.* -import net.kyori.adventure.text.Component -import java.io.File -import kotlin.math.roundToInt +import kr.toxicity.hud.image.LoadedImage -//TODO Fix this +//TODO replace it to proper background in the future. class HudBackground( override val path: String, - private val id: String, - private val yamlObject: YamlObject -) : HudConfiguration { + val name: String, - private val lineMap = HashMap>() + val left: LoadedImage, + val right: LoadedImage, + val body: LoadedImage, - private val location = PixelLocation(yamlObject) - private val imageBuilder = BackgroundType.valueOf(yamlObject.getAsString("type", "auto").uppercase()) - - fun generateImage(resource: GlobalResource, line: Int, index: Int): ImageTriple { - val triple = lineMap.computeIfAbsent(line) { - val image = imageBuilder.parse(id, line, yamlObject) - val leftX = image.maxOf { - it.first.xOffset - } - val nameMap = run { - val list = ArrayList() - var i = 0 - image.forEach { - list.add(ImageTriple( - leftX, - it.first.toNamed("background_${id}_${line}_${++i}"), - it.second.toNamed("background_${id}_${line}_${++i}"), - it.third.toNamed("background_${id}_${line}_${++i}") - )) - } - list - } - nameMap.forEach { - it.forEach { image -> - PackGenerator.addTask(resource.textures + image.name) { - image.image.image.toByteArray() - } - } - } - nameMap - } - return if (index == 0) triple.first() else if (index == triple.lastIndex) triple.last() else triple[1.coerceAtMost(triple.lastIndex)] - } - - inner class ImageTriple( - private val max: Int, - private val first: NamedLoadedImage, - private val second: NamedLoadedImage, - private val third: NamedLoadedImage - ) : Iterable { - override fun iterator(): Iterator = listOf(first, second, third).iterator() -// fun toBackground(shaders: HudShader, y: Int, scale: Double): BackgroundRenderer { -// var char = start -// fun NamedLoadedImage.toWidth(): WidthComponent { -// val c = (++char).parseChar() -// val height = (image.image.height.toDouble() * scale).roundToInt() -// val div = height.toDouble() / image.image.height.toDouble() -// HudImpl.createBit(shaders, y + location.y) { -// json.add(jsonObjectOf( -// "type" to "bitmap", -// "file" to "$NAME_SPACE_ENCODED:$name", -// "ascent" to it, -// "height" to height, -// "chars" to jsonArrayOf(c) -// )) -// } -// return WidthComponent(Component.text().content(c), (image.image.width * div).roundToInt()) -// } -// return BackgroundRenderer( -// location.x, -// BackgroundRenderer.BackgroundComponent( -// max, -// first.toWidth(), -// second.toWidth(), -// third.toWidth() -// ) -// ) -// } - } - - enum class BackgroundType { - AUTO { - override fun parse(id: String, line: Int, yamlObject: YamlObject): List { - val image = File(DATA_FOLDER.subFolder("assets"), yamlObject.get("file") - .ifNull("value 'file' not set in $id") - .asString() - .replace('/', File.separatorChar)) - .ifNotExist { - "This file doesn't exist: $id in $name" - } - .toImage() - return BackgroundImage.splitOf(line, image) - } - } - ; - abstract fun parse(id: String, line: Int, yamlObject: YamlObject): List - } -} \ No newline at end of file + val location: PixelLocation, +) : HudConfiguration \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/compass/CompassType.kt b/dist/src/main/kotlin/kr/toxicity/hud/compass/CompassType.kt index 2cd6f740..5a69b38b 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/compass/CompassType.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/compass/CompassType.kt @@ -1,6 +1,7 @@ package kr.toxicity.hud.compass import kr.toxicity.hud.api.yaml.YamlObject +import kr.toxicity.hud.compass.type.CircleCompass import kr.toxicity.hud.resource.GlobalResource import java.io.File diff --git a/dist/src/main/kotlin/kr/toxicity/hud/compass/CircleCompass.kt b/dist/src/main/kotlin/kr/toxicity/hud/compass/type/CircleCompass.kt similarity index 99% rename from dist/src/main/kotlin/kr/toxicity/hud/compass/CircleCompass.kt rename to dist/src/main/kotlin/kr/toxicity/hud/compass/type/CircleCompass.kt index 706c3ce1..8e32be35 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/compass/CircleCompass.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/compass/type/CircleCompass.kt @@ -1,4 +1,4 @@ -package kr.toxicity.hud.compass +package kr.toxicity.hud.compass.type import com.google.gson.JsonArray import kr.toxicity.hud.api.component.WidthComponent @@ -6,6 +6,7 @@ import kr.toxicity.hud.api.configuration.HudObjectType import kr.toxicity.hud.api.player.HudPlayer import kr.toxicity.hud.api.update.UpdateEvent import kr.toxicity.hud.api.yaml.YamlObject +import kr.toxicity.hud.compass.CompassImpl import kr.toxicity.hud.equation.TEquation import kr.toxicity.hud.hud.HudImpl import kr.toxicity.hud.location.PixelLocation diff --git a/dist/src/main/kotlin/kr/toxicity/hud/component/LayoutComponentContainer.kt b/dist/src/main/kotlin/kr/toxicity/hud/component/LayoutComponentContainer.kt index 31523bfc..60079257 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/component/LayoutComponentContainer.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/component/LayoutComponentContainer.kt @@ -2,8 +2,8 @@ package kr.toxicity.hud.component import kr.toxicity.hud.api.component.PixelComponent import kr.toxicity.hud.api.component.WidthComponent -import kr.toxicity.hud.layout.LayoutAlign -import kr.toxicity.hud.layout.LayoutOffset +import kr.toxicity.hud.layout.enums.LayoutAlign +import kr.toxicity.hud.layout.enums.LayoutOffset import kr.toxicity.hud.util.EMPTY_WIDTH_COMPONENT import kr.toxicity.hud.util.toSpaceComponent diff --git a/dist/src/main/kotlin/kr/toxicity/hud/equation/EquationPairLocation.kt b/dist/src/main/kotlin/kr/toxicity/hud/equation/EquationPairLocation.kt index 29b73a93..50914321 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/equation/EquationPairLocation.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/equation/EquationPairLocation.kt @@ -2,7 +2,7 @@ package kr.toxicity.hud.equation import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.image.LocationGroup +import kr.toxicity.hud.location.LocationGroup import kr.toxicity.hud.location.GuiLocation class EquationPairLocation( diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt index b52d2749..4f53ce2b 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt @@ -9,7 +9,7 @@ import kr.toxicity.hud.api.update.UpdateEvent import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.configuration.HudConfiguration import kr.toxicity.hud.location.PixelLocation -import kr.toxicity.hud.location.AnimationType +import kr.toxicity.hud.location.animation.AnimationType import kr.toxicity.hud.manager.ConfigManagerImpl import kr.toxicity.hud.manager.LayoutManager import kr.toxicity.hud.manager.ShaderManagerImpl diff --git a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt index 3152c0a8..38344028 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt @@ -4,6 +4,8 @@ import kr.toxicity.hud.api.component.PixelComponent import kr.toxicity.hud.api.component.WidthComponent import kr.toxicity.hud.api.player.HudPlayer import kr.toxicity.hud.api.update.UpdateEvent +import kr.toxicity.hud.image.LoadedImage +import kr.toxicity.hud.layout.BackgroundLayout import kr.toxicity.hud.layout.TextLayout import kr.toxicity.hud.location.GuiLocation import kr.toxicity.hud.location.PixelLocation @@ -15,8 +17,10 @@ import kr.toxicity.hud.renderer.TextRenderer import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.shader.HudShader import kr.toxicity.hud.shader.ShaderGroup +import kr.toxicity.hud.text.BackgroundKey import kr.toxicity.hud.text.HudTextData import kr.toxicity.hud.util.* +import net.kyori.adventure.text.Component import kotlin.math.roundToInt class HudTextElement( @@ -87,7 +91,43 @@ class HudTextElement( PackGenerator.addTask(resource.font + "$textEncoded.json") { jsonObjectOf("providers" to array).toByteArray() } - key.apply { + BackgroundKey( + key, + //TODO replace it to proper background in the future. + text.background?.let { + fun getString(image: LoadedImage, file: String): WidthComponent { + val result = (++textIndex).parseChar() + val height = (image.image.height.toDouble() * text.backgroundScale).roundToInt() + val div = height.toDouble() / image.image.height + HudImpl.createBit(HudShader( + gui, + text.renderScale, + text.layer - 1, + false, + loc.opacity * it.location.opacity, + text.property + ), loc.y + it.location.y + lineIndex * text.lineWidth) { y -> + array.add(jsonObjectOf( + "type" to "bitmap", + "file" to "$NAME_SPACE_ENCODED:$file.png", + "ascent" to y, + "height" to height, + "chars" to jsonArrayOf(result) + )) + } + return WidthComponent(Component.text() + .font(key) + .content(result) + .append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt()) + } + BackgroundLayout( + it.location.x, + getString(it.left, "background_${it.name}_left".encodeKey()), + getString(it.right, "background_${it.name}_right".encodeKey()), + getString(it.body, "background_${it.name}_body".encodeKey()) + ) + } + ).apply { TextManagerImpl.setKey(group, this) } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/image/HudImage.kt b/dist/src/main/kotlin/kr/toxicity/hud/image/HudImage.kt index 94fb548c..791f446e 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/image/HudImage.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/image/HudImage.kt @@ -2,6 +2,7 @@ package kr.toxicity.hud.image import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.configuration.HudConfiguration +import kr.toxicity.hud.image.enums.ImageType import kr.toxicity.hud.manager.ListenerManagerImpl import kr.toxicity.hud.util.toConditions diff --git a/dist/src/main/kotlin/kr/toxicity/hud/image/LocationGroup.kt b/dist/src/main/kotlin/kr/toxicity/hud/image/LocationGroup.kt deleted file mode 100644 index 89a1f7e4..00000000 --- a/dist/src/main/kotlin/kr/toxicity/hud/image/LocationGroup.kt +++ /dev/null @@ -1,9 +0,0 @@ -package kr.toxicity.hud.image - -import kr.toxicity.hud.location.GuiLocation -import kr.toxicity.hud.location.PixelLocation - -data class LocationGroup( - val gui: GuiLocation, - val pixel: PixelLocation -) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/image/ImageType.kt b/dist/src/main/kotlin/kr/toxicity/hud/image/enums/ImageType.kt similarity index 98% rename from dist/src/main/kotlin/kr/toxicity/hud/image/ImageType.kt rename to dist/src/main/kotlin/kr/toxicity/hud/image/enums/ImageType.kt index a95b579c..f13097aa 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/image/ImageType.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/image/enums/ImageType.kt @@ -1,4 +1,4 @@ -package kr.toxicity.hud.image +package kr.toxicity.hud.image.enums import kr.toxicity.hud.api.component.PixelComponent import kr.toxicity.hud.api.listener.HudListener diff --git a/dist/src/main/kotlin/kr/toxicity/hud/image/SplitType.kt b/dist/src/main/kotlin/kr/toxicity/hud/image/enums/SplitType.kt similarity index 97% rename from dist/src/main/kotlin/kr/toxicity/hud/image/SplitType.kt rename to dist/src/main/kotlin/kr/toxicity/hud/image/enums/SplitType.kt index d3d05a56..9b003a53 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/image/SplitType.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/image/enums/SplitType.kt @@ -1,5 +1,7 @@ -package kr.toxicity.hud.image +package kr.toxicity.hud.image.enums +import kr.toxicity.hud.image.LoadedImage +import kr.toxicity.hud.image.NamedLoadedImage import kr.toxicity.hud.util.circleCut import kr.toxicity.hud.util.removeEmptyWidth import java.awt.AlphaComposite diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/BackgroundLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/BackgroundLayout.kt new file mode 100644 index 00000000..d84eaf26 --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/BackgroundLayout.kt @@ -0,0 +1,12 @@ +package kr.toxicity.hud.layout + +import kr.toxicity.hud.api.component.WidthComponent + +//TODO replace it to proper background in the future. +class BackgroundLayout( + val x: Int, + + val left: WidthComponent, + val right: WidthComponent, + val body: WidthComponent +) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/HeadLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/HeadLayout.kt index eb98c927..14c356c9 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/HeadLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/HeadLayout.kt @@ -1,6 +1,7 @@ package kr.toxicity.hud.layout import kr.toxicity.hud.api.yaml.YamlObject +import kr.toxicity.hud.layout.enums.LayoutAlign import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.manager.PlayerHeadManager import kr.toxicity.hud.player.head.HeadRenderType diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutGroup.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutGroup.kt index 94d3521c..6adf9ef9 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutGroup.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutGroup.kt @@ -2,7 +2,9 @@ package kr.toxicity.hud.layout import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.configuration.HudConfiguration -import kr.toxicity.hud.location.AnimationLocation +import kr.toxicity.hud.layout.enums.LayoutAlign +import kr.toxicity.hud.layout.enums.LayoutOffset +import kr.toxicity.hud.location.animation.AnimationLocation import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.util.* import net.kyori.adventure.audience.Audience diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt index db0061f5..4b94e315 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/TextLayout.kt @@ -1,8 +1,11 @@ package kr.toxicity.hud.layout import kr.toxicity.hud.api.yaml.YamlObject +import kr.toxicity.hud.background.HudBackground import kr.toxicity.hud.equation.TEquation +import kr.toxicity.hud.layout.enums.LayoutAlign import kr.toxicity.hud.location.PixelLocation +import kr.toxicity.hud.manager.BackgroundManager import kr.toxicity.hud.manager.ConfigManagerImpl import kr.toxicity.hud.manager.TextManagerImpl import kr.toxicity.hud.text.HudText @@ -32,10 +35,10 @@ class TextLayout( DecimalFormat(it) } ?: ConfigManagerImpl.numberFormat val disableNumberFormat: Boolean = yamlObject.getAsBoolean("disable-number-format", true) -// val background: HudBackground? = yamlObject.get("background")?.asString()?.let { -// BackgroundManager.getBackground(it) -// } -// val backgroundScale: Double = yamlObject.getAsDouble("background-scale", scale) + val background: HudBackground? = yamlObject.get("background")?.asString()?.let { + BackgroundManager.getBackground(it) + } + val backgroundScale: Double = yamlObject.getAsDouble("background-scale", scale) val emojiLocation: PixelLocation = yamlObject.get("emoji-pixel")?.asObject()?.let { PixelLocation(it) } ?: PixelLocation.zero diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutAlign.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/enums/LayoutAlign.kt similarity index 61% rename from dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutAlign.kt rename to dist/src/main/kotlin/kr/toxicity/hud/layout/enums/LayoutAlign.kt index 1c2be3da..32e0278a 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutAlign.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/enums/LayoutAlign.kt @@ -1,4 +1,4 @@ -package kr.toxicity.hud.layout +package kr.toxicity.hud.layout.enums enum class LayoutAlign { LEFT, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutOffset.kt b/dist/src/main/kotlin/kr/toxicity/hud/layout/enums/LayoutOffset.kt similarity index 61% rename from dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutOffset.kt rename to dist/src/main/kotlin/kr/toxicity/hud/layout/enums/LayoutOffset.kt index dff7d076..b8536884 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/layout/LayoutOffset.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/layout/enums/LayoutOffset.kt @@ -1,4 +1,4 @@ -package kr.toxicity.hud.layout +package kr.toxicity.hud.layout.enums enum class LayoutOffset { LEFT, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/location/LocationGroup.kt b/dist/src/main/kotlin/kr/toxicity/hud/location/LocationGroup.kt new file mode 100644 index 00000000..6d90dd21 --- /dev/null +++ b/dist/src/main/kotlin/kr/toxicity/hud/location/LocationGroup.kt @@ -0,0 +1,6 @@ +package kr.toxicity.hud.location + +data class LocationGroup( + val gui: GuiLocation, + val pixel: PixelLocation +) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/location/AnimationLocation.kt b/dist/src/main/kotlin/kr/toxicity/hud/location/animation/AnimationLocation.kt similarity index 92% rename from dist/src/main/kotlin/kr/toxicity/hud/location/AnimationLocation.kt rename to dist/src/main/kotlin/kr/toxicity/hud/location/animation/AnimationLocation.kt index 10458ab5..c8b19c70 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/location/AnimationLocation.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/location/animation/AnimationLocation.kt @@ -1,7 +1,8 @@ -package kr.toxicity.hud.location +package kr.toxicity.hud.location.animation import kr.toxicity.hud.api.yaml.YamlObject import kr.toxicity.hud.equation.EquationTriple +import kr.toxicity.hud.location.PixelLocation import kotlin.math.roundToInt class AnimationLocation( diff --git a/dist/src/main/kotlin/kr/toxicity/hud/location/AnimationType.kt b/dist/src/main/kotlin/kr/toxicity/hud/location/animation/AnimationType.kt similarity index 55% rename from dist/src/main/kotlin/kr/toxicity/hud/location/AnimationType.kt rename to dist/src/main/kotlin/kr/toxicity/hud/location/animation/AnimationType.kt index a30adcd9..0571043c 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/location/AnimationType.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/location/animation/AnimationType.kt @@ -1,4 +1,4 @@ -package kr.toxicity.hud.location +package kr.toxicity.hud.location.animation enum class AnimationType { LOOP, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt index 406d8001..e5ecfbe1 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt @@ -1,13 +1,15 @@ package kr.toxicity.hud.manager import kr.toxicity.hud.background.HudBackground +import kr.toxicity.hud.location.PixelLocation +import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.util.* import net.kyori.adventure.audience.Audience +import java.io.File -//TODO Fix this +//TODO replace it to proper background in the future. object BackgroundManager : BetterHudManager { - private val backgroundMap = HashMap() override fun start() { @@ -19,14 +21,31 @@ object BackgroundManager : BetterHudManager { override fun reload(sender: Audience, resource: GlobalResource) { val folder = DATA_FOLDER.subFolder("backgrounds") backgroundMap.clear() - folder.forEachAllYaml(sender) { file, id, yamlObject -> - runWithExceptionHandling(sender, "Unable to load this background: $id in ${file.name}") { - backgroundMap.putSync("background", id) { - HudBackground( - file.path, - id, - yamlObject - ) + folder.forEach { + if (it.extension == "yml") { + runWithExceptionHandling(sender, "Unable to load this yml: ${it.name}") { + val yaml = it.toYaml() + val name = it.nameWithoutExtension + val backgroundFolder = folder.subFolder(name) + fun getImage(imageName: String) = File(backgroundFolder, "$imageName.png") + .ifNotExist("this image doesn't exist: $imageName.png in $name") + .toImage() + .removeEmptyWidth() + .ifNull("this image is empty: $imageName.png in $name").apply { + PackGenerator.addTask(resource.textures + "${"background_${name}_$imageName".encodeKey()}.png") { + image.toByteArray() + } + } + backgroundMap.putSync("background", name) { + HudBackground( + it.path, + name, + getImage("left"), + getImage("right"), + getImage("body"), + PixelLocation(yaml) + ) + } } } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/CommandManager.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/CommandManager.kt index 947ed343..07067fc0 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/CommandManager.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/CommandManager.kt @@ -44,6 +44,14 @@ object CommandManager : BetterHudManager { } } }, MiniMessage.miniMessage(), BOOTSTRAP.logger()) + .exceptionHandler { + if (ConfigManagerImpl.debug) { + warn( + "Stack trace:", + it.stackTraceToString() + ) + } + } .addSerializer(HudPlayerStack::class.java, ClassSerializer.builder { _, s -> if (s == "all") HudPlayerStack(PlayerManagerImpl.allHudPlayer) else PlayerManagerImpl.getHudPlayer(s)?.let { diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/ImageManager.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/ImageManager.kt index 730e3f1e..324e43c7 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/ImageManager.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/ImageManager.kt @@ -2,8 +2,8 @@ package kr.toxicity.hud.manager import kr.toxicity.hud.api.component.WidthComponent import kr.toxicity.hud.image.HudImage -import kr.toxicity.hud.image.ImageType -import kr.toxicity.hud.image.SplitType +import kr.toxicity.hud.image.enums.ImageType +import kr.toxicity.hud.image.enums.SplitType import kr.toxicity.hud.pack.PackGenerator import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.shader.ShaderGroup diff --git a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt index a1f2066b..e9576afd 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/manager/TextManagerImpl.kt @@ -12,7 +12,6 @@ import kr.toxicity.hud.shader.ShaderGroup import kr.toxicity.hud.text.* import kr.toxicity.hud.util.* import net.kyori.adventure.audience.Audience -import net.kyori.adventure.key.Key import java.awt.AlphaComposite import java.awt.Font import java.awt.font.FontRenderContext @@ -60,7 +59,7 @@ object TextManagerImpl : BetterHudManager, TextManager { private val textCacheMap = HashMap() private val textWidthMap = HashMap() - private val textKeyMap = ConcurrentHashMap() + private val textKeyMap = ConcurrentHashMap() private val defaultBitmapImageMap = HashMap() private val translatableString = HashMap>() @@ -130,7 +129,7 @@ object TextManagerImpl : BetterHudManager, TextManager { @Synchronized fun getKey(shaderGroup: ShaderGroup) = textKeyMap[shaderGroup] @Synchronized - fun setKey(shaderGroup: ShaderGroup, key: Key) { + fun setKey(shaderGroup: ShaderGroup, key: BackgroundKey) { textKeyMap[shaderGroup] = key } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt index 17aed9e5..bd3a004c 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt @@ -77,7 +77,6 @@ class PopupImpl( loc += GuiLocation(gui) } PopupLayout( - resource, json, LayoutManager.getLayout(layout).ifNull("this layout doesn't exist: $layout"), this@PopupImpl, diff --git a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt index 3c4ef1a4..ad99a0a3 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/popup/PopupLayout.kt @@ -7,9 +7,11 @@ import kr.toxicity.hud.api.player.HudPlayer import kr.toxicity.hud.api.update.UpdateEvent import kr.toxicity.hud.component.LayoutComponentContainer import kr.toxicity.hud.hud.HudImpl -import kr.toxicity.hud.image.LocationGroup +import kr.toxicity.hud.image.LoadedImage +import kr.toxicity.hud.layout.BackgroundLayout +import kr.toxicity.hud.location.LocationGroup import kr.toxicity.hud.layout.LayoutGroup -import kr.toxicity.hud.location.AnimationType +import kr.toxicity.hud.location.animation.AnimationType import kr.toxicity.hud.location.GuiLocation import kr.toxicity.hud.location.PixelLocation import kr.toxicity.hud.manager.* @@ -20,16 +22,15 @@ import kr.toxicity.hud.player.head.HeadRenderType.STANDARD import kr.toxicity.hud.renderer.HeadRenderer import kr.toxicity.hud.renderer.ImageRenderer import kr.toxicity.hud.renderer.TextRenderer -import kr.toxicity.hud.resource.GlobalResource import kr.toxicity.hud.shader.HudShader import kr.toxicity.hud.shader.ShaderGroup +import kr.toxicity.hud.text.BackgroundKey import kr.toxicity.hud.text.HudTextData import kr.toxicity.hud.util.* import net.kyori.adventure.text.Component import kotlin.math.roundToInt class PopupLayout( - private val resource: GlobalResource, private val json: JsonArray, private val layout: LayoutGroup, private val parent: PopupImpl, @@ -241,7 +242,43 @@ class PopupLayout( PackGenerator.addTask(file + "$textEncoded.json") { jsonObjectOf("providers" to array).toByteArray() } - key.apply { + BackgroundKey( + key, + //TODO replace it to proper background in the future. + textLayout.background?.let { + fun getString(image: LoadedImage, file: String): WidthComponent { + val result = (++imageTextIndex).parseChar() + val height = (image.image.height.toDouble() * textLayout.backgroundScale).roundToInt() + val div = height.toDouble() / image.image.height + HudImpl.createBit(HudShader( + elementGui, + textLayout.renderScale, + textLayout.layer - 1, + false, + pixel.opacity * it.location.opacity, + textLayout.property + ), pixel.y + it.location.y + lineIndex * textLayout.lineWidth) { y -> + array.add(jsonObjectOf( + "type" to "bitmap", + "file" to "$NAME_SPACE_ENCODED:$file.png", + "ascent" to y, + "height" to height, + "chars" to jsonArrayOf(result) + )) + } + return WidthComponent(Component.text() + .font(key) + .content(result) + .append(NEGATIVE_ONE_SPACE_COMPONENT.component), (image.image.width.toDouble() * div).roundToInt()) + } + BackgroundLayout( + it.location.x, + getString(it.left, "background_${it.name}_left".encodeKey()), + getString(it.right, "background_${it.name}_right".encodeKey()), + getString(it.body, "background_${it.name}_body".encodeKey()) + ) + } + ).apply { TextManagerImpl.setKey(group, this) } } diff --git a/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt b/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt deleted file mode 100644 index c64c4c6a..00000000 --- a/dist/src/main/kotlin/kr/toxicity/hud/renderer/BackgroundRenderer.kt +++ /dev/null @@ -1,31 +0,0 @@ -package kr.toxicity.hud.renderer - -import kr.toxicity.hud.api.component.WidthComponent -import kr.toxicity.hud.util.toSpaceComponent - -//TODO Fix this -class BackgroundRenderer( - val x: Int, - private val component: BackgroundComponent -) { - fun build(width: Int): WidthComponent { - var comp = x.toSpaceComponent() - var w = 0 - var children = component.x.toSpaceComponent() + component.start - while (w < width) { - children += component.center - w += component.center.width - } - children += component.end - comp += (-comp.width).toSpaceComponent() + children - return comp - } - - - class BackgroundComponent( - val x: Int, - val start: WidthComponent, - val center: WidthComponent, - val end: WidthComponent - ) -} \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/renderer/HeadRenderer.kt b/dist/src/main/kotlin/kr/toxicity/hud/renderer/HeadRenderer.kt index ec7377f5..2b590053 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/renderer/HeadRenderer.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/renderer/HeadRenderer.kt @@ -5,7 +5,7 @@ import kr.toxicity.hud.api.component.WidthComponent import kr.toxicity.hud.api.player.HudPlayer import kr.toxicity.hud.api.player.HudPlayerHead import kr.toxicity.hud.api.update.UpdateEvent -import kr.toxicity.hud.layout.LayoutAlign +import kr.toxicity.hud.layout.enums.LayoutAlign import kr.toxicity.hud.manager.PlaceholderManagerImpl import kr.toxicity.hud.manager.PlayerHeadManager import kr.toxicity.hud.manager.PlayerManagerImpl diff --git a/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt b/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt index cc3aebce..6a610553 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt @@ -5,7 +5,7 @@ import kr.toxicity.hud.api.component.WidthComponent import kr.toxicity.hud.api.player.HudPlayer import kr.toxicity.hud.api.update.UpdateEvent import kr.toxicity.hud.equation.TEquation -import kr.toxicity.hud.layout.LayoutAlign +import kr.toxicity.hud.layout.enums.LayoutAlign import kr.toxicity.hud.manager.PlaceholderManagerImpl import kr.toxicity.hud.manager.PlayerManagerImpl import kr.toxicity.hud.placeholder.ConditionBuilder @@ -99,13 +99,26 @@ class TextRenderer( val compList = buildPattern(targetHudPlayer) .parseToComponent() .split(data.splitWidth, space, widthViewer) - val max = compList.maxOf { - it.width - } + var max = 0 compList.forEachIndexed { index, comp -> if (data.font.lastIndex < index) return@forEachIndexed - comp.component.font(data.font[index]) - widthComp = if (widthComp.width == 0) comp else widthComp plusWithAlign comp + val backgroundKey = data.font[index] + var finalComp = comp + finalComp.component.font(backgroundKey.key) + //TODO replace it to proper background in the future. + backgroundKey.background?.let { + val builder = Component.text().append(it.left.component) + var length = 0 + while (length < comp.width) { + builder.append(it.body.component) + length += it.body.width + } + val total = it.left.width + length + it.right.width + val minus = -total + (length - comp.width) / 2 + it.left.width - it.x + finalComp = it.x.toSpaceComponent() + WidthComponent(builder.append(it.right.component), total) + minus.toSpaceComponent() + finalComp + } + if (finalComp.width > max) max = finalComp.width + widthComp = if (widthComp.width == 0) finalComp else widthComp plusWithAlign finalComp } widthComp.toPixelComponent(when (align) { LayoutAlign.LEFT -> x diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/BackgroundKey.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/BackgroundKey.kt index 9c2f1226..34e3e1e6 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/text/BackgroundKey.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/BackgroundKey.kt @@ -1,9 +1,10 @@ package kr.toxicity.hud.text -import kr.toxicity.hud.renderer.BackgroundRenderer +import kr.toxicity.hud.layout.BackgroundLayout import net.kyori.adventure.key.Key -data class BackgroundKey( +//TODO replace it to proper background in the future. +class BackgroundKey( val key: Key, - val background: BackgroundRenderer? + val background: BackgroundLayout? ) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt index 2d6b65fa..e1dd190b 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/text/HudTextData.kt @@ -1,9 +1,7 @@ package kr.toxicity.hud.text -import net.kyori.adventure.key.Key - class HudTextData( - val font: List, + val font: List, val imageCodepoint: Map, val splitWidth: Int ) \ No newline at end of file diff --git a/dist/src/main/kotlin/kr/toxicity/hud/util/Functions.kt b/dist/src/main/kotlin/kr/toxicity/hud/util/Functions.kt index e5d849c3..98b51d80 100644 --- a/dist/src/main/kotlin/kr/toxicity/hud/util/Functions.kt +++ b/dist/src/main/kotlin/kr/toxicity/hud/util/Functions.kt @@ -1,7 +1,7 @@ package kr.toxicity.hud.util import kr.toxicity.hud.equation.TEquation -import kr.toxicity.hud.layout.LayoutAlign +import kr.toxicity.hud.layout.enums.LayoutAlign import kr.toxicity.hud.manager.ConfigManagerImpl import net.kyori.adventure.audience.Audience