better trade
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
const mineflayer = require('mineflayer');
|
||||
const minecraftData = require('minecraft-data');
|
||||
const { pathfinder, Movements, goals: { GoalNear } } = require('mineflayer-pathfinder');
|
||||
const Vec3 = require('vec3');
|
||||
const {sleep} = require('../utils');
|
||||
|
||||
@ -106,8 +107,11 @@ class CJbot{
|
||||
// server
|
||||
async __onReady(){try{
|
||||
|
||||
this.bot.loadPlugin(pathfinder);
|
||||
this.mcData = minecraftData(this.bot.version);
|
||||
|
||||
this.defaultMove = new Movements(this.bot, this.mcData);
|
||||
this.defaultMove.canDig = false
|
||||
this.bot.pathfinder.setMovements(this.defaultMove);
|
||||
|
||||
// Add the listeners to the bot. We do this so if the bot loses
|
||||
// connection, the mineflayer instance will also be lost.
|
||||
@ -306,6 +310,7 @@ class CJbot{
|
||||
await this.commands[cmd].function.call(this, from, ...parts);
|
||||
}catch(error){
|
||||
this.whisper(from, `The command encountered an error.`);
|
||||
this.whisper(from, `ERROR: ${error}`);
|
||||
console.error(`Chat command error on ${cmd} from ${from}\n`, error);
|
||||
}
|
||||
this.__unLockCommand();
|
||||
@ -337,6 +342,40 @@ class CJbot{
|
||||
throw new Error('Not supported block identifier');
|
||||
}
|
||||
|
||||
async _goTo(block, range=2){
|
||||
block = this.__blockOrVec(block);
|
||||
|
||||
return await this.bot.pathfinder.goto(new GoalNear(...block.position.toArray(), range));
|
||||
}
|
||||
|
||||
goTo(options){
|
||||
console.log('goTo options', options)
|
||||
return new Promise(async(resolve, reject)=>{
|
||||
|
||||
let range = options.range || 2;
|
||||
|
||||
try{
|
||||
await this._goTo(options.where, range)
|
||||
return resolve();
|
||||
}catch(error){
|
||||
console.log('goTo error', error)
|
||||
if(options.reTry) return reject('Action can not move to where')
|
||||
await this._goTo(options, true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async goToReturn(options){
|
||||
let here = this.bot.entity.position;
|
||||
let hereYaw = this.bot.entity.yaw
|
||||
await this.goTo(options);
|
||||
return async () =>{
|
||||
await this.goTo({where: here, range: 0}, true);
|
||||
await sleep(500);
|
||||
await this.bot.look(Math.floor(hereYaw), 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async __nextContainerSlot(window, item) {
|
||||
let firstEmptySlot = false;
|
||||
@ -356,11 +395,28 @@ class CJbot{
|
||||
return firstEmptySlot;
|
||||
}
|
||||
|
||||
async dumpToChest(block, blockName, amount) {
|
||||
async openContainer(block){
|
||||
let count = 0;
|
||||
block = this.__blockOrVec(block);
|
||||
|
||||
this.bot.openContainer(block);
|
||||
let window = await this.once('windowOpen');
|
||||
while(!this.bot.currentWindow){
|
||||
let window = this.bot.openContainer(block);
|
||||
await sleep(1500);
|
||||
if(this.bot.currentWindow?.title){
|
||||
break;
|
||||
}
|
||||
this.bot.removeAllListeners('windowOpen');
|
||||
|
||||
if(count++ == 3) throw 'Block wont open';
|
||||
}
|
||||
|
||||
return this.bot.currentWindow;
|
||||
|
||||
}
|
||||
|
||||
async dumpToChest(block, blockName, amount) {
|
||||
|
||||
let window = await this.openContainer(block);
|
||||
|
||||
let items = window.slots.slice(window.inventoryStart).filter(function(item){
|
||||
if(!item) return false;
|
||||
@ -369,8 +425,7 @@ class CJbot{
|
||||
});
|
||||
|
||||
for(let item of items){
|
||||
console.log(`Trying to place ${item.name}`)
|
||||
await sleep(100);
|
||||
await sleep(500);
|
||||
let currentSlot = Number(item.slot);
|
||||
if(!window.slots[currentSlot]) continue;
|
||||
|
||||
@ -381,18 +436,17 @@ class CJbot{
|
||||
// continue;
|
||||
// }
|
||||
|
||||
console.log('item', this.mcData.itemsByName[item.name])
|
||||
|
||||
try{
|
||||
console.log('transfer', await this.bot.transfer({
|
||||
await this.bot.transfer({
|
||||
window,
|
||||
itemType: this.mcData.itemsByName[item.name].id,
|
||||
sourceStart: currentSlot,
|
||||
sourceEnd: currentSlot+1,
|
||||
destStart: 0,
|
||||
destEnd: window.inventoryStart,
|
||||
destEnd: window.inventoryStart-1,
|
||||
count: amount || item.count,
|
||||
}))
|
||||
})
|
||||
|
||||
if(amount) amount -= item.count
|
||||
}catch(error){
|
||||
@ -401,7 +455,7 @@ class CJbot{
|
||||
// await this.bot.moveSlotItem(currentSlot, chestSlot);
|
||||
}
|
||||
|
||||
await sleep(100);
|
||||
await sleep(1000);
|
||||
await this.bot.closeWindow(window);
|
||||
|
||||
return amount ? amount : true;
|
||||
|
Reference in New Issue
Block a user