Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions prismarine-viewer/viewer/lib/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the key (index number) consistent across the versions that support it? We can check it entities data of minecraft-data module

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it is definitely not right here! The 3 index is custom_name_visible property for entities. However 23 for text_display is text and for block_display is block_state property (not text), so it will result in error spamming in the console. So we should do a check here, however I might think of a better solution. Definitely code needs to be refactored (I'll gladly help you here). At least we should use property names, not number :d

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for context:
image

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sadly dont have permissions to push

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sadly dont have permissions to push

Sorry, forgot that GitHub treats org forks differently from personal ones :S Will make PRs from a personal one in the future.

Also, it is definitely not right here! The 3 index is custom_name_visible property for entities. However 23 for text_display is text and for block_display is block_state property (not text), so it will result in error spamming in the console. So we should do a check here, however I might think of a better solution. Definitely code needs to be refactored (I'll gladly help you here). At least we should use property names, not number :d

You are right, I didn't consider that. I kinda thought the indexes are entity-specific but it makes sense that they aren't. Feel free to leave PRs open in the future for me to fix such stuff directly in them. Will open another PR which improves the situation here (and maybe even adds more features of the text displays already, will have to look at how difficult to do that is with the current name tag system)

if (entity.name !== 'player' && displayText) {
addNametag({ ...entity, username: displayText }, this.entitiesOptions, this.entities[entity.id].children.find(c => c.name === 'mesh'))
}
Expand Down
4 changes: 2 additions & 2 deletions prismarine-viewer/viewer/lib/entity/EntityMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

function addCube(attr, boneId, bone, cube, texWidth = 64, texHeight = 64) {

Check warning on line 97 in prismarine-viewer/viewer/lib/entity/EntityMesh.js

View workflow job for this annotation

GitHub Actions / build-and-deploy

Function 'addCube' has too many parameters (6). Maximum allowed is 4
const cubeRotation = new THREE.Euler(0, 0, 0)
if (cube.rotation) {
cubeRotation.x = -cube.rotation[0] * Math.PI / 180
Expand Down Expand Up @@ -224,8 +224,8 @@
'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 = {
Expand Down
4 changes: 4 additions & 0 deletions prismarine-viewer/viewer/lib/entity/entities.json
Original file line number Diff line number Diff line change
Expand Up @@ -16390,6 +16390,10 @@
},
"render_controllers": ["controller.render.strider"]
},
"text_display": {
"identifier": "minecraft:text_display",
"geometry": {}
},
"trident": {
"identifier": "minecraft:thrown_trident",
"textures": {
Expand Down
Loading