feat: basic support for text_display entity (#230)
This commit is contained in:
parent
f2552e70e1
commit
4dac577dfc
3 changed files with 20 additions and 5 deletions
|
|
@ -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'))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
|
|||
|
|
@ -16390,6 +16390,10 @@
|
|||
},
|
||||
"render_controllers": ["controller.render.strider"]
|
||||
},
|
||||
"text_display": {
|
||||
"identifier": "minecraft:text_display",
|
||||
"geometry": {}
|
||||
},
|
||||
"trident": {
|
||||
"identifier": "minecraft:thrown_trident",
|
||||
"textures": {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue