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/elytra.js
Views: 789
1
// This example will shoot the player that said "fire" in chat, when it is said in chat.
2
const mineflayer = require('mineflayer')
3
4
if (process.argv.length < 4 || process.argv.length > 6) {
5
console.log('Usage : node elytra.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] : 'elytraer',
13
password: process.argv[5]
14
})
15
16
bot.on('error', err => {
17
console.log(err)
18
})
19
20
bot.on('kicked', err => {
21
console.log(err)
22
})
23
24
bot.on('spawn', async function () {
25
bot.chat(`/give ${bot.username} minecraft:elytra`)
26
bot.chat(`/give ${bot.username} minecraft:firework_rocket 64`)
27
28
await sleep(1000)
29
const elytraItem = bot.inventory.slots.find(item => item?.name === 'elytra')
30
if (elytraItem == null) {
31
console.log('no elytra')
32
return
33
}
34
await bot.equip(elytraItem, 'torso')
35
const fireworkItem = bot.inventory.slots.find(item => item?.name === 'firework_rocket')
36
if (fireworkItem == null) {
37
console.log('no fireworks')
38
return
39
}
40
await bot.equip(fireworkItem, 'hand')
41
})
42
43
bot.on('chat', async (username, message) => {
44
if (message === 'fly') {
45
await bot.look(bot.entity.yaw, 50 * Math.PI / 180)
46
bot.setControlState('jump', true)
47
bot.setControlState('jump', false)
48
await sleep(50)
49
50
// try to fly
51
try {
52
await bot.elytraFly()
53
} catch (err) {
54
bot.chat(`Failed to fly: ${err}`)
55
return
56
}
57
await sleep(50)
58
59
// use rocket
60
bot.activateItem()
61
}
62
})
63
64
function sleep (ms) {
65
return new Promise(resolve => setTimeout(resolve, ms))
66
}
67
68