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/armor_stand.js
Views: 789
1
/*
2
* This script will apply armor onto an armor stand within 4 blocks of the bot
3
*/
4
const mineflayer = require('mineflayer')
5
6
if (process.argv.length < 4 || process.argv.length > 6) {
7
console.log('Usage : node armor_stand.js <host> <port> [<name>] [<password>]')
8
process.exit(1)
9
}
10
11
const bot = mineflayer.createBot({
12
host: process.argv[2],
13
port: parseInt(process.argv[3]),
14
username: process.argv[4] ? process.argv[4] : 'armorStand',
15
password: process.argv[5]
16
})
17
18
const armorTypes = {
19
helmet: [0, 1.8, 0],
20
chestplate: [0, 1.2, 0],
21
leggings: [0, 0.75, 0],
22
boots: [0, 0.1, 0]
23
}
24
25
bot.on('chat', async (username, message) => {
26
const [mainCommand, subCommand] = message.split(' ')
27
if (mainCommand !== 'equip' && mainCommand !== 'unequip') return
28
29
const armorStand = bot.nearestEntity(e => e.displayName === 'Armor Stand' && bot.entity.position.distanceTo(e.position) < 4)
30
if (!armorStand) {
31
bot.chat('No armor stands nearby!')
32
return
33
}
34
35
if (mainCommand === 'equip') {
36
let armor = null
37
// parse chat
38
Object.keys(armorTypes).forEach(armorType => {
39
if (subCommand !== armorType) return
40
armor = bot.inventory.items().find(item => item.name.includes(armorType))
41
})
42
43
if (armor === null) {
44
bot.chat('I have no armor items in my inventory!')
45
return
46
}
47
48
await bot.equip(armor, 'hand')
49
bot.activateEntityAt(armorStand, armorStand.position)
50
} else if (mainCommand === 'unequip') {
51
await bot.unequip('hand')
52
53
const offset = armorTypes[subCommand]
54
if (!offset) return
55
56
bot.activateEntityAt(armorStand, armorStand.position.offset(...offset))
57
}
58
})
59
60