2018-05-13 16:50:16 -04:00
|
|
|
'use strict'
|
2016-04-27 19:27:32 -04:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
const mc = require('minecraft-protocol')
|
2016-01-24 21:53:34 -05:00
|
|
|
|
2018-05-13 16:50:16 -04:00
|
|
|
if (process.argv.length < 4 || process.argv.length > 6) {
|
|
|
|
console.log('Usage : node echo.js <host> <port> [<name>] [<password>]')
|
|
|
|
process.exit(1)
|
2016-01-24 21:53:34 -05:00
|
|
|
}
|
|
|
|
|
2017-07-13 08:03:52 -04:00
|
|
|
const client = mc.createClient({
|
2017-07-12 07:07:25 -04:00
|
|
|
version: false,
|
2016-01-24 21:53:34 -05:00
|
|
|
host: process.argv[2],
|
|
|
|
port: parseInt(process.argv[3]),
|
2018-05-13 16:50:16 -04:00
|
|
|
username: process.argv[4] ? process.argv[4] : 'echo',
|
2016-01-24 21:53:34 -05:00
|
|
|
password: process.argv[5]
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|
2016-01-24 21:53:34 -05: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('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
|
2018-09-24 16:07:34 -04:00
|
|
|
client.write('chat', { message: msg })
|
2016-01-31 01:14:13 -05:00
|
|
|
}
|
2018-05-13 16:50:16 -04:00
|
|
|
})
|