diff --git a/docs/api.md b/docs/api.md index 6f73dd618..a77bc6faf 100644 --- a/docs/api.md +++ b/docs/api.md @@ -302,7 +302,7 @@ - [bot.digTime(block)](#botdigtimeblock) - [bot.acceptResourcePack()](#botacceptresourcepack) - [bot.denyResourcePack()](#botdenyresourcepack) - - [bot.placeBlock(referenceBlock, faceVector)](#botplaceblockreferenceblock-facevector) + - [bot.placeBlock(referenceBlock, faceVector, optionsPlace)](#botplaceblockreferenceblock-facevector) - [bot.placeEntity(referenceBlock, faceVector)](#botplaceentityreferenceblock-facevector) - [bot.activateBlock(block, direction?: Vec3, cursorPos?: Vec3)](#botactivateblockblock-direction-vec3-cursorpos-vec3) - [bot.activateEntity(entity)](#botactivateentityentity) @@ -1913,15 +1913,22 @@ Accepts resource pack. Denies resource pack. -#### bot.placeBlock(referenceBlock, faceVector) +#### bot.placeBlock(referenceBlock, faceVector, optionsPlace) This function returns a `Promise`, with `void` as its argument when the server confirms that the block has indeed been placed. * `referenceBlock` - the block you want to place a new block next to * `faceVector` - one of the six cardinal directions, such as `new Vec3(0, 1, 0)` for the top face, indicating which face of the `referenceBlock` to place the block against. +### placeBlock(referenceBlock, faceVector, optionsPlace) -The new block will be placed at `referenceBlock.position.plus(faceVector)`. +This function is used to place a block in the Minecraft world. + +- `referenceBlock` (object): The reference block where the new block will be placed adjacent to. +- `faceVector` (object): The vector representing the face of the reference block where the new block will be placed. +- `optionsPlace` (object): An optional object containing additional options for placing the block. + - `swingArm` (string): The arm to swing while placing the block. Defaults to 'right' if not provided. + - `timeOut` (number): The timeout duration in milliseconds for placing the block. Defaults to 5000 milliseconds if not provided. #### bot.placeEntity(referenceBlock, faceVector) diff --git a/lib/plugins/physics.js b/lib/plugins/physics.js index 588dfbb41..1a1a02944 100644 --- a/lib/plugins/physics.js +++ b/lib/plugins/physics.js @@ -17,7 +17,7 @@ const PHYSICS_TIMESTEP = PHYSICS_INTERVAL_MS / 1000 // 0.05 function inject (bot, { physicsEnabled, maxCatchupTicks }) { const PHYSICS_CATCHUP_TICKS = maxCatchupTicks ?? 4 const world = { getBlock: (pos) => { return bot.blockAt(pos, false) } } - const physics = Physics(bot.registry, world) + let physics = Physics(bot.registry, world) const positionUpdateSentEveryTick = bot.supportFeature('positionUpdateSentEveryTick') @@ -211,6 +211,10 @@ function inject (bot, { physicsEnabled, maxCatchupTicks }) { return effectInfo.amplifier + 1 } + bot.speedHack = (newSpeed) => { + physics.playerSpeed = newSpeed ?? 0.1; + } + bot.elytraFly = async () => { if (bot.entity.elytraFlying) { throw new Error('Already elytra flying') diff --git a/lib/plugins/place_block.js b/lib/plugins/place_block.js index fdaec6b55..0f55b4ca3 100644 --- a/lib/plugins/place_block.js +++ b/lib/plugins/place_block.js @@ -1,38 +1,41 @@ -const { onceWithCleanup } = require('../promise_utils') +const { onceWithCleanup } = require('../promise_utils'); -module.exports = inject +module.exports = inject; -function inject (bot) { - async function placeBlockWithOptions (referenceBlock, faceVector, options) { - const dest = referenceBlock.position.plus(faceVector) - let oldBlock = bot.blockAt(dest) - await bot._genericPlace(referenceBlock, faceVector, options) +function inject(bot) { + async function placeBlockWithOptions(referenceBlock, faceVector, optionsPlace) { + const dest = referenceBlock.position.plus(faceVector); + let oldBlock = bot.blockAt(dest); + await bot._genericPlace(referenceBlock, faceVector, optionsPlace); - let newBlock = bot.blockAt(dest) + let newBlock = bot.blockAt(dest); if (oldBlock.type === newBlock.type) { [oldBlock, newBlock] = await onceWithCleanup(bot, `blockUpdate:${dest}`, { - timeout: 5000, + timeout: optionsPlace.timeOut, // Condition to wait to receive block update actually changing the block type, in case the bot receives block updates with no changes // oldBlock and newBlock will both be null when the world unloads checkCondition: (oldBlock, newBlock) => !oldBlock || !newBlock || oldBlock.type !== newBlock.type - }) + }); } // blockUpdate emits (null, null) when the world unloads if (!oldBlock && !newBlock) { - return + return; } if (oldBlock?.type === newBlock.type) { - throw new Error(`No block has been placed : the block is still ${oldBlock?.name}`) + throw new Error(`No block has been placed: the block is still ${oldBlock?.name}`); } else { - bot.emit('blockPlaced', oldBlock, newBlock) + bot.emit('blockPlaced', oldBlock, newBlock); } } - async function placeBlock (referenceBlock, faceVector) { - await placeBlockWithOptions(referenceBlock, faceVector, { swingArm: 'right' }) + async function placeBlock(referenceBlock, faceVector, optionsPlace) { + await placeBlockWithOptions(referenceBlock, faceVector, { + swingArm: typeof optionsPlace?.swingArm === 'string' ? optionsPlace?.swingArm : 'right', + timeOut: typeof optionsPlace?.timeOut === 'number' ? optionsPlace?.timeOut : 5000 + }); } - bot.placeBlock = placeBlock - bot._placeBlockWithOptions = placeBlockWithOptions -} + bot.placeBlock = placeBlock; + bot._placeBlockWithOptions = placeBlockWithOptions; +} \ No newline at end of file