Skip to content

Commit ac2a44a

Browse files
committed
prevent null read
1 parent 71d3559 commit ac2a44a

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lib/plugins/physics.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,22 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) {
295295
bot._client.on('explosion', explosion => {
296296
// TODO: emit an explosion event with more info
297297
if (bot.physicsEnabled && bot.game.gameMode !== 'creative') {
298-
if (explosion.playerKnockback) { // 1.21.3+
299-
bot.entity.velocity.add(explosion.playerMotionX, explosion.playerMotionY, explosion.playerMotionZ)
300-
}
301-
if ('playerMotionX' in explosion) {
302-
bot.entity.velocity.x += explosion.playerMotionX
303-
bot.entity.velocity.y += explosion.playerMotionY
304-
bot.entity.velocity.z += explosion.playerMotionZ
298+
// Check if playerMotion properties exist and are numbers before using them
299+
const motionX = explosion.playerMotionX;
300+
const motionY = explosion.playerMotionY;
301+
const motionZ = explosion.playerMotionZ;
302+
const hasMotion = typeof motionX === 'number' && typeof motionY === 'number' && typeof motionZ === 'number';
303+
304+
if (hasMotion) {
305+
if (explosion.playerKnockback) { // 1.21.3+ - Note: This might be redundant if playerKnockback already implies motion exists. Added check for safety.
306+
bot.entity.velocity.add(motionX, motionY, motionZ)
307+
} else if ('playerMotionX' in explosion) { // Older versions check
308+
bot.entity.velocity.x += motionX
309+
bot.entity.velocity.y += motionY
310+
bot.entity.velocity.z += motionZ
311+
}
312+
} else {
313+
console.warn('Received explosion packet with missing or invalid playerMotion data. Skipping velocity update.');
305314
}
306315
}
307316
})

0 commit comments

Comments
 (0)