2018-05-13 16:50:16 -04:00
|
|
|
const mc = require('minecraft-protocol')
|
2022-08-15 18:57:26 -04:00
|
|
|
const readline = require('readline')
|
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,
|
2023-01-14 14:33:04 -05:00
|
|
|
terminal: false,
|
|
|
|
prompt: 'Enter a message> '
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2022-08-15 18:57:26 -04:00
|
|
|
const [,, host, port, username] = process.argv
|
|
|
|
if (!host || !port) {
|
|
|
|
console.error('Usage: node client_chat.js <host> <port> <username>')
|
|
|
|
console.error('Usage (offline mode): node client_chat.js <host> <port> offline')
|
2018-05-13 16:50:16 -04:00
|
|
|
process.exit(1)
|
2014-01-05 07:02:24 -05:00
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2022-08-15 18:57:26 -04:00
|
|
|
host,
|
|
|
|
port,
|
|
|
|
username,
|
|
|
|
auth: username === 'offline' ? 'offline' : 'microsoft'
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
|
2022-08-15 18:57:26 -04:00
|
|
|
// Boilerplate
|
2018-05-13 16:50:16 -04:00
|
|
|
client.on('disconnect', function (packet) {
|
2022-08-15 18:57:26 -04:00
|
|
|
console.log('Disconnected from server : ' + packet.reason)
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
client.on('end', function () {
|
|
|
|
console.log('Connection lost')
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
|
|
|
|
client.on('error', function (err) {
|
2022-08-15 18:57:26 -04:00
|
|
|
console.log('Error occurred')
|
2018-05-13 16:50:16 -04:00
|
|
|
console.log(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|
|
|
|
|
2022-08-15 18:57:26 -04:00
|
|
|
client.on('connect', () => {
|
|
|
|
const ChatMessage = require('prismarine-chat')(client.version)
|
|
|
|
|
|
|
|
console.log('Connected to server')
|
2023-01-14 14:33:04 -05:00
|
|
|
rl.prompt()
|
2022-08-15 18:57:26 -04:00
|
|
|
|
2023-01-16 14:35:26 -05:00
|
|
|
client.on('playerChat', function ({ senderName, plainMessage, unsignedContent, formattedMessage, verified }) {
|
|
|
|
let content
|
|
|
|
|
|
|
|
const allowInsecureChat = true
|
|
|
|
|
|
|
|
if (formattedMessage) content = JSON.parse(formattedMessage)
|
|
|
|
else if (allowInsecureChat && unsignedContent) content = JSON.parse(unsignedContent)
|
|
|
|
else content = { text: plainMessage }
|
|
|
|
|
|
|
|
const chat = new ChatMessage(content)
|
|
|
|
console.log(senderName, { trugie: 'Verified:', false: 'UNVERIFIED:' }[verified] || '', chat.toAnsi())
|
2022-08-15 18:57:26 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// Send the queued messages
|
|
|
|
const queuedChatMessages = []
|
2018-05-13 16:50:16 -04:00
|
|
|
client.on('state', function (newState) {
|
2022-08-15 18:57:26 -04:00
|
|
|
if (newState === mc.states.PLAY) {
|
|
|
|
queuedChatMessages.forEach(message => client.chat(message))
|
|
|
|
queuedChatMessages.length = 0
|
2014-01-05 07:02:24 -05:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
|
|
|
|
2022-08-15 18:57:26 -04:00
|
|
|
// Listen for messages written to the console, send them to game chat
|
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
|
|
|
}
|
2022-08-15 18:57:26 -04:00
|
|
|
if (!client.chat) {
|
|
|
|
queuedChatMessages.push(line)
|
|
|
|
} else {
|
|
|
|
client.chat(line)
|
2015-05-14 16:08:49 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|