CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/test/externalTests/placeEntity.js
Views: 789
1
const assert = require('assert')
2
const { Vec3 } = require('vec3')
3
const { once } = require('../../lib/promise_utils')
4
5
module.exports = (version) => {
6
async function runTest (bot, testFunction) {
7
await testFunction(bot)
8
}
9
10
const tests = []
11
12
function addTest (name, f) {
13
tests[name] = bot => runTest(bot, f)
14
}
15
16
addTest('place crystal', async (bot) => {
17
if (!bot.registry.itemsByName.end_crystal) return // unsupported
18
await bot.test.setBlock({ z: 1, relative: true, blockName: 'obsidian' })
19
await bot.test.awaitItemReceived(`/give ${bot.username} end_crystal`)
20
const crystal = await bot.placeEntity(bot.blockAt(bot.entity.position.offset(0, 0, 1)), new Vec3(0, 1, 0))
21
assert(crystal !== null)
22
let name = 'EnderCrystal'
23
if (bot.supportFeature('enderCrystalNameEndsInErNoCaps')) {
24
name = 'ender_crystal'
25
} else if (bot.supportFeature('entityNameLowerCaseNoUnderscore')) {
26
name = 'endercrystal'
27
} else if (bot.supportFeature('enderCrystalNameNoCapsWithUnderscore')) {
28
name = 'end_crystal'
29
}
30
const entity = bot.nearestEntity(o => o.name === name)
31
assert(entity?.name === name)
32
bot.attack(entity)
33
await once(bot, 'entityGone')
34
await bot.test.setBlock({ z: 1, blockName: 'air', relative: true })
35
})
36
37
addTest('place boat', async (bot) => {
38
async function placeBlocksForTest (blockName) {
39
for (let z = -1; z >= -3; z--) {
40
const y = -1
41
for (let x = -1; x <= 1; x++) {
42
await bot.test.setBlock({ x, y, z, blockName, relative: true })
43
}
44
}
45
}
46
47
await placeBlocksForTest('water')
48
await bot.test.awaitItemReceived(`/give ${bot.username} ${bot.registry.oak_boat ? 'oak_boat' : 'boat'}`)
49
const boat = await bot.placeEntity(bot.blockAt(bot.entity.position.offset(0, -1, -2)), new Vec3(0, -1, 0))
50
assert(boat !== null)
51
const name = bot.supportFeature('entityNameUpperCaseNoUnderscore') ? 'Boat' : 'boat'
52
const entity = bot.nearestEntity(o => o.name === name)
53
assert(entity?.name === name)
54
await placeBlocksForTest('air')
55
bot.attack(entity)
56
await once(bot, 'entityGone')
57
})
58
59
addTest('place summon egg', async (bot) => {
60
let command
61
if (bot.registry.isOlderThan('1.9')) {
62
command = '/give @p spawn_egg 1 54' // 1.8
63
} else if (bot.registry.isOlderThan('1.11')) {
64
command = '/give @p spawn_egg 1 0 {EntityTag:{id:Zombie}}' // 1.9 / 1.10
65
} else if (bot.registry.isOlderThan('1.12')) {
66
command = '/give @p spawn_egg 1 0 {EntityTag:{id:minecraft:zombie}}' // 1.11
67
} else if (bot.registry.isOlderThan('1.13')) {
68
command = '/give @p spawn_egg 1 0 {EntityTag:{id:zombie}}' // 1.12
69
} else {
70
command = '/give @p zombie_spawn_egg 1' // >1.12
71
}
72
await bot.test.awaitItemReceived(command)
73
const zombie = await bot.placeEntity(bot.blockAt(bot.entity.position.offset(0, 0, 1)), new Vec3(0, 1, 0))
74
assert(zombie !== null)
75
const name = bot.supportFeature('entityNameUpperCaseNoUnderscore') ? 'Zombie' : 'zombie'
76
const entity = bot.nearestEntity(o => o.name === name)
77
assert(entity?.name === name)
78
bot.chat(`/kill @e[type=${name}]`) // use /kill instead of bot.attack() because it takes more than one hit to kill
79
await once(bot, 'entityGone')
80
})
81
82
addTest('place armor stand', async (bot) => {
83
await bot.test.awaitItemReceived(`/give ${bot.username} armor_stand`)
84
const armorStand = await bot.placeEntity(bot.blockAt(bot.entity.position.offset(0, 0, 1)), new Vec3(0, 1, 0))
85
assert(armorStand !== null)
86
let name
87
if (bot.supportFeature('entityNameUpperCaseNoUnderscore')) {
88
name = 'ArmorStand'
89
} else if (bot.supportFeature('entityNameLowerCaseNoUnderscore')) {
90
name = 'armorstand'
91
} else {
92
name = 'armor_stand'
93
}
94
const entity = bot.nearestEntity(o => o.name === name)
95
assert(entity?.name === name)
96
bot.attack(entity)
97
await once(bot, 'entityGone')
98
})
99
100
return tests
101
}
102
103