Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/externalTests/activateItem.js
9427 views
1
const assert = require('assert')
2
3
module.exports = () => async (bot) => {
4
const Item = require('prismarine-item')(bot.registry)
5
6
// Test that activateItem sends the bot's actual rotation (PR #3840).
7
// Only applies to versions that use the 'use_item' packet with rotation.
8
if (!bot.supportFeature('useItemWithOwnPacket')) return
9
10
await bot.test.becomeCreative()
11
await bot.test.clearInventory()
12
13
const snowballItem = bot.registry.itemsByName.snowball
14
if (!snowballItem) return
15
16
// Give the bot a snowball and switch to survival to throw it
17
await bot.test.setInventorySlot(36, new Item(snowballItem.id, 16, 0))
18
await bot.test.becomeSurvival()
19
await bot.test.wait(250)
20
21
// Look south (+Z direction): yaw = PI, pitch = 0
22
await bot.look(Math.PI, 0, true)
23
await bot.test.wait(250)
24
25
// Intercept the outgoing use_item packet to verify rotation
26
const sentPacket = await new Promise((resolve) => {
27
const origWrite = bot._client.write
28
bot._client.write = function (name, data) {
29
origWrite.apply(bot._client, arguments)
30
if (name === 'use_item') {
31
bot._client.write = origWrite
32
resolve(data)
33
}
34
}
35
bot.activateItem()
36
})
37
38
// Verify the rotation was sent (not zeros)
39
assert(sentPacket.rotation, 'use_item packet should have rotation field')
40
const { x: sentYaw, y: sentPitch } = sentPacket.rotation
41
42
// With pitch = 0, the notchian pitch should be ~0
43
assert(Math.abs(sentPitch) < 1, `Pitch should be near 0 for level look, got ${sentPitch}`)
44
45
// With yaw = PI (south), the notchian yaw should be ~0 (due to toNotchianYaw = degrees(PI - yaw))
46
// toNotchianYaw(PI) = degrees(PI - PI) = 0
47
assert(Math.abs(sentYaw) < 1, `Yaw should be near 0 for south-facing look, got ${sentYaw}`)
48
49
// Now test a different direction: look east (yaw = PI/2 in mineflayer)
50
await bot.look(Math.PI * 3 / 2, -0.5, true)
51
await bot.test.wait(250)
52
53
const sentPacket2 = await new Promise((resolve) => {
54
const origWrite = bot._client.write
55
bot._client.write = function (name, data) {
56
origWrite.apply(bot._client, arguments)
57
if (name === 'use_item') {
58
bot._client.write = origWrite
59
resolve(data)
60
}
61
}
62
bot.activateItem()
63
})
64
65
const { x: sentYaw2, y: sentPitch2 } = sentPacket2.rotation
66
67
// toNotchianYaw(3*PI/2) = degrees(PI - 3*PI/2) = degrees(-PI/2) = -90
68
assert(Math.abs(sentYaw2 - (-90)) < 1, `Yaw should be near -90 for east-facing look, got ${sentYaw2}`)
69
70
// toNotchianPitch(-0.5) = degrees(0.5) ≈ 28.65
71
const expectedPitch = (0.5 * 180) / Math.PI
72
assert(Math.abs(sentPitch2 - expectedPitch) < 1, `Pitch should be near ${expectedPitch}, got ${sentPitch2}`)
73
74
await bot.test.becomeCreative()
75
await bot.test.clearInventory()
76
}
77
78