Download survival.js from Fadi49/My: direct link, hf CLI and curl.
- Browser
- Download file 5.99 kB
-
https://huggingface.co/spaces/Fadi49/My/resolve/main/survival.js
- Command line
-
hf download hf://spaces/Fadi49/My/survival.js
-
curl -L -o survival.js https://huggingface.co/spaces/Fadi49/My/resolve/main/survival.js
5.99 kB
| ; | |
| /** | |
| * Survival 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 FOOD_ITEMS = [ | |
| 'bread','cooked_beef','cooked_porkchop','cooked_chicken','cooked_mutton', | |
| 'cooked_rabbit','cooked_cod','cooked_salmon','steak','baked_potato', | |
| 'pumpkin_pie','mushroom_stew','beetroot_soup','rabbit_stew', | |
| 'apple','golden_apple','sweet_berries','glow_berries','chorus_fruit', | |
| 'dried_kelp','cake','cookie','melon_slice', | |
| ]; | |
| const DANGER_BLOCKS = new Set([ | |
| 'lava','flowing_lava','fire','soul_fire','cactus','magma_block', | |
| 'sweet_berry_bush','wither_rose','powder_snow', | |
| ]); | |
| class SurvivalSystem extends EventEmitter { | |
| constructor(bot, config = {}) { | |
| super(); | |
| this.bot = bot; | |
| this.config = { | |
| hungerCheckInterval: 3000, | |
| healthCheckInterval: 1000, | |
| safeFallHeight: 4, | |
| ...config, | |
| }; | |
| this._active = false; | |
| this._timers = []; | |
| this._eating = false; | |
| this._lastFood = 0; | |
| this._foodEaten = 0; | |
| this._log('INIT', 'نظام البقاء جاهز'); | |
| } | |
| _log(tag, msg) { | |
| const ts = new Date().toISOString().replace('T', ' ').slice(0, 19); | |
| console.log(`[${ts}] [SURVIVAL] ${tag} ${msg}`); | |
| } | |
| start() { | |
| if (this._active) return; | |
| this._active = true; | |
| this._log('START', 'بدء نظام البقاء'); | |
| this._addTimer(() => this._checkHunger(), this.config.hungerCheckInterval); | |
| this._addTimer(() => this._checkHealth(), this.config.healthCheckInterval); | |
| this._addTimer(() => this._avoidDanger(), 1500); | |
| this._addTimer(() => this._checkFall(), 2000); | |
| } | |
| stop() { | |
| this._active = 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); | |
| } | |
| // ═══ فحص الجوع ═══ | |
| _checkHunger() { | |
| if (!this.bot?.entity || this._eating) return; | |
| const food = this.bot.food; | |
| if (food !== undefined && food < 14) { | |
| this._log('🍖', `جوع (food: ${food}) — يأكل`); | |
| this._eat(); | |
| } | |
| } | |
| // ═══ فحص الصحة ═══ | |
| _checkHealth() { | |
| if (!this.bot?.entity) return; | |
| const health = this.bot.health; | |
| if (health !== undefined && health < 6) { | |
| this._log('❤️', `صحة قليلة (${health}) — يأكل`); | |
| this._eat(); | |
| } | |
| } | |
| // ═══ أكل ═══ | |
| async _eat() { | |
| if (this._eating || !this.bot) return; | |
| this._eating = true; | |
| try { | |
| const foodItem = this.bot.inventory.items().find(i => FOOD_ITEMS.includes(i.name)); | |
| if (!foodItem) { | |
| this._log('⚠️', 'لا طعام متاح!'); | |
| this.emit('noFood'); | |
| this._eating = false; | |
| return; | |
| } | |
| await this.bot.equip(foodItem, 'hand'); | |
| await this.bot.consume(); | |
| this._foodEaten++; | |
| this._lastFood = Date.now(); | |
| this._log('✅', `أكل ${foodItem.name} (إجمالي: ${this._foodEaten})`); | |
| this.emit('ate', foodItem.name); | |
| } catch(e) { | |
| this._log('❌', `خطأ في الأكل: ${e.message}`); | |
| } finally { | |
| this._eating = false; | |
| } | |
| } | |
| // ═══ تجنب الخطر ═══ | |
| _avoidDanger() { | |
| if (!this.bot?.entity) return; | |
| const pos = this.bot.entity.position; | |
| const blockBelow = this.bot.blockAt(pos.offset(0, -1, 0)); | |
| if (!blockBelow) return; | |
| if (DANGER_BLOCKS.has(blockBelow.name)) { | |
| this._log('⚠️', `خطر! ${blockBelow.name} تحتينا — نهرب`); | |
| this.emit('danger', blockBelow.name); | |
| // اهرب من الخطر | |
| const yaw = Math.random() * Math.PI * 2; | |
| this.bot.look(yaw, 0, false); | |
| this.bot.setControlState('forward', true); | |
| this.bot.setControlState('sprint', true); | |
| setTimeout(() => { | |
| try { | |
| this.bot.setControlState('forward', false); | |
| this.bot.setControlState('sprint', false); | |
| } catch(_) {} | |
| }, rand(1000, 2000)); | |
| } | |
| } | |
| // ═══ تجنب السقوط ═══ | |
| _checkFall() { | |
| if (!this.bot?.entity) return; | |
| const pos = this.bot.entity.position; | |
| // فحص لو قدامنا حفرة | |
| const forward = this.bot.blockAt(pos.offset(0, -3, 0)); | |
| if (forward?.name === 'air') { | |
| this._log('⚠️', 'حفرة قدامنا — نتفاداها'); | |
| this.bot.setControlState('back', true); | |
| setTimeout(() => { try { this.bot.setControlState('back', false); } catch(_) {} }, 500); | |
| } | |
| } | |
| // ═══ أكل من الصندوق لو مش متاح ═══ | |
| async _searchFoodInChests() { | |
| if (!this.bot) return null; | |
| const chestBlock = this.bot.findBlock({ | |
| matching: this.bot.registry?.blocksByName?.chest?.id, | |
| maxDistance: 16, count: 1, | |
| }); | |
| if (!chestBlock) return null; | |
| try { | |
| await this.bot.openChest(chestBlock); | |
| await sleep(500); | |
| const chestItems = this.bot.currentWindow?.items() || []; | |
| const food = chestItems.find(i => FOOD_ITEMS.includes(i.name)); | |
| if (food) { | |
| await this.bot.clickWindow(food.slot, 1, 0); | |
| await sleep(300); | |
| this.bot.closeWindow(); | |
| return food; | |
| } | |
| this.bot.closeWindow(); | |
| } catch(_) {} | |
| return null; | |
| } | |
| getStats() { | |
| return { | |
| foodEaten: this._foodEaten, | |
| lastFood: this._lastFood ? new Date(this._lastFood).toISOString() : null, | |
| active: this._active, | |
| hunger: this.bot?.food, | |
| health: this.bot?.health, | |
| }; | |
| } | |
| destroy() { | |
| this.stop(); | |
| this.removeAllListeners(); | |
| } | |
| } | |
| module.exports = { SurvivalSystem, FOOD_ITEMS }; | |