Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/book.js
1467 views
1
const assert = require('assert')
2
const { once } = require('../promise_utils')
3
4
module.exports = inject
5
6
function inject (bot) {
7
const Item = require('prismarine-item')(bot.registry)
8
9
let editBook
10
if (bot.supportFeature('editBookIsPluginChannel')) {
11
bot._client.registerChannel('MC|BEdit', 'slot')
12
bot._client.registerChannel('MC|BSign', 'slot')
13
editBook = (book, pages, title, slot, signing = false) => {
14
if (signing) bot._client.writeChannel('MC|BSign', Item.toNotch(book))
15
else bot._client.writeChannel('MC|BEdit', Item.toNotch(book))
16
}
17
} else if (bot.supportFeature('hasEditBookPacket')) {
18
if (bot.supportFeature('editBookPacketUsesNbt')) { // 1.13 - 1.17
19
editBook = (book, pages, title, slot, signing = false, hand = 0) => {
20
bot._client.write('edit_book', {
21
hand: slot,
22
pages,
23
title
24
})
25
}
26
} else { // 1.18+
27
editBook = (book, pages, title, slot, signing = false, hand = 0) => {
28
bot._client.write('edit_book', {
29
new_book: Item.toNotch(book),
30
signing,
31
hand
32
})
33
}
34
}
35
}
36
37
async function write (slot, pages, author, title, signing) {
38
assert.ok(slot >= 0 && slot <= 44, 'slot out of inventory range')
39
const book = bot.inventory.slots[slot]
40
assert.ok(book && book.type === bot.registry.itemsByName.writable_book.id, `no book found in slot ${slot}`)
41
const quickBarSlot = bot.quickBarSlot
42
const moveToQuickBar = slot < 36
43
44
if (moveToQuickBar) {
45
await bot.moveSlotItem(slot, 36)
46
}
47
48
bot.setQuickBarSlot(moveToQuickBar ? 0 : slot - 36)
49
50
const modifiedBook = await modifyBook(moveToQuickBar ? 36 : slot, pages, author, title, signing)
51
editBook(modifiedBook, pages, title, moveToQuickBar ? 0 : slot - 36, signing)
52
await once(bot.inventory, `updateSlot:${moveToQuickBar ? 36 : slot}`)
53
54
bot.setQuickBarSlot(quickBarSlot)
55
56
if (moveToQuickBar) {
57
await bot.moveSlotItem(36, slot)
58
}
59
}
60
61
function modifyBook (slot, pages, author, title, signing) {
62
const book = Object.assign({}, bot.inventory.slots[slot])
63
if (!book.nbt || book.nbt.type !== 'compound') {
64
book.nbt = {
65
type: 'compound',
66
name: '',
67
value: {}
68
}
69
}
70
if (signing) {
71
if (bot.supportFeature('clientUpdateBookIdWhenSign')) {
72
book.type = bot.registry.itemsByName.written_book.id
73
}
74
book.nbt.value.author = {
75
type: 'string',
76
value: author
77
}
78
book.nbt.value.title = {
79
type: 'string',
80
value: title
81
}
82
}
83
book.nbt.value.pages = {
84
type: 'list',
85
value: {
86
type: 'string',
87
value: pages
88
}
89
}
90
bot.inventory.updateSlot(slot, book)
91
return book
92
}
93
94
bot.writeBook = async (slot, pages) => {
95
await write(slot, pages, null, null, false)
96
}
97
98
bot.signBook = async (slot, pages, author, title) => {
99
await write(slot, pages, author, title, true)
100
}
101
}
102
103