UNPKG

@nxg-org/mineflayer-util-plugin

Version:

mineflayer utils for NextGEN mineflayer plugins.

142 lines (141 loc) 5.35 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InventoryFunctions = void 0; const util_1 = require("util"); const sleep = (0, util_1.promisify)(setTimeout); /** * Error codes: * * 0 = completed successfully. * * 1 = Item is already equipped. * * 2 = Item could not be found. * * 3 = We are currently equipping an item. */ class InventoryFunctions { constructor(bot) { this.bot = bot; this._equippingMainHand = false; this._equippingOffHand = false; this._equippingOtherSlot = false; this.usingMainHand = false; this.usingOffHand = false; } set equippingOtherSlot(value) { if (this._equippingOtherSlot === value) return; // if (value) this.bot.emit("startedEquippingOtherSlot"); // else this.bot.emit("stoppedEquippingOtherSlot"); this._equippingOtherSlot = value; } get equippingOtherSlot() { return this._equippingOtherSlot; } set equippingMainHand(value) { if (this._equippingMainHand === value) return; // if (value) this.bot.emit("startedEquippingMainHand"); // else this.bot.emit("stoppedEquippingMainHand"); this._equippingMainHand = value; } get equippingMainHand() { return this._equippingMainHand; } set equippingOffHand(value) { if (this._equippingOffHand === value) return; // if (value) this.bot.emit("startedEquippingOffHand"); // else this.bot.emit("stoppedEquippingOffHand"); this._equippingOffHand = value; } get equippingOffHand() { return this._equippingOffHand; } get equipmentSlots() { const ret = ["hand", "head", "torso", "legs", "feet"]; if (!this.bot.supportFeature('doesntHaveOffHandSlot')) { ret.push("off-hand"); } return ret; } getAllItems() { return [ ...this.bot.inventory.items(), ...this.equipmentSlots.map((name) => this.bot.inventory.slots[this.bot.getEquipmentDestSlot(name)]), ].filter((e) => !!e); } getAllItemsExceptCurrent(current) { return [ ...this.bot.inventory.items(), ...(this.equipmentSlots .filter((name) => name !== current)) .map((name) => this.bot.inventory.slots[this.bot.getEquipmentDestSlot(name)]), ].filter((e) => !!e); } getHandWithItem(offhand) { return this.bot.inventory.slots[this.bot.getEquipmentDestSlot(this.getHand(offhand))]; } getHand(offhand = false) { return offhand ? "off-hand" : "hand"; } findItemByID(itemId, metadata) { var _a, _b; if (metadata) return (_a = this.getAllItems().find((item) => item.type === itemId && item.metadata === metadata)) !== null && _a !== void 0 ? _a : null; else return (_b = this.getAllItems().find((item) => item.type === itemId)) !== null && _b !== void 0 ? _b : null; } findItem(name, metadata) { var _a, _b; if (metadata) return (_a = this.getAllItems().find((item) => item.name === name && item.metadata === metadata)) !== null && _a !== void 0 ? _a : null; else return (_b = this.getAllItems().find((item) => item.name === name)) !== null && _b !== void 0 ? _b : null; } //alias. has(name, metadata) { return !!this.findItem(name, metadata); } equipItemRaw(item, dest) { return __awaiter(this, void 0, void 0, function* () { if (this.bot.inventory.slots[this.bot.getEquipmentDestSlot(dest)] === item) return true; yield this.bot.equip(item, dest); return false; }); } customEquip(item, destination, retries = 1) { return __awaiter(this, void 0, void 0, function* () { for (let i = 0; i < retries; i++) { try { yield this.bot.equip(item, destination); return true; } catch (error) { if (this.bot.inventory.selectedItem) { const slot = this.bot.inventory.firstEmptyInventorySlot(false) || -999; try { yield this.bot.clickWindow(slot, 0, 0); } catch (error) { return false; } } } } return false; }); } } exports.InventoryFunctions = InventoryFunctions;