Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Jul 6, 2024
1 parent a4b1b4b commit c90e109
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/plugins/physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -165,28 +186,43 @@ 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
// This function rounds to the nearest 50ms (or PHYSICS_INTERVAL_MS) and checks if a second has passed.
(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
} else if (positionUpdated) {
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
// For versions >= 1.12, onGround !== lastSent.onGround should be used, but it doesn't ever trigger outside of login
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
}

Expand Down

0 comments on commit c90e109

Please sign in to comment.