diff --git a/docs/api.md b/docs/api.md index b154409ed..e885e9214 100644 --- a/docs/api.md +++ b/docs/api.md @@ -38,6 +38,15 @@ - [furnace.outputItem()](#furnaceoutputitem) - [furnace.fuel](#furnacefuel) - [furnace.progress](#furnaceprogress) + - [mineflayer.CartographyTable](#mineflayercartographytable) + - [cartographyTable.takeMap()](#cartographytabletakemap) + - [cartographyTable.takeModifier()](#cartographytabletakemodifier) + - [cartographyTable.takeOutput()](#cartographytabletakeoutput) + - [cartographyTable.putMap(itemType, metadata, count)](#cartographytableputmapitemtype-metadata-count) + - [cartographyTable.putModifier(itemType, metadata, count)](#cartographytableputmodifieritemtype-metadata-count) + - [cartographyTable.mapItem()](#cartographytablemapitem) + - [cartographyTable.modifierItem()](#cartographytablemodifieritem) + - [cartographyTable.outputItem()](#cartographytableoutputitem) - [mineflayer.EnchantmentTable](#mineflayerenchantmenttable) - [enchantmentTable "ready"](#enchantmenttable-ready) - [enchantmentTable.targetItem()](#enchantmenttabletargetitem) @@ -325,6 +334,7 @@ - [bot.openContainer(containerBlock or containerEntity, direction?, cursorPos?)](#botopencontainercontainerblock-or-containerentity-direction-cursorpos) - [bot.openChest(chestBlock or minecartchestEntity, direction?, cursorPos?)](#botopenchestchestblock-or-minecartchestentity-direction-cursorpos) - [bot.openFurnace(furnaceBlock)](#botopenfurnacefurnaceblock) + - [bot.openCartographyTable(cartographyTableBlock)](#botopencartographytablecartographytableblock) - [bot.openDispenser(dispenserBlock)](#botopendispenserdispenserblock) - [bot.openEnchantmentTable(enchantmentTableBlock)](#botopenenchantmenttableenchantmenttableblock) - [bot.openAnvil(anvilBlock)](#botopenanvilanvilblock) @@ -543,6 +553,47 @@ How much fuel is left between 0 and 1. How much cooked the input is between 0 and 1. +### mineflayer.CartographyTable + +Extends windows.Window for cartography table +See `bot.openCartographyTable(cartographyTableBlock)`. + +#### cartographyTable.takeMap() + +This function returns a `Promise`, with `item` as its argument upon completion. + + +#### cartographyTable.takeModifier() + +This function returns a `Promise`, with `item` as its argument upon completion. + + +#### cartographyTable.takeOutput() + +This function returns a `Promise`, with `item` as its argument upon completion. + + +#### cartographyTable.putMap(itemType, metadata, count) + +This function returns a `Promise`, with `void` as its argument upon completion. + +#### cartographyTable.putModifier(itemType, metadata, count) + +This function returns a `Promise`, with `void` as its argument upon completion. + +#### cartographyTable.mapItem() + +Returns `Item` instance which is the filled map being affected. + +#### cartographyTable.modifierItem() + +Returns `Item` instance which is the item being used to effect the filled map, e.g. Glass Pane, paper, empty map, etc. + +#### cartographyTable.outputItem() + +Returns `Item` instance which is the output (aka the modified map). + + ### mineflayer.EnchantmentTable Extends windows.Window for enchantment tables @@ -2083,6 +2134,10 @@ Deprecated. Same as `openContainer` Returns a promise on a `Furnace` instance which represents the furnace you are opening. +#### bot.openCartographyTable(cartographyTableBlock) + +Returns a promise on a `CartographyTable` instance which represents the cartography table you are opening. + #### bot.openDispenser(dispenserBlock) Deprecated. Same as `openContainer` diff --git a/lib/loader.js b/lib/loader.js index 11ebf38d5..d468d4eda 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -21,6 +21,7 @@ const plugins = { explosion: require('./plugins/explosion'), fishing: require('./plugins/fishing'), furnace: require('./plugins/furnace'), + cartography_table: require('./plugins/cartography_table'), game: require('./plugins/game'), health: require('./plugins/health'), inventory: require('./plugins/inventory'), diff --git a/lib/plugins/cartography_table.js b/lib/plugins/cartography_table.js new file mode 100644 index 000000000..b5819ef8a --- /dev/null +++ b/lib/plugins/cartography_table.js @@ -0,0 +1,75 @@ +const assert = require('assert') + +module.exports = inject + +function inject(bot) { + const allowedWindowTypes = ['minecraft:cartography'] + + function matchWindowType(window) { + for (const type of allowedWindowTypes) { + if (window.type.startsWith(type)) return true + } + return false + } + + async function openCartographyTable(cartographyTableBlock) { + const cartographyTable = await bot.openBlock(cartographyTableBlock) + if (!matchWindowType(cartographyTable)) { + throw new Error('This is not a cartographyTable-like window') + } + + cartographyTable.takeMap = takeMap + cartographyTable.takeModifier = takeModifier + cartographyTable.takeOutput = takeOutput + cartographyTable.putMap = putMap + cartographyTable.putModifier = putModifier + cartographyTable.mapItem = function () { return this.slots[0] } + cartographyTable.modifierItem = function () { return this.slots[1] } + cartographyTable.outputItem = function () { return this.slots[2] } + + return cartographyTable + + async function takeSomething(item) { + assert.ok(item) + await bot.putAway(item.slot) + return item + } + + async function takeMap() { + return takeSomething(cartographyTable.mapItem()) + } + + async function takeModifier() { + return takeSomething(cartographyTable.modifierItem()) + } + + async function takeOutput() { + return takeSomething(cartographyTable.outputItem()) + } + + async function putSomething(destSlot, itemType, metadata, count) { + const options = { + window: cartographyTable, + itemType, + metadata, + count, + sourceStart: cartographyTable.inventoryStart, + sourceEnd: cartographyTable.inventoryEnd, + destStart: destSlot, + destEnd: destSlot + 1 + } + await bot.transfer(options) + } + + async function putMap(itemType, metadata, count) { + await putSomething(0, itemType, metadata, count) + } + + async function putModifier(itemType, metadata, count) { + await putSomething(1, itemType, metadata, count) + } + } + + bot.openCartographyTable = openCartographyTable + +}