Spaces:
Configuration error

My / crafting.js
Fadi49's picture
Upload 38 files
5871ebc verified
Raw History Blame Contribute Delete
5.83 kB
'use strict';
/**
* Crafting 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 CRAFT_PRIORITY = [
{ item: 'crafting_table', count: 1, desc: 'طاولة صناعة' },
{ item: 'wooden_pickaxe', count: 1, desc: 'فأس خشبية' },
{ item: 'wooden_axe', count: 1, desc: 'فأس خشبية' },
{ item: 'furnace', count: 1, desc: 'فرن' },
{ item: 'stone_pickaxe', count: 1, desc: 'فأس حجرية' },
{ item: 'stone_axe', count: 1, desc: 'فأس حجرية' },
{ item: 'chest', count: 2, desc: 'صندوق' },
{ item: 'torch', count: 16, desc: 'مشاعل' },
{ item: 'iron_pickaxe', count: 1, desc: 'فأس حديدية' },
{ item: 'iron_sword', count: 1, desc: 'سيف حديدي' },
{ item: 'shield', count: 1, desc: 'درع' },
];
class CraftingSystem extends EventEmitter {
constructor(bot, config = {}) {
super();
this.bot = bot;
this.config = {
craftInterval: 20000,
...config,
};
this._active = false;
this._timers = [];
this._crafting = false;
this._itemsCrafted = 0;
this._craftedItems = {};
this._log('INIT', 'نظام الصناعة جاهز');
}
_log(tag, msg) {
const ts = new Date().toISOString().replace('T', ' ').slice(0, 19);
console.log(`[${ts}] [CRAFT] ${tag} ${msg}`);
}
start() {
if (this._active) return;
this._active = true;
this._log('START', 'بدء نظام الصناعة');
this._addTimer(() => this._autoCraft(), this.config.craftInterval);
}
stop() {
this._active = false;
this._crafting = 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);
}
// ═══ صناعة تلقائية ═══
async _autoCraft() {
if (this._crafting || !this.bot?.entity) return;
if (chance(50)) return;
this._crafting = true;
try {
// دور على طاولة صناعة
const table = this.bot.findBlock({
matching: this.bot.registry?.blocksByName?.crafting_table?.id,
maxDistance: 16, count: 1,
});
if (!table) {
// اصنع طاولة صناعة
await this._craftItem('crafting_table', 1);
return;
}
// جرّب تصنع أي حاجة من القائمة
for (const recipe of CRAFT_PRIORITY) {
if (this._craftedItems[recipe.item]) continue;
const success = await this._craftItem(recipe.item, recipe.count);
if (success) break;
}
} catch(e) {
this._log('❌', `خطأ: ${e.message}`);
} finally {
this._crafting = false;
}
}
// ═══ صناعة صنف معين ═══
async _craftItem(itemName, count = 1) {
if (!this.bot) return false;
try {
const mcData = require('minecraft-data')(this.bot.version);
const itemInfo = mcData.itemsByName[itemName];
if (!itemInfo) return false;
const table = this.bot.findBlock({
matching: this.bot.registry?.blocksByName?.crafting_table?.id,
maxDistance: 16, count: 1,
});
if (table) {
await this._walkTo(table.position);
await sleep(300);
}
const recipe = this.bot.recipesFor(itemInfo.id, null, 1, table)[0];
if (recipe) {
await this.bot.craft(recipe, count, table || null);
this._itemsCrafted += count;
this._craftedItems[itemName] = (this._craftedItems[itemName] || 0) + count;
this._log('🔨', `صنّع ${itemName} x${count}`);
this.emit('crafted', itemName, count);
return true;
}
} catch(_) {}
return false;
}
// ═══ صهر في الفرن ═══
async _smeltItems(input, fuel, output, count = 1) {
if (!this.bot) return false;
const furnace = this.bot.findBlock({
matching: this.bot.registry?.blocksByName?.furnace?.id,
maxDistance: 16, count: 1,
});
if (!furnace) return false;
try {
await this._walkTo(furnace.position);
await sleep(300);
await this.bot.activateBlock(furnace);
await sleep(1000);
this.bot.closeWindow();
this._log('🔥', `صهر ${input} → ${output}`);
this.emit('smelted', input, output);
return true;
} catch(_) {}
return false;
}
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, 500));
this.bot.setControlState('forward', false);
}
// ═══ فحص لو محتاج حاجة ═══
needItem(itemName) {
if (!this.bot) return false;
return !this.bot.inventory.items().some(i => i.name === itemName);
}
// ═══ فحص المخزون ═══
getItemCount(itemName) {
if (!this.bot) return 0;
return this.bot.inventory.items().filter(i => i.name === itemName).reduce((sum, i) => sum + i.count, 0);
}
getStats() {
return {
itemsCrafted: this._itemsCrafted,
craftedItems: { ...this._craftedItems },
crafting: this._crafting,
active: this._active,
};
}
destroy() {
this.stop();
this.removeAllListeners();
}
}
module.exports = { CraftingSystem, CRAFT_PRIORITY };