Skip to content

Commit f95a076

Browse files
committed
feat: add auto_sleep and fall_protection modes
Add two new survival modes: - fall_protection: Uses water bucket (MLG water) when falling from height >10 blocks. Checks for ground proximity before activating. High priority mode that interrupts all actions. - auto_sleep: Automatically finds and sleeps in nearby beds when night falls (13000-23000 ticks). Checks every 30 seconds to avoid spamming. Uses the existing goToBed skill which handles all bed types. Handles occupied bed errors gracefully. Both modes use English narration and follow the existing mode patterns (say/execute helpers, proper active/interrupts flags).
1 parent 2f6764d commit f95a076

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

src/agent/modes.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,58 @@ const modes_list = [
8787
}
8888
}
8989
},
90+
{
91+
name: 'fall_protection',
92+
description: 'Use water bucket to break a long fall (MLG water). Requires a water bucket in inventory.',
93+
interrupts: ['all'],
94+
on: true,
95+
active: false,
96+
fallStartY: null,
97+
update: async function (agent) {
98+
const bot = agent.bot;
99+
const pos = bot.entity.position;
100+
const vel = bot.entity.velocity;
101+
102+
// Detect if falling (negative Y velocity)
103+
if (vel.y < -0.5) {
104+
if (this.fallStartY === null) {
105+
this.fallStartY = pos.y;
106+
}
107+
108+
const fallDistance = this.fallStartY - pos.y;
109+
110+
// If falling more than 10 blocks and accelerating, try MLG water
111+
if (fallDistance > 10 && vel.y < -0.8) {
112+
const waterBucket = bot.inventory.items().find(item => item.name === 'water_bucket');
113+
if (waterBucket && !this.active) {
114+
// Check if ground is near (within 4 blocks below)
115+
const groundBlock = bot.blockAt(pos.offset(0, -4, 0));
116+
if (groundBlock && groundBlock.name !== 'air' && groundBlock.name !== 'water') {
117+
execute(this, agent, async () => {
118+
say(agent, 'MLG water!');
119+
try {
120+
await bot.equip(waterBucket, 'hand');
121+
await bot.lookAt(pos.offset(0, -3, 0));
122+
bot.activateItem();
123+
await new Promise(r => setTimeout(r, 500));
124+
// Pick up water after landing
125+
const waterBlock = world.getNearestBlock(bot, 'water', 3);
126+
if (waterBlock) {
127+
await bot.lookAt(waterBlock.position);
128+
bot.activateItem();
129+
}
130+
} catch (e) {
131+
console.log('[FALL_PROTECTION] Error:', e.message);
132+
}
133+
});
134+
}
135+
}
136+
}
137+
} else {
138+
this.fallStartY = null;
139+
}
140+
}
141+
},
90142
{
91143
name: 'unstuck',
92144
description: 'Attempt to get unstuck when in the same place for a while. Interrupts some actions.',
@@ -169,6 +221,45 @@ const modes_list = [
169221
}
170222
}
171223
},
224+
{
225+
name: 'auto_sleep',
226+
description: 'Automatically sleep in a nearby bed when night falls to skip the night and avoid monsters.',
227+
interrupts: ['action:followPlayer'],
228+
on: true,
229+
active: false,
230+
lastSleepCheck: 0,
231+
update: async function (agent) {
232+
const bot = agent.bot;
233+
234+
// Only check every 30 seconds to avoid spamming
235+
if (Date.now() - this.lastSleepCheck < 30000) return;
236+
this.lastSleepCheck = Date.now();
237+
238+
// Check if it's nighttime (13000-23000 ticks) and we're not already sleeping
239+
const time = bot.time.timeOfDay;
240+
const isNight = time >= 13000 && time <= 23000;
241+
if (!isNight || bot.isSleeping) return;
242+
243+
// Look for a bed within 32 blocks using block name matching (beds are named like 'white_bed', 'red_bed', etc.)
244+
const beds = bot.findBlocks({
245+
matching: (block) => block.name.includes('bed'),
246+
maxDistance: 32,
247+
count: 1
248+
});
249+
if (beds.length > 0) {
250+
execute(this, agent, async () => {
251+
say(agent, 'It\'s getting dark, I should sleep.');
252+
try {
253+
await skills.goToBed(bot);
254+
} catch (e) {
255+
if (e.message && e.message.includes('occupied')) {
256+
say(agent, 'The bed is occupied.');
257+
}
258+
}
259+
});
260+
}
261+
}
262+
},
172263
{
173264
name: 'hunting',
174265
description: 'Hunt nearby animals when idle.',

0 commit comments

Comments
 (0)