Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/block_actions.js
1467 views
1
const { Vec3 } = require('vec3')
2
3
module.exports = inject
4
5
const CARDINALS = {
6
north: new Vec3(0, 0, -1),
7
south: new Vec3(0, 0, 1),
8
west: new Vec3(-1, 0, 0),
9
east: new Vec3(1, 0, 0)
10
}
11
12
const FACING_MAP = {
13
north: { west: 'right', east: 'left' },
14
south: { west: 'left', east: 'right' },
15
west: { north: 'left', south: 'right' },
16
east: { north: 'right', south: 'left' }
17
}
18
19
function inject (bot) {
20
const { instruments, blocks } = bot.registry
21
22
// Stores how many players have currently open a container at a certain position
23
const openCountByPos = {}
24
25
function parseChestMetadata (chestBlock) {
26
const chestTypes = ['single', 'right', 'left']
27
28
return bot.supportFeature('doesntHaveChestType')
29
? { facing: Object.keys(CARDINALS)[chestBlock.metadata - 2] }
30
: {
31
waterlogged: !(chestBlock.metadata & 1),
32
type: chestTypes[(chestBlock.metadata >> 1) % 3],
33
facing: Object.keys(CARDINALS)[Math.floor(chestBlock.metadata / 6)]
34
}
35
}
36
37
function getChestType (chestBlock) { // Returns 'single', 'right' or 'left'
38
if (bot.supportFeature('doesntHaveChestType')) {
39
const facing = parseChestMetadata(chestBlock).facing
40
41
if (!facing) return 'single'
42
43
// We have to check if the adjacent blocks in the perpendicular cardinals are the same type
44
const perpendicularCardinals = Object.keys(FACING_MAP[facing])
45
for (const cardinal of perpendicularCardinals) {
46
const cardinalOffset = CARDINALS[cardinal]
47
if (bot.blockAt(chestBlock.position.plus(cardinalOffset))?.type === chestBlock.type) {
48
return FACING_MAP[cardinal][facing]
49
}
50
}
51
52
return 'single'
53
} else {
54
return parseChestMetadata(chestBlock).type
55
}
56
}
57
58
bot._client.on('block_action', (packet) => {
59
const pt = new Vec3(packet.location.x, packet.location.y, packet.location.z)
60
const block = bot.blockAt(pt)
61
62
// Ignore on non-vanilla blocks
63
if (block === null || !blocks[packet.blockId]) { return }
64
65
const blockName = blocks[packet.blockId].name
66
67
if (blockName === 'noteblock') { // Pre 1.13
68
bot.emit('noteHeard', block, instruments[packet.byte1], packet.byte2)
69
} else if (blockName === 'note_block') { // 1.13 onward
70
bot.emit('noteHeard', block, instruments[Math.floor(block.metadata / 50)], Math.floor((block.metadata % 50) / 2))
71
} else if (blockName === 'sticky_piston' || blockName === 'piston') {
72
bot.emit('pistonMove', block, packet.byte1, packet.byte2)
73
} else {
74
let block2 = null
75
76
if (blockName === 'chest' || blockName === 'trapped_chest') {
77
const chestType = getChestType(block)
78
if (chestType === 'right') {
79
const index = Object.values(FACING_MAP[parseChestMetadata(block).facing]).indexOf('left')
80
const cardinalBlock2 = Object.keys(FACING_MAP[parseChestMetadata(block).facing])[index]
81
const block2Position = block.position.plus(CARDINALS[cardinalBlock2])
82
block2 = bot.blockAt(block2Position)
83
} else if (chestType === 'left') return // Omit left part of the chest so 'chestLidMove' doesn't emit twice when it's a double chest
84
}
85
86
// Emit 'chestLidMove' only if the number of players with the lid open changes
87
if (openCountByPos[block.position] !== packet.byte2) {
88
bot.emit('chestLidMove', block, packet.byte2, block2)
89
90
if (packet.byte2 > 0) {
91
openCountByPos[block.position] = packet.byte2
92
} else {
93
delete openCountByPos[block.position]
94
}
95
}
96
}
97
})
98
99
bot._client.on('block_break_animation', (packet) => {
100
const destroyStage = packet.destroyStage
101
const pt = new Vec3(packet.location.x, packet.location.y, packet.location.z)
102
const block = bot.blockAt(pt)
103
const entity = bot.entities[packet.entityId]
104
105
if (destroyStage < 0 || destroyStage > 9) {
106
// http://minecraft.wiki/w/Protocol#Block_Break_Progress
107
// "0-9 to set it, any other value to remove it"
108
bot.emit('blockBreakProgressEnd', block, entity)
109
} else {
110
bot.emit('blockBreakProgressObserved', block, destroyStage, entity)
111
}
112
})
113
}
114
115