feat: basic support for text_display entity (#230)

This commit is contained in:
Max Lee 2024-11-20 09:04:59 +01:00 committed by GitHub
commit 4dac577dfc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 20 additions and 5 deletions

View file

@ -29,17 +29,27 @@ function getUsernameTexture (username: string, { fontFamily = 'sans-serif' }: an
const padding = 5
ctx.font = `${fontSize}px ${fontFamily}`
const textWidth = ctx.measureText(username).width + padding * 2
const lines = String(username).split('\n')
let textWidth = 0
for (const line of lines) {
const width = ctx.measureText(line).width + padding * 2
if (width > textWidth) textWidth = width
}
canvas.width = textWidth
canvas.height = fontSize + padding * 2
canvas.height = (fontSize + padding * 2) * lines.length
ctx.fillStyle = 'rgba(0, 0, 0, 0.3)'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.font = `${fontSize}px ${fontFamily}`
ctx.fillStyle = 'white'
ctx.fillText(username, padding, fontSize)
let i = 0
for (const line of lines) {
i++
ctx.fillText(line, padding + (textWidth - ctx.measureText(line).width) / 2, fontSize * i)
}
return canvas
}
@ -454,6 +464,7 @@ export class Entities extends EventEmitter {
// ---
// not player
const displayText = entity.metadata?.[3] && this.parseEntityLabel(entity.metadata[2])
|| entity.metadata?.[23] && this.parseEntityLabel(entity.metadata[23]) // text displays
if (entity.name !== 'player' && displayText) {
addNametag({ ...entity, username: displayText }, this.entitiesOptions, this.entities[entity.id].children.find(c => c.name === 'mesh'))
}

View file

@ -224,8 +224,8 @@ export const knownNotHandled = [
'item_display', 'item_frame',
'lightning_bolt', 'marker',
'painting', 'spawner_minecart',
'spectral_arrow', 'text_display',
'tnt', 'trader_llama', 'zombie_horse'
'spectral_arrow', 'tnt',
'trader_llama', 'zombie_horse'
]
export const temporaryMap = {

View file

@ -16390,6 +16390,10 @@
},
"render_controllers": ["controller.render.strider"]
},
"text_display": {
"identifier": "minecraft:text_display",
"geometry": {}
},
"trident": {
"identifier": "minecraft:thrown_trident",
"textures": {