feat: add auto respawn support

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-02-23 22:35:05 +08:00
parent c13c4aefca
commit 11c91600da
4 changed files with 76 additions and 36 deletions

View File

@ -20,6 +20,7 @@
"clean": "rimraf dist", "clean": "rimraf dist",
"watch": "tsc --watch", "watch": "tsc --watch",
"build": "yarn clean && tsc", "build": "yarn clean && tsc",
"start":"node dist/index.js",
"test": "echo \"Error: run tests from root\" && exit 1" "test": "echo \"Error: run tests from root\" && exit 1"
}, },
"dependencies": { "dependencies": {

View File

@ -87,7 +87,11 @@ function $(input: any) {
if (typeof input === "string") { if (typeof input === "string") {
input = JSON.parse(input) input = JSON.parse(input)
} }
return mcColor2ANSI(json2text(input) + '§r') input = json2text(input) + '§r'
if (input.startsWith('§卐')) {
input = input.substring(2)
}
return mcColor2ANSI(input)
} }
export { export {

View File

@ -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) {
}
})
}

View File

@ -1,53 +1,61 @@
import { createInterface } from 'readline' import { createInterface } from 'readline'
import { createClient } from 'minecraft-protocol' import { createClient } from 'minecraft-protocol'
import { $ } from './color'
import { attachForge } from './forge' import { attachForge } from './forge'
import { attachEvents } from './event'
let client = createClient({ let username = process.argv[2] || 'Mr_jtb'
let client = createConnection('192.168.2.5', 25577, username)
function createConnection(host: string, port: number, username: string) {
let client = createClient({
version: '1.12.2', version: '1.12.2',
host: '192.168.2.5', host,
port: 25577, port,
username: process.argv[2] || 'Mr_jtb', username,
skipValidation: true skipValidation: true
})
attachForge(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)
} attachForge(client)
attachEvents(client)
return client;
}
client.on('end', (resone) => {
console.log("Client End Resone:", resone)
client = createConnection('192.168.2.5', 25577, username)
}) })
const rl = createInterface({ const rl = createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout, output: process.stdout,
terminal: false terminal: true,
prompt: ''
}) })
rl.on('line', function(line) { rl.on('line', function(line) {
if (line === '') { switch (line) {
return case "":
} else if (line === '/quit') { 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') console.info('Disconnected')
client.end("") client.end("")
return break;
} else if (line === '/end') { case "//end":
console.info('Forcibly ended client') console.info('Forcibly ended client')
process.exit(0) process.exit(0)
} default:
client.write('chat', { message: line }) client.write('chat', { message: line })
}
rl.prompt()
}) })