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