From d5d0b4a6a56ed671ab1696ca41565ca62aa7645e Mon Sep 17 00:00:00 2001 From: WorldEditAxe <84165981+WorldEditAxe@users.noreply.github.com> Date: Wed, 21 Jun 2023 10:45:22 +0000 Subject: [PATCH] eaglerproxy 1.0.7, eagpaas 1.0.1, smart compile --- .config/build.sh | 2 +- src/plugins/EagProxyAAS/utils.ts | 2 +- src/proxy/Player.ts | 70 +++++++++++++++++++------------- 3 files changed, 44 insertions(+), 30 deletions(-) diff --git a/.config/build.sh b/.config/build.sh index 659cbbe..0061c43 100755 --- a/.config/build.sh +++ b/.config/build.sh @@ -5,7 +5,7 @@ npm install echo "compiling eaglerproxy, please wait... " echo "this may take a while when you first compile the proxy, or when the proxy has to be recompiled (usually due to modification of anything in the src folder, including config.ts)" -echo "after the initial compile, startups will be a lot more faster and snappier" +echo "after the initial compile, consequent startups will be a lot more faster and snappier" set +e diff --git a/src/plugins/EagProxyAAS/utils.ts b/src/plugins/EagProxyAAS/utils.ts index 754d7bb..c77ff16 100644 --- a/src/plugins/EagProxyAAS/utils.ts +++ b/src/plugins/EagProxyAAS/utils.ts @@ -253,6 +253,7 @@ export async function onConnect(client: ClientState) { skipValidation: true, hideErrors: true }) + break } catch (err) { if (!client.gameClient.ended) { sendMessage(client.gameClient, `Something went wrong whilst switching servers: ${err.message}.`, { color: "red" }) @@ -261,7 +262,6 @@ export async function onConnect(client: ClientState) { } } } - break } } } catch (err) { diff --git a/src/proxy/Player.ts b/src/proxy/Player.ts index d8dfdf4..de02cdf 100644 --- a/src/proxy/Player.ts +++ b/src/proxy/Player.ts @@ -141,39 +141,51 @@ export class Player extends EventEmitter { await this._bindListenersMineClient(this.serverConnection) } - public async switchServers(options: ClientOptions) { + public switchServers(options: ClientOptions) { if (!this._alreadyConnected) throw new Error(`Invalid state: Player hasn't already been connected to a server, and .switchServers() has been called. Please use .connect() when initially connecting to a server, and only use .switchServers() if you want to switch servers.`) - this._switchingServers = true - this.ws.send(this._serializer.createPacketBuffer({ - name: 'chat', - params: { - message: `${Enums.ChatColor.GRAY}Switching servers...`, - position: 1 - } - })) - this.ws.send(this._serializer.createPacketBuffer({ - name: 'playerlist_header', - params: { - header: JSON.stringify({ - text: "" - }), - footer: JSON.stringify({ - text: "" + return new Promise(async (res, rej) => { + const oldConnection = this.serverConnection + this._switchingServers = true + + this.ws.send(this._serializer.createPacketBuffer({ + name: 'chat', + params: { + message: `${Enums.ChatColor.GRAY}Switching servers...`, + position: 1 + } + })) + this.ws.send(this._serializer.createPacketBuffer({ + name: 'playerlist_header', + params: { + header: JSON.stringify({ + text: "" + }), + footer: JSON.stringify({ + text: "" + }) + } + })) + + this.serverConnection = createClient(Object.assign({ + version: '1.8.9', + keepAlive: false, + hideErrors: false + }, options)) + + await this._bindListenersMineClient(this.serverConnection, true, () => oldConnection.end()) + .then(() => { + this.emit('switchServer', this.serverConnection, this) + res() }) - } - })) - this.serverConnection.end() - this.serverConnection = createClient(Object.assign({ - version: '1.8.9', - keepAlive: false, - hideErrors: false - }, options)) - await this._bindListenersMineClient(this.serverConnection, true) - this.emit('switchServer', this.serverConnection, this) + .catch(err => { + this.serverConnection = oldConnection + rej(err) + }) + }) } - private async _bindListenersMineClient(client: Client, switchingServers?: boolean) { + private async _bindListenersMineClient(client: Client, switchingServers?: boolean, onSwitch?: Function) { return new Promise((res, rej) => { let stream = false, uuid const listener = msg => { @@ -214,6 +226,7 @@ export class Player extends EventEmitter { })) pckSeq.forEach(p => this.ws.send(p)) stream = true + if (onSwitch) onSwitch() res(null) } else if (meta.name == 'success' && meta.state == states.LOGIN && !uuid) { uuid = packet.uuid @@ -225,6 +238,7 @@ export class Player extends EventEmitter { params: packet })) stream = true + if (onSwitch) onSwitch() res(null) } else if (meta.name == 'success' && meta.state == states.LOGIN && !uuid) { uuid = packet.uuid