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/farmer.js
Views: 789
1
const { Vec3 } = require('vec3')
2
const mineflayer = require('mineflayer')
3
4
if (process.argv.length < 4 || process.argv.length > 6) {
5
console.log('Usage : node farmer.js <host> <port> [<name>] [<password>]')
6
process.exit(1)
7
}
8
9
const bot = mineflayer.createBot({
10
host: process.argv[2],
11
port: parseInt(process.argv[3]),
12
username: process.argv[4] ? process.argv[4] : 'farmer',
13
password: process.argv[5]
14
})
15
16
// To fish we have to give bot the seeds
17
// /give farmer wheat_seeds 64
18
19
function blockToSow () {
20
return bot.findBlock({
21
point: bot.entity.position,
22
matching: bot.registry.blocksByName.farmland.id,
23
maxDistance: 6,
24
useExtraInfo: (block) => {
25
const blockAbove = bot.blockAt(block.position.offset(0, 1, 0))
26
return !blockAbove || blockAbove.type === 0
27
}
28
})
29
}
30
31
function blockToHarvest () {
32
return bot.findBlock({
33
point: bot.entity.position,
34
maxDistance: 6,
35
matching: (block) => {
36
return block && block.type === bot.registry.blocksByName.wheat.id && block.metadata === 7
37
}
38
})
39
}
40
41
async function loop () {
42
try {
43
while (1) {
44
const toHarvest = blockToHarvest()
45
if (toHarvest) {
46
await bot.dig(toHarvest)
47
} else {
48
break
49
}
50
}
51
while (1) {
52
const toSow = blockToSow()
53
if (toSow) {
54
await bot.equip(bot.registry.itemsByName.wheat_seeds.id, 'hand')
55
await bot.placeBlock(toSow, new Vec3(0, 1, 0))
56
} else {
57
break
58
}
59
}
60
} catch (e) {
61
console.log(e)
62
}
63
64
// No block to harvest or sow. Postpone next loop a bit
65
setTimeout(loop, 1000)
66
}
67
68
bot.once('login', loop)
69
70