Something went wrong. Try again.
(WIP) Modern Minecraft server-side HUD framework.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303package net.hudkit.api.preprocess
import net.kyori.adventure.key.Keyimport net.hudkit.api.text.info.CharacterInfoStoreimport net.hudkit.api.text.info.DefaultFontCharacterInfoStoreimport net.hudkit.api.text.info.StaticCharacterInfoStoreimport net.hudkit.api.Constantsimport net.hudkit.api.preprocess.fonts.BitmapFontProviderimport net.hudkit.api.preprocess.fonts.Font
enum class Anchor(val glOffset: IntArray) { TOP_LEFT(intArrayOf(-1, 0)), TOP_CENTER(intArrayOf(0, 0)), TOP_RIGHT(intArrayOf(+1, 0)), MIDDLE_LEFT(intArrayOf(-1, -1)), CENTER(intArrayOf(0, -1)), MIDDLE_RIGHT(intArrayOf(+1, -1)), BOTTOM_LEFT(intArrayOf(-1, -2)), BOTTOM_CENTER(intArrayOf(0, -2)), BOTTOM_RIGHT(intArrayOf(+1, -2));
companion object { fun fromString(value: String): Anchor { return when (value) { "top-left" -> TOP_LEFT "top-center" -> TOP_CENTER "top-right" -> TOP_RIGHT "middle-left" -> MIDDLE_LEFT "center" -> CENTER "middle-right" -> MIDDLE_RIGHT "bottom-left" -> BOTTOM_LEFT "bottom-center" -> BOTTOM_CENTER "bottom-right" -> BOTTOM_RIGHT else -> TOP_LEFT } } }}
enum class IsolateType { TEXT, IMAGE, RECTANGLE, GENERIC}
class IsolateManager(val hudPreprocessor: HUDPreprocessor, val getAsset: (name: String) -> ByteArray) { val isolates = mutableListOf<Isolate>( GenericIsolate(0,0, Anchor.CENTER, 0, hudPreprocessor) ) val rootIsolate: Isolate get() = isolates[0]
@Synchronized fun createIsolate( y: Int? = null, anchor: Anchor? = null, size: Int? = null, type: IsolateType? = null, rounding: Int? = null, pixelation: Float? = null, color: Int? = null, src: String? = null, ascent: Int? = null, inheritedFrom: Isolate = rootIsolate ): Isolate { if (y == null && anchor == null && size == null && type == null && rounding == null && pixelation == null && color == null && src == null && ascent == null) { return inheritedFrom }
// something changed, create a new isolate val index = isolates.size val resolvedY = (y ?: 0) + inheritedFrom.y val resolvedAnchor = anchor ?: inheritedFrom.anchor val resolvedSize = (size ?: 0) + inheritedFrom.size val resolvedType = type ?: inheritedFrom.type val resolvedRounding = rounding ?: (inheritedFrom as? RectangleIsolate)?.rounding ?: 0 val resolvedPixelation = pixelation ?: (inheritedFrom as? RectangleIsolate)?.pixelation ?: 0f val resolvedColor = color ?: (inheritedFrom as? RectangleIsolate)?.color ?: 0x0000007F val resolvedAscent = ascent ?: (inheritedFrom as? ImageIsolate)?.userAscent ?: 0 val src = src ?: ""
val isolate = when (resolvedType) { IsolateType.TEXT -> TextIsolate(index, resolvedY, resolvedAnchor, resolvedSize, hudPreprocessor) IsolateType.IMAGE -> ImageIsolate(index, resolvedY, resolvedAnchor, resolvedSize, hudPreprocessor, src, getAsset, resolvedAscent) IsolateType.RECTANGLE -> RectangleIsolate(index, resolvedY, resolvedAnchor, resolvedSize, hudPreprocessor, resolvedRounding, resolvedPixelation, resolvedColor) else -> GenericIsolate(index, resolvedY, resolvedAnchor, resolvedSize, hudPreprocessor) }
val existing = getIsolateByString(isolate.getIsolateString()) if (existing != null) { return existing }
// register new isolate isolates.add(isolate) isolate.register() return isolate }
fun getIsolateByString(isolateString: String): Isolate? { return isolates.find { it.getIsolateString() == isolateString } }}
/** * Represents a unique HUD isolate. An isolate is internally a font that the shader can isolate and render at a specific position. * * WARNING: Images are not yet supported. * WARNING: Size is currently unstable and may not work as expected. */abstract class Isolate( index: Int, var y: Int, val anchor: Anchor, val size: Int, val hudPreprocessor: HUDPreprocessor) { val yOffset: Int = Constants.getYOffsetFromIndex(index)
abstract val type: IsolateType
open fun register() { val fontPath = "${Constants.ASSETS_DIRECTORY}/${Constants.NAMESPACE}/${Constants.FONTS_DIRECTORY}/${getIsolateString()}.json" hudPreprocessor.addFile(fontPath, getFontJson()) }
open fun getFontJson(): String { val font = Font.minecraftDefault() font.incrementAscent(yOffset) return font.toJsonString() }
abstract fun getCharacterInfoStore(): CharacterInfoStore
open fun getIsolateString(): String { return "hud_${type.name.lowercase()}_${anchor.name.lowercase()}_${y}_$size" }
open fun getIsolateKey(): Key { return Key.key(Constants.NAMESPACE, getIsolateString()) }}
class GenericIsolate(index: Int, y: Int, anchor: Anchor, size: Int, hudPreprocessor: HUDPreprocessor) : Isolate( index, y, anchor, size, hudPreprocessor) { override val type: IsolateType = IsolateType.GENERIC override fun getCharacterInfoStore(): CharacterInfoStore { return StaticCharacterInfoStore(0) }
override fun register() {
}}
class TextIsolate( index: Int, y: Int, anchor: Anchor, size: Int, hudPreprocessor: HUDPreprocessor) : Isolate(index, y, anchor, size, hudPreprocessor) { override val type = IsolateType.TEXT
override fun getCharacterInfoStore(): CharacterInfoStore { return DefaultFontCharacterInfoStore() }}
class ImageIsolate( index: Int, y: Int, anchor: Anchor, size: Int, hudPreprocessor: HUDPreprocessor, val src: String, val getAsset: (name: String) -> ByteArray, val userAscent: Int = 0) : Isolate(index, y, anchor, size, hudPreprocessor) { override val type = IsolateType.IMAGE private var assetData: ByteArray? = null private var imageWidth: Int = 0 private var imageHeight: Int = 0
fun downloadAsset() { val split = src.split("://") if (split.size != 2) { throw IllegalArgumentException("src string contains invalid format") } val protocol = split[0] val path = split[1] when (protocol) { "project" -> { assetData = getAsset(path) // Read image dimensions val inputStream = java.io.ByteArrayInputStream(assetData) val image = javax.imageio.ImageIO.read(inputStream) imageWidth = image.width imageHeight = image.height } else -> throw RuntimeException("Unsupported asset source protocol: $protocol") } }
override fun register() { downloadAsset()
val texturePath = "${Constants.ASSETS_DIRECTORY}/${Constants.NAMESPACE}/textures/${getIsolateString()}.png" hudPreprocessor.addFile(texturePath, assetData!!) super.register() }
override fun getFontJson(): String { val font = Font(arrayOf( BitmapFontProvider( chars = arrayOf("\uE300"), file = "${Constants.NAMESPACE}:${getIsolateString()}.png", ascent = size + userAscent + yOffset, height = size ) )) return font.toJsonString() }
override fun getIsolateString(): String { return "hud_${type.name.lowercase()}_${anchor.name.lowercase()}_${y}_${size}_${userAscent}_${src.split("//")[1]}" }
fun getWidth(): Int { if (imageHeight == 0) return 0 // Calculate width based on aspect ratio: width = originalWidth * (size / originalHeight) return (imageWidth.toFloat() * size.toFloat() / imageHeight.toFloat()).toInt() }
override fun getCharacterInfoStore(): CharacterInfoStore { return StaticCharacterInfoStore(getWidth()) }}
class RectangleIsolate( index: Int, y: Int, anchor: Anchor, size: Int, hudPreprocessor: HUDPreprocessor, val rounding: Int = 0, val pixelation: Float = 0f, val color: Int = 0x0000007F) : Isolate(index, y, anchor, size, hudPreprocessor) {
override val type = IsolateType.RECTANGLE
override fun getCharacterInfoStore(): CharacterInfoStore { // Return 0 because CharacterWidthCalculator adds +1 for inter-char spacing. // The createBackground function adds createArbitrarySpace(-1) after the marker // to cancel out the 1-pixel advance, making the net advance 0. // The actual rectangle width is added separately via createArbitrarySpace(width). return StaticCharacterInfoStore(0) }
override fun getIsolateString(): String { return "hud_${type.name.lowercase()}_${anchor.name.lowercase()}_${y}_${size}_r${rounding}_p${pixelation}_${color.toString(16)}" }
override fun register() { super.register() val texturePath = "${Constants.ASSETS_DIRECTORY}/${Constants.NAMESPACE}/textures/${getIsolateString()}.png" hudPreprocessor.addFile(texturePath, createColoredPng()) }
override fun getFontJson(): String { val font = Font(arrayOf( BitmapFontProvider( chars = arrayOf("\uE300"), file = "${Constants.NAMESPACE}:${getIsolateString()}.png", ascent = yOffset, height = 1 ) )) return font.toJsonString() }
private fun createColoredPng(): ByteArray { val image = java.awt.image.BufferedImage(1, 1, java.awt.image.BufferedImage.TYPE_INT_ARGB) // Convert from RRGGBBAA to AARRGGBB (Java's ARGB format) val r = (color shr 24) and 0xFF val g = (color shr 16) and 0xFF val b = (color shr 8) and 0xFF val a = color and 0xFF val argb = (a shl 24) or (r shl 16) or (g shl 8) or b image.setRGB(0, 0, argb) val outputStream = java.io.ByteArrayOutputStream() javax.imageio.ImageIO.write(image, "png", outputStream) return outputStream.toByteArray() }}