'use strict'; /** * Mining System — نظام التعدين * يروح يحفر، يجمع معادن، يكتشف كهوف */ const { EventEmitter } = require('events'); const sleep = ms => new Promise(r => setTimeout(r, Math.max(0, ms))); const rand = (a, b) => Math.floor(Math.random() * (b - a + 1)) + a; const chance = pct => Math.random() * 100 < pct; const pick = arr => arr[Math.floor(Math.random() * arr.length)]; const ORE_PRIORITY = [ 'diamond_ore','deepslate_diamond_ore', 'emerald_ore','deepslate_emerald_ore', 'gold_ore','deepslate_gold_ore', 'iron_ore','deepslate_iron_ore', 'redstone_ore','deepslate_redstone_ore', 'lapis_ore','deepslate_lapis_ore', 'coal_ore','deepslate_coal_ore', 'copper_ore','deepslate_copper_ore', ]; const VALUABLE_ITEMS = new Set([ 'diamond','emerald','gold_ingot','iron_ingot','netherite_scrap', 'netherite_ingot','redstone','lapis_lazuli','coal','copper_ingot', 'raw_iron','raw_gold','raw_copper','flint', ]); class MiningSystem extends EventEmitter { constructor(bot, config = {}) { super(); this.bot = bot; this.config = { scanRadius: 24, mineDepth: 40, torchInterval: 12, ...config, }; this._active = false; this._timers = []; this._mining = false; this._blocksMined = 0; this._oresFound = {}; this._totalValue = 0; this._log('INIT', 'نظام التعدين جاهز'); } _log(tag, msg) { const ts = new Date().toISOString().replace('T', ' ').slice(0, 19); console.log(`[${ts}] [MINING] ${tag} ${msg}`); } start() { if (this._active) return; this._active = true; this._log('START', 'بدء نظام التعدين'); // يدور على معادن كل 5 ثواني this._addTimer(() => this._scanForOres(), 5000); // يحط مشاعل تلقائياً this._addTimer(() => this._placeTorches(), 8000); } stop() { this._active = false; this._mining = false; this._timers.forEach(t => { try { clearInterval(t); clearTimeout(t); } catch(_) {} }); this._timers = []; this._log('STOP', 'إيقاف نظام التعدين'); } _addTimer(fn, interval) { const t = setInterval(() => { if (this._active) fn(); }, interval); this._timers.push(t); } // ═══ كشف المعادن القريبة ═══ _scanForOres() { if (!this.bot?.entity || this._mining) return; const pos = this.bot.entity.position; const radius = this.config.scanRadius; for (let x = -radius; x <= radius; x += 2) { for (let y = -radius; y <= radius; y++) { for (let z = -radius; z <= radius; z += 2) { const block = this.bot.blockAt(pos.offset(x, y, z)); if (!block) continue; if (ORE_PRIORITY.includes(block.name)) { this._mineBlock(block); return; } } } } } // ═══ حفر كتلة ═══ async _mineBlock(block) { if (this._mining || !this.bot?.entity) return; this._mining = true; try { // اختار أفضل مجرفة/فأس await this._equipBestTool(block.name); // امشي للكتلة await this._walkTo(block.position); // حفر try { await this.bot.dig(block); this._blocksMined++; // سجّل المعدن if (ORE_PRIORITY.includes(block.name)) { const ore = block.name.replace('deepslate_', '').replace('_ore', ''); this._oresFound[ore] = (this._oresFound[ore] || 0) + 1; this._totalValue += this._getOreValue(ore); this._log('⛏️', `حفر ${block.name} (إجمالي: ${this._blocksMined})`); this.emit('ore', ore, block.name); } } catch(_) {} await sleep(rand(100, 300)); } catch(e) { this._log('❌', `خطأ: ${e.message}`); } finally { this._mining = false; } } // ═══ اختيار أفضل أداة ═══ async _equipBestTool(blockName) { if (!this.bot) return; const isOre = blockName.includes('ore'); const isStone = ['stone','deepslate','granite','diorite','andesite'].some(s => blockName.includes(s)); let toolType = 'pickaxe'; if (blockName.includes('log') || blockName.includes('wood')) toolType = 'axe'; else if (blockName.includes('dirt') || blockName.includes('sand') || blockName.includes('gravel')) toolType = 'shovel'; const items = this.bot.inventory.items(); const tools = items.filter(i => i.name.includes(toolType)); if (tools.length > 0) { // الأعلى في الترتيب const best = tools.sort((a, b) => this._toolRank(b.name) - this._toolRank(a.name))[0]; try { await this.bot.equip(best, 'hand'); } catch(_) {} } } _toolRank(name) { if (name.includes('netherite')) return 6; if (name.includes('diamond')) return 5; if (name.includes('iron')) return 4; if (name.includes('stone')) return 3; if (name.includes('golden')) return 2; if (name.includes('wooden')) return 1; return 0; } // ═══ المشي ═══ async _walkTo(position) { if (!this.bot?.entity) return; const yaw = Math.atan2( position.z - this.bot.entity.position.z, position.x - this.bot.entity.position.x ); this.bot.look(yaw, 0, false); this.bot.setControlState('forward', true); await sleep(rand(200, 600)); this.bot.setControlState('forward', false); } // ═══ وضع مشاعل ═══ async _placeTorches() { if (!this.bot?.entity) return; const torch = this.bot.inventory.items().find(i => i.name === 'torch' || i.name === 'soul_torch'); if (!torch) return; // لو المشاعل قليلة، حط واحدة كل فتره const torchCount = this.bot.inventory.items().filter(i => i.name === 'torch').length; if (torchCount > 3 && chance(50)) return; try { await this.bot.equip(torch, 'hand'); const block = this.bot.blockAt(this.bot.entity.position.offset(0, -1, 0)); if (block && block.name !== 'air') { await this.bot.placeBlock(block, { x: 0, y: 1, z: 0 }); } } catch(_) {} } // ═══ قيمة المعدن ═══ _getOreValue(ore) { const values = { diamond: 10, emerald: 8, gold: 5, iron: 3, redstone: 2, lapis: 2, coal: 1, copper: 1 }; return values[ore] || 1; } // ═══ النزول للأعماق ═══ async digDown() { if (!this.bot?.entity) return; this._log('🔽', 'النزول للأعماق'); for (let i = 0; i < this.config.mineDepth; i++) { if (!this.bot?.entity || !this._active) break; const block = this.bot.blockAt(this.bot.entity.position.offset(0, -1, 0)); if (block && block.name !== 'air' && block.name !== 'water' && block.name !== 'lava') { try { await this._equipBestTool(block.name); await this.bot.dig(block); this._blocksMined++; } catch(_) {} } await sleep(rand(150, 400)); } } getStats() { return { blocksMined: this._blocksMined, oresFound: { ...this._oresFound }, totalValue: this._totalValue, mining: this._mining, active: this._active, }; } destroy() { this.stop(); this.removeAllListeners(); } } module.exports = { MiningSystem, ORE_PRIORITY, VALUABLE_ITEMS };