feat: add auto respawn support
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
e8d41e8a43
commit
f660b4a4f8
@ -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": {
|
||||||
|
@ -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 {
|
||||||
|
27
packages/client/src/event.ts
Normal file
27
packages/client/src/event.ts
Normal 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) {
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
@ -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'
|
||||||
version: '1.12.2',
|
let client = createConnection('192.168.2.5', 25577, username)
|
||||||
host: '192.168.2.5',
|
|
||||||
port: 25577,
|
|
||||||
username: process.argv[2] || 'Mr_jtb',
|
|
||||||
skipValidation: true
|
|
||||||
})
|
|
||||||
|
|
||||||
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) => {
|
attachForge(client)
|
||||||
// Listen for chat messages and echo them back.
|
attachEvents(client)
|
||||||
var jsonMsg = JSON.parse(packet.message);
|
return client;
|
||||||
console.log($(jsonMsg))
|
}
|
||||||
})
|
|
||||||
|
|
||||||
client.on('state', (newState, oldState) => {
|
client.on('end', (resone) => {
|
||||||
console.log('Client Change State', oldState, 'to', newState)
|
console.log("Client End Resone:", resone)
|
||||||
let targetServer = process.argv[3]
|
client = createConnection('192.168.2.5', 25577, username)
|
||||||
if (newState == "play" && targetServer) {
|
|
||||||
setTimeout(() => {
|
|
||||||
client.write('chat', {
|
|
||||||
message: '/server ' + targetServer
|
|
||||||
})
|
|
||||||
}, 3000)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
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;
|
||||||
console.info('Disconnected')
|
case "/respawn":
|
||||||
client.end("")
|
client.write('client_command', { payload: 0 })
|
||||||
return
|
// client.write("respawn", {
|
||||||
} else if (line === '/end') {
|
|
||||||
console.info('Forcibly ended client')
|
// })
|
||||||
process.exit(0)
|
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()
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user