diff --git a/packages/client/src/event.ts b/packages/client/src/event.ts index 27ce454c..124e8a6e 100644 --- a/packages/client/src/event.ts +++ b/packages/client/src/event.ts @@ -3,12 +3,12 @@ import { $ } from './color' export function attachEvents(client) { client.on('chat', (packet) => { // Listen for chat messages and echo them back. - var jsonMsg = JSON.parse(packet.message); + var jsonMsg = JSON.parse(packet.message) console.log($(jsonMsg)) }) client.on('state', (newState, oldState) => { console.log('Client Change State', oldState, 'to', newState) - let targetServer = process.argv[3] + let targetServer = process.argv[6] if (newState == "play" && targetServer) { setTimeout(() => { client.write('chat', { @@ -24,4 +24,10 @@ export function attachEvents(client) { } else if (packet.health > 0) { } }) + client.on('kick_disconnect', (packet) => { + console.log($(packet.reason)) + }) + client.on('disconnect', (packet) => { + console.log($(packet.reason)) + }) } diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index 0d740170..3a2e7feb 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -5,33 +5,52 @@ import { attachForge } from './forge' import { attachEvents } from './event' let username = process.argv[2] || 'Mr_jtb' -let version = process.argv[3] || '1.12.2' -let address = process.argv[4] || '192.168.2.5:25577' -let realAddress = address.split(":"); -let client = createConnection(realAddress[0], parseInt(realAddress[1] || "25565"), username) +let password = process.argv[3] || '' +let version = process.argv[4] || '1.12.2' +let readAddress = process.argv[5] || '192.168.2.5:25577' +let realAddress = readAddress.split(":") +let address = realAddress[0] +let port = parseInt(realAddress[1] || "25565") +let client = commandLineCreateClient() -function createConnection(host: string, port: number, username: string) { +function commandLineCreateClient() { + return createConnection(address, port, username, password) +} + +function createConnection(host: string, port: number, username: string, password: string) { let client = createClient({ version, host, port, username, - skipValidation: true + password, + clientToken: 'd02c7f39-2376-45da-a5a5-50e24fa8b185', + //@ts-ignore + authServer: 'https://mcsso.yumc.pw/api/yggdrasil/authserver', + sessionServer: 'https://mcsso.yumc.pw/api/yggdrasil/sessionserver' }) + attachCommon(client) attachForge(client) attachEvents(client) - return client; + return client } -client.on('error', (error) => { - console.log("Client Error", error) -}) - -client.on('end', (resone) => { - console.log("Client End Resone:", resone) - client = createConnection('192.168.2.5', 25577, username) -}) +function attachCommon(client) { + client.on('error', (error) => { + console.log("Client Error", error) + }) + client.on('end', (resone) => { + console.log("Client End Resone:", resone) + if (`${resone}` != "SocketClosed") { + setTimeout(() => { + client = commandLineCreateClient() + }, 500) + } else { + process.exit(0) + } + }) +} const rl = createInterface({ input: process.stdin, @@ -54,22 +73,22 @@ const rl = createInterface({ rl.on('line', function (line) { switch (line) { case "": - break; + break case "eval": - break; + break case "write": - break; + break case "/respawn": client.write('client_command', { payload: 0 }) - break; + break case "//reco": client.end("") - client = createConnection('192.168.2.5', 25577, username) - break; + client = commandLineCreateClient() + break case "//quit": console.info('Disconnected') client.end("") - break; + break case "//end": console.info('Forcibly ended client') process.exit(0)