Path: blob/master/test/externalTests/activateItem.js
9427 views
const assert = require('assert')12module.exports = () => async (bot) => {3const Item = require('prismarine-item')(bot.registry)45// Test that activateItem sends the bot's actual rotation (PR #3840).6// Only applies to versions that use the 'use_item' packet with rotation.7if (!bot.supportFeature('useItemWithOwnPacket')) return89await bot.test.becomeCreative()10await bot.test.clearInventory()1112const snowballItem = bot.registry.itemsByName.snowball13if (!snowballItem) return1415// Give the bot a snowball and switch to survival to throw it16await bot.test.setInventorySlot(36, new Item(snowballItem.id, 16, 0))17await bot.test.becomeSurvival()18await bot.test.wait(250)1920// Look south (+Z direction): yaw = PI, pitch = 021await bot.look(Math.PI, 0, true)22await bot.test.wait(250)2324// Intercept the outgoing use_item packet to verify rotation25const sentPacket = await new Promise((resolve) => {26const origWrite = bot._client.write27bot._client.write = function (name, data) {28origWrite.apply(bot._client, arguments)29if (name === 'use_item') {30bot._client.write = origWrite31resolve(data)32}33}34bot.activateItem()35})3637// Verify the rotation was sent (not zeros)38assert(sentPacket.rotation, 'use_item packet should have rotation field')39const { x: sentYaw, y: sentPitch } = sentPacket.rotation4041// With pitch = 0, the notchian pitch should be ~042assert(Math.abs(sentPitch) < 1, `Pitch should be near 0 for level look, got ${sentPitch}`)4344// With yaw = PI (south), the notchian yaw should be ~0 (due to toNotchianYaw = degrees(PI - yaw))45// toNotchianYaw(PI) = degrees(PI - PI) = 046assert(Math.abs(sentYaw) < 1, `Yaw should be near 0 for south-facing look, got ${sentYaw}`)4748// Now test a different direction: look east (yaw = PI/2 in mineflayer)49await bot.look(Math.PI * 3 / 2, -0.5, true)50await bot.test.wait(250)5152const sentPacket2 = await new Promise((resolve) => {53const origWrite = bot._client.write54bot._client.write = function (name, data) {55origWrite.apply(bot._client, arguments)56if (name === 'use_item') {57bot._client.write = origWrite58resolve(data)59}60}61bot.activateItem()62})6364const { x: sentYaw2, y: sentPitch2 } = sentPacket2.rotation6566// toNotchianYaw(3*PI/2) = degrees(PI - 3*PI/2) = degrees(-PI/2) = -9067assert(Math.abs(sentYaw2 - (-90)) < 1, `Yaw should be near -90 for east-facing look, got ${sentYaw2}`)6869// toNotchianPitch(-0.5) = degrees(0.5) ≈ 28.6570const expectedPitch = (0.5 * 180) / Math.PI71assert(Math.abs(sentPitch2 - expectedPitch) < 1, `Pitch should be near ${expectedPitch}, got ${sentPitch2}`)7273await bot.test.becomeCreative()74await bot.test.clearInventory()75}767778