Saed / building.js
Saedmh's picture
Upload 37 files
a08f3c3 verified
Raw History Blame Contribute Delete
7.16 kB
'use strict';
/**
* Building 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 BUILD_MATERIALS = [
'oak_planks','spruce_planks','birch_planks','jungle_planks',
'acacia_planks','dark_oak_planks','cobblestone','stone_bricks',
'smooth_stone','bricks','glass','oak_fence',
];
class BuildingSystem extends EventEmitter {
constructor(bot, config = {}) {
super();
this.bot = bot;
this.config = {
buildRadius: 4,
wallHeight: 3,
...config,
};
this._active = false;
this._timers = [];
this._building = false;
this._blocksPlaced = 0;
this._structures = 0;
this._log('INIT', 'نظام البناء جاهز');
}
_log(tag, msg) {
const ts = new Date().toISOString().replace('T', ' ').slice(0, 19);
console.log(`[${ts}] [BUILDING] ${tag} ${msg}`);
}
start() {
if (this._active) return;
this._active = true;
this._log('START', 'بدء نظام البناء');
// يرتب المخزون كل 30 ثانية
this._addTimer(() => this._organizeInventory(), 30000);
}
stop() {
this._active = false;
this._building = 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 buildShelter() {
if (this._building || !this.bot?.entity) return;
this._building = true;
this._log('🏠', 'بناء ملجأ');
try {
const plank = this.bot.inventory.items().find(i => i.name.includes('plank'));
if (!plank) {
this._log('⚠️', 'لا خشب متاح للبناء');
return;
}
await this.bot.equip(plank, 'hand');
const pos = this.bot.entity.position;
const base = { x: Math.round(pos.x), y: Math.round(pos.y), z: Math.round(pos.z) };
// بناء أرضية 5x5
await this._buildFloor(base);
// بناء جدران
await this._buildWalls(base);
// بناء سقف
await this._buildRoof(base);
// وضع مشاعل
await this._placeTorches(base);
this._structures++;
this._log('✅', `بنى ملجأ (إجمالي: ${this._structures})`);
this.emit('built', 'shelter');
} catch(e) {
this._log('❌', `خطأ: ${e.message}`);
} finally {
this._building = false;
}
}
// ═══ بناء أرضية ═══
async _buildFloor(base) {
if (!this.bot) return;
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
const pos = { x: base.x + x, y: base.y - 1, z: base.z + z };
await this._placeBlock(pos);
}
}
}
// ═══ بناء جدران ═══
async _buildWalls(base) {
if (!this.bot) return;
const h = this.config.wallHeight;
for (let y = 0; y <= h; y++) {
// الجدران الأربعة
for (let x = -2; x <= 2; x++) {
await this._placeBlock({ x: base.x + x, y: base.y + y, z: base.z - 2 });
await this._placeBlock({ x: base.x + x, y: base.y + y, z: base.z + 2 });
}
for (let z = -1; z <= 1; z++) {
await this._placeBlock({ x: base.x - 2, y: base.y + y, z: base.z + z });
await this._placeBlock({ x: base.x + 2, y: base.y + y, z: base.z + z });
}
}
}
// ═══ بناء سقف ═══
async _buildRoof(base) {
if (!this.bot) return;
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
await this._placeBlock({ x: base.x + x, y: base.y + 4, z: base.z + z });
}
}
}
// ═══ وضع كتلة ═══
async _placeBlock(pos) {
if (!this.bot) return;
try {
const block = this.bot.blockAt(pos);
if (block?.name !== 'air') return;
// دور على كتلة تحتها نقدر نحط عليها
const below = this.bot.blockAt({ x: pos.x, y: pos.y - 1, z: pos.z });
if (!below || below.name === 'air') return;
await this.bot.placeBlock(below, { x: 0, y: 1, z: 0 });
this._blocksPlaced++;
await sleep(rand(50, 150));
} catch(_) {}
}
// ═══ وضع مشاعل ═══
async _placeTorches(base) {
if (!this.bot) return;
const torch = this.bot.inventory.items().find(i => i.name === 'torch');
if (!torch) return;
try {
await this.bot.equip(torch, 'hand');
const spots = [
{ x: base.x - 2, y: base.y + 1, z: base.z - 2 },
{ x: base.x + 2, y: base.y + 1, z: base.z + 2 },
];
for (const spot of spots) {
const block = this.bot.blockAt({ x: spot.x, y: spot.y - 1, z: spot.z });
if (block && block.name !== 'air') {
await this.bot.placeBlock(block, { x: 0, y: 1, z: 0 });
await sleep(200);
}
}
} catch(_) {}
}
// ═══ ترتيب المخزون ═══
async _organizeInventory() {
if (!this.bot) return;
const chest = this.bot.findBlock({
matching: this.bot.registry?.blocksByName?.chest?.id,
maxDistance: 16, count: 1,
});
if (!chest) return;
try {
await this.bot.openChest(chest);
await sleep(500);
// ضع الأغراض الزائدة في الصندوق
const items = this.bot.inventory.items();
const keep = ['sword','axe','pickaxe','shovel','bow','shield','torch','bread','water_bucket'];
for (const item of items) {
if (keep.some(k => item.name.includes(k))) continue;
if (chance(40)) {
try {
await this.bot.clickWindow(item.slot, 0, 0);
await sleep(100);
} catch(_) {}
}
}
this.bot.closeWindow();
this._log('📦', 'رتب المخزون');
} catch(_) {}
}
// ═══ بناء سلم ═══
async buildStairs(height = 5) {
if (!this.bot?.entity) return;
const plank = this.bot.inventory.items().find(i => i.name.includes('plank'));
if (!plank) return;
await this.bot.equip(plank, 'hand');
const pos = this.bot.entity.position;
const base = { x: Math.round(pos.x), y: Math.round(pos.y), z: Math.round(pos.z) };
for (let i = 0; i < height; i++) {
await this._placeBlock({ x: base.x + i, y: base.y + i, z: base.z });
await this._placeBlock({ x: base.x + i, y: base.y + i + 1, z: base.z });
}
this._log('🪜', `بنى سلم بارتفاع ${height}`);
}
getStats() {
return {
blocksPlaced: this._blocksPlaced,
structures: this._structures,
building: this._building,
active: this._active,
};
}
destroy() {
this.stop();
this.removeAllListeners();
}
}
module.exports = { BuildingSystem, BUILD_MATERIALS };