forked from wmantly/mc-bot-town
		
	better trade
This commit is contained in:
		| @ -55,7 +55,6 @@ let sites = { | ||||
|     german: { | ||||
|         bot: 'linda', | ||||
|         desc: `Get an invite you Germans area.`, | ||||
|         allowed: ['wmantly', 'useless666', 'tux4242', 'owenshorts', 'VinceNL', 'Ethan63020', 'Ethan63021', 'pi_chef', 'YTMatze', 'mytzor', 'pi_chef', '1_cut', 'nootbot'], | ||||
|         allowed: ['wmantly', 'useless666', 'tux4242', 'owenshorts', 'VinceNL', 'Ethan63020', 'Ethan63021', 'pi_chef', 'YTMatze', 'mytzor', 'pi_chef', '1_cut', 'nootbot', 'Lost_Imback',], | ||||
|     }, | ||||
| } | ||||
|  | ||||
| @ -10,7 +10,7 @@ const customerSlots = [5, 6, 7, 8, 14, 15, 16, 17, 23, 24, 25, 26]; | ||||
| function findChestBySign(bot, text){ | ||||
|     let sign = bot.bot.findBlock({ | ||||
|         useExtraInfo: true, | ||||
|         // maxDistance: 1, | ||||
|         maxDistance: 32, | ||||
|         matching: (block)=> { | ||||
|             if(block.name.includes('sign') && block.signText.includes(text)){ | ||||
|                 return true; | ||||
| @ -49,7 +49,7 @@ module.exports = { | ||||
|                 this.bot.closeWindow('window'); | ||||
|                 this.bot.removeAllListeners('windowOpen'); | ||||
|                 this.whisper(from, `I have things to do, I cant wait on you all day!`) | ||||
|             }, 30000); | ||||
|             }, 120000); | ||||
|  | ||||
|             // Check to see if the remote user has agreed to the trade. | ||||
|             let confirmationCheck = setInterval(async ()=>{ | ||||
| @ -64,17 +64,32 @@ module.exports = { | ||||
|  | ||||
|             // If the trade took so long it timed out, just kill the whole thing. | ||||
|             if(timeoutCheck._destroyed) return; | ||||
|             console.log('clearing timeout'); | ||||
|             clearTimeout(timeoutCheck) | ||||
|  | ||||
|             // Give MC a moment | ||||
|             await sleep(1000); | ||||
|             // await sleep(1000); | ||||
|  | ||||
|             let goBack = await this.goToReturn({where: chestBlock, reTry: true}) | ||||
|  | ||||
|             let isPutAway = await this.dumpToChest(chestBlock) | ||||
|  | ||||
|             await goBack(); | ||||
|  | ||||
|             await this.whisper(from, `I put ${isPutAway ? 'all' : 'some'} items in your chest.`); | ||||
|         } | ||||
|     }, | ||||
|     'test': { | ||||
|         desc: 'go away', | ||||
|         allowed: ['useless666'], | ||||
|         async function(from){ | ||||
|             let chestBlock = findChestBySign(this, from); | ||||
|              | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| @ -21,11 +21,16 @@ for(let name in conf.mc.bots){ | ||||
| 	} | ||||
|  | ||||
| 	bot.on('onReady', async function(argument) { | ||||
| 		inventoryViewer(bot.bot); | ||||
| 		// inventoryViewer(bot.bot); | ||||
| 		await sleep(1000); | ||||
| 		bot.bot.setControlState('jump', true); | ||||
| 		console.log(bot.bot.entity); | ||||
| 		setTimeout(()=> bot.bot.setControlState('jump', false), 5000) | ||||
| 	}) | ||||
|  | ||||
| 	// bot.on('message', function(...args){ | ||||
| 	// 	console.log('message | ', ...args) | ||||
| 	// }) | ||||
| } | ||||
|  | ||||
| (async ()=>{ | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
							
								
								
									
										1925
									
								
								nodejs/package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										1925
									
								
								nodejs/package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user