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/jumper.js
Views: 789
1
/*
2
* Jumping is fun. Riding pigs is even funnier!
3
*
4
* Learn how to make your bot interactive with this example.
5
*
6
* This bot can move, jump, ride vehicles, attack nearby entities and much more.
7
*/
8
const mineflayer = require('mineflayer')
9
10
if (process.argv.length < 4 || process.argv.length > 6) {
11
console.log('Usage : node jumper.js <host> <port> [<name>] [<password>]')
12
process.exit(1)
13
}
14
15
const bot = mineflayer.createBot({
16
host: process.argv[2],
17
port: parseInt(process.argv[3]),
18
username: process.argv[4] ? process.argv[4] : 'jumper',
19
password: process.argv[5]
20
})
21
22
let target = null
23
24
bot.on('chat', (username, message) => {
25
if (username === bot.username) return
26
target = bot.players[username].entity
27
let entity
28
switch (message) {
29
case 'forward':
30
bot.setControlState('forward', true)
31
break
32
case 'back':
33
bot.setControlState('back', true)
34
break
35
case 'left':
36
bot.setControlState('left', true)
37
break
38
case 'right':
39
bot.setControlState('right', true)
40
break
41
case 'sprint':
42
bot.setControlState('sprint', true)
43
break
44
case 'stop':
45
bot.clearControlStates()
46
break
47
case 'jump':
48
bot.setControlState('jump', true)
49
bot.setControlState('jump', false)
50
break
51
case 'jump a lot':
52
bot.setControlState('jump', true)
53
break
54
case 'stop jumping':
55
bot.setControlState('jump', false)
56
break
57
case 'attack':
58
entity = bot.nearestEntity()
59
if (entity) {
60
bot.attack(entity, true)
61
} else {
62
bot.chat('no nearby entities')
63
}
64
break
65
case 'mount':
66
entity = bot.nearestEntity((entity) => { return entity.name === 'minecart' })
67
if (entity) {
68
bot.mount(entity)
69
} else {
70
bot.chat('no nearby objects')
71
}
72
break
73
case 'dismount':
74
bot.dismount()
75
break
76
case 'move vehicle forward':
77
bot.moveVehicle(0.0, 1.0)
78
break
79
case 'move vehicle backward':
80
bot.moveVehicle(0.0, -1.0)
81
break
82
case 'move vehicle left':
83
bot.moveVehicle(1.0, 0.0)
84
break
85
case 'move vehicle right':
86
bot.moveVehicle(-1.0, 0.0)
87
break
88
case 'tp':
89
bot.entity.position.y += 10
90
break
91
case 'pos':
92
bot.chat(bot.entity.position.toString())
93
break
94
case 'yp':
95
bot.chat(`Yaw ${bot.entity.yaw}, pitch: ${bot.entity.pitch}`)
96
break
97
}
98
})
99
100
bot.once('spawn', () => {
101
// keep your eyes on the target, so creepy!
102
setInterval(watchTarget, 50)
103
104
function watchTarget () {
105
if (!target) return
106
bot.lookAt(target.position.offset(0, target.height, 0))
107
}
108
})
109
110
bot.on('mount', () => {
111
bot.chat(`mounted ${bot.vehicle.displayName}`)
112
})
113
114
bot.on('dismount', (vehicle) => {
115
bot.chat(`dismounted ${vehicle.displayName}`)
116
})
117
118