Path: blob/master/lib/plugins/simple_inventory.js
1467 views
const assert = require('assert')12module.exports = inject34const QUICK_BAR_COUNT = 95const QUICK_BAR_START = 3667function inject (bot) {8let nextQuickBarSlot = 0910const armorSlots = {11head: 5,12torso: 6,13legs: 7,14feet: 815}1617if (!bot.supportFeature('doesntHaveOffHandSlot')) {18armorSlots['off-hand'] = 4519}2021async function tossStack (item) {22assert.ok(item)23await bot.clickWindow(item.slot, 0, 0)24await bot.clickWindow(-999, 0, 0)25bot.closeWindow(bot.currentWindow || bot.inventory)26}2728async function toss (itemType, metadata, count) {29const window = bot.currentWindow || bot.inventory30const options = {31window,32itemType,33metadata,34count,35sourceStart: window.inventoryStart,36sourceEnd: window.inventoryEnd,37destStart: -99938}39await bot.transfer(options)40}4142async function unequip (destination) {43if (destination === 'hand') {44await equipEmpty()45} else {46await disrobe(destination)47}48}4950function setQuickBarSlot (slot) {51assert.ok(slot >= 0)52assert.ok(slot < 9)53if (bot.quickBarSlot === slot) return54bot.quickBarSlot = slot55bot._client.write('held_item_slot', {56slotId: slot57})58bot.updateHeldItem()59}6061async function equipEmpty () {62for (let i = 0; i < QUICK_BAR_COUNT; ++i) {63if (!bot.inventory.slots[QUICK_BAR_START + i]) {64setQuickBarSlot(i)65return66}67}68const slot = bot.inventory.firstEmptyInventorySlot()69if (!slot) {70await bot.tossStack(bot.heldItem)71return72}73const equipSlot = QUICK_BAR_START + bot.quickBarSlot74await bot.clickWindow(equipSlot, 0, 0)75await bot.clickWindow(slot, 0, 0)76if (bot.inventory.selectedItem) {77await bot.clickWindow(-999, 0, 0)78}79}8081async function disrobe (destination) {82assert.strictEqual(bot.currentWindow, null)83const destSlot = getDestSlot(destination)84await bot.putAway(destSlot)85}8687async function equip (item, destination) {88if (typeof item === 'number') {89item = bot.inventory.findInventoryItem(item)90}91if (item == null || typeof item !== 'object') {92throw new Error('Invalid item object in equip (item is null or typeof item is not object)')93}94if (!destination || destination === null) {95destination = 'hand'96}97const sourceSlot = item.slot98let destSlot = getDestSlot(destination)99100if (sourceSlot === destSlot) {101// don't need to do anything102return103}104105if (destination !== 'hand') {106await bot.moveSlotItem(sourceSlot, destSlot)107return108}109110if (destSlot >= QUICK_BAR_START && destSlot < (QUICK_BAR_START + QUICK_BAR_COUNT) && sourceSlot >= QUICK_BAR_START && sourceSlot < (QUICK_BAR_START + QUICK_BAR_COUNT)) {111// all we have to do is change the quick bar selection112bot.setQuickBarSlot(sourceSlot - QUICK_BAR_START)113return114}115116// find an empty slot on the quick bar to put the source item in117destSlot = bot.inventory.firstEmptySlotRange(QUICK_BAR_START, QUICK_BAR_START + QUICK_BAR_COUNT)118if (destSlot == null) {119// LRU cache for the quick bar items120destSlot = QUICK_BAR_START + nextQuickBarSlot121nextQuickBarSlot = (nextQuickBarSlot + 1) % QUICK_BAR_COUNT122}123setQuickBarSlot(destSlot - QUICK_BAR_START)124await bot.moveSlotItem(sourceSlot, destSlot)125}126127function getDestSlot (destination) {128if (destination === 'hand') {129return QUICK_BAR_START + bot.quickBarSlot130} else {131const destSlot = armorSlots[destination]132assert.ok(destSlot != null, `invalid destination: ${destination}`)133return destSlot134}135}136137function leftMouse (slot) {138return bot.clickWindow(slot, 0, 0)139}140141function rightMouse (slot) {142return bot.clickWindow(slot, 1, 0)143}144145bot.equip = equip146bot.unequip = unequip147bot.toss = toss148bot.tossStack = tossStack149bot.setQuickBarSlot = setQuickBarSlot150bot.getEquipmentDestSlot = getDestSlot151bot.simpleClick = { leftMouse, rightMouse }152153// constants154bot.QUICK_BAR_START = QUICK_BAR_START155}156157158