From c90e109fb44e6069957ead75a5cf33b6b4138cd7 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Sat, 6 Jul 2024 22:08:23 +0300 Subject: [PATCH] fix --- lib/plugins/physics.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/plugins/physics.js b/lib/plugins/physics.js index 79fe868bf..80ecd10ca 100644 --- a/lib/plugins/physics.js +++ b/lib/plugins/physics.js @@ -96,6 +96,27 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) { doPhysicsTimer = null } + function sendPacketVehicleMove (position) { + bot.vehicleMove ??= { + sideways: 0, + forward: 0, + jump: 0 + } + for (let key of Object.keys(bot.vehicleMove)) { + bot.vehicleMove[key] = Math.min(Math.abs(bot.vehicleMove[key]), 0.9800000190734863) * Math.sign(bot.vehicleMove[key]) + } + bot._client.write('steer_vehicle', { + sideways: bot.vehicleMove.sideways, + forward: bot.vehicleMove.forward, + jump: bot.vehicleMove.jump, + }) + bot.vehicleMove = { + sideways: 0, + forward: 0, + jump: 0 + } + } + function sendPacketPosition (position, onGround) { // sends data, no logic const oldPos = new Vec3(lastSent.x, lastSent.y, lastSent.z) @@ -165,6 +186,8 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) { const position = bot.entity.position const onGround = bot.entity.onGround + const onAVehicle = !!bot.entity.vehicle + // Only send a position update if necessary, select the appropriate packet const positionUpdated = lastSent.x !== position.x || lastSent.y !== position.y || lastSent.z !== position.z || // Send a position update every second, even if no other update was made @@ -172,6 +195,11 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) { (Math.round((now - lastSent.time) / PHYSICS_INTERVAL_MS) * PHYSICS_INTERVAL_MS) >= 1000 const lookUpdated = lastSent.yaw !== yaw || lastSent.pitch !== pitch + let sendLook = false + if (onAVehicle) { + sendLook = true + } + if (positionUpdated && lookUpdated) { sendPacketPositionAndLook(position, yaw, pitch, onGround) lastSent.time = now // only reset if positionUpdated is true @@ -179,7 +207,7 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) { sendPacketPosition(position, onGround) lastSent.time = now // only reset if positionUpdated is true } else if (lookUpdated) { - sendPacketLook(yaw, pitch, onGround) + sendLook = true } else if (positionUpdateSentEveryTick || onGround !== lastSent.onGround) { // For versions < 1.12, one player packet should be sent every tick // for the server to update health correctly @@ -187,6 +215,14 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) { bot._client.write('flying', { onGround: bot.entity.onGround }) } + if (sendLook) { + sendPacketLook(yaw, pitch, onGround) + } + + if (onAVehicle) { + sendPacketVehicleMove(position) + } + lastSent.onGround = bot.entity.onGround // onGround is always set }