From d6c003850d86ba0c09408175454d73346a6057d4 Mon Sep 17 00:00:00 2001 From: William Mantly Date: Fri, 2 Jun 2023 12:41:43 -0400 Subject: [PATCH] more trade? --- nodejs/controller/commands/trade.js | 2 +- nodejs/controller/mc-bot.js | 3 +- nodejs/model/minecraft.js | 49 ++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/nodejs/controller/commands/trade.js b/nodejs/controller/commands/trade.js index 1ae9568..5d9d4d0 100644 --- a/nodejs/controller/commands/trade.js +++ b/nodejs/controller/commands/trade.js @@ -69,7 +69,7 @@ module.exports = { // Give MC a moment await sleep(1000); - let isPutAway = await this.putInChest(chestBlock) + let isPutAway = await this.dumpToChest(chestBlock) await this.whisper(from, `I put ${isPutAway ? 'all' : 'some'} items in your chest.`); } } diff --git a/nodejs/controller/mc-bot.js b/nodejs/controller/mc-bot.js index a47f57b..b798f43 100644 --- a/nodejs/controller/mc-bot.js +++ b/nodejs/controller/mc-bot.js @@ -4,7 +4,7 @@ const {sleep} = require('../utils'); const conf = require('../conf'); const {CJbot} = require('../model/minecraft'); -// const inventoryViewer = require('mineflayer-web-inventory'); +const inventoryViewer = require('mineflayer-web-inventory'); const commands = require('./commands'); @@ -21,6 +21,7 @@ for(let name in conf.mc.bots){ } bot.on('onReady', async function(argument) { + inventoryViewer(bot.bot); await sleep(1000); bot.bot.setControlState('jump', true); setTimeout(()=> bot.bot.setControlState('jump', false), 5000) diff --git a/nodejs/model/minecraft.js b/nodejs/model/minecraft.js index 76f11de..bda3a55 100644 --- a/nodejs/model/minecraft.js +++ b/nodejs/model/minecraft.js @@ -1,6 +1,7 @@ 'use strict'; const mineflayer = require('mineflayer'); +const minecraftData = require('minecraft-data'); const Vec3 = require('vec3'); const {sleep} = require('../utils'); @@ -105,6 +106,9 @@ class CJbot{ // server async __onReady(){try{ + this.mcData = minecraftData(this.bot.version); + + // Add the listeners to the bot. We do this so if the bot loses // connection, the mineflayer instance will also be lost. this.__startListeners(); @@ -352,35 +356,56 @@ class CJbot{ return firstEmptySlot; } - async putInChest(block, blockName, amount) { + async dumpToChest(block, blockName, amount) { block = this.__blockOrVec(block); this.bot.openContainer(block); let window = await this.once('windowOpen'); - for(let item of window.slots.slice(window.inventoryStart).filter(function(item){ + let items = window.slots.slice(window.inventoryStart).filter(function(item){ if(!item) return false; if(blockName && blockName !== item.name) return false; return true; - })){ + }); + + for(let item of items){ + console.log(`Trying to place ${item.name}`) + await sleep(100); let currentSlot = Number(item.slot); if(!window.slots[currentSlot]) continue; - if(amount && !amount--) return; - let chestSlot = await this.__nextContainerSlot(window, item); - console.log('next slot', chestSlot); - await this.bot.moveSlotItem(currentSlot, chestSlot) - - let res = await this.putInChest(...arguments); - if(res === false) return amount ? amount : false; + // let chestSlot = await this.__nextContainerSlot(window, item); + // console.log('next chest slot', chestSlot) + // if(!chestSlot){ + // console.log(`No room for ${item.name}`) + // continue; + // } + + console.log('item', this.mcData.itemsByName[item.name]) + + try{ + console.log('transfer', await this.bot.transfer({ + window, + itemType: this.mcData.itemsByName[item.name].id, + sourceStart: currentSlot, + sourceEnd: currentSlot+1, + destStart: 0, + destEnd: window.inventoryStart, + count: amount || item.count, + })) + + if(amount) amount -= item.count + }catch(error){ + console.log('error?', item.count, error.message, error); + } + // await this.bot.moveSlotItem(currentSlot, chestSlot); } + await sleep(100); await this.bot.closeWindow(window); return amount ? amount : true; } - - } module.exports = {CJbot};