Skip to content

Commit 4dac577

Browse files
authored
feat: basic support for text_display entity (#230)
1 parent f2552e7 commit 4dac577

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

prismarine-viewer/viewer/lib/entities.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,27 @@ function getUsernameTexture (username: string, { fontFamily = 'sans-serif' }: an
2929
const padding = 5
3030
ctx.font = `${fontSize}px ${fontFamily}`
3131

32-
const textWidth = ctx.measureText(username).width + padding * 2
32+
const lines = String(username).split('\n')
33+
34+
let textWidth = 0
35+
for (const line of lines) {
36+
const width = ctx.measureText(line).width + padding * 2
37+
if (width > textWidth) textWidth = width
38+
}
3339

3440
canvas.width = textWidth
35-
canvas.height = fontSize + padding * 2
41+
canvas.height = (fontSize + padding * 2) * lines.length
3642

3743
ctx.fillStyle = 'rgba(0, 0, 0, 0.3)'
3844
ctx.fillRect(0, 0, canvas.width, canvas.height)
3945

4046
ctx.font = `${fontSize}px ${fontFamily}`
4147
ctx.fillStyle = 'white'
42-
ctx.fillText(username, padding, fontSize)
48+
let i = 0
49+
for (const line of lines) {
50+
i++
51+
ctx.fillText(line, padding + (textWidth - ctx.measureText(line).width) / 2, fontSize * i)
52+
}
4353

4454
return canvas
4555
}
@@ -454,6 +464,7 @@ export class Entities extends EventEmitter {
454464
// ---
455465
// not player
456466
const displayText = entity.metadata?.[3] && this.parseEntityLabel(entity.metadata[2])
467+
|| entity.metadata?.[23] && this.parseEntityLabel(entity.metadata[23]) // text displays
457468
if (entity.name !== 'player' && displayText) {
458469
addNametag({ ...entity, username: displayText }, this.entitiesOptions, this.entities[entity.id].children.find(c => c.name === 'mesh'))
459470
}

prismarine-viewer/viewer/lib/entity/EntityMesh.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ export const knownNotHandled = [
224224
'item_display', 'item_frame',
225225
'lightning_bolt', 'marker',
226226
'painting', 'spawner_minecart',
227-
'spectral_arrow', 'text_display',
228-
'tnt', 'trader_llama', 'zombie_horse'
227+
'spectral_arrow', 'tnt',
228+
'trader_llama', 'zombie_horse'
229229
]
230230

231231
export const temporaryMap = {

prismarine-viewer/viewer/lib/entity/entities.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16390,6 +16390,10 @@
1639016390
},
1639116391
"render_controllers": ["controller.render.strider"]
1639216392
},
16393+
"text_display": {
16394+
"identifier": "minecraft:text_display",
16395+
"geometry": {}
16396+
},
1639316397
"trident": {
1639416398
"identifier": "minecraft:thrown_trident",
1639516399
"textures": {

0 commit comments

Comments
 (0)