|
| 1 | +package gg.grounds.proxy.velocity.motd |
| 2 | + |
| 3 | +import com.google.gson.JsonParser |
| 4 | +import com.velocitypowered.api.proxy.server.ServerPing |
| 5 | +import net.kyori.adventure.text.Component |
| 6 | +import net.kyori.adventure.text.ObjectComponent |
| 7 | +import net.kyori.adventure.text.TextComponent |
| 8 | +import net.kyori.adventure.text.format.Style |
| 9 | +import net.kyori.adventure.text.format.TextDecoration |
| 10 | +import net.kyori.adventure.text.`object`.SpriteObjectContents |
| 11 | +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer |
| 12 | +import org.junit.jupiter.api.Assertions.* |
| 13 | +import org.junit.jupiter.api.Test |
| 14 | + |
| 15 | +class NativeMotdSpriteTest { |
| 16 | + private val document = |
| 17 | + JsonParser.parseString( |
| 18 | + """{"nodes":[{"type":"group","style":{"color":"#ffffff","shadowColor":0},"children":[{"type":"text","value":"A"},{"type":"sprite","atlas":"minecraft:gui","sprite":"minecraft:icon/trial_available"},{"type":"text","value":"B"}]}]}""" |
| 19 | + ) |
| 20 | + .asJsonObject |
| 21 | + |
| 22 | + private fun sprites(node: Component): List<ObjectComponent> = |
| 23 | + (if (node is ObjectComponent) listOf(node) else emptyList()) + |
| 24 | + node.children().flatMap(::sprites) |
| 25 | + |
| 26 | + @Test |
| 27 | + fun `typed source compiles to native sprite and preserves transparent shadow`() { |
| 28 | + val result = CompiledServerText(document).render(emptyMap()) |
| 29 | + val sprite = sprites(result).single() |
| 30 | + assertTrue(sprite.contents() is SpriteObjectContents) |
| 31 | + fun shadow(c: Component, inherited: Style): Int? { |
| 32 | + val style = c.style().merge(inherited, Style.Merge.Strategy.IF_ABSENT_ON_TARGET) |
| 33 | + return if (c is ObjectComponent) style.shadowColor()?.value() |
| 34 | + else c.children().firstNotNullOfOrNull { shadow(it, style) } |
| 35 | + } |
| 36 | + assertEquals(0, shadow(result, Style.empty())) |
| 37 | + assertEquals( |
| 38 | + "minecraft:icon/trial_available", |
| 39 | + (sprite.contents() as SpriteObjectContents).sprite().asString(), |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `native sprite width participates in center alignment`() { |
| 45 | + val result = CompiledMotd(document, listOf("center")).render(emptyMap(), 100) |
| 46 | + var prefix = 0 |
| 47 | + fun walk(c: Component, inherited: Style) { |
| 48 | + val style = c.style().merge(inherited, Style.Merge.Strategy.IF_ABSENT_ON_TARGET) |
| 49 | + if (c is TextComponent) |
| 50 | + for (char in c.content()) if (char == ' ') |
| 51 | + prefix += |
| 52 | + if (style.decoration(TextDecoration.BOLD) == TextDecoration.State.TRUE) 5 |
| 53 | + else 4 |
| 54 | + c.children().forEach { walk(it, style) } |
| 55 | + } |
| 56 | + walk(result, Style.empty()) |
| 57 | + assertEquals(40, prefix) |
| 58 | + assertEquals(1, sprites(result).size) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `older protocol omits sprites while keeping adjacent content and network counts`() { |
| 63 | + val template = |
| 64 | + ServerListTemplate.parse( |
| 65 | + JsonParser.parseString( |
| 66 | + """{"schemaVersion":1,"motd":{"text":$document,"alignments":["left"]},"version":{"mode":"automatic"},"icon":null,"players":{"hidden":false,"online":{"mode":"inherit"},"maximum":{"mode":"inherit"}},"hover":{"mode":"inherit"}}""" |
| 67 | + ) |
| 68 | + .asJsonObject |
| 69 | + ) |
| 70 | + val base = |
| 71 | + ServerPing.builder() |
| 72 | + .version(ServerPing.Version(777, "26.3")) |
| 73 | + .description(Component.empty()) |
| 74 | + .onlinePlayers(1) |
| 75 | + .maximumPlayers(100) |
| 76 | + .build() |
| 77 | + for ((protocol, count) in listOf(767 to 17, 777 to 29)) { |
| 78 | + val result = |
| 79 | + ServerListEvaluator.evaluate( |
| 80 | + base, |
| 81 | + template, |
| 82 | + count, |
| 83 | + protocol, |
| 84 | + null, |
| 85 | + "eu", |
| 86 | + "Europe", |
| 87 | + compiledMotd = template.motd, |
| 88 | + ) |
| 89 | + assertEquals(if (protocol == 777) 1 else 0, sprites(result.descriptionComponent!!).size) |
| 90 | + assertEquals(count, result.players.get().online) |
| 91 | + fun literal(c: Component): String = |
| 92 | + (if (c is TextComponent) c.content() else "") + |
| 93 | + c.children().joinToString("") { literal(it) } |
| 94 | + assertEquals("AB", literal(result.descriptionComponent!!)) |
| 95 | + if (protocol < 773) |
| 96 | + assertEquals( |
| 97 | + "AB", |
| 98 | + PlainTextComponentSerializer.plainText() |
| 99 | + .serialize(result.descriptionComponent!!), |
| 100 | + ) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `uneven gradients retain and tint native sprite objects`() { |
| 106 | + val copy = document.deepCopy() |
| 107 | + copy |
| 108 | + .getAsJsonArray("nodes")[0] |
| 109 | + .asJsonObject |
| 110 | + .getAsJsonObject("style") |
| 111 | + .add( |
| 112 | + "gradient", |
| 113 | + JsonParser.parseString( |
| 114 | + """[{"color":"#ff0000","position":0},{"color":"#00ff00","position":0.2},{"color":"#0000ff","position":1}]""" |
| 115 | + ), |
| 116 | + ) |
| 117 | + val result = CompiledServerText(copy).render(emptyMap()) |
| 118 | + assertEquals(1, sprites(result).size) |
| 119 | + fun color(c: Component, inherited: Style): String? { |
| 120 | + val style = c.style().merge(inherited, Style.Merge.Strategy.IF_ABSENT_ON_TARGET) |
| 121 | + return if (c is ObjectComponent) style.color()?.asHexString()?.lowercase() |
| 122 | + else c.children().firstNotNullOfOrNull { color(it, style) } |
| 123 | + } |
| 124 | + assertEquals("#009f60", color(result, Style.empty())) |
| 125 | + } |
| 126 | +} |
0 commit comments