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/collectblock.js
Views: 789
1
/**
2
* A quick and easy implementation of the collect block plugin. (Requires mineflayer-pathfinder and mineflayer-collectblock)
3
*/
4
const mineflayer = require('mineflayer')
5
const pathfinder = require('mineflayer-pathfinder').pathfinder
6
const collectBlock = require('mineflayer-collectblock').plugin
7
8
if (process.argv.length < 4 || process.argv.length > 6) {
9
console.log('Usage : node collectblock.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] : 'collector',
17
password: process.argv[5]
18
})
19
20
// Load pathfinder and collect block plugins
21
bot.loadPlugin(pathfinder)
22
bot.loadPlugin(collectBlock)
23
24
// Listen for when a player says "collect [something]" in chat
25
bot.on('chat', (username, message) => {
26
const args = message.split(' ')
27
if (args[0] !== 'collect') return
28
29
// Get the correct block type
30
const blockType = bot.registry.blocksByName[args[1]]
31
if (!blockType) {
32
bot.chat("I don't know any blocks with that name.")
33
return
34
}
35
36
bot.chat('Collecting the nearest ' + blockType.name)
37
38
// Try and find that block type in the world
39
const block = bot.findBlock({
40
matching: blockType.id,
41
maxDistance: 64
42
})
43
44
if (!block) {
45
bot.chat("I don't see that block nearby.")
46
return
47
}
48
49
// Collect the block if we found one
50
bot.collectBlock.collect(block, err => {
51
if (err) bot.chat(err.message)
52
})
53
})
54
55