mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-24 08:17:52 -05:00
35 lines
1 KiB
JavaScript
35 lines
1 KiB
JavaScript
const mc = require('minecraft-protocol')
|
|
|
|
if (process.argv.length < 4 || process.argv.length > 6) {
|
|
console.log('Usage : node echo.js <host> <port> [<name>]')
|
|
process.exit(1)
|
|
}
|
|
|
|
const client = mc.createClient({
|
|
host: process.argv[2],
|
|
port: parseInt(process.argv[3]),
|
|
username: process.argv[4] ? process.argv[4] : 'echo'
|
|
})
|
|
client.on('error', function (err) {
|
|
console.error(err)
|
|
})
|
|
|
|
client.on('connect', function () {
|
|
console.info('connected')
|
|
})
|
|
client.on('disconnect', function (packet) {
|
|
console.log('disconnected: ' + packet.reason)
|
|
})
|
|
client.on('end', function () {
|
|
console.log('Connection lost')
|
|
})
|
|
client.on('chat', function (packet) {
|
|
const jsonMsg = JSON.parse(packet.message)
|
|
if (jsonMsg.translate === 'chat.type.announcement' || jsonMsg.translate === 'chat.type.text') {
|
|
const username = jsonMsg.with[0].text
|
|
const msg = jsonMsg.with[1]
|
|
if (username === client.username) return
|
|
if (msg.text) client.write('chat', { message: msg.text })
|
|
else client.write('chat', { message: msg })
|
|
}
|
|
})
|