node-minecraft-protocol/examples/server.js

77 lines
1.7 KiB
JavaScript
Raw Normal View History

2013-01-03 22:01:17 -05:00
var mc = require('../');
var yellow = '§e';
var players = [];
var options = {
2013-01-04 01:45:57 -05:00
'online-mode': false,
2013-01-03 22:01:17 -05:00
motd: 'Vox Industries',
2013-01-04 01:45:57 -05:00
'max-players': 127,
port: 25565,
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) {
var player = {
client: client,
username: client.username,
index: players.length
};
players.push(player);
server.players = players.length;
2013-01-03 22:01:17 -05:00
2013-01-04 01:45:57 -05:00
broadcast(yellow + player.username+' joined the game.');
var addr = client.socket.remoteAddress + ':' + client.socket.remotePort;
console.log(player.username+' connected', '('+addr+')');
2013-01-03 22:01:17 -05:00
2013-01-04 01:45:57 -05:00
client.on('end', function() {
players.splice(player.index, 1);
server.players = players.length;
2013-01-03 22:01:17 -05:00
2013-01-04 01:45:57 -05:00
broadcast(yellow + player.username+' left the game.', player);
console.log(player.username+' disconnected', '('+addr+')');
});
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, {
entityId: 0,
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) {
var message = '<'+player.username+'>' + ' ' + data.message;
broadcast(message);
console.log(message);
});
});
2013-01-03 22:01:17 -05:00
server.on('error', function(error) {
console.log('Error:', error);
});
2013-01-04 01:45:57 -05:00
server.on('listening', function() {
console.log('Server listening on port', server.socket.address().port);
2013-01-03 22:01:17 -05:00
});
function broadcast(message, exclude) {
for(var i = 0; i < players.length; i++) {
if(players[i].username !== exclude && i !== exclude && players[i] !== exclude) {
players[i].client.write(0x03, { message: message });
}
}
}