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/fisherman.js
Views: 789
1
const mineflayer = require('mineflayer')
2
3
if (process.argv.length < 4 || process.argv.length > 6) {
4
console.log('Usage : node fisherman.js <host> <port> [<name>] [<password>]')
5
process.exit(1)
6
}
7
8
const bot = mineflayer.createBot({
9
host: process.argv[2],
10
port: parseInt(process.argv[3]),
11
username: process.argv[4] ? process.argv[4] : 'fisherman',
12
password: process.argv[5]
13
})
14
15
// To fish we have to give bot the fishing rod and teleport bot to the water
16
// /give fisherman fishing_rod 1
17
// /teleport fisherman ~ ~ ~
18
19
// To eat we have to apply hunger first
20
// /effect fisherman minecraft:hunger 1 255
21
22
bot.on('message', (cm) => {
23
if (cm.toString().includes('start')) {
24
startFishing()
25
}
26
27
if (cm.toString().includes('stop')) {
28
stopFishing()
29
}
30
31
if (cm.toString().includes('eat')) {
32
eat()
33
}
34
})
35
36
let nowFishing = false
37
38
function onCollect (player, entity) {
39
if (entity.kind === 'Drops' && player === bot.entity) {
40
bot.removeListener('playerCollect', onCollect)
41
startFishing()
42
}
43
}
44
45
async function startFishing () {
46
bot.chat('Fishing')
47
try {
48
await bot.equip(bot.registry.itemsByName.fishing_rod.id, 'hand')
49
} catch (err) {
50
return bot.chat(err.message)
51
}
52
53
nowFishing = true
54
bot.on('playerCollect', onCollect)
55
56
try {
57
await bot.fish()
58
} catch (err) {
59
bot.chat(err.message)
60
}
61
nowFishing = false
62
}
63
64
function stopFishing () {
65
bot.removeListener('playerCollect', onCollect)
66
67
if (nowFishing) {
68
bot.activateItem()
69
}
70
}
71
72
async function eat () {
73
stopFishing()
74
75
try {
76
await bot.equip(bot.registry.itemsByName.fish.id, 'hand')
77
} catch (err) {
78
return bot.chat(err.message)
79
}
80
81
try {
82
await bot.consume()
83
} catch (err) {
84
return bot.chat(err.message)
85
}
86
}
87
88