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/graffiti.js
Views: 789
1
/*
2
* What's better than a bot that knows how to read and understands art?
3
*
4
* Learn how easy it is to interact with signs and paintings in this example.
5
*
6
* You can send commands to this bot using chat messages, the bot will
7
* reply by telling you the name of the nearest painting or the text written on
8
* the nearest sign, and you can also update signs with custom messages!
9
*
10
* To update a sign simply send a message in this format: write [your message]
11
*/
12
const mineflayer = require('mineflayer')
13
14
if (process.argv.length < 4 || process.argv.length > 6) {
15
console.log('Usage : node graffiti.js <host> <port> [<name>] [<password>]')
16
process.exit(1)
17
}
18
19
const bot = mineflayer.createBot({
20
host: process.argv[2],
21
port: parseInt(process.argv[3]),
22
username: process.argv[4] ? process.argv[4] : 'graffiti',
23
password: process.argv[5]
24
})
25
26
bot.on('chat', (username, message) => {
27
if (username === bot.username) return
28
switch (true) {
29
case /^watch$/.test(message):
30
watchPaintingOrSign()
31
break
32
case /^write .+$/.test(message):
33
// write message
34
// ex: write I love diamonds
35
updateSign(message)
36
break
37
}
38
})
39
40
function watchPaintingOrSign () {
41
const paintingBlock = bot.findBlock({
42
matching (block) {
43
return !!block.painting
44
}
45
})
46
const signBlock = bot.findBlock({
47
matching: ['painting', 'sign'].map(name => bot.registry.blocksByName[name].id)
48
})
49
if (signBlock) {
50
bot.chat(`The sign says: ${signBlock.signText}`)
51
} else if (paintingBlock) {
52
bot.chat(`The painting is: ${paintingBlock.painting.name}`)
53
} else {
54
bot.chat('There are no signs or paintings near me')
55
}
56
}
57
58
function updateSign (message) {
59
const signBlock = bot.findBlock({
60
matching: ['painting', 'sign'].map(name => bot.registry.blocksByName[name].id)
61
})
62
if (signBlock) {
63
bot.updateSign(signBlock, message.split(' ').slice(1).join(' '))
64
bot.chat('Sign updated')
65
} else {
66
bot.chat('There are no signs near me')
67
}
68
}
69
70