Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optionsPlace to placeBlock #3331

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 5 additions & 1 deletion lib/plugins/physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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')
Expand Down
39 changes: 21 additions & 18 deletions lib/plugins/place_block.js
Original file line number Diff line number Diff line change
@@ -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;
}
Loading