forked from PrismarineJS/mineflayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
farmer.js
68 lines (60 loc) · 1.59 KB
/
farmer.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
const { Vec3 } = require('vec3')
const mineflayer = require('mineflayer')
if (process.argv.length < 4 || process.argv.length > 6) {
console.log('Usage : node farmer.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] : 'farmer',
password: process.argv[5]
})
// To fish we have to give bot the seeds
// /give farmer wheat_seeds 64
function blockToSow () {
return bot.findBlock({
point: bot.entity.position,
matching: bot.registry.blocksByName.farmland.id,
maxDistance: 6,
useExtraInfo: (block) => {
const blockAbove = bot.blockAt(block.position.offset(0, 1, 0))
return !blockAbove || blockAbove.type === 0
}
})
}
function blockToHarvest () {
return bot.findBlock({
point: bot.entity.position,
maxDistance: 6,
matching: (block) => {
return block && block.type === bot.registry.blocksByName.wheat.id && block.metadata === 7
}
})
}
async function loop () {
try {
while (1) {
const toHarvest = blockToHarvest()
if (toHarvest) {
await bot.dig(toHarvest)
} else {
break
}
}
while (1) {
const toSow = blockToSow()
if (toSow) {
await bot.equip(bot.registry.itemsByName.wheat_seeds.id, 'hand')
await bot.placeBlock(toSow, new Vec3(0, 1, 0))
} else {
break
}
}
} catch (e) {
console.log(e)
}
// No block to harvest or sow. Postpone next loop a bit
setTimeout(loop, 1000)
}
bot.once('login', loop)