Skip to content

Commit

Permalink
Fix 1.20.4 server resource pack error (#3320)
Browse files Browse the repository at this point in the history
* Added a basic emit/catch for the add_resource_pack and remove_resource_pack packets

* Fixed a typo in a variable name

* Fixed a misnaming.

* Changed resourcePack event to allow uuid or hash

* Updated to a dictionary for active packs, also updated acceptResourcePack to  allow 1.20.3 / 1.20.4 accept pack packets

* Fixed errors in Lint test

* Updated add_resource_pack to use UUID

* Removed test print

* Fixed feature checking

* Sent resource pack loaded packet and now it works

* Fixed coding conventions
  • Loading branch information
TerminalCalamitas authored Mar 17, 2024
1 parent d9e9e15 commit 7c01eeb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export interface BotEvents {
teamMemberRemoved: (team: Team) => Promise<void> | void
bossBarDeleted: (bossBar: BossBar) => Promise<void> | void
bossBarUpdated: (bossBar: BossBar) => Promise<void> | void
resourcePack: (url: string, hash: string) => Promise<void> | void
resourcePack: (url: string, hash?: string, uuid?: string) => Promise<void> | void
particle: (particle: Particle) => Promise<void> | void
}

Expand Down
53 changes: 51 additions & 2 deletions lib/plugins/resource_pack.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,51 @@
const UUID = require('uuid-1345')

module.exports = inject

function inject (bot) {
let uuid
let latestHash
let latestUUID
let activeResourcePacks = {}
const TEXTURE_PACK_RESULTS = {
SUCCESSFULLY_LOADED: 0,
DECLINED: 1,
FAILED_DOWNLOAD: 2,
ACCEPTED: 3
}

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
const uuid = new UUID(data.uuid)
// Adding the pack to a set by uuid
latestUUID = uuid
activeResourcePacks[uuid] = data.url

bot.emit('resourcePack', data.url, uuid)
})

bot._client.on('remove_resource_pack', (data) => { // Doesn't emit anything because it is removing rather than adding
// if uuid isn't provided remove all packs
if (data.uuid === undefined) {
activeResourcePacks = {}
} else {
// Try to remove uuid from set
try {
delete activeResourcePacks[new UUID(data.uuid)]
} catch (error) {
console.error('Tried to remove UUID but it was not in the active list.')
}
}
})

bot._client.on('resource_pack_send', (data) => {
bot.emit('resourcePack', data.url, data.hash)
latestHash = data.hash
if (bot.supportFeature('resourcePackUsesUUID')) {
uuid = new UUID(data.uuid)
bot.emit('resourcePack', uuid, data.url)
latestUUID = uuid
} else {
bot.emit('resourcePack', data.url, data.hash)
latestHash = data.hash
}
})

function acceptResourcePack () {
Expand All @@ -24,6 +58,15 @@ function inject (bot) {
result: TEXTURE_PACK_RESULTS.SUCCESSFULLY_LOADED,
hash: latestHash
})
} else if (bot.supportFeature('resourcePackUsesUUID')) {
bot._client.write('resource_pack_receive', {
uuid: latestUUID,
result: TEXTURE_PACK_RESULTS.ACCEPTED
})
bot._client.write('resource_pack_receive', {
uuid: latestUUID,
result: TEXTURE_PACK_RESULTS.SUCCESSFULLY_LOADED
})
} else {
bot._client.write('resource_pack_receive', {
result: TEXTURE_PACK_RESULTS.ACCEPTED
Expand All @@ -35,6 +78,12 @@ function inject (bot) {
}

function denyResourcePack () {
if (bot.supportFeature('resourcePackUsesUUID')) {
bot._client.write('resource_pack_receive', {
uuid: latestUUID,
result: TEXTURE_PACK_RESULTS.DECLINED
})
}
bot._client.write('resource_pack_receive', {
result: TEXTURE_PACK_RESULTS.DECLINED
})
Expand Down

0 comments on commit 7c01eeb

Please sign in to comment.