fix(statemachine): 🥅 fix for lookatPlayer when no or invalid player
Fixes the case when player isn't nearby or entity is invalid due to teleport
This commit is contained in:
		| @@ -78,8 +78,9 @@ function init(smName = "dummy", webserver) { | ||||
|         actions: { | ||||
|             // action implementation | ||||
|             lookAtPlayerOnce: (context, event) => { | ||||
|                 const player = context?.player || bot.nearestEntity(entity => entity.type === 'player'); | ||||
|                 if (player.position || player.entity) { | ||||
|                 const player = context?.player && (context?.player?.isValid || context?.player?.entity?.isValid) | ||||
|                     || bot.nearestEntity(entity => entity.type === 'player'); | ||||
|                 if (player?.position || player?.entity) { | ||||
|                     context.player = player; | ||||
|                     bot.lookAt((player.entity || player).position.offset(0, 1, 0)); | ||||
|                 } | ||||
| @@ -87,9 +88,10 @@ function init(smName = "dummy", webserver) { | ||||
|         }, | ||||
|         activities: { | ||||
|             lookAtPlayer: (context, event) => { | ||||
|                 const player = context?.player || bot.nearestEntity(entity => entity.type === 'player'); | ||||
|                 // TODO  check every event? | ||||
|                 if (player.position || player.entity) { | ||||
|                 const player = (context?.player?.isValid || context?.player?.entity?.isValid) && context?.player | ||||
|                     || bot.nearestEntity(entity => entity.type === 'player' && entity.isValid); | ||||
|                 // TODO check pos every event? | ||||
|                 if (player?.position || player?.entity) { | ||||
|                     context.player = player; | ||||
|                     function looks() { | ||||
|                         bot.lookAt((player.entity || player).position.offset(0, 1, 0)); | ||||
| @@ -97,6 +99,11 @@ function init(smName = "dummy", webserver) { | ||||
|                     bot.on("time", looks); | ||||
|                     return () => bot.off("time", looks); | ||||
|                 } | ||||
|                 else { | ||||
|                     quiet || bot.chat("look: no valid players"); | ||||
|                     // TODO use xstate logger | ||||
|                     context.debug && console.log("sm: no valid player", this, context, player); | ||||
|                 } | ||||
|             } | ||||
|         }, | ||||
|         delays: { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user