Skip to content

Commit

Permalink
Hover contents name can also be a component (#61)
Browse files Browse the repository at this point in the history
* Hover contents name can also be a component

* FIx PR comment
  • Loading branch information
creesch authored Jan 1, 2025
1 parent 81a51eb commit 51473f5
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/client/resources/web/js/messages/message_parsing.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const VALID_HOVER_EVENTS = ['show_text', 'show_item', 'show_entity'];
/**
* @typedef {Object} ShowEntityHoverEvent
* @property {'show_entity'} action - Displays entity information
* @property {{ type: string, id: unknown, name?: string }} [contents] - The entity data to show
* @property {{ type: string, id: unknown, name?: string | Component }} [contents] - The entity data to show
* @property {string} [value] - Deprecated: SNBT representation of the entity data to show.
*/

Expand Down Expand Up @@ -329,12 +329,24 @@ export function assertIsComponent(component, path = []) {

if (
'name' in hoverEvent.contents &&
typeof hoverEvent.contents.name !== 'string'
hoverEvent.contents.name !== null
) {
throw new ComponentError(
'HoverEvent.contents.name is not a string',
[...path, 'contents', 'name'],
);
if (typeof hoverEvent.contents.name === 'string') {
return;
}

if (typeof hoverEvent.contents.name !== 'object') {
throw new ComponentError(
'HoverEvent.contents.name is not a string or valid component',
[...path, 'contents', 'name'],
);
}

assertIsComponent(hoverEvent.contents.name, [
...path,
'contents',
'name',
]);
}
}

Expand Down Expand Up @@ -822,6 +834,10 @@ function formatHoverEvent(hoverEvent) {
return '';
}

if (typeof hoverEvent.contents.name === 'object') {
return formatComponentPlainText(hoverEvent.contents.name);
}

return hoverEvent.contents.name || 'Unnamed Entity';
}
}
Expand Down

0 comments on commit 51473f5

Please sign in to comment.