Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/chest.js
1467 views
1
const { Vec3 } = require('vec3')
2
3
module.exports = inject
4
5
function inject (bot) {
6
const allowedWindowTypes = ['minecraft:generic', 'minecraft:chest', 'minecraft:dispenser', 'minecraft:ender_chest', 'minecraft:shulker_box', 'minecraft:hopper', 'minecraft:container', 'minecraft:dropper', 'minecraft:trapped_chest', 'minecraft:barrel', 'minecraft:white_shulker_box', 'minecraft:orange_shulker_box', 'minecraft:magenta_shulker_box', 'minecraft:light_blue_shulker_box', 'minecraft:yellow_shulker_box', 'minecraft:lime_shulker_box', 'minecraft:pink_shulker_box', 'minecraft:gray_shulker_box', 'minecraft:light_gray_shulker_box', 'minecraft:cyan_shulker_box', 'minecraft:purple_shulker_box', 'minecraft:blue_shulker_box', 'minecraft:brown_shulker_box', 'minecraft:green_shulker_box', 'minecraft:red_shulker_box', 'minecraft:black_shulker_box']
7
function matchWindowType (window) {
8
for (const type of allowedWindowTypes) {
9
if (window.type.startsWith(type)) return true
10
}
11
return false
12
}
13
14
async function openContainer (containerToOpen, direction, cursorPos) {
15
direction = direction ?? new Vec3(0, 1, 0)
16
cursorPos = cursorPos ?? new Vec3(0.5, 0.5, 0.5)
17
let chest
18
if (containerToOpen.constructor.name === 'Block' && allowedWindowTypes.map(name => name.replace('minecraft:', '')).includes(containerToOpen.name)) {
19
chest = await bot.openBlock(containerToOpen, direction, cursorPos)
20
} else if (containerToOpen.constructor.name === 'Entity') {
21
chest = await bot.openEntity(containerToOpen)
22
} else {
23
throw new Error('containerToOpen is neither a block nor an entity')
24
}
25
26
if (!matchWindowType(chest)) { throw new Error('Non-container window used as a container: ' + JSON.stringify(chest)) }
27
return chest
28
}
29
30
bot.openContainer = openContainer
31
bot.openChest = openContainer
32
bot.openDispenser = openContainer
33
}
34
35