more score tests

This commit is contained in:
Moritz Zwerger 2023-11-07 18:23:05 +01:00
parent 46f69edb09
commit 1a55742f8f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 84 additions and 9 deletions

View File

@ -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!")
}
}

View File

@ -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
}

View File

@ -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<PlayerRenderer<*>>()
@ -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<PlayerRenderer<*>>()
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()
}
// TODO: teams, invisibility, score, profile, correct text
fun `player with score`() {
val score = createScore()
score.setScore()
score.updateScore()
score.assertText()
}
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
}

View File

@ -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<String, ScoreboardScore> = lockMapOf()

View File

@ -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