(bot)
| 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') { |
nothing calls this directly
no test coverage detected
searching dependent graphs…