2013-01-03 22:01:17 -05:00
|
|
|
var mc = require('../');
|
|
|
|
|
|
|
|
var yellow = '§e';
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
motd: 'Vox Industries',
|
2013-01-04 01:45:57 -05:00
|
|
|
'max-players': 127,
|
|
|
|
port: 25565,
|
2013-02-10 17:51:47 -05:00
|
|
|
'online-mode': false,
|
2013-01-03 22:01:17 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var server = mc.createServer(options);
|
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
server.on('login', function(client) {
|
2013-01-04 22:47:54 -05:00
|
|
|
broadcast(yellow + client.username+' joined the game.');
|
2013-01-04 01:45:57 -05:00
|
|
|
var addr = client.socket.remoteAddress + ':' + client.socket.remotePort;
|
2013-01-04 22:47:54 -05:00
|
|
|
console.log(client.username+' connected', '('+addr+')');
|
2013-01-03 22:01:17 -05:00
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
client.on('end', function() {
|
2013-01-04 22:47:54 -05:00
|
|
|
broadcast(yellow + client.username+' left the game.', client);
|
2013-01-04 20:22:19 -05:00
|
|
|
console.log(client.username+' disconnected', '('+addr+')');
|
2013-01-04 01:45:57 -05:00
|
|
|
});
|
2013-01-03 22:01:17 -05:00
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
// send init data so client will start rendering world
|
|
|
|
client.write(0x01, {
|
2013-01-04 20:22:19 -05:00
|
|
|
entityId: client.id,
|
2013-01-04 01:45:57 -05:00
|
|
|
levelType: 'default',
|
|
|
|
gameMode: 1,
|
|
|
|
dimension: 0,
|
|
|
|
difficulty: 2,
|
|
|
|
maxPlayers: server.maxPlayers
|
|
|
|
});
|
|
|
|
client.write(0x0d, {
|
|
|
|
x: 0,
|
|
|
|
y: 256,
|
|
|
|
stance: 255,
|
|
|
|
z: 0,
|
|
|
|
yaw: 0,
|
|
|
|
pitch: 0,
|
|
|
|
onGround: true
|
|
|
|
});
|
2013-01-03 22:01:17 -05:00
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
client.on(0x03, function(data) {
|
2013-01-04 20:22:19 -05:00
|
|
|
var message = '<'+client.username+'>' + ' ' + data.message;
|
2013-07-09 02:11:09 -04:00
|
|
|
broadcast(message, client, client.username);
|
2013-01-04 01:45:57 -05:00
|
|
|
console.log(message);
|
|
|
|
});
|
|
|
|
});
|
2013-01-03 22:01:17 -05:00
|
|
|
|
|
|
|
server.on('error', function(error) {
|
2013-07-09 02:11:09 -04:00
|
|
|
console.log('Error:', error);
|
2013-01-03 22:01:17 -05:00
|
|
|
});
|
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
server.on('listening', function() {
|
2013-07-09 02:11:09 -04:00
|
|
|
console.log('Server listening on port', server.socketServer.address().port);
|
2013-01-03 22:01:17 -05:00
|
|
|
});
|
|
|
|
|
2013-07-09 02:11:09 -04:00
|
|
|
function broadcast(message, exclude, username) {
|
|
|
|
var client, translate, username;
|
|
|
|
translate = username ? 'chat.type.announcement' : 'chat.type.text';
|
|
|
|
username = username || 'Server';
|
2013-01-04 20:22:19 -05:00
|
|
|
for (var clientId in server.clients) {
|
|
|
|
client = server.clients[clientId];
|
2013-07-09 02:11:09 -04:00
|
|
|
if (client !== exclude) {
|
|
|
|
var msg = {
|
|
|
|
translate: translate,
|
|
|
|
using: [
|
|
|
|
username,
|
|
|
|
'Hello, world!'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
client.write(0x03, { message: JSON.stringify(msg) });
|
|
|
|
}
|
2013-01-04 20:22:19 -05:00
|
|
|
}
|
2013-01-03 22:01:17 -05:00
|
|
|
}
|