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/anvil.js
Views: 789
const assert = require('assert')1const { once } = require('../../lib/promise_utils')23module.exports = () => {4async function runTest (bot, testFunction) {5const Item = require('prismarine-item')(bot.registry)6const renameCost = () => bot.registry.isNewerOrEqualTo('1.8.9') ? 0 : 1 // weird quirk of anvils7const renameName = (name) => bot.registry.isOlderThan('1.13.2') ? name : JSON.stringify({ text: name }) // weird quirk of anvils8await bot.test.becomeCreative()9await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.anvil.id, 1))10await bot.test.becomeSurvival()11await bot.test.placeBlock(36, bot.entity.position.offset(1, 0, 0))1213if (bot.registry.isNewerOrEqualTo('1.13')) bot.chat(`/xp set ${bot.username} 999 levels`)14else {15bot.chat(`/xp -2147483648L ${bot.username}`)16await once(bot, 'experience')17bot.chat(`/xp 999L ${bot.username}`)18}1920await once(bot, 'experience')2122const b = bot.findBlock({ matching: bot.registry.blocksByName.anvil.id }) // find anvil before tests so all tests can use it2324function makeBook (enchants) {25return makeItem({ type: bot.registry.itemsByName.enchanted_book.id, count: 1, enchants })26}2728function makeItem (opts) {29const { type, count = 1, enchants, repairCost } = opts30const item = new Item(type, count)31if (enchants) item.enchants = enchants32if (repairCost) item.repairCost = repairCost33return item34}35await testFunction(b, renameCost, renameName, Item, bot, makeBook, makeItem)36}3738const tests = []3940function addTest (name, f) {41tests[name] = bot => runTest(bot, f)42}4344addTest('combine two items', async (b, renameCost, renameName, Item, bot, makeBook, makeItem) => { // combine two items45await bot.test.becomeCreative()46// get items47await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.diamond_sword.id, 1))48await bot.test.setInventorySlot(37, makeBook([{ name: 'sharpness', lvl: 5 }]))49await bot.test.becomeSurvival()5051const anvil = await bot.openAnvil(b)5253const sword = anvil.findInventoryItem(bot.registry.itemsByName.diamond_sword.id)54const book = anvil.findInventoryItem(bot.registry.itemsByName.enchanted_book.id)5556await anvil.combine(sword, book)57// test result58assert.strictEqual(bot.experience.level, 994)59assert.strictEqual(anvil.slots[3].repairCost, 1)60assert.deepStrictEqual(anvil.slots[3].enchants, [{ name: 'sharpness', lvl: 5 }])61anvil.close()62await bot.test.wait(1000)63})6465addTest('combine with nbt selection two items', async (b, renameCost, renameName, Item, bot, makeBook, makeItem) => { // combining two items in inventory, but there are three items, so this is more a test of using nbt when picking the item in inventory66bot.chat(`/clear ${bot.username}`)67await bot.test.becomeCreative()6869await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.diamond_sword.id, 1))70await bot.test.setInventorySlot(37, makeItem({ type: bot.registry.itemsByName.diamond_sword.id, enchants: [{ name: 'sharpness', lvl: 5 }] }))71await bot.test.setInventorySlot(38, makeBook([{ name: 'unbreaking', lvl: 3 }]))7273await bot.test.becomeSurvival()7475const anvil = await bot.openAnvil(b)7677const sword = bot.inventory.slots[37]78const book = anvil.findInventoryItem(bot.registry.itemsByName.enchanted_book.id)7980await anvil.combine(sword, book)81// test result82assert.strictEqual(bot.experience.level, 996)83assert.strictEqual(anvil.slots[3].repairCost, 1)84assert.deepStrictEqual(anvil.slots[3].enchants, [{ name: 'sharpness', lvl: 5 }, { name: 'unbreaking', lvl: 3 }])85anvil.close()86await bot.test.wait(1000)87})8889addTest('using anvil.rename', async (b, renameCost, renameName, Item, bot, makeBook, makeItem) => { // using anvil.rename90bot.chat(`/clear ${bot.username}`)91await bot.test.becomeCreative()9293await bot.test.setInventorySlot(36, makeItem({ type: bot.registry.itemsByName.diamond_sword.id }))9495await bot.test.becomeSurvival()9697const anvil = await bot.openAnvil(b)9899const sword = anvil.findInventoryItem(bot.registry.itemsByName.diamond_sword.id)100await anvil.rename(sword, 'hello')101// test result102assert.strictEqual(bot.experience.level, 998)103assert.strictEqual(anvil.slots[3].repairCost, renameCost())104assert.deepStrictEqual(anvil.slots[3].customName, renameName('hello'))105anvil.close()106await bot.test.wait(1000)107})108109addTest('two item + rename', async (b, renameCost, renameName, Item, bot, makeBook, makeItem) => { // test 2 + a rename110bot.chat(`/clear ${bot.username}`)111await bot.test.becomeCreative()112113await bot.test.setInventorySlot(36, new Item(bot.registry.itemsByName.diamond_sword.id, 1))114await bot.test.setInventorySlot(37, makeItem({ type: bot.registry.itemsByName.diamond_sword.id, enchants: [{ name: 'sharpness', lvl: 5 }] }))115await bot.test.setInventorySlot(38, makeBook([{ name: 'unbreaking', lvl: 3 }]))116117await bot.test.becomeSurvival()118119const anvil = await bot.openAnvil(b)120121const sword = bot.inventory.slots[37]122const book = anvil.findInventoryItem(bot.registry.itemsByName.enchanted_book.id)123124await anvil.combine(sword, book, 'lol')125// test result126assert.strictEqual(bot.experience.level, 995)127assert.strictEqual(anvil.slots[3].repairCost, 1)128assert.deepStrictEqual(anvil.slots[3].enchants, [{ name: 'sharpness', lvl: 5 }, { name: 'unbreaking', lvl: 3 }])129assert.strictEqual(anvil.slots[3].customName, renameName('lol'))130anvil.close()131await bot.test.wait(1000)132})133134return tests135}136137138