Skip to content

Commit

Permalink
Acknowledge resource packs (accept if forced)
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Feb 7, 2024
1 parent fa253e3 commit 2795733
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/minecraft/packets/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ const packetIds = {
CLIENTBOUND_PING_CONFIGURATION: generateIdFunction([
[protocolMap['1.20.2'], 0x04]
]),
CLIENTBOUND_ADD_RESOURCE_PACK_CONF: generateIdFunction([
[protocolMap['1.20.3'], 0x07],
[protocolMap['1.20.2'], 0x06]
]),

// ===========================
// Serverbound (configuration)
Expand All @@ -53,6 +57,9 @@ const packetIds = {
SERVERBOUND_PONG_CONFIGURATION: generateIdFunction([
[protocolMap['1.20.2'], 0x04]
]),
SERVERBOUND_RESOURCE_PACK_RESPONSE_CONF: generateIdFunction([
[protocolMap['1.20.2'], 0x05]
]),

// ==================
// Clientbound (play)
Expand Down Expand Up @@ -155,6 +162,18 @@ const packetIds = {
[protocolMap['1.20.3'], 0x67],
[protocolMap['1.20.2'], 0x65]
]),
// AKA Resource Pack Send
// AKA Resource Pack
CLIENTBOUND_ADD_RESOURCE_PACK_PLAY: generateIdFunction([
[protocolMap['1.20.3'], 0x44],
[protocolMap['1.20.2'], 0x42],
[protocolMap['1.19.4'], 0x40],
[protocolMap['1.19.3'], 0x3c],
[protocolMap['1.19.2'], 0x3d],
[protocolMap['1.19'], 0x3a],
[protocolMap['1.17'], 0x3c],
[protocolMap['1.16.4'], 0x38]
]),

// ==================
// Serverbound (play)
Expand Down Expand Up @@ -218,6 +237,15 @@ const packetIds = {
]),
SERVERBOUND_ACKNOWLEDGE_CONFIGURATION: generateIdFunction([
[protocolMap['1.20.2'], 0x0b]
]),
// AKA Resource Pack Status
// AKA Resource Pack
SERVERBOUND_RESOURCE_PACK_RESPONSE_PLAY: generateIdFunction([
[protocolMap['1.20.3'], 0x43],
[protocolMap['1.20.2'], 0x27],
[protocolMap['1.19.2'], 0x24],
[protocolMap['1.19'], 0x23],
[protocolMap['1.16.4'], 0x21]
])
}

Expand Down
49 changes: 47 additions & 2 deletions src/utilities/connection/packetHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,41 @@ const handleSystemMessage = (
}
}

const handleResourcePack = async (
connection: ServerConnection,
packet: Packet,
version: number
): Promise<void> => {
const is117 = version >= protocolMap['1.17']
const is1203 = version >= protocolMap['1.20.3']
let data = packet.data
const uuid = is1203 ? data.slice(0, 16) : null
if (is1203) data = data.slice(16) // Get UUID on 1.20.3+
const [urlLength, urlLengthLength] = readVarInt(data)
data = data.slice(urlLength + urlLengthLength) // Remove URL
const [hashLength, hashLengthLength] = readVarInt(data)
data = data.slice(hashLength + hashLengthLength) // Remove Hash
const forced = is117 ? data.readInt8() : 0 // Get Forced on 1.17+
// TODO: Support resource packs correctly in future, with Prompt Message on 1.17+
// For now, ack if required, else reject.
// 1.17: 0 - Successful download, 2 - Failed download, 3 - Accepted, 1 - Rejected
const response = writeVarInt(forced ? 3 : 1)
const responseId =
connection.state === ConnectionState.CONFIGURATION
? packetIds.SERVERBOUND_RESOURCE_PACK_RESPONSE_CONF(version)
: packetIds.SERVERBOUND_RESOURCE_PACK_RESPONSE_PLAY(version)
await connection.writePacket(
responseId ?? 0,
uuid ? concatPacketData([uuid, response]) : response
)
if (forced) {
await connection.writePacket(
packetIds.SERVERBOUND_RESOURCE_PACK_RESPONSE_CONF(version) ?? 0,
uuid ? concatPacketData([uuid, writeVarInt(0)]) : writeVarInt(0)
)
}
}

export const packetHandler =
(
performedInitialActionsRef: React.MutableRefObject<boolean>,
Expand Down Expand Up @@ -239,7 +274,12 @@ export const packetHandler =
connection // Pong (play)
.writePacket(responseId ?? 0, packet.data)
.catch(handleError(addMessage, unknownError))
}
} else if (
packet.id === packetIds.CLIENTBOUND_ADD_RESOURCE_PACK_PLAY(version)
)
handleResourcePack(connection, packet, version).catch(
handleError(addMessage, unknownError)
)
} else if (connection.state === ConnectionState.CONFIGURATION) {
if (packet.id === packetIds.CLIENTBOUND_PING_CONFIGURATION(version)) {
const responseId = packetIds.SERVERBOUND_PONG_CONFIGURATION(version)
Expand All @@ -251,6 +291,11 @@ export const packetHandler =
packet.id === packetIds.CLIENTBOUND_START_CONFIGURATION(version)
) {
setLoading('Reconfiguring...')
}
} else if (
packet.id === packetIds.CLIENTBOUND_ADD_RESOURCE_PACK_CONF(version)
)
handleResourcePack(connection, packet, version).catch(
handleError(addMessage, unknownError)
)
}
}

0 comments on commit 2795733

Please sign in to comment.