From 1a55742f8f7b6fee6d45b009130a6618a1b5e5a0 Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Tue, 7 Nov 2023 18:23:05 +0100 Subject: [PATCH] more score tests --- .../feature/text/BillbaordTextTestUtil.kt | 2 +- .../text/name/EntityNameFeatureTest.kt | 18 +++++- .../text/name/EntityScoreFeatureTest.kt | 61 ++++++++++++++++++- .../data/scoreboard/ScoreboardObjective.kt | 6 +- .../feature/text/name/EntityScoreFeature.kt | 6 +- 5 files changed, 84 insertions(+), 9 deletions(-) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/BillbaordTextTestUtil.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/BillbaordTextTestUtil.kt index 4b936d341..2dae294c8 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/BillbaordTextTestUtil.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/BillbaordTextTestUtil.kt @@ -23,6 +23,6 @@ object BillbaordTextTestUtil { fun BillboardTextFeature.assertEmpty() { val text = this.text ?: return if (text.length == 0) return - throw AssertionError("Text is empty but should not be!") + throw AssertionError("Text is not empty but should be!") } } diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityNameFeatureTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityNameFeatureTest.kt index d92aa142d..82952e3d7 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityNameFeatureTest.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityNameFeatureTest.kt @@ -26,6 +26,7 @@ import de.bixilon.minosoft.data.entities.entities.monster.Zombie import de.bixilon.minosoft.data.entities.entities.player.RemotePlayerEntity import de.bixilon.minosoft.data.entities.entities.vehicle.boat.Boat import de.bixilon.minosoft.data.registries.entities.EntityFactory +import de.bixilon.minosoft.data.text.ChatComponent import de.bixilon.minosoft.data.text.TextComponent import de.bixilon.minosoft.gui.rendering.entities.EntityRendererTestUtil.create import de.bixilon.minosoft.gui.rendering.entities.feature.text.BillbaordTextTestUtil.assertEmpty @@ -46,7 +47,7 @@ class EntityNameFeatureTest { } private fun EntityNameFeature.customName(name: Any?) { - renderer.entity.data[Entity.CUSTOM_NAME_DATA] = name + renderer.entity.data[Entity.CUSTOM_NAME_DATA] = ChatComponent.of(name) } private fun EntityNameFeature.isNameVisible(visible: Boolean) { @@ -89,9 +90,10 @@ class EntityNameFeatureTest { fun `correct animal name`() { val name = create(Pig) val text = TextComponent("Pepper:") + name.isNameVisible(true) name.customName(text) name.updateName() - assertSame(name.text, text) // TODO: verify visibility + assertSame(name.text, text) } fun `animal with custom name visible`() { @@ -124,7 +126,7 @@ class EntityNameFeatureTest { name.customName("Pepper") name.setTargeted(distance = 10.0) name.updateName() - name.assertText() + name.assertEmpty() } fun `remote player entity`() { @@ -241,8 +243,18 @@ class EntityNameFeatureTest { name.assertEmpty() } + fun `profile disabled`() { + val name = create(Pig) + name.renderer.renderer.profile.features.name.enabled = false + name.customName("Pepper") + name.isNameVisible(true) + name.updateName() + name.assertEmpty() + } + // TODO: targeted mob, invisible zombie // TODO: mob, armor stand, player (local/remote), pig, non living (boat?) // TODO: isInvisible, teams (with team nametag visibility), // TODO: profile + // TODO: render distance, sneaking } diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityScoreFeatureTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityScoreFeatureTest.kt index 876fe89f8..18b269e3d 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityScoreFeatureTest.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityScoreFeatureTest.kt @@ -16,14 +16,24 @@ package de.bixilon.minosoft.gui.rendering.entities.feature.text.name import de.bixilon.kutil.cast.CastUtil.unsafeCast import de.bixilon.kutil.reflection.ReflectionUtil.forceSet import de.bixilon.minosoft.data.entities.entities.player.RemotePlayerEntity +import de.bixilon.minosoft.data.scoreboard.ScoreboardObjective +import de.bixilon.minosoft.data.scoreboard.ScoreboardPositions +import de.bixilon.minosoft.data.scoreboard.ScoreboardScore +import de.bixilon.minosoft.data.text.BaseComponent +import de.bixilon.minosoft.data.text.TextComponent +import de.bixilon.minosoft.data.text.formatting.color.ChatColors import de.bixilon.minosoft.gui.rendering.entities.EntityRendererTestUtil.create import de.bixilon.minosoft.gui.rendering.entities.feature.text.BillbaordTextTestUtil.assertEmpty +import de.bixilon.minosoft.gui.rendering.entities.feature.text.BillbaordTextTestUtil.assertText +import de.bixilon.minosoft.gui.rendering.entities.feature.text.BillboardTextFeature import de.bixilon.minosoft.gui.rendering.entities.renderer.living.player.PlayerRenderer +import org.testng.Assert.assertEquals import org.testng.annotations.Test @Test(groups = ["entities", "rendering"]) class EntityScoreFeatureTest { private val updateScore = EntityScoreFeature::class.java.getDeclaredMethod("updateScore").apply { isAccessible = true } + private val updateNameOffset = EntityScoreFeature::class.java.getDeclaredMethod("updateNameOffset").apply { isAccessible = true } private fun createScore(): EntityScoreFeature { val renderer = create().create(RemotePlayerEntity).unsafeCast>() @@ -36,12 +46,61 @@ class EntityScoreFeatureTest { updateScore.invoke(this) } + private fun EntityScoreFeature.updateNameOffset() { + updateNameOffset.invoke(this) + } + + private fun EntityScoreFeature.setScore() { + val renderer = this.renderer.unsafeCast>() + val objective = ScoreboardObjective("name", TextComponent("Score").color(ChatColors.LIGHT_PURPLE)) + renderer.renderer.connection.scoreboard.positions[ScoreboardPositions.BELOW_NAME] = objective + objective.scores[renderer.entity.additional.name] = ScoreboardScore("name", objective, null, 1) + renderer.renderer.features.score.update() + } + fun `player without score`() { val score = createScore() score.updateScore() score.assertEmpty() } + fun `player with score`() { + val score = createScore() + score.setScore() + score.updateScore() + score.assertText() + } - // TODO: teams, invisibility, score, profile, correct text + fun `name offset without score`() { + val score = createScore() + score.setScore() + score.updateScore() + score.updateNameOffset() + assertEquals(score.renderer.name.offset, BillboardTextFeature.DEFAULT_OFFSET) + } + + fun `name offset with score`() { + val score = createScore() + score.setScore() + score.updateScore() + score.updateNameOffset() + assertEquals(score.renderer.name.offset, BillboardTextFeature.DEFAULT_OFFSET + 0.24f) + } + + fun `profile disabled`() { + val score = createScore() + score.renderer.renderer.profile.features.score.enabled = false + score.setScore() + score.updateScore() + score.assertEmpty() + } + + fun `correct text`() { + val score = createScore() + score.setScore() + score.updateScore() + assertEquals(score.text, BaseComponent("1", " ", TextComponent("Score").color(ChatColors.LIGHT_PURPLE))) + } + + // TODO: teams, invisibility } diff --git a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.kt b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.kt index d867831cb..d04666142 100644 --- a/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.kt +++ b/src/main/java/de/bixilon/minosoft/data/scoreboard/ScoreboardObjective.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * Copyright (C) 2020-2023 Moritz Zwerger * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * @@ -19,8 +19,8 @@ import de.bixilon.minosoft.protocol.packets.s2c.play.scoreboard.objective.Create class ScoreboardObjective( val name: String, - var displayName: ChatComponent, - var unit: CreateObjectiveS2CP.ObjectiveUnits, + var displayName: ChatComponent? = null, + var unit: CreateObjectiveS2CP.ObjectiveUnits = CreateObjectiveS2CP.ObjectiveUnits.INTEGER, ) { val scores: LockMap = lockMapOf() diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityScoreFeature.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityScoreFeature.kt index da2003894..322c3e20a 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityScoreFeature.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/entities/feature/text/name/EntityScoreFeature.kt @@ -31,12 +31,16 @@ class EntityScoreFeature(renderer: PlayerRenderer<*>) : BillboardTextFeature(ren this.delta += delta if (this.delta >= UPDATE_INTERVAL) { updateScore() - renderer.name.offset = if (this.text != null) NAME_OFFSET else DEFAULT_OFFSET + updateNameOffset() this.delta = 0.0f } super.update(millis, delta) } + private fun updateNameOffset() { + renderer.name.offset = if (this.text != null) NAME_OFFSET else DEFAULT_OFFSET + } + private fun updateScore() { if (!renderScore()) { this.text = null