|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (process.argv.length < 4 || process.argv.length > 6) { |
|
|
console.log('Usage : node storageBot.js <host> <port> [<name>] [<password>]') |
|
|
process.exit(1) |
|
|
} |
|
|
|
|
|
|
|
|
const mineflayer = require('mineflayer') |
|
|
const collectBlock = require('mineflayer-collectblock').plugin |
|
|
|
|
|
|
|
|
const bot = mineflayer.createBot({ |
|
|
host: process.argv[2], |
|
|
port: parseInt(process.argv[3]), |
|
|
username: process.argv[4] ? process.argv[4] : 'storageBot', |
|
|
password: process.argv[5] |
|
|
}) |
|
|
|
|
|
|
|
|
bot.loadPlugin(collectBlock) |
|
|
|
|
|
|
|
|
let mcData |
|
|
bot.once('login', () => { |
|
|
mcData = require('minecraft-data')(bot.version) |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bot.once('spawn', () => { |
|
|
bot.collectBlock.chestLocations = bot.findBlocks({ |
|
|
matching: mcData.blocksByName.chest.id, |
|
|
maxDistance: 16, |
|
|
count: 999999 |
|
|
}) |
|
|
|
|
|
if (bot.collectBlock.chestLocations.length === 0) { |
|
|
bot.chat("I don't see any chests nearby.") |
|
|
} else { |
|
|
for (const chestPos of bot.collectBlock.chestLocations) { |
|
|
bot.chat(`I found a chest at ${chestPos}`) |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
bot.on('chat', async (username, message) => { |
|
|
|
|
|
|
|
|
const args = message.split(' ') |
|
|
if (args[0] !== 'collect') return |
|
|
|
|
|
|
|
|
let count = 1 |
|
|
if (args.length === 3) count = parseInt(args[1]) |
|
|
|
|
|
|
|
|
let type = args[1] |
|
|
if (args.length === 3) type = args[2] |
|
|
|
|
|
|
|
|
const blockType = mcData.blocksByName[type] |
|
|
if (!blockType) { |
|
|
bot.chat(`I don't know any blocks named ${type}.`) |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
const blocks = bot.findBlocks({ |
|
|
matching: blockType.id, |
|
|
maxDistance: 64, |
|
|
count: count |
|
|
}) |
|
|
|
|
|
|
|
|
if (blocks.length === 0) { |
|
|
bot.chat("I don't see that block nearby.") |
|
|
return |
|
|
} |
|
|
|
|
|
|
|
|
const targets = [] |
|
|
for (let i = 0; i < Math.min(blocks.length, count); i++) { |
|
|
targets.push(bot.blockAt(blocks[i])) |
|
|
} |
|
|
|
|
|
|
|
|
bot.chat(`Found ${targets.length} ${type}(s)`) |
|
|
|
|
|
|
|
|
try { |
|
|
await bot.collectBlock.collect(targets) |
|
|
|
|
|
bot.chat('Done') |
|
|
} catch (err) { |
|
|
|
|
|
bot.chat(err.message) |
|
|
console.log(err) |
|
|
} |
|
|
}) |
|
|
|