mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
367c01567c
* Initial 1.19.1/2 signed chat impl * lint * remove node 15 nullish operators * fix undefined uuid error * handle player left * fix * add some feature flags * fix * Fix test to use new client.chat() wrapper method * refactoring * corrections, working client example * refactoring * message expiry checking * Fix UUID write serialization * Remove padding from client login to match vanilla client * Fix server verification * update packet field terminology * Add some tech docs Rename `map` field in Pending to not conflict with Array.map method * update tech doc * lint * Bump mcdata and pauth * add doc on playerChat event, .chat function * update doc * use supportFeature, update doc Co-authored-by: Romain Beaumont <romain.rom1@gmail.com>
78 lines
2 KiB
JavaScript
78 lines
2 KiB
JavaScript
const mc = require('minecraft-protocol')
|
|
const readline = require('readline')
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout,
|
|
terminal: false,
|
|
prompt: 'Enter a message> '
|
|
})
|
|
|
|
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')
|
|
process.exit(1)
|
|
}
|
|
|
|
const client = mc.createClient({
|
|
host,
|
|
port,
|
|
username,
|
|
auth: username === 'offline' ? 'offline' : 'microsoft'
|
|
})
|
|
|
|
// Boilerplate
|
|
client.on('disconnect', function (packet) {
|
|
console.log('Disconnected from server : ' + packet.reason)
|
|
})
|
|
|
|
client.on('end', function () {
|
|
console.log('Connection lost')
|
|
process.exit()
|
|
})
|
|
|
|
client.on('error', function (err) {
|
|
console.log('Error occurred')
|
|
console.log(err)
|
|
process.exit(1)
|
|
})
|
|
|
|
client.on('connect', () => {
|
|
const ChatMessage = require('prismarine-chat')(client.version)
|
|
|
|
console.log('Connected to server')
|
|
rl.prompt()
|
|
|
|
client.on('playerChat', function ({ senderName, message, formattedMessage, verified }) {
|
|
const chat = new ChatMessage(formattedMessage ? JSON.parse(formattedMessage) : message)
|
|
console.log(senderName, { true: 'Verified:', false: 'UNVERIFIED:' }[verified] || '', chat.toAnsi())
|
|
})
|
|
})
|
|
|
|
// Send the queued messages
|
|
const queuedChatMessages = []
|
|
client.on('state', function (newState) {
|
|
if (newState === mc.states.PLAY) {
|
|
queuedChatMessages.forEach(message => client.chat(message))
|
|
queuedChatMessages.length = 0
|
|
}
|
|
})
|
|
|
|
// Listen for messages written to the console, send them to game chat
|
|
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)
|
|
}
|
|
if (!client.chat) {
|
|
queuedChatMessages.push(line)
|
|
} else {
|
|
client.chat(line)
|
|
}
|
|
})
|