Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/examples/book.js
9427 views
1
const mineflayer = require('mineflayer')
2
3
if (process.argv.length < 4 || process.argv.length > 6) {
4
console.log('Usage : node book.js <host> <port> [<name>] [<password>]')
5
process.exit(1)
6
}
7
8
const bot = mineflayer.createBot({
9
host: process.argv[2],
10
port: parseInt(process.argv[3]),
11
username: process.argv[4] ? process.argv[4] : 'book',
12
password: process.argv[5]
13
})
14
15
const pages = [
16
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
17
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
18
'Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.',
19
'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
20
].map(page => page
21
.split(' ')
22
.map((word, i) => `§${(i % 13 + 1).toString(16)}${i % 2 ? '§l' : ''}${word}`)
23
.join(' '))
24
25
bot.once('login', () => console.log('logged in'))
26
27
bot.on('chat', (username, message) => {
28
if (username === bot.username) return
29
switch (message) {
30
case 'print':
31
print()
32
break
33
case 'write':
34
write()
35
break
36
case 'toss':
37
toss()
38
break
39
}
40
})
41
42
function toss () {
43
const [book] = bot.inventory.items().filter(({ name }) => name === 'writable_book')
44
bot.tossStack(book)
45
}
46
47
async function write () {
48
const [book] = bot.inventory.items().filter(({ name }) => name === 'writable_book')
49
if (!book) {
50
bot.chat("I don't have a book.")
51
return
52
}
53
await bot.writeBook(book.slot, pages)
54
print()
55
}
56
57
function print () {
58
const [book] = bot.inventory.items().filter(({ name }) => name === 'writable_book')
59
book.nbt.value.pages.value.value.forEach((page, i) => bot.chat(`Page ${i + 1}: ${page.replace(/§[a-z0-9]/g, '')}`))
60
}
61
62