Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS
GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/lib/plugins/resource_pack.js
1467 views
1
const UUID = require('uuid-1345')
2
3
module.exports = inject
4
5
function inject (bot) {
6
let uuid
7
let latestHash
8
let latestUUID
9
let activeResourcePacks = {}
10
const TEXTURE_PACK_RESULTS = {
11
SUCCESSFULLY_LOADED: 0,
12
DECLINED: 1,
13
FAILED_DOWNLOAD: 2,
14
ACCEPTED: 3
15
}
16
17
bot._client.on('add_resource_pack', (data) => { // Emits the same as resource_pack_send but sends uuid rather than hash because that's how active packs are tracked
18
const uuid = new UUID(data.uuid)
19
// Adding the pack to a set by uuid
20
latestUUID = uuid
21
activeResourcePacks[uuid] = data.url
22
23
bot.emit('resourcePack', data.url, uuid)
24
})
25
26
bot._client.on('remove_resource_pack', (data) => { // Doesn't emit anything because it is removing rather than adding
27
// if uuid isn't provided remove all packs
28
if (data.uuid === undefined) {
29
activeResourcePacks = {}
30
} else {
31
// Try to remove uuid from set
32
try {
33
delete activeResourcePacks[new UUID(data.uuid)]
34
} catch (error) {
35
console.error('Tried to remove UUID but it was not in the active list.')
36
}
37
}
38
})
39
40
bot._client.on('resource_pack_send', (data) => {
41
if (bot.supportFeature('resourcePackUsesUUID')) {
42
uuid = new UUID(data.uuid)
43
bot.emit('resourcePack', uuid, data.url)
44
latestUUID = uuid
45
} else {
46
bot.emit('resourcePack', data.url, data.hash)
47
latestHash = data.hash
48
}
49
})
50
51
function acceptResourcePack () {
52
if (bot.supportFeature('resourcePackUsesHash')) {
53
bot._client.write('resource_pack_receive', {
54
result: TEXTURE_PACK_RESULTS.ACCEPTED,
55
hash: latestHash
56
})
57
bot._client.write('resource_pack_receive', {
58
result: TEXTURE_PACK_RESULTS.SUCCESSFULLY_LOADED,
59
hash: latestHash
60
})
61
} else if (bot.supportFeature('resourcePackUsesUUID')) {
62
bot._client.write('resource_pack_receive', {
63
uuid: latestUUID,
64
result: TEXTURE_PACK_RESULTS.ACCEPTED
65
})
66
bot._client.write('resource_pack_receive', {
67
uuid: latestUUID,
68
result: TEXTURE_PACK_RESULTS.SUCCESSFULLY_LOADED
69
})
70
} else {
71
bot._client.write('resource_pack_receive', {
72
result: TEXTURE_PACK_RESULTS.ACCEPTED
73
})
74
bot._client.write('resource_pack_receive', {
75
result: TEXTURE_PACK_RESULTS.SUCCESSFULLY_LOADED
76
})
77
}
78
}
79
80
function denyResourcePack () {
81
if (bot.supportFeature('resourcePackUsesUUID')) {
82
bot._client.write('resource_pack_receive', {
83
uuid: latestUUID,
84
result: TEXTURE_PACK_RESULTS.DECLINED
85
})
86
}
87
bot._client.write('resource_pack_receive', {
88
result: TEXTURE_PACK_RESULTS.DECLINED
89
})
90
}
91
92
bot.acceptResourcePack = acceptResourcePack
93
bot.denyResourcePack = denyResourcePack
94
}
95
96