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/digger.js
Views: 789
1
/*
2
* Never spend hours mining from ground to bedrock again!
3
*
4
* Learn how to create a simple bot that is capable of digging the block
5
* below his feet and then going back up by creating a dirt column to the top.
6
*
7
* As always, you can send the bot commands using chat messages, and monitor
8
* his inventory at any time.
9
*
10
* Remember that in survival mode he might not have enough dirt to get back up,
11
* so be sure to teach him a few more tricks before leaving him alone at night.
12
*/
13
const mineflayer = require('mineflayer')
14
const vec3 = require('vec3')
15
16
if (process.argv.length < 4 || process.argv.length > 6) {
17
console.log('Usage : node digger.js <host> <port> [<name>] [<password>]')
18
process.exit(1)
19
}
20
21
const bot = mineflayer.createBot({
22
host: process.argv[2],
23
port: parseInt(process.argv[3]),
24
username: process.argv[4] ? process.argv[4] : 'digger',
25
password: process.argv[5]
26
})
27
28
bot.on('chat', async (username, message) => {
29
if (username === bot.username) return
30
switch (message) {
31
case 'loaded':
32
await bot.waitForChunksToLoad()
33
bot.chat('Ready!')
34
break
35
case 'list':
36
sayItems()
37
break
38
case 'dig':
39
dig()
40
break
41
case 'build':
42
build()
43
break
44
case 'equip dirt':
45
equipDirt()
46
break
47
}
48
})
49
50
function sayItems (items = bot.inventory.items()) {
51
const output = items.map(itemToString).join(', ')
52
if (output) {
53
bot.chat(output)
54
} else {
55
bot.chat('empty')
56
}
57
}
58
59
async function dig () {
60
let target
61
if (bot.targetDigBlock) {
62
bot.chat(`already digging ${bot.targetDigBlock.name}`)
63
} else {
64
target = bot.blockAt(bot.entity.position.offset(0, -1, 0))
65
if (target && bot.canDigBlock(target)) {
66
bot.chat(`starting to dig ${target.name}`)
67
try {
68
await bot.dig(target)
69
bot.chat(`finished digging ${target.name}`)
70
} catch (err) {
71
console.log(err.stack)
72
}
73
} else {
74
bot.chat('cannot dig')
75
}
76
}
77
}
78
79
function build () {
80
const referenceBlock = bot.blockAt(bot.entity.position.offset(0, -1, 0))
81
const jumpY = Math.floor(bot.entity.position.y) + 1.0
82
bot.setControlState('jump', true)
83
bot.on('move', placeIfHighEnough)
84
85
let tryCount = 0
86
87
async function placeIfHighEnough () {
88
if (bot.entity.position.y > jumpY) {
89
try {
90
await bot.placeBlock(referenceBlock, vec3(0, 1, 0))
91
bot.setControlState('jump', false)
92
bot.removeListener('move', placeIfHighEnough)
93
bot.chat('Placing a block was successful')
94
} catch (err) {
95
tryCount++
96
if (tryCount > 10) {
97
bot.chat(err.message)
98
bot.setControlState('jump', false)
99
bot.removeListener('move', placeIfHighEnough)
100
}
101
}
102
}
103
}
104
}
105
106
async function equipDirt () {
107
let itemsByName
108
if (bot.supportFeature('itemsAreNotBlocks')) {
109
itemsByName = 'itemsByName'
110
} else if (bot.supportFeature('itemsAreAlsoBlocks')) {
111
itemsByName = 'blocksByName'
112
}
113
try {
114
await bot.equip(bot.registry[itemsByName].dirt.id, 'hand')
115
bot.chat('equipped dirt')
116
} catch (err) {
117
bot.chat(`unable to equip dirt: ${err.message}`)
118
}
119
}
120
121
function itemToString (item) {
122
if (item) {
123
return `${item.name} x ${item.count}`
124
} else {
125
return '(nothing)'
126
}
127
}
128
129