Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 14 additions & 5 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1820,13 +1820,15 @@ This function returns a `Promise`, with `Entity` as its argument upon completion

The new block will be placed at `referenceBlock.position.plus(faceVector)`.

#### bot.activateBlock(block)
#### bot.activateBlock(block, direction?: Vec3, cursorPos?: Vec3)

This function returns a `Promise`, with `void` as its argument upon completion.

Punch a note block, open a door, etc.

* `block` - the block to activate
* `direction` Optional defaults to `new Vec3(0, 1, 0)` (up). A vector off the direction the container block should be interacted with. Dose nothing when a container entity is targeted.
* `cursorPos` Optional defaults to `new Vec3(0.5, 0.5, 0.5)` (block center). The curos position when opening the block instance. This is send with the activate block packet. Dose nothing when a container entity is targeted.

#### bot.activateEntity(entity)

Expand Down Expand Up @@ -1927,11 +1929,16 @@ This function returns a `Promise`, with `void` as its argument when the writing
* `slot` is in inventory window coordinates (where 36 is the first quickbar slot, etc.).
* `pages` is an array of strings represents the pages.

#### bot.openContainer(containerBlock or containerEntity)
#### bot.openContainer(containerBlock or containerEntity, direction?, cursorPos?)
Opens a block container or entity.

* `containerBlock` or `containerEntity` The block instance to open or the entity to open.
* `direction` Optional defaults to `new Vec3(0, 1, 0)` (up). A vector off the direction the container block should be interacted with. Dose nothing when a container entity is targeted.
* `cursorPos` Optional defaults to `new Vec3(0.5, 0.5, 0.5)` (block center). The curos position when opening the block instance. This is send with the activate block packet. Dose nothing when a container entity is targeted.

Returns a promise on a `Container` instance which represents the container you are opening.

#### bot.openChest(chestBlock or minecartchestEntity)
#### bot.openChest(chestBlock or minecartchestEntity, direction?, cursorPos?)

Deprecated. Same as `openContainer`

Expand Down Expand Up @@ -2028,11 +2035,13 @@ Transfer some kind of item from one range to an other. `options` is an object co
* `count` : the amount of items to transfer. Default: `1`
* `nbt` : nbt data of the item to transfer. Default: `nullish` (ignores nbt)

#### bot.openBlock(block)
#### bot.openBlock(block, direction?: Vec3, cursorPos?: Vec3)

Open a block, for example a chest, returns a promise on the opening `Window`.

* `block` is the block the bot will open
* `block` is the block the bot will open.
* `direction` Optional defaults to `new Vec3(0, 1, 0)` (up). A vector off the direction the container block should be interacted with. Dose nothing when a container entity is targeted.
* `cursorPos` Optional defaults to `new Vec3(0.5, 0.5, 0.5)` (block center). The curos position when opening the block instance. This is send with the activate block packet. Dose nothing when a container entity is targeted.

#### bot.openEntity(entity)

Expand Down
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export interface Bot extends TypedEmitter<BotEvents> {

placeEntity: (referenceBlock: Block, faceVector: Vec3) => Promise<Entity>

activateBlock: (block: Block) => Promise<void>
activateBlock: (block: Block, direction?: Vec3, cursorPos?: Vec3) => Promise<void>

activateEntity: (block: Entity) => Promise<void>

Expand Down Expand Up @@ -337,9 +337,9 @@ export interface Bot extends TypedEmitter<BotEvents> {
pages: string[]
) => Promise<void>

openContainer: (chest: Block | Entity) => Promise<Chest | Furnace | Dispenser>
openContainer: (chest: Block | Entity, direction?: Vec3, cursorPos?: Vec3) => Promise<Chest | Furnace | Dispenser>

openChest: (chest: Block | Entity) => Promise<Chest>
openChest: (chest: Block | Entity, direction?: number, cursorPos?: Vec3) => Promise<Chest>

openFurnace: (furnace: Block) => Promise<Furnace>

Expand Down Expand Up @@ -380,7 +380,7 @@ export interface Bot extends TypedEmitter<BotEvents> {

transfer: (options: TransferOptions) => Promise<void>

openBlock: (block: Block, Class: new () => EventEmitter) => Promise<void>
openBlock: (block: Block, direction?: Vec3, cursorPos?: Vec3) => Promise<void>

openEntity: (block: Entity, Class: new () => EventEmitter) => Promise<void>

Expand Down
7 changes: 5 additions & 2 deletions lib/plugins/chest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { Vec3 } = require('vec3')

module.exports = inject

Expand All @@ -11,10 +12,12 @@ function inject (bot) {
return false
}

async function openContainer (containerToOpen) {
async function openContainer (containerToOpen, direction, cursorPos) {
direction = direction ?? new Vec3(0, 1, 0)
cursorPos = cursorPos ?? new Vec3(0.5, 0.5, 0.5)
let chest
if (containerToOpen.constructor.name === 'Block' && allowedWindowTypes.map(name => name.replace('minecraft:', '')).includes(containerToOpen.name)) {
chest = await bot.openBlock(containerToOpen)
chest = await bot.openBlock(containerToOpen, direction, cursorPos)
} else if (containerToOpen.constructor.name === 'Entity') {
chest = await bot.openEntity(containerToOpen)
} else {
Expand Down
63 changes: 44 additions & 19 deletions lib/plugins/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,45 +150,48 @@ function inject (bot, { hideErrors }) {
}
}

async function activateBlock (block) {
async function activateBlock (block, direction, cursorPos) {
direction = direction ?? new Vec3(0, 1, 0)
const directionNum = vectorToDirection(direction) // The packet needs a number as the direction
cursorPos = cursorPos ?? new Vec3(0.5, 0.5, 0.5)
// TODO: tell the server that we are not sneaking while doing this
await bot.lookAt(block.position.offset(0.5, 0.5, 0.5), false)
// place block message
if (bot.supportFeature('blockPlaceHasHeldItem')) {
bot._client.write('block_place', {
location: block.position,
direction: 1,
direction: directionNum,
heldItem: Item.toNotch(bot.heldItem),
cursorX: 8,
cursorY: 8,
cursorZ: 8
cursorX: cursorPos.scaled(16).x,
cursorY: cursorPos.scaled(16).y,
cursorZ: cursorPos.scaled(16).z
})
} else if (bot.supportFeature('blockPlaceHasHandAndIntCursor')) {
bot._client.write('block_place', {
location: block.position,
direction: 1,
direction: directionNum,
hand: 0,
cursorX: 8,
cursorY: 8,
cursorZ: 8
cursorX: cursorPos.scaled(16).x,
cursorY: cursorPos.scaled(16).y,
cursorZ: cursorPos.scaled(16).z
})
} else if (bot.supportFeature('blockPlaceHasHandAndFloatCursor')) {
bot._client.write('block_place', {
location: block.position,
direction: 1,
direction: directionNum,
hand: 0,
cursorX: 0.5,
cursorY: 0.5,
cursorZ: 0.5
cursorX: cursorPos.x,
cursorY: cursorPos.y,
cursorZ: cursorPos.z
})
} else if (bot.supportFeature('blockPlaceHasInsideBlock')) {
bot._client.write('block_place', {
location: block.position,
direction: 1,
direction: directionNum,
hand: 0,
cursorX: 0.5,
cursorY: 0.5,
cursorZ: 0.5,
cursorX: cursorPos.x,
cursorY: cursorPos.y,
cursorZ: cursorPos.z,
insideBlock: false
})
}
Expand Down Expand Up @@ -339,8 +342,8 @@ function inject (bot, { hideErrors }) {
}
}

async function openBlock (block) {
bot.activateBlock(block)
async function openBlock (block, direction, cursorPos) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you forgot the default here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I just noticed that there are also types missing

bot.activateBlock(block, direction, cursorPos)
const [window] = await once(bot, 'windowOpen')
extendWindow(window)
return window
Expand Down Expand Up @@ -641,6 +644,28 @@ function inject (bot, { hideErrors }) {
bot.emit(`setWindowItems:${window.id}`)
})

/**
* Convert a vector direction to minecraft packet number direction
* @param {Vec3} v
* @returns {number}
*/
function vectorToDirection (v) {
if (v.y < 0) {
return 0
} else if (v.y > 0) {
return 1
} else if (v.z < 0) {
return 2
} else if (v.z > 0) {
return 3
} else if (v.x < 0) {
return 4
} else if (v.x > 0) {
return 5
}
assert.ok(false, `invalid direction vector ${v}`)
}

bot.activateBlock = activateBlock
bot.activateEntity = activateEntity
bot.activateEntityAt = activateEntityAt
Expand Down