CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/examples/blockfinder.js
Views: 789
1
/*
2
* This simple bot will help you find any block
3
*/
4
const mineflayer = require('mineflayer')
5
6
const { performance } = require('perf_hooks')
7
8
if (process.argv.length < 4 || process.argv.length > 6) {
9
console.log('Usage : node blockfinder.js <host> <port> [<name>] [<password>]')
10
process.exit(1)
11
}
12
13
const bot = mineflayer.createBot({
14
host: process.argv[2],
15
port: parseInt(process.argv[3]),
16
username: process.argv[4] ? process.argv[4] : 'finder',
17
password: process.argv[5]
18
})
19
20
bot.on('chat', async (username, message) => {
21
if (username === bot.username) return
22
23
if (message === 'loaded') {
24
console.log(bot.entity.position)
25
await bot.waitForChunksToLoad()
26
bot.chat('Ready!')
27
}
28
29
if (message.startsWith('find')) {
30
const name = message.split(' ')[1]
31
if (bot.registry.blocksByName[name] === undefined) {
32
bot.chat(`${name} is not a block name`)
33
return
34
}
35
const ids = [bot.registry.blocksByName[name].id]
36
37
const startTime = performance.now()
38
const blocks = bot.findBlocks({ matching: ids, maxDistance: 128, count: 10 })
39
const time = (performance.now() - startTime).toFixed(2)
40
41
bot.chat(`I found ${blocks.length} ${name} blocks in ${time} ms`)
42
}
43
})
44
45