2018-05-13 16:50:16 -04:00
|
|
|
const readline = require('readline')
|
|
|
|
const mc = require('minecraft-protocol')
|
|
|
|
const states = mc.states
|
2015-09-20 16:00:43 -04:00
|
|
|
|
2014-01-05 07:02:24 -05:00
|
|
|
|
2017-07-13 08:03:52 -04:00
|
|
|
const rl = readline.createInterface({
|
2015-05-14 16:08:49 -04:00
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout,
|
|
|
|
terminal: false
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
function printHelp () {
|
|
|
|
console.log('usage: node client_chat.js <hostname> <port> <user> [<password>]')
|
2014-01-05 07:02:24 -05:00
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
if (process.argv.length < 5) {
|
|
|
|
console.log('Too few arguments!')
|
|
|
|
printHelp()
|
|
|
|
process.exit(1)
|
2014-01-05 07:02:24 -05:00
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
process.argv.forEach(function (val) {
|
|
|
|
if (val === '-h') {
|
|
|
|
printHelp()
|
|
|
|
process.exit(0)
|
2015-05-14 16:08:49 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
let host = process.argv[2]
|
|
|
|
let port = parseInt(process.argv[3])
|
|
|
|
const user = process.argv[4]
|
|
|
|
const passwd = process.argv[5]
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2020-07-14 15:39:47 -04:00
|
|
|
let ChatMessage;
|
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
if (host.indexOf(':') !== -1) {
|
|
|
|
port = host.substring(host.indexOf(':') + 1)
|
|
|
|
host = host.substring(0, host.indexOf(':'))
|
2014-01-05 07:02:24 -05:00
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
console.log('connecting to ' + host + ':' + port)
|
|
|
|
console.log('user: ' + user)
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2015-05-14 16:08:49 -04:00
|
|
|
host: host,
|
|
|
|
port: port,
|
|
|
|
username: user,
|
|
|
|
password: passwd
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
client.on('kick_disconnect', function (packet) {
|
2020-07-14 15:39:47 -04:00
|
|
|
console.info('Kicked for ' + packet.reason)
|
2018-05-13 16:50:16 -04:00
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
const chats = []
|
|
|
|
|
|
|
|
client.on('connect', function () {
|
2020-07-14 15:39:47 -04:00
|
|
|
ChatMessage = require('prismarine-chat')(client.version)
|
|
|
|
console.info('Successfully connected to ' + host + ':' + port)
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
client.on('disconnect', function (packet) {
|
|
|
|
console.log('disconnected: ' + packet.reason)
|
|
|
|
})
|
|
|
|
|
|
|
|
client.on('end', function () {
|
|
|
|
console.log('Connection lost')
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
|
|
|
client.on('error', function (err) {
|
|
|
|
console.log('Error occured')
|
|
|
|
console.log(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
|
|
|
|
client.on('state', function (newState) {
|
|
|
|
if (newState === states.PLAY) {
|
|
|
|
chats.forEach(function (chat) {
|
2018-09-24 16:07:34 -04:00
|
|
|
client.write('chat', { message: chat })
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2014-01-05 07:02:24 -05:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
rl.on('line', function (line) {
|
|
|
|
if (line === '') {
|
|
|
|
return
|
|
|
|
} else if (line === '/quit') {
|
|
|
|
console.info('Disconnected from ' + host + ':' + port)
|
|
|
|
client.end()
|
|
|
|
return
|
|
|
|
} else if (line === '/end') {
|
|
|
|
console.info('Forcibly ended client')
|
|
|
|
process.exit(0)
|
2015-05-14 16:08:49 -04:00
|
|
|
}
|
2018-09-24 16:07:34 -04:00
|
|
|
if (!client.write('chat', { message: line })) {
|
2018-05-13 16:50:16 -04:00
|
|
|
chats.push(line)
|
2015-05-14 16:08:49 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
client.on('chat', function (packet) {
|
2020-07-14 15:39:47 -04:00
|
|
|
if (!ChatMessage) return // Return if ChatMessage is not loaded yet.
|
2018-05-13 16:50:16 -04:00
|
|
|
const j = JSON.parse(packet.message)
|
2020-07-14 15:39:47 -04:00
|
|
|
const chat = new ChatMessage(j)
|
|
|
|
console.info(chat.toAnsi())
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|