feat: add auto respawn support

Signed-off-by: MiaoWoo <admin@yumc.pw>
clean
MiaoWoo 2020-02-23 22:35:05 +08:00
parent e8d41e8a43
commit f660b4a4f8
4 changed files with 76 additions and 36 deletions

View File

@ -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": {

View File

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

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