2018-05-13 16:50:16 -04:00
|
|
|
const mc = require('minecraft-protocol')
|
2015-09-20 16:19:39 -04:00
|
|
|
|
2020-12-06 12:53:04 -05:00
|
|
|
if (process.argv.length < 4 || process.argv.length > 6) {
|
2020-12-06 12:48:52 -05:00
|
|
|
console.log('Usage : node echo.js <host> <port> [<name>]')
|
2018-05-13 16:50:16 -04:00
|
|
|
process.exit(1)
|
2015-09-20 16:19:39 -04:00
|
|
|
}
|
2013-12-30 10:05:22 -05:00
|
|
|
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2015-09-20 16:19:39 -04:00
|
|
|
host: process.argv[2],
|
|
|
|
port: parseInt(process.argv[3]),
|
2020-12-06 12:48:52 -05:00
|
|
|
username: process.argv[4] ? process.argv[4] : 'echo'
|
|
|
|
})
|
|
|
|
client.on('error', function (err) {
|
|
|
|
console.error(err)
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2015-09-20 16:19:39 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
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]
|
2018-08-27 06:44:20 -04:00
|
|
|
if (username === client.username) return
|
2018-09-25 17:09:37 -04:00
|
|
|
if (msg.text) client.write('chat', { message: msg.text })
|
|
|
|
else client.write('chat', { message: msg })
|
2013-07-09 02:11:09 -04:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|