node-minecraft-protocol/examples/server_helloworld/server_helloworld.js
2019-12-29 23:27:04 +01:00

59 lines
1.2 KiB
JavaScript

const mc = require('minecraft-protocol')
const options = {
'online-mode': true
}
const server = mc.createServer(options)
server.on('login', function (client) {
const addr = client.socket.remoteAddress
console.log('Incoming connection', '(' + addr + ')')
client.on('end', function () {
console.log('Connection closed', '(' + addr + ')')
})
client.on('error', function (error) {
console.log('Error:', error)
})
// send init data so client will start rendering world
client.write('login', {
entityId: client.id,
levelType: 'default',
gameMode: 0,
dimension: 0,
difficulty: 2,
maxPlayers: server.maxPlayers,
reducedDebugInfo: false,
enableRespawnScreen: true,
hashedSeed: [0, 0]
})
client.write('position', {
x: 0,
y: 1.62,
z: 0,
yaw: 0,
pitch: 0,
flags: 0x00
})
const msg = {
translate: 'chat.type.announcement',
with: [
'Server',
'Hello, world!'
]
}
client.write('chat', { message: JSON.stringify(msg), position: 0 })
})
server.on('error', function (error) {
console.log('Error:', error)
})
server.on('listening', function () {
console.log('Server listening on port', server.socketServer.address().port)
})