From b65edc3420fdbd80f86aabd6cd2e12e1f6705351 Mon Sep 17 00:00:00 2001 From: Ronnie <105398949+Dueel@users.noreply.github.com> Date: Sat, 3 Aug 2024 20:44:57 -0400 Subject: [PATCH] updating to ModAPI 1.0 --- mod.ts | 19 +- package-lock.json | 260 ------------------ package.json | 3 +- .../clientbound/v1/{ => event}/location.ts | 10 +- packets/clientbound/v1/hello.ts | 30 ++ packets/clientbound/{v1 => v2}/party_info.ts | 41 ++- packets/serverbound/v1/location.ts | 29 -- packets/serverbound/v1/register.ts | 74 +++++ packets/serverbound/{v1 => v2}/party_info.ts | 2 +- 9 files changed, 150 insertions(+), 318 deletions(-) delete mode 100644 package-lock.json rename packets/clientbound/v1/{ => event}/location.ts (87%) create mode 100644 packets/clientbound/v1/hello.ts rename packets/clientbound/{v1 => v2}/party_info.ts (53%) delete mode 100644 packets/serverbound/v1/location.ts create mode 100644 packets/serverbound/v1/register.ts rename packets/serverbound/{v1 => v2}/party_info.ts (97%) diff --git a/mod.ts b/mod.ts index e8c5421..e1402b9 100644 --- a/mod.ts +++ b/mod.ts @@ -1,10 +1,11 @@ -import * as serverbound_location_v1 from './packets/serverbound/v1/location' -import * as serverbound_party_info_v1 from './packets/serverbound/v1/party_info' +import * as serverbound_register_v1 from './packets/serverbound/v1/register' +import * as serverbound_party_info_v2 from './packets/serverbound/v2/party_info' import * as serverbound_ping_v1 from './packets/serverbound/v1/ping' import * as serverbound_player_info_v1 from './packets/serverbound/v1/player_info' -import * as clientbound_location_v1 from './packets/clientbound/v1/location' -import * as clientbound_party_info_v1 from './packets/clientbound/v1/party_info' +import * as clientbound_location_v1 from './packets/clientbound/v1/event/location' +import * as clientbound_hello_v1 from './packets/clientbound/v1/hello' +import * as clientbound_party_info_v2 from './packets/clientbound/v2/party_info' import * as clientbound_ping_v1 from './packets/clientbound/v1/ping' import * as clientbound_player_info_v1 from './packets/clientbound/v1/player_info' import { PacketReader, PacketWriter } from '@lilithmod/unborn-mcproto' @@ -41,10 +42,12 @@ export * from './enums' */ export const serverboundPackets: Record>> = { 1: { - location: serverbound_location_v1, - party_info: serverbound_party_info_v1, + register: serverbound_register_v1, ping: serverbound_ping_v1, player_info: serverbound_player_info_v1 + }, + 2: { + party_info: serverbound_party_info_v2 } } @@ -55,9 +58,11 @@ export const serverboundPackets: Record>> = { 1: { location: clientbound_location_v1, - party_info: clientbound_party_info_v1, ping: clientbound_ping_v1, player_info: clientbound_player_info_v1 + }, + 2: { + party_info: clientbound_party_info_v2, } } diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 53b8637..0000000 --- a/package-lock.json +++ /dev/null @@ -1,260 +0,0 @@ -{ - "name": "hypixel-plugin-message", - "version": "0.3.1", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "name": "hypixel-plugin-message", - "version": "0.3.1", - "license": "ISC", - "dependencies": { - "@lilithmod/unborn-mcproto": "^0.14.0" - }, - "devDependencies": { - "@types/node": "^20.12.7" - } - }, - "node_modules/@lilithmod/unborn-mcproto": { - "version": "0.14.0", - "resolved": "https://registry.npmjs.org/@lilithmod/unborn-mcproto/-/unborn-mcproto-0.14.0.tgz", - "integrity": "sha512-aFTr088HSOMN8C7qn97Ad+B/hllGExX9Pq4oLiAs4Dtx9m+a7Qtu3fLQgR4c+S6ti0ttHmPm+99TKCnpk5G8/w==", - "dependencies": { - "@sapphire/fetch": "^2.2.0", - "nbt-ts": "^1.3.5", - "prismarine-nbt": "^2.2.0", - "uuid-1345": "^1.0.2" - } - }, - "node_modules/@sapphire/fetch": { - "version": "2.4.2", - "resolved": "https://registry.npmjs.org/@sapphire/fetch/-/fetch-2.4.2.tgz", - "integrity": "sha512-aLZJ+ij3vAkHH6JlCbkT7Nj0dNLFdOEv0m1apNPFtUzHpaFdFeayYjLVfsY1JFVssvzjVr4x+mQTQ+XQIqraSg==", - "dependencies": { - "cross-fetch": "^4.0.0" - }, - "engines": { - "node": ">=v14.0.0", - "npm": ">=7.0.0" - } - }, - "node_modules/@types/node": { - "version": "20.12.7", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.12.7.tgz", - "integrity": "sha512-wq0cICSkRLVaf3UGLMGItu/PtdY7oaXaI/RVU+xliKVOtRna3PRY57ZDfztpDL0n11vfymMUnXv8QwYCO7L1wg==", - "dev": true, - "dependencies": { - "undici-types": "~5.26.4" - } - }, - "node_modules/ajv": { - "version": "6.12.6", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", - "integrity": "sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==", - "dependencies": { - "fast-deep-equal": "^3.1.1", - "fast-json-stable-stringify": "^2.0.0", - "json-schema-traverse": "^0.4.1", - "uri-js": "^4.2.2" - }, - "funding": { - "type": "github", - "url": "https://github.com/sponsors/epoberezkin" - } - }, - "node_modules/cross-fetch": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-4.0.0.tgz", - "integrity": "sha512-e4a5N8lVvuLgAWgnCrLr2PP0YyDOTHa9H/Rj54dirp61qXnNq46m82bRhNqIA5VccJtWBvPTFRV3TtvHUKPB1g==", - "dependencies": { - "node-fetch": "^2.6.12" - } - }, - "node_modules/fast-deep-equal": { - "version": "3.1.3", - "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", - "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==" - }, - "node_modules/fast-json-stable-stringify": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", - "integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==" - }, - "node_modules/inherits": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" - }, - "node_modules/json-schema-traverse": { - "version": "0.4.1", - "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz", - "integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg==" - }, - "node_modules/lodash.get": { - "version": "4.4.2", - "resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz", - "integrity": "sha512-z+Uw/vLuy6gQe8cfaFWD7p0wVv8fJl3mbzXh33RS+0oW2wvUqiRXiQ69gLWSLpgB5/6sU+r6BlQR0MBILadqTQ==" - }, - "node_modules/lodash.reduce": { - "version": "4.6.0", - "resolved": "https://registry.npmjs.org/lodash.reduce/-/lodash.reduce-4.6.0.tgz", - "integrity": "sha512-6raRe2vxCYBhpBu+B+TtNGUzah+hQjVdu3E17wfusjyrXBka2nBS8OH/gjVZ5PvHOhWmIZTYri09Z6n/QfnNMw==" - }, - "node_modules/macaddress": { - "version": "0.5.3", - "resolved": "https://registry.npmjs.org/macaddress/-/macaddress-0.5.3.tgz", - "integrity": "sha512-vGBKTA+jwM4KgjGZ+S/8/Mkj9rWzePyGY6jManXPGhiWu63RYwW8dKPyk5koP+8qNVhPhHgFa1y/MJ4wrjsNrg==" - }, - "node_modules/nbt-ts": { - "version": "1.3.6", - "resolved": "https://registry.npmjs.org/nbt-ts/-/nbt-ts-1.3.6.tgz", - "integrity": "sha512-TFMiJu0BQrH6IrVjVgk1osoN58Ep3SDz4rIM5h5y3nabT6sdSujnKKV9nJyEWBO/JfdWWCma3fiagcOvlXLojw==" - }, - "node_modules/node-fetch": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz", - "integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==", - "dependencies": { - "whatwg-url": "^5.0.0" - }, - "engines": { - "node": "4.x || >=6.0.0" - }, - "peerDependencies": { - "encoding": "^0.1.0" - }, - "peerDependenciesMeta": { - "encoding": { - "optional": true - } - } - }, - "node_modules/prismarine-nbt": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/prismarine-nbt/-/prismarine-nbt-2.5.0.tgz", - "integrity": "sha512-F0/8UAa9SDDnAGrBYqZc4nG8h2zj5cE2eAJU5xlDR/IsQQ3moVxkOjE3h3nMv6SbvZrvAcgX7waA/nd9LLHYdA==", - "dependencies": { - "protodef": "^1.9.0" - } - }, - "node_modules/protodef": { - "version": "1.15.0", - "resolved": "https://registry.npmjs.org/protodef/-/protodef-1.15.0.tgz", - "integrity": "sha512-bZ2Omw8dT+DACjJHLrBWZlqN4MlT9g9oSpJDdkUAJOStUzgJp+Zn42FJfPUdwutUxjaxA0PftN0PDlNa2XbneA==", - "dependencies": { - "lodash.get": "^4.4.2", - "lodash.reduce": "^4.6.0", - "protodef-validator": "^1.3.0", - "readable-stream": "^3.0.3" - }, - "engines": { - "node": ">=14" - } - }, - "node_modules/protodef-validator": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/protodef-validator/-/protodef-validator-1.3.1.tgz", - "integrity": "sha512-lZ5FWKZYR9xOjpMw1+EfZRfCjzNRQWPq+Dk+jki47Sikl2EeWEPnTfnJERwnU/EwFq6us+0zqHHzSsmLeYX+Lg==", - "dependencies": { - "ajv": "^6.5.4" - }, - "bin": { - "protodef-validator": "cli.js" - } - }, - "node_modules/punycode": { - "version": "2.3.1", - "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", - "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", - "engines": { - "node": ">=6" - } - }, - "node_modules/readable-stream": { - "version": "3.6.2", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", - "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", - "dependencies": { - "inherits": "^2.0.3", - "string_decoder": "^1.1.1", - "util-deprecate": "^1.0.1" - }, - "engines": { - "node": ">= 6" - } - }, - "node_modules/safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ] - }, - "node_modules/string_decoder": { - "version": "1.3.0", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", - "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", - "dependencies": { - "safe-buffer": "~5.2.0" - } - }, - "node_modules/tr46": { - "version": "0.0.3", - "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", - "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" - }, - "node_modules/undici-types": { - "version": "5.26.5", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", - "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", - "dev": true - }, - "node_modules/uri-js": { - "version": "4.4.1", - "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", - "integrity": "sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==", - "dependencies": { - "punycode": "^2.1.0" - } - }, - "node_modules/util-deprecate": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==" - }, - "node_modules/uuid-1345": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/uuid-1345/-/uuid-1345-1.0.2.tgz", - "integrity": "sha512-bA5zYZui+3nwAc0s3VdGQGBfbVsJLVX7Np7ch2aqcEWFi5lsAEcmO3+lx3djM1npgpZI8KY2FITZ2uYTnYUYyw==", - "dependencies": { - "macaddress": "^0.5.1" - } - }, - "node_modules/webidl-conversions": { - "version": "3.0.1", - "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", - "integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==" - }, - "node_modules/whatwg-url": { - "version": "5.0.0", - "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", - "integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==", - "dependencies": { - "tr46": "~0.0.3", - "webidl-conversions": "^3.0.0" - } - } - } -} diff --git a/package.json b/package.json index ed662dc..c8316cc 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "author": "", "license": "ISC", "dependencies": { - "@lilithmod/unborn-mcproto": "^0.14.0" + "@lilithmod/unborn-mcproto": "^0.14.0", + "hypixel-plugin-message": "file:" }, "devDependencies": { "@types/node": "^20.12.7" diff --git a/packets/clientbound/v1/location.ts b/packets/clientbound/v1/event/location.ts similarity index 87% rename from packets/clientbound/v1/location.ts rename to packets/clientbound/v1/event/location.ts index 199384e..e2f36db 100644 --- a/packets/clientbound/v1/location.ts +++ b/packets/clientbound/v1/event/location.ts @@ -1,6 +1,6 @@ -import { VersionedPacket } from '../../../mod' +import { VersionedPacket } from '../../../../mod' import { PacketReader, PacketWriter } from '@lilithmod/unborn-mcproto' -import { Environment, environmentToId, getEnvironmentFromId } from '../../../enums' +import { Environment, environmentToId, getEnvironmentFromId } from '../../../../enums' const CURRENT_VERSION = 1 @@ -9,8 +9,6 @@ const CURRENT_VERSION = 1 * Similar to /locraw, and based on the same data. */ export interface ClientboundLocationPacketV1 extends VersionedPacket { - environment: Environment - proxyName: string serverName: string serverType?: string lobbyName?: string @@ -28,8 +26,6 @@ export function read(buffer: Buffer): ClientboundLocationPacketV1 { const packet: ClientboundLocationPacketV1 = { version: reader.id, - environment: getEnvironmentFromId(reader.readVarInt()), - proxyName: reader.readString(), serverName: reader.readString(), serverType: reader.readOptional(reader.readString.bind(reader)), lobbyName: reader.readOptional(reader.readString.bind(reader)), @@ -54,8 +50,6 @@ export function write(packet: ClientboundLocationPacketV1): Buffer { const writer = new PacketWriter(CURRENT_VERSION) writer - .writeVarInt(environmentToId(packet.environment)) - .writeString(packet.proxyName) .writeString(packet.serverName) .writeOptional(packet.serverType, writer.writeString.bind(writer)) .writeOptional(packet.lobbyName, writer.writeString.bind(writer)) diff --git a/packets/clientbound/v1/hello.ts b/packets/clientbound/v1/hello.ts new file mode 100644 index 0000000..49605d9 --- /dev/null +++ b/packets/clientbound/v1/hello.ts @@ -0,0 +1,30 @@ +import { PacketReader, PacketWriter } from '@lilithmod/unborn-mcproto' + +export interface ClientboundHelloPacket { + environment: number +} + +/** + * Reads a clientbound hello packet from a buffer. + * @param buffer A buffer containing the packet data. + * @returns The parsed ClientboundHelloPacket + */ +export function read(buffer: Buffer): ClientboundHelloPacket { + const reader = new PacketReader(buffer) + const environmentId = reader.readVarInt() + + const packet: ClientboundHelloPacket = { + environment: environmentId + } + + return packet +} + +/** + * Writes a clientbound hello packet to a new buffer. + * @param packet The packet to write. + * @returns A buffer containing the packet data. + */ +export function write(packet: ClientboundHelloPacket): Buffer { + return new PacketWriter(packet.environment).buffer +} \ No newline at end of file diff --git a/packets/clientbound/v1/party_info.ts b/packets/clientbound/v2/party_info.ts similarity index 53% rename from packets/clientbound/v1/party_info.ts rename to packets/clientbound/v2/party_info.ts index acfabe7..1fcee34 100644 --- a/packets/clientbound/v1/party_info.ts +++ b/packets/clientbound/v2/party_info.ts @@ -3,10 +3,20 @@ import { PacketReader, PacketWriter } from '@lilithmod/unborn-mcproto' const CURRENT_VERSION = 1 -export interface ClientboundPartyInfoPacketV1 extends VersionedPacket { +enum PartyRole { + LEADER, + MOD, + MEMBER +} + +interface PartyMember { + uuid: string + role: PartyRole +} + +export interface ClientboundPartyInfoPacketV2 extends VersionedPacket { inParty: boolean - leader?: string - members?: string[] + members?: Map } /** @@ -14,18 +24,23 @@ export interface ClientboundPartyInfoPacketV1 extends VersionedPacket { * @param buffer A buffer containing the packet data. The version number should be the first entry in the buffer. * @returns */ -export function read(buffer: Buffer): ClientboundPartyInfoPacketV1 { +export function read(buffer: Buffer): ClientboundPartyInfoPacketV2 { const reader = new PacketReader(buffer) const inParty = reader.readBool() - const packet: ClientboundPartyInfoPacketV1 = { + const packet: ClientboundPartyInfoPacketV2 = { version: reader.id, - inParty + inParty, + members: new Map() } if (inParty) { - packet.leader = reader.readUUID() - packet.members = reader.readArray(reader.readUUID.bind(reader)) + const memberCount = reader.readVarInt() + for (let i = 0; i < memberCount; i++) { + const uuid = reader.readUUID() + const role = reader.readVarInt() as PartyRole + packet.members?.set(uuid, { uuid, role }) + } } return packet @@ -36,16 +51,18 @@ export function read(buffer: Buffer): ClientboundPartyInfoPacketV1 { * @param packet The packet to write. * @returns A buffer containing the packet data. A success byte should be written to the buffer before this data. */ -export function write(packet: ClientboundPartyInfoPacketV1): Buffer { +export function write(packet: ClientboundPartyInfoPacketV2): Buffer { const writer = new PacketWriter(CURRENT_VERSION) writer .writeBool(packet.inParty) if (packet.inParty) { - writer - .writeUUID(packet.leader!) - .writeArray(packet.members!, writer.writeUUID.bind(writer)) + writer.writeVarInt(packet.members?.size ?? 0) + for (const [uuid, member] of packet.members ?? []) { + writer.writeUUID(uuid) + writer.writeVarInt(member.role) + } } return writer.buffer diff --git a/packets/serverbound/v1/location.ts b/packets/serverbound/v1/location.ts deleted file mode 100644 index dd2ef5e..0000000 --- a/packets/serverbound/v1/location.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { VersionedPacket } from '../../../mod' -import { PacketReader, PacketWriter } from '@lilithmod/unborn-mcproto' - -const CURRENT_VERSION = 1 - -/** - * Represents a serverbound location packet. - * These are empty since they are only used to request a packet from the server. - */ -export interface ServerboundLocationPacketV1 extends VersionedPacket {} - -/** - * Reads a clientbound location packet from a buffer. - * @param buffer A buffer containing the packet data. The version number should be the first entry in the buffer. - * @returns - */ -export function read(buffer: Buffer): ServerboundLocationPacketV1 { - const reader = new PacketReader(buffer) - return { version: reader.id } -} - -/** - * Writes a new serverbound location packet to a new buffer. The server will return either an error or a successful location packet. - * @param packet The packet to write. - * @returns A buffer containing the packet data. - */ -export function write(): Buffer { - return new PacketWriter(CURRENT_VERSION).buffer -} \ No newline at end of file diff --git a/packets/serverbound/v1/register.ts b/packets/serverbound/v1/register.ts new file mode 100644 index 0000000..b84cc01 --- /dev/null +++ b/packets/serverbound/v1/register.ts @@ -0,0 +1,74 @@ +import { VersionedPacket } from '../../../mod' +import { PacketReader, PacketWriter } from '@lilithmod/unborn-mcproto' + +const MAX_IDENTIFIER_LENGTH = 20 +const MAX_IDENTIFIERS = 5 +const CURRENT_VERSION = 1 + +export interface ServerboundRegisterPacketV1 extends VersionedPacket { + subscribedEvents: Map +} + +export interface PacketRegistry { + getEventVersions(subscribedEventIdentifiers: Set): Map +} + +/** + * Creates a new ServerboundRegisterPacket. + * @param registry The packet registry to use for getting event versions. + * @param subscribedEventIdentifiers The set of event identifiers to subscribe to. + * @returns A ServerboundRegisterPacketV1 object. + * @throws Error if the number of subscribed events exceeds MAX_IDENTIFIERS. + */ +export function create(registry: PacketRegistry, subscribedEventIdentifiers: Set): ServerboundRegisterPacketV1 { + const subscribedEvents = registry.getEventVersions(subscribedEventIdentifiers) + + if (subscribedEvents.size > MAX_IDENTIFIERS) { + throw new Error(`wantedPackets cannot contain more than ${MAX_IDENTIFIERS} identifiers`) + } + + return { + version: CURRENT_VERSION, + subscribedEvents + } +} + +/** + * Reads a serverbound register packet from a buffer. + * @param buffer A buffer containing the packet data. + * @returns The parsed ServerboundRegisterPacketV1 + * @throws Error if the number of subscribed events exceeds MAX_IDENTIFIERS. + */ +export function read(buffer: Buffer): ServerboundRegisterPacketV1 { + const reader = new PacketReader(buffer) + const version = reader.readVarInt() + const size = reader.readVarInt() + + if (size > MAX_IDENTIFIERS) { + throw new Error(`wantedPackets cannot contain more than ${MAX_IDENTIFIERS} identifiers`) + } + + const subscribedEvents = new Map() + for (let i = 0; i < size; i++) { + const key = reader.readString(); + const value = reader.readVarInt() + subscribedEvents.set(key, value) + } + + return { version, subscribedEvents } +} + +/** + * Writes a serverbound register packet to a new buffer. + * @param packet The packet to write. + * @returns A buffer containing the packet data. + */ +export function write(packet: ServerboundRegisterPacketV1): Buffer { + const writer = new PacketWriter(CURRENT_VERSION) + writer.writeVarInt(packet.subscribedEvents.size) + for (const [event, version] of packet.subscribedEvents) { + writer.writeString(event) + writer.writeVarInt(version) + } + return writer.buffer +} \ No newline at end of file diff --git a/packets/serverbound/v1/party_info.ts b/packets/serverbound/v2/party_info.ts similarity index 97% rename from packets/serverbound/v1/party_info.ts rename to packets/serverbound/v2/party_info.ts index a2d4547..bdd715e 100644 --- a/packets/serverbound/v1/party_info.ts +++ b/packets/serverbound/v2/party_info.ts @@ -1,7 +1,7 @@ import { VersionedPacket } from '../../../mod' import { PacketReader, PacketWriter } from '@lilithmod/unborn-mcproto' -const CURRENT_VERSION = 1 +const CURRENT_VERSION = 2 /** * Represents a serverbound party info packet.