Skip to content

Commit 3b2a3f3

Browse files
committed
fix: address Copilot review feedback on fall_protection and auto_sleep
- Unify velocity threshold to -0.5 for consistent fall detection - Capture current position inside execute callback to avoid stale coords - Poll bot.entity.onGround instead of fixed 500ms wait for landing - Fix night time range: use >= 13000 (wraps from 23999 to 0) - Add null guard on error object in auto_sleep catch - Log non-occupied sleep errors instead of swallowing them silently
1 parent f95a076 commit 3b2a3f3

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

src/agent/modes.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,20 +107,34 @@ const modes_list = [
107107

108108
const fallDistance = this.fallStartY - pos.y;
109109

110-
// If falling more than 10 blocks and accelerating, try MLG water
111-
if (fallDistance > 10 && vel.y < -0.8) {
110+
// If falling more than 10 blocks and still accelerating, try MLG water
111+
if (fallDistance > 10 && vel.y < -0.5) {
112112
const waterBucket = bot.inventory.items().find(item => item.name === 'water_bucket');
113113
if (waterBucket && !this.active) {
114114
// Check if ground is near (within 4 blocks below)
115115
const groundBlock = bot.blockAt(pos.offset(0, -4, 0));
116116
if (groundBlock && groundBlock.name !== 'air' && groundBlock.name !== 'water') {
117117
execute(this, agent, async () => {
118+
const currentPos = bot.entity.position.clone();
118119
say(agent, 'MLG water!');
119120
try {
120121
await bot.equip(waterBucket, 'hand');
121-
await bot.lookAt(pos.offset(0, -3, 0));
122+
await bot.lookAt(currentPos.offset(0, -3, 0));
122123
bot.activateItem();
123-
await new Promise(r => setTimeout(r, 500));
124+
// Wait until the bot has actually landed
125+
await new Promise(resolve => {
126+
const start = Date.now();
127+
const checkLanding = () => {
128+
if (bot.entity.onGround || bot.entity.velocity.y >= 0) {
129+
return resolve();
130+
}
131+
if (Date.now() - start > 2000) {
132+
return resolve();
133+
}
134+
setTimeout(checkLanding, 50);
135+
};
136+
checkLanding();
137+
});
124138
// Pick up water after landing
125139
const waterBlock = world.getNearestBlock(bot, 'water', 3);
126140
if (waterBlock) {
@@ -235,9 +249,9 @@ const modes_list = [
235249
if (Date.now() - this.lastSleepCheck < 30000) return;
236250
this.lastSleepCheck = Date.now();
237251

238-
// Check if it's nighttime (13000-23000 ticks) and we're not already sleeping
252+
// Check if it's nighttime (time >= 13000 ticks) and we're not already sleeping
239253
const time = bot.time.timeOfDay;
240-
const isNight = time >= 13000 && time <= 23000;
254+
const isNight = time >= 13000;
241255
if (!isNight || bot.isSleeping) return;
242256

243257
// Look for a bed within 32 blocks using block name matching (beds are named like 'white_bed', 'red_bed', etc.)
@@ -252,8 +266,12 @@ const modes_list = [
252266
try {
253267
await skills.goToBed(bot);
254268
} catch (e) {
255-
if (e.message && e.message.includes('occupied')) {
269+
if (e && e.message && e.message.includes('occupied')) {
256270
say(agent, 'The bed is occupied.');
271+
} else {
272+
const errorMessage = e && e.message ? e.message : 'unknown reason';
273+
console.log('[AUTO_SLEEP] Error:', errorMessage);
274+
say(agent, `I couldn't sleep (${errorMessage}).`);
257275
}
258276
}
259277
});

0 commit comments

Comments
 (0)