Skip to content

Commit 7076fe5

Browse files
committed
feat: NameTagElementHolder
1 parent e11d98b commit 7076fe5

File tree

4 files changed

+81
-45
lines changed

4 files changed

+81
-45
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ kotlin_version=2.2.0
1515
fabric_kotlin_version=1.13.4
1616

1717
# mod properties
18-
mod_version=1.0.1
18+
mod_version=1.1.0
1919
maven_group=net.mcbrawls
2020
mod_id=populus
2121

src/main/kotlin/net/mcbrawls/populus/AbstractFakePlayerEntity.kt

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package net.mcbrawls.populus
33
import com.mojang.authlib.GameProfile
44
import com.mojang.authlib.properties.Property
55
import eu.pb4.polymer.core.api.entity.PolymerEntity
6-
import eu.pb4.polymer.virtualentity.api.ElementHolder
76
import eu.pb4.polymer.virtualentity.api.attachment.EntityAttachment
8-
import eu.pb4.polymer.virtualentity.api.elements.TextDisplayElement
97
import net.minecraft.entity.EntityType
108
import net.minecraft.entity.data.DataTracker
11-
import net.minecraft.entity.decoration.DisplayEntity
129
import net.minecraft.entity.mob.PathAwareEntity
1310
import net.minecraft.entity.player.PlayerEntity
1411
import net.minecraft.network.packet.Packet
@@ -17,10 +14,8 @@ import net.minecraft.network.packet.s2c.play.PlayerRemoveS2CPacket
1714
import net.minecraft.server.network.ServerPlayerEntity
1815
import net.minecraft.text.Text
1916
import net.minecraft.util.Arm
20-
import net.minecraft.util.math.AffineTransformation
2117
import net.minecraft.world.GameMode
2218
import net.minecraft.world.World
23-
import org.joml.Vector3f
2419
import xyz.nucleoid.packettweaker.PacketContext
2520
import java.util.EnumSet
2621
import java.util.function.Consumer
@@ -36,37 +31,28 @@ abstract class AbstractFakePlayerEntity(type: EntityType<out AbstractFakePlayerE
3631
*/
3732
open val modelParts: Byte = Byte.MAX_VALUE
3833

39-
/**
40-
* The base name tag offset from the passenger position.
41-
*/
42-
open val baseNameTagOffset: Float = 0.2f
43-
44-
/**
45-
* The name tag offset for additional lines.
46-
*/
47-
open val additionalNameTagOffset: Float = 0.25f
48-
4934
/**
5035
* The generated profile name for this fake player.
5136
*/
5237
val defaultProfileName: String by lazy { uuid.toString().substring(0..<16) }
5338

54-
private var nameElementAttachment: EntityAttachment? = null
39+
var nameHolder: NameTagElementHolder = NameTagElementHolder()
40+
private set
41+
42+
var nameAttachment: EntityAttachment? = null
43+
private set
5544

5645
init {
57-
refreshNameElements()
46+
nameAttachment = EntityAttachment.ofTicking(nameHolder, this)
47+
refreshNameTag()
5848
}
5949

6050
/**
6151
* Refreshes attached elements.
6252
*/
63-
fun refreshNameElements() {
64-
nameElementAttachment?.destroy()
65-
66-
val elements = createNameAttachmentElement()
67-
val holder = ElementHolder()
68-
elements.forEach(holder::addPassengerElement)
69-
nameElementAttachment = EntityAttachment.ofTicking(holder, this)
53+
fun refreshNameTag() {
54+
val text = createDisplayNameText()
55+
nameHolder.setText(text)
7056
}
7157

7258
/**
@@ -77,25 +63,6 @@ abstract class AbstractFakePlayerEntity(type: EntityType<out AbstractFakePlayerE
7763
return emptyList()
7864
}
7965

80-
/**
81-
* Creates the attached elements for the player's name tag.
82-
* @return null to use default name tag
83-
*/
84-
open fun createNameAttachmentElement(): List<TextDisplayElement> {
85-
val components = createDisplayNameText()
86-
return components.reversed().mapIndexed { i, text ->
87-
val element = TextDisplayElement(text)
88-
89-
val transformation = AffineTransformation(Vector3f(0.0f, baseNameTagOffset + (additionalNameTagOffset * i), 0.0f), null, null, null)
90-
element.setTransformation(transformation)
91-
92-
element.billboardMode = DisplayEntity.BillboardMode.CENTER
93-
element.teleportDuration = 1
94-
95-
element
96-
}
97-
}
98-
9966
/**
10067
* Creates the player list entry for this player. Necessary for the player entity to spawn.
10168
*/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package net.mcbrawls.populus
2+
3+
import eu.pb4.polymer.virtualentity.api.ElementHolder
4+
import eu.pb4.polymer.virtualentity.api.elements.TextDisplayElement
5+
import net.minecraft.entity.decoration.DisplayEntity
6+
import net.minecraft.text.Text
7+
import net.minecraft.util.math.AffineTransformation
8+
import org.joml.Vector3f
9+
10+
class NameTagElementHolder : ElementHolder() {
11+
/**
12+
* The base name tag offset from the passenger position.
13+
*/
14+
var baseNameTagOffset: Float = 0.2f
15+
set(value) {
16+
field = value
17+
refreshElements()
18+
}
19+
20+
/**
21+
* The name tag offset for additional lines.
22+
*/
23+
var additionalNameTagOffset: Float = 0.25f
24+
set(value) {
25+
field = value
26+
refreshElements()
27+
}
28+
29+
private val components: MutableList<Text> = mutableListOf()
30+
31+
fun setText(text: Collection<Text>) {
32+
components.clear()
33+
components.addAll(text)
34+
35+
refreshElements()
36+
}
37+
38+
fun setText(vararg text: Text) {
39+
setText(text.toList())
40+
}
41+
42+
/**
43+
* Creates the attached elements for the player's name tag.
44+
* @return null to use default name tag
45+
*/
46+
fun createElements(): List<TextDisplayElement> {
47+
return components.reversed().mapIndexed { i, text ->
48+
val element = TextDisplayElement(text)
49+
50+
val translation = Vector3f(0.0f, baseNameTagOffset + (additionalNameTagOffset * i), 0.0f)
51+
val transformation = AffineTransformation(translation, null, null, null)
52+
element.setTransformation(transformation)
53+
54+
element.billboardMode = DisplayEntity.BillboardMode.CENTER
55+
element.teleportDuration = 1
56+
57+
element
58+
}
59+
}
60+
61+
fun refreshElements() {
62+
entityIds.forEach(attachedPassengerEntityIds::removeInt)
63+
64+
if (attachedPassengerEntityIds.isEmpty()) {
65+
val elements = createElements()
66+
elements.forEach(::addPassengerElement)
67+
}
68+
}
69+
}

src/main/kotlin/net/mcbrawls/populus/NpcEntity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class NpcEntity(type: EntityType<NpcEntity>, world: World) : AbstractFakePlayerE
1717

1818
override fun createDisplayNameText(): List<Text> {
1919
val name = createProfileName()
20-
return listOf(Text.literal("Fake ($name)"))
20+
return listOf(Text.literal("NPC"), Text.literal(name))
2121
}
2222

2323
companion object {

0 commit comments

Comments
 (0)