Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/place_block.js
1467 views
1
const { onceWithCleanup } = require('../promise_utils')
2
3
module.exports = inject
4
5
function inject (bot) {
6
async function placeBlockWithOptions (referenceBlock, faceVector, options) {
7
const dest = referenceBlock.position.plus(faceVector)
8
let oldBlock = bot.blockAt(dest)
9
await bot._genericPlace(referenceBlock, faceVector, options)
10
11
let newBlock = bot.blockAt(dest)
12
if (oldBlock.type === newBlock.type) {
13
[oldBlock, newBlock] = await onceWithCleanup(bot, `blockUpdate:${dest}`, {
14
timeout: 5000,
15
// Condition to wait to receive block update actually changing the block type, in case the bot receives block updates with no changes
16
// oldBlock and newBlock will both be null when the world unloads
17
checkCondition: (oldBlock, newBlock) => !oldBlock || !newBlock || oldBlock.type !== newBlock.type
18
})
19
}
20
21
// blockUpdate emits (null, null) when the world unloads
22
if (!oldBlock && !newBlock) {
23
return
24
}
25
if (oldBlock?.type === newBlock.type) {
26
throw new Error(`No block has been placed : the block is still ${oldBlock?.name}`)
27
} else {
28
bot.emit('blockPlaced', oldBlock, newBlock)
29
}
30
}
31
32
async function placeBlock (referenceBlock, faceVector) {
33
await placeBlockWithOptions(referenceBlock, faceVector, { swingArm: 'right' })
34
}
35
36
bot.placeBlock = placeBlock
37
bot._placeBlockWithOptions = placeBlockWithOptions
38
}
39
40