89 lines
No EOL
2.5 KiB
JavaScript
89 lines
No EOL
2.5 KiB
JavaScript
const mineflayer = require('mineflayer');
|
|
const pathfinder = require('mineflayer-pathfinder').pathfinder;
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const { getSafeZones, isInSafeZone } = require('./memory/locations');
|
|
const db = require('./db');
|
|
|
|
// configuration file.
|
|
const config = require('./config.json');
|
|
|
|
const { StateMachine } = require('./core/state-machine');
|
|
const context = require('./core/context');
|
|
|
|
// === Bot Config ===
|
|
const bot = mineflayer.createBot({
|
|
host: config.bot.host,
|
|
port: config.bot.port,
|
|
username: config.bot.name,
|
|
auth: config.bot.auth,
|
|
version: config.bot.version,
|
|
});
|
|
|
|
bot.loadPlugin(pathfinder);
|
|
context.setBot(bot);
|
|
context.setDB(db);
|
|
|
|
let allowDestruction = false;
|
|
|
|
context.setAllowDestruction = (value) => { allowDestruction = value; };
|
|
context.getAllowDestruction = () => allowDestruction;
|
|
|
|
const originalDig = bot.dig;
|
|
|
|
bot.dig = async function (block, ...args) {
|
|
return new Promise((resolve, reject) => {
|
|
getSafeZones(db, async (err, zones) => {
|
|
if (err) {
|
|
console.warn('[SAFEZONE] Error loading zones:', err);
|
|
return reject(err);
|
|
}
|
|
|
|
// Check override first
|
|
if (context.getAllowDestruction() === true) {
|
|
try {
|
|
const result = await originalDig.call(bot, block, ...args);
|
|
console.log(`[DIG-OVERRIDE] ${block.name} at ${block.position}`);
|
|
return resolve(result);
|
|
} catch (digErr) {
|
|
return reject(digErr);
|
|
}
|
|
}
|
|
|
|
// Block dig inside safe zone
|
|
if (isInSafeZone(block.position, zones)) {
|
|
bot.chat("You're not allowed to dig here — it's a protected zone.");
|
|
console.log(`[BLOCKED] Attempted to dig inside safe zone: ${block.name} at ${block.position}`);
|
|
return resolve(false);
|
|
}
|
|
|
|
// Normal dig
|
|
try {
|
|
const result = await originalDig.call(bot, block, ...args);
|
|
console.log(`[DIG] ${block.name} at ${block.position} (allowed)`);
|
|
return resolve(result);
|
|
} catch (digErr) {
|
|
return reject(digErr);
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
bot.once('spawn', async () => {
|
|
console.log(`${config.bot.name} has spawned.`);
|
|
const stateMachine = new StateMachine(path.join(__dirname, 'states'));
|
|
context.setStateMachine(stateMachine);
|
|
await stateMachine.run('Observe');
|
|
});
|
|
|
|
bot.on('chat', (username, message) => {
|
|
if (username !== bot.username) {
|
|
context.setLastChat({ username, message });
|
|
context.getStateMachine().transition('HandleChat');
|
|
}
|
|
});
|
|
|
|
bot.on('death', () => {
|
|
context.getStateMachine().transition('ActOnMemory');
|
|
}); |