const { getBot } = require('../core/context'); const { saveMemory, getMemory } = require('../memory'); const db = require('../db'); // Define what kinds of blocks to remember. This should probably be a variable later so that I can add to this list. const INTERESTING_BLOCKS = [ 'coal_ore', 'iron_ore', 'diamond_ore', 'gold_ore', 'emerald_ore', 'redstone_ore', 'lapis_ore', 'copper_ore', 'oak_log', 'birch_log', 'spruce_log', 'crafting_table', 'furnace', 'chest', 'anvil', 'enchanting_table', 'lectern', 'bell', 'bed', 'beacon', 'portal' ]; const COOLDOWN_MINUTES = 5; module.exports = async function logSurroundings() { const bot = getBot(); if (!bot) return; const found = bot.findBlocks({ matching: block => INTERESTING_BLOCKS.includes(block.name), maxDistance: 12, count: 10 }); const now = Date.now(); const observedTypes = new Set(); for (const pos of found) { const block = bot.blockAt(pos); if (!block || !block.name) continue; const label = `found:${block.name}:${pos.x},${pos.y},${pos.z}`; const memoryData = { name: block.name, position: pos, timestamp: now }; // Store this exact block if not seen before db.get(`SELECT id FROM memory WHERE label = ?`, [label], (err, row) => { if (err || row) return; saveMemory(db, label, memoryData); }); if (!observedTypes.has(block.name)) { observedTypes.add(block.name); const typeLabel = `recent:${block.name}`; getMemory(db, typeLabel, (err, memory) => { if (err || !memory) { saveMemory(db, typeLabel, { lastSeen: now }); bot.chat(`I noticed ${block.name.replace(/_/g, ' ')} nearby.`); } else { const age = (now - memory.lastSeen) / (1000 * 60); // in minutes if (age > COOLDOWN_MINUTES) { saveMemory(db, typeLabel, { lastSeen: now }); bot.chat(`I saw another ${block.name.replace(/_/g, ' ')} nearby.`); } } }); } } const newTypes = Array.from(observedTypes); if (newTypes.length > 0) { const counts = {}; for (const pos of found) { const name = bot.blockAt(pos)?.name; if (newTypes.includes(name)) { counts[name] = (counts[name] || 0) + 1; } } const summaryMsg = Object.entries(counts) .map(([name, count]) => `${count} ${name.replace(/_/g, ' ')}`) .join(', '); if (summaryMsg) bot.chat(`I noticed: ${summaryMsg}`); } };