Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/test/externalTests/useChests.js
Views: 789
const { Vec3 } = require('vec3')1const assert = require('assert')2const { once, onceWithCleanup } = require('../../lib/promise_utils')34module.exports = () => async (bot) => {5const Item = require('prismarine-item')(bot.registry)67bot.test.groundY = bot.supportFeature('tallWorld') ? -60 : 489const smallChestLocation = new Vec3(0, bot.test.groundY, -1)10const largeChestLocations = [new Vec3(0, bot.test.groundY, 1), new Vec3(1, bot.test.groundY, 1)]11const smallTrappedChestLocation = new Vec3(1, bot.test.groundY, 0)12const largeTrappedChestLocations = [13new Vec3(-1, bot.test.groundY, 1),14new Vec3(-1, bot.test.groundY, 0)15]16const chestSlot = 3617const trappedChestSlot = 3718const boneSlot = 381920let blockItemsByName21if (bot.supportFeature('itemsAreNotBlocks')) {22blockItemsByName = 'itemsByName'23} else if (bot.supportFeature('itemsAreAlsoBlocks')) {24blockItemsByName = 'blocksByName'25}2627const chestBlockId = bot.registry.blocksByName.chest.id28const trappedChestBlockId = bot.registry.blocksByName.trapped_chest.id2930function itemByName (items, name) {31for (let i = 0; i < items.length; ++i) {32const item = items[i]33if (item && item.name === name) return item34}35return null36}3738async function depositBones (chestLocation, count) {39const chest = await bot.openContainer(bot.blockAt(chestLocation))40assert(chest.containerItems().length === 0)41assert(chest.items().length > 0)42const name = 'bone'43const item = itemByName(chest.items(), name)44if (!item) {45bot.test.sayEverywhere(`unknown item ${name}`)46throw new Error(`unknown item ${name}`)47}48await chest.deposit(item.type, null, count)49chest.close()50}5152async function withdrawBones (chestLocation, count) {53const chest = await bot.openContainer(bot.blockAt(chestLocation))54const name = 'bone'55const item = itemByName(chest.containerItems(), name)56if (!item) {57bot.test.sayEverywhere(`unknown item ${name}`)58throw new Error(`unknown item ${name}`)59}60await chest.withdraw(item.type, null, count)61assert(chest.containerItems().length === 0)62assert(chest.items().length > 0)63chest.close()64}6566await bot.test.setInventorySlot(chestSlot, new Item(bot.registry[blockItemsByName].chest.id, 3, 0))67await bot.test.setInventorySlot(trappedChestSlot, new Item(bot.registry[blockItemsByName].trapped_chest.id, 3, 0))68await bot.test.setInventorySlot(boneSlot, new Item(bot.registry.itemsByName.bone.id, 3, 0))6970await bot.test.becomeSurvival()7172// place the chests around us73await bot.test.placeBlock(chestSlot, largeChestLocations[0])74await bot.test.placeBlock(chestSlot, largeChestLocations[1])75await bot.test.placeBlock(chestSlot, smallChestLocation)76await bot.test.placeBlock(trappedChestSlot, largeTrappedChestLocations[0])77await bot.test.placeBlock(trappedChestSlot, largeTrappedChestLocations[1])78await bot.test.placeBlock(trappedChestSlot, smallTrappedChestLocation)7980assert.strictEqual(bot.blockAt(largeChestLocations[0]).type, chestBlockId)81assert.strictEqual(bot.blockAt(largeChestLocations[1]).type, chestBlockId)82assert.strictEqual(bot.blockAt(smallChestLocation).type, chestBlockId)83assert.strictEqual(bot.blockAt(largeTrappedChestLocations[0]).type, trappedChestBlockId)84assert.strictEqual(bot.blockAt(largeTrappedChestLocations[1]).type, trappedChestBlockId)85assert.strictEqual(bot.blockAt(smallTrappedChestLocation).type, trappedChestBlockId)8687// Test that "chestLidMove" is emitted only once when opening a double chest88let emitted = false89bot.on('chestLidMove', handler)90async function handler (block, isOpen, block2) {91if (emitted) {92assert.fail(new Error('chestLidMove emitted twice'))93} else {94emitted = true9596let blockAssert = false; let block2Assert = false97for (const location of largeChestLocations) {98if (location.equals(block.position)) blockAssert = true99if (location.equals(block2.position)) block2Assert = true100}101assert(blockAssert && block2Assert, new Error('The block instance emitted by chestLidMove is not part of the chest oppened'))102assert.strictEqual(isOpen, 1, new Error('isOpen should be 1 when opened by one only player'))103104await bot.test.wait(500)105106bot.removeListener('chestLidMove', handler)107chest.close()108}109}110const chest = await bot.openContainer(bot.blockAt(largeChestLocations[0]))111await once(chest, 'close')112113await depositBones(smallChestLocation, 1)114await depositBones(largeChestLocations[0], 2)115116assert(bot.inventory.items().length === 0)117118await withdrawBones(smallChestLocation, 1)119await withdrawBones(largeChestLocations[0], 2)120121await depositBones(smallTrappedChestLocation, 1)122await depositBones(largeTrappedChestLocations[0], 2)123124assert(bot.inventory.items().length === 0)125126await withdrawBones(smallTrappedChestLocation, 1)127await withdrawBones(largeTrappedChestLocations[0], 2)128129const itemsWithStackSize = {13064: ['stone', 'mycelium'],13116: ['ender_pearl', 'egg'],1321: ['fishing_rod', 'bow']133}134135function getRandomStackableItem () {136if (Math.random() < 0.75) {137return itemsWithStackSize[64][~~(Math.random() * itemsWithStackSize[64].length)]138} else {139if (Math.random() < 0.5) {140return itemsWithStackSize[16][~~(Math.random() * itemsWithStackSize[16].length)]141} else {142return itemsWithStackSize[1][~~(Math.random() * itemsWithStackSize[1].length)]143}144}145}146147async function createRandomLayout (window, slotPopulationFactor) {148await bot.test.becomeCreative()149150for (let slot = 0; slot < window.inventoryStart; slot++) {151if (Math.random() < slotPopulationFactor) {152const randomItem = getRandomStackableItem()153const item = bot.registry.itemsByName[randomItem]154bot.chat(`/give ${bot.username} ${item.name} ${Math.ceil(Math.random() * item.stackSize)}`)155await onceWithCleanup(window, 'updateSlot', {156timeout: 5000,157checkCondition: (slot, oldItem, newItem) => slot === window.hotbarStart && newItem?.name === item.name158})159160// await bot.clickWindow(slot, 0, 2)161await bot.moveSlotItem(window.hotbarStart, slot)162}163}164165await bot.test.becomeSurvival()166}167168async function testMouseClick (window, clicks) {169let iterations = 0170while (iterations++ < clicks) {171await bot.clickWindow(~~(Math.random() * window.inventoryStart), 0, 0)172}173}174175function clearLargeChest () {176bot.chat(`/setblock ${largeChestLocations[0].x} ${largeChestLocations[0].y} ${largeChestLocations[0].z} chest`)177bot.chat(`/setblock ${largeChestLocations[1].x} ${largeChestLocations[1].y} ${largeChestLocations[1].z} chest`)178}179180const window = await bot.openContainer(bot.blockAt(largeChestLocations[0]))181await createRandomLayout(window, 0.95)182183await testMouseClick(window, 250)184185window.close()186clearLargeChest()187}188189190