Skip to content

Commit

Permalink
Player attached text display
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Jul 19, 2024
1 parent 67aaf5d commit 729160c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 17 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ kotlin_version=1.9.24
fabric_kotlin_version=1.10.20

# Mod Properties
mod_version=1.0.0
mod_version=1.1.0

maven_group=net.mcbrawls
mod_id=brawls-entities
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/net/mcbrawls/entities/BrawlsAPIEntities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.mcbrawls.entities

import eu.pb4.polymer.core.api.entity.PolymerEntityUtils
import net.mcbrawls.entities.entity.DisplayedBlockEntity
import net.mcbrawls.entities.entity.PlayerAttachedTextDisplayEntity
import net.mcbrawls.entities.entity.TemporaryTextDisplayEntity
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityType
Expand All @@ -23,8 +24,19 @@ object BrawlsAPIEntities {
"temporary_text_display",
EntityType.Builder.create(::TemporaryTextDisplayEntity, SpawnGroup.MISC)
.dimensions(0.0f, 0.0f)
.disableSaving()
.maxTrackingRange(2)
.trackingTickInterval(10)
.trackingTickInterval(1)
)

val PLAYER_ATTACHED_TEXT_DISPLAY = register(
"player_attached_text_display",
EntityType.Builder.create(::PlayerAttachedTextDisplayEntity, SpawnGroup.MISC)
.dimensions(0.0f, 0.0f)
.disableSaving()
.disableSummon()
.maxTrackingRange(2)
.trackingTickInterval(1)
)

fun <E : Entity> register(id: String, builder: EntityType.Builder<E>): EntityType<E> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package net.mcbrawls.entities.entity

import dev.andante.audience.player.StandalonePlayerReference
import eu.pb4.polymer.core.api.entity.PolymerEntity
import net.mcbrawls.entities.entity.PlayerAttachedTextDisplayEntity.TextSupplier
import net.minecraft.entity.EntityType
import net.minecraft.entity.decoration.DisplayEntity.TextDisplayEntity
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.Text
import net.minecraft.util.math.Vec3d
import net.minecraft.world.World

open class PlayerAttachedTextDisplayEntity(
/**
* The player to be attached to.
*/
private val player: StandalonePlayerReference?,

/**
* Provides the text component for this text display.
*/
private val textSupplier: TextSupplier,

/**
* The offset from the top of the player's hitbox to display the entity.
*/
private val positionOffset: Vec3d = DEFAULT_POSITION_OFFSET,

type: EntityType<*>,
world: World
) : TextDisplayEntity(type, world), PolymerEntity {
open val serverPlayer: ServerPlayerEntity? get() = player?.player

constructor(type: EntityType<*>, world: World) : this(null, TextSupplier { player, _ -> Text.literal(player.playerName) }, DEFAULT_POSITION_OFFSET, type, world)

init {
setBillboardMode(BillboardMode.CENTER)
}

override fun tick() {
super.tick()

val player = serverPlayer
if (player == null) {
discard()
} else {
val position = player.pos.add(0.0, player.height.toDouble(), 0.0).add(positionOffset)
setPosition(position)

val text = textSupplier.getText(player, age)
setText(text)
}
}

override fun getPolymerEntityType(player: ServerPlayerEntity): EntityType<*> {
return EntityType.TEXT_DISPLAY
}

fun interface TextSupplier {
fun getText(player: ServerPlayerEntity, age: Int): Text
}

companion object {
val DEFAULT_POSITION_OFFSET = Vec3d(0.0, 0.5, 0.0)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package net.mcbrawls.entities.entity

import eu.pb4.polymer.core.api.entity.PolymerEntity
import net.mcbrawls.entities.entity.TemporaryTextDisplayEntity.TextSupplier
import net.minecraft.entity.EntityType
import net.minecraft.entity.decoration.DisplayEntity.TextDisplayEntity
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.Text
import net.minecraft.util.math.Vec3d
import net.minecraft.world.World

class TemporaryTextDisplayEntity(
open class TemporaryTextDisplayEntity(
/**
* Provides the text component for this text display.
*/
Expand All @@ -19,37 +22,45 @@ class TemporaryTextDisplayEntity(

type: EntityType<*>,
world: World
) : TextDisplayEntity(type, world) {
constructor(
/**
* The text to display on this entity.
*/
text: Text,
) : TextDisplayEntity(type, world), PolymerEntity {
constructor(type: EntityType<*>, world: World) : this(TextSupplier { entity, _ -> entity.displayName ?: Text.literal(entity.nameForScoreboard) }, DEFAULT_MAX_AGE, type, world)

maxAge: Int = DEFAULT_MAX_AGE,
private var initialPosition: Vec3d? = null

type: EntityType<*>,
world: World
) : this(TextSupplier { text }, maxAge, type, world)

constructor(type: EntityType<*>, world: World) : this(Text.literal("Temp"), DEFAULT_MAX_AGE, type, world)
init {
setBillboardMode(BillboardMode.CENTER)
}

override fun tick() {
super.tick()

if (age > maxAge) {
discard()
} else {
val text = textSupplier.getText(age)
val initialPosition: Vec3d = initialPosition ?: let {
initialPosition = pos
pos
}

val percentage = age / maxAge.toFloat()
val yOffset = MAX_Y_OFFSET * percentage
setPosition(initialPosition.add(0.0, yOffset, 0.0))

val text = textSupplier.getText(this, age)
setText(text)
}
}

override fun getPolymerEntityType(player: ServerPlayerEntity): EntityType<*> {
return EntityType.TEXT_DISPLAY
}

fun interface TextSupplier {
fun getText(age: Int): Text
fun getText(entity: TemporaryTextDisplayEntity, age: Int): Text
}

companion object {
const val DEFAULT_MAX_AGE: Int = 20
const val MAX_Y_OFFSET: Double = 0.25
}
}
1 change: 1 addition & 0 deletions src/main/resources/brawls-entities.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ transitive-accessible method net/minecraft/entity/decoration/InteractionEntity s
transitive-accessible method net/minecraft/entity/decoration/InteractionEntity setInteractionHeight (F)V
transitive-accessible method net/minecraft/entity/decoration/InteractionEntity setResponse (Z)V
transitive-accessible method net/minecraft/entity/decoration/DisplayEntity$TextDisplayEntity setBackground (I)V
transitive-accessible method net/minecraft/entity/decoration/DisplayEntity setBillboardMode (Lnet/minecraft/entity/decoration/DisplayEntity$BillboardMode;)V

0 comments on commit 729160c

Please sign in to comment.