Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/examples/place_end_crystal/index.js
Views: 789
// This example shows how to tell the bot to place an end crystal1// Example commands:2// "place crystal near bot" - find a block near the bot that has a block empty above it, then place a crystal on it3// "place crystal near ExplodeMe" - find a block near the player with username "ExplodeMe" that has an empty block above it, then place a crystal on it45const mineflayer = require('mineflayer')6const pathfinder = require('mineflayer-pathfinder')7const { Vec3 } = require('vec3')8const AABB = require('prismarine-physics/lib/aabb')910if (process.argv.length < 4 || process.argv.length > 6) {11console.log('Usage : node ansi.js <host> <port> [<name>] [<password>]')12process.exit(1)13}14const bot = mineflayer.createBot({15host: process.argv[2],16port: parseInt(process.argv[3]),17username: process.argv[4] ? process.argv[4] : 'crystal_bot',18password: process.argv[5],19plugins: [pathfinder.pathfinder]20})2122const MAX_DIST_FROM_BLOCK_TO_PLACE = 42324bot.on('chat', async (ign, msg) => {25// solve where the bot will place the crystal near26let findBlocksNearPoint = null27if (msg === 'place crystal near bot') {28findBlocksNearPoint = bot.entity.position29} else if (msg.startsWith('place crystal near ')) {30const playerUsername = msg.split(' ').pop()31const player = bot.players[playerUsername]32if (!player) return bot.chat(`Couldn't find player with the username: "${playerUsername}"`)33findBlocksNearPoint = player.entity.position34} else {35return // don't do anything36}3738// find end crystal(s) in inventory39const item = bot.inventory.findInventoryItem(bot.registry.itemsByName.end_crystal.id)40if (!item) bot.chat("I don't have any ender crystals")4142// find the crystal43const block = bot.findBlock({44point: findBlocksNearPoint,45matching: ['bedrock', 'obsidian'].map(blockName => bot.registry.blocksByName[blockName].id),46useExtraInfo: block => {47const hasAirAbove = bot.blockAt(block.position.offset(0, 1, 0)).name === 'air'48const botNotStandingOnBlock = block.position.xzDistanceTo(bot.entity.position) > 249// do no intersecting entity check50const { x: aboveX, y: aboveY, z: aboveZ } = block.position.offset(0, 1, 0)51const blockBoundingBox = new AABB(aboveX, aboveY, aboveZ, aboveX + 1, aboveY + 2, aboveZ + 1)52const entityAABBs = Object.values(bot.entities).map(entity => {53// taken from https://github.com/PrismarineJS/prismarine-physics/blob/d145e54a4bb8604300258badd7563f59f2101922/index.js#L9254const w = entity.height / 255const { x, y, z } = entity.position56return new AABB(-w, 0, -w, w, entity.height, w).offset(x, y, z)57})58const hasIntersectingEntities = entityAABBs.filter(aabb => aabb.intersects(blockBoundingBox)).length === 059return hasAirAbove && botNotStandingOnBlock && !hasIntersectingEntities60}61})62if (!block) return bot.chat("Couldn't find bedrock or obsidian block that has air above it near myself.")6364// get to the crystal65if (block.position.xzDistanceTo(bot.entity.position) > MAX_DIST_FROM_BLOCK_TO_PLACE) await bot.pathfinder.goto(new pathfinder.goals.GoalNear(block.position.x, block.position.y, block.position.z, MAX_DIST_FROM_BLOCK_TO_PLACE))66// get ready to place crystal67await bot.equip(item, 'hand')68await bot.lookAt(block.position, true)69// place crystal70await bot.placeEntity(block, new Vec3(0, 1, 0))71bot.chat('I placed an end crystal!')72})737475