Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/fishing.js
1467 views
1
const { Vec3 } = require('vec3')
2
const { createDoneTask, createTask } = require('../promise_utils')
3
4
module.exports = inject
5
6
function inject (bot) {
7
let bobberId = 90
8
// Before 1.14 the bobber entity keep changing name at each version (but the id stays 90)
9
// 1.14 changes the id, but hopefully we can stick with the name: fishing_bobber
10
// the alternative would be to rename it in all version of mcData
11
if (bot.supportFeature('fishingBobberCorrectlyNamed')) {
12
bobberId = bot.registry.entitiesByName.fishing_bobber.id
13
}
14
15
let fishingTask = createDoneTask()
16
let lastBobber = null
17
18
bot._client.on('spawn_entity', (packet) => {
19
if (packet.type === bobberId && !fishingTask.done && !lastBobber) {
20
lastBobber = bot.entities[packet.entityId]
21
}
22
})
23
24
bot._client.on('world_particles', (packet) => {
25
if (!lastBobber || fishingTask.done) return
26
27
const pos = lastBobber.position
28
29
const bobberCondition = bot.registry.supportFeature('updatedParticlesPacket')
30
? ((packet.particle.type === 'fishing' || packet.particle.type === 'bubble') && packet.amount === 6 && pos.distanceTo(new Vec3(packet.x, pos.y, packet.z)) <= 1.23)
31
// This "(particles.fishing ?? particles.bubble).id" condition doesn't make sense (these are both valid types)
32
: (packet.particleId === (bot.registry.particlesByName.fishing ?? bot.registry.particlesByName.bubble).id && packet.particles === 6 && pos.distanceTo(new Vec3(packet.x, pos.y, packet.z)) <= 1.23)
33
34
if (bobberCondition) {
35
bot.activateItem()
36
lastBobber = undefined
37
fishingTask.finish()
38
}
39
})
40
bot._client.on('entity_destroy', (packet) => {
41
if (!lastBobber) return
42
if (packet.entityIds.some(id => id === lastBobber.id)) {
43
lastBobber = undefined
44
fishingTask.cancel(new Error('Fishing cancelled'))
45
}
46
})
47
48
async function fish () {
49
if (!fishingTask.done) {
50
fishingTask.cancel(new Error('Fishing cancelled due to calling bot.fish() again'))
51
}
52
53
fishingTask = createTask()
54
55
bot.activateItem()
56
57
await fishingTask.promise
58
}
59
60
bot.fish = fish
61
}
62
63