From b458da7a6e3692fcde38ba08185b8b731df88e79 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Sun, 23 Feb 2020 22:35:05 +0800 Subject: [PATCH] feat: add auto respawn support Signed-off-by: MiaoWoo --- packages/client/package.json | 1 + packages/client/src/color.ts | 6 ++- packages/client/src/event.ts | 27 +++++++++++++ packages/client/src/index.ts | 78 ++++++++++++++++++++---------------- 4 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 packages/client/src/event.ts diff --git a/packages/client/package.json b/packages/client/package.json index e3317e8b..ff10eacb 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -20,6 +20,7 @@ "clean": "rimraf dist", "watch": "tsc --watch", "build": "yarn clean && tsc", + "start":"node dist/index.js", "test": "echo \"Error: run tests from root\" && exit 1" }, "dependencies": { diff --git a/packages/client/src/color.ts b/packages/client/src/color.ts index 848d2ac2..db8d3076 100644 --- a/packages/client/src/color.ts +++ b/packages/client/src/color.ts @@ -87,7 +87,11 @@ function $(input: any) { if (typeof input === "string") { input = JSON.parse(input) } - return mcColor2ANSI(json2text(input) + '§r') + input = json2text(input) + '§r' + if (input.startsWith('§卐')) { + input = input.substring(2) + } + return mcColor2ANSI(input) } export { diff --git a/packages/client/src/event.ts b/packages/client/src/event.ts new file mode 100644 index 00000000..27ce454c --- /dev/null +++ b/packages/client/src/event.ts @@ -0,0 +1,27 @@ +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); + console.log($(jsonMsg)) + }) + client.on('state', (newState, oldState) => { + console.log('Client Change State', oldState, 'to', newState) + let targetServer = process.argv[3] + if (newState == "play" && targetServer) { + setTimeout(() => { + client.write('chat', { + message: '/server ' + targetServer + }) + }, 3000) + } + }) + client.on('update_health', (packet) => { + if (packet.health <= 0) { + console.log("Player Dead Auto Respawn...") + client.write('client_command', { payload: 0 }) + } else if (packet.health > 0) { + } + }) +} diff --git a/packages/client/src/index.ts b/packages/client/src/index.ts index a02da0f2..810dd2bb 100644 --- a/packages/client/src/index.ts +++ b/packages/client/src/index.ts @@ -1,53 +1,61 @@ import { createInterface } from 'readline' import { createClient } from 'minecraft-protocol' -import { $ } from './color' import { attachForge } from './forge' +import { attachEvents } from './event' -let client = createClient({ - version: '1.12.2', - host: '192.168.2.5', - port: 25577, - username: process.argv[2] || 'Mr_jtb', - skipValidation: true -}) +let username = process.argv[2] || 'Mr_jtb' +let client = createConnection('192.168.2.5', 25577, username) -attachForge(client) +function createConnection(host: string, port: number, username: string) { + let client = createClient({ + version: '1.12.2', + host, + port, + username, + skipValidation: true + }) -client.on('chat', (packet) => { - // Listen for chat messages and echo them back. - var jsonMsg = JSON.parse(packet.message); - console.log($(jsonMsg)) -}) + attachForge(client) + attachEvents(client) + return client; +} -client.on('state', (newState, oldState) => { - console.log('Client Change State', oldState, 'to', newState) - let targetServer = process.argv[3] - if (newState == "play" && targetServer) { - setTimeout(() => { - client.write('chat', { - message: '/server ' + targetServer - }) - }, 3000) - } +client.on('end', (resone) => { + console.log("Client End Resone:", resone) + client = createConnection('192.168.2.5', 25577, username) }) const rl = createInterface({ input: process.stdin, output: process.stdout, - terminal: false + terminal: true, + prompt: '' }) rl.on('line', function(line) { - if (line === '') { - return - } else if (line === '/quit') { - console.info('Disconnected') - client.end("") - return - } else if (line === '/end') { - console.info('Forcibly ended client') - process.exit(0) + switch (line) { + case "": + break; + case "/respawn": + client.write('client_command', { payload: 0 }) + // client.write("respawn", { + + // }) + break; + case "//reco": + client.end("") + client = createConnection('192.168.2.5', 25577, username) + break; + case "//quit": + console.info('Disconnected') + client.end("") + break; + case "//end": + console.info('Forcibly ended client') + process.exit(0) + default: + client.write('chat', { message: line }) } - client.write('chat', { message: line }) + rl.prompt() })