From 66d26ad2e6a6673e97e6bd08b67d97de052984ba Mon Sep 17 00:00:00 2001 From: Valery-a <83373303+Valery-a@users.noreply.github.com> Date: Sun, 1 Sep 2024 17:53:48 +0300 Subject: [PATCH] feat: add visuals for entities damaging (#186) --- docs-assets/handled-packets.md | 2 +- prismarine-viewer/viewer/lib/entities.js | 18 ++++++++++++++++++ src/entities.ts | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs-assets/handled-packets.md b/docs-assets/handled-packets.md index 0671987c..497ec5ec 100644 --- a/docs-assets/handled-packets.md +++ b/docs-assets/handled-packets.md @@ -32,8 +32,8 @@ ❌ world_border_warning_reach ❌ simulation_distance ❌ chunk_biomes -❌ damage_event ❌ hurt_animation +✅ damage_event ✅ spawn_entity ✅ spawn_entity_experience_orb ✅ named_entity_spawn diff --git a/prismarine-viewer/viewer/lib/entities.js b/prismarine-viewer/viewer/lib/entities.js index 01ce7d6d..59d1164a 100644 --- a/prismarine-viewer/viewer/lib/entities.js +++ b/prismarine-viewer/viewer/lib/entities.js @@ -496,4 +496,22 @@ export class Entities extends EventEmitter { new TWEEN.Tween(e.rotation).to({ y: e.rotation.y + dy }, TWEEN_DURATION).start() } } + + handleDamageEvent(entityId, damageAmount) { + const entityMesh = this.entities[entityId]?.children.find(c => c.name === 'mesh') + if (entityMesh) { + entityMesh.traverse((child) => { + if (child instanceof THREE.Mesh) { + const clonedMaterial = child.material.clone() + clonedMaterial.dispose() + child.material = child.material.clone() + const originalColor = child.material.color.clone() + child.material.color.set(0xff_00_00) + new TWEEN.Tween(child.material.color) + .to(originalColor, 500) + .start() + } + }) + } + } } diff --git a/src/entities.ts b/src/entities.ts index b238d4ea..b5daa220 100644 --- a/src/entities.ts +++ b/src/entities.ts @@ -1,4 +1,5 @@ import { Entity } from 'prismarine-entity' +import { versionToNumber } from 'prismarine-viewer/viewer/prepare/utils' import tracker from '@nxg-org/mineflayer-tracker' import { loader as autoJumpPlugin } from '@nxg-org/mineflayer-auto-jump' import { subscribeKey } from 'valtio/utils' @@ -88,6 +89,21 @@ customEvents.on('gameLoaded', () => { } }) + bot._client.on('damage_event', (data) => { + const { entityId, sourceTypeId: damage } = data + if (viewer.entities.entities[entityId]) { + viewer.entities.handleDamageEvent(entityId, damage) + } + }) + + bot._client.on('entity_status', (data) => { + if (versionToNumber(bot.version) >= versionToNumber('1.19.4')) return + const { entityId, entityStatus } = data + if (entityStatus === 2 && viewer.entities.entities[entityId]) { + viewer.entities.handleDamageEvent(entityId, entityStatus) + } + }) + const loadedSkinEntityIds = new Set() const playerRenderSkin = (e: Entity) => {