Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/generic_place.js
1467 views
1
const assert = require('assert')
2
module.exports = inject
3
4
function inject (bot) {
5
const Item = require('prismarine-item')(bot.registry)
6
/**
7
*
8
* @param {import('prismarine-block').Block} referenceBlock
9
* @param {import('vec3').Vec3} faceVector
10
* @param {{half?: 'top'|'bottom', delta?: import('vec3').Vec3, forceLook?: boolean | 'ignore', offhand?: boolean, swingArm?: 'right' | 'left', showHand?: boolean}} options
11
*/
12
async function _genericPlace (referenceBlock, faceVector, options) {
13
let handToPlaceWith = 0
14
if (options.offhand) {
15
if (!bot.inventory.slots[45]) {
16
throw new Error('must be holding an item in the off-hand to place')
17
}
18
handToPlaceWith = 1
19
} else if (!bot.heldItem) {
20
throw new Error('must be holding an item to place')
21
}
22
23
// Look at the center of the face
24
let dx = 0.5 + faceVector.x * 0.5
25
let dy = 0.5 + faceVector.y * 0.5
26
let dz = 0.5 + faceVector.z * 0.5
27
if (dy === 0.5) {
28
if (options.half === 'top') dy += 0.25
29
else if (options.half === 'bottom') dy -= 0.25
30
}
31
if (options.delta) {
32
dx = options.delta.x
33
dy = options.delta.y
34
dz = options.delta.z
35
}
36
if (options.forceLook !== 'ignore') {
37
await bot.lookAt(referenceBlock.position.offset(dx, dy, dz), options.forceLook)
38
}
39
// TODO: tell the server that we are sneaking while doing this
40
const pos = referenceBlock.position
41
42
if (options.swingArm) {
43
bot.swingArm(options.swingArm, options.showHand)
44
}
45
46
if (bot.supportFeature('blockPlaceHasHeldItem')) {
47
const packet = {
48
location: pos,
49
direction: vectorToDirection(faceVector),
50
heldItem: Item.toNotch(bot.heldItem),
51
cursorX: Math.floor(dx * 16),
52
cursorY: Math.floor(dy * 16),
53
cursorZ: Math.floor(dz * 16)
54
}
55
bot._client.write('block_place', packet)
56
} else if (bot.supportFeature('blockPlaceHasHandAndIntCursor')) {
57
bot._client.write('block_place', {
58
location: pos,
59
direction: vectorToDirection(faceVector),
60
hand: handToPlaceWith,
61
cursorX: Math.floor(dx * 16),
62
cursorY: Math.floor(dy * 16),
63
cursorZ: Math.floor(dz * 16)
64
})
65
} else if (bot.supportFeature('blockPlaceHasHandAndFloatCursor')) {
66
bot._client.write('block_place', {
67
location: pos,
68
direction: vectorToDirection(faceVector),
69
hand: handToPlaceWith,
70
cursorX: dx,
71
cursorY: dy,
72
cursorZ: dz
73
})
74
} else if (bot.supportFeature('blockPlaceHasInsideBlock')) {
75
bot._client.write('block_place', {
76
location: pos,
77
direction: vectorToDirection(faceVector),
78
hand: handToPlaceWith,
79
cursorX: dx,
80
cursorY: dy,
81
cursorZ: dz,
82
insideBlock: false,
83
sequence: 0, // 1.19.0
84
worldBorderHit: false // 1.21.3
85
})
86
}
87
88
return pos
89
}
90
bot._genericPlace = _genericPlace
91
}
92
93
function vectorToDirection (v) {
94
if (v.y < 0) {
95
return 0
96
} else if (v.y > 0) {
97
return 1
98
} else if (v.z < 0) {
99
return 2
100
} else if (v.z > 0) {
101
return 3
102
} else if (v.x < 0) {
103
return 4
104
} else if (v.x > 0) {
105
return 5
106
}
107
assert.ok(false, `invalid direction vector ${v}`)
108
}
109
110