From a00082b57a91d5de9c33e2a0b70b2fe76147b58f Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Wed, 26 Aug 2015 21:48:56 +0200 Subject: [PATCH] add username to the player object --- doc/api.md | 5 +++++ lib/playerPlugins/chat.js | 2 +- lib/playerPlugins/log.js | 10 +++++----- lib/playerPlugins/login.js | 13 +++++++------ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/doc/api.md b/doc/api.md index 18f1946..583ff6f 100644 --- a/doc/api.md +++ b/doc/api.md @@ -41,6 +41,7 @@ - [Player](#player) - [Properties](#properties-1) - [player.entity](#playerentity) + - [player.username](#playerusername) - [Events](#events-1) - ["connected"](#connected) - ["spawned"](#spawned) @@ -201,6 +202,10 @@ broadcasts `message` to all the players with the optional `color`. The entity of the player, of type `CraftyJS.Entity` +#### player.username + +The username of the player + ### Events #### "connected" diff --git a/lib/playerPlugins/chat.js b/lib/playerPlugins/chat.js index 9d98de7..e04ad27 100644 --- a/lib/playerPlugins/chat.js +++ b/lib/playerPlugins/chat.js @@ -3,7 +3,7 @@ module.exports=inject; function inject(serv,player) { player._client.on('chat', function (data) { - serv.broadcast('<' + player._client.username + '>' + ' ' + data.message); + serv.broadcast('<' + player.username + '>' + ' ' + data.message); player.emit("chat",data.message); }); diff --git a/lib/playerPlugins/log.js b/lib/playerPlugins/log.js index 05b24e1..65d8aba 100644 --- a/lib/playerPlugins/log.js +++ b/lib/playerPlugins/log.js @@ -5,8 +5,8 @@ function inject(serv,player) player.on("connected",function(){ var addr = player._client.socket.remoteAddress + ':' + player._client.socket.remotePort; - console.log("[INFO]: " + player._client.username + ' connected', '(' + addr + ')'); - serv.log("[INFO]: " + player._client.username + ' connected', '(' + addr + ')'); + console.log("[INFO]: " + player.username + ' connected', '(' + addr + ')'); + serv.log("[INFO]: " + player.username + ' connected', '(' + addr + ')'); }); player.on("spawned",function(){ @@ -15,8 +15,8 @@ function inject(serv,player) }); player.on("disconnected",function(){ - console.log("[INFO]: " + player._client.username + ' disconnected', '(' + addr + ')'); - serv.log("[INFO]: " + player._client.username + ' disconnected', '(' + addr + ')'); + console.log("[INFO]: " + player.username + ' disconnected', '(' + addr + ')'); + serv.log("[INFO]: " + player.username + ' disconnected', '(' + addr + ')'); }); player.on("error",function(error){ @@ -25,7 +25,7 @@ function inject(serv,player) }); player.on("chat",function(message){ - message = '<' + player._client.username + '>' + ' ' + message; + message = '<' + player.username + '>' + ' ' + message; console.log("[INFO] " + message); serv.log("[INFO] " + message); }); diff --git a/lib/playerPlugins/login.js b/lib/playerPlugins/login.js index 950e9c7..8158f07 100644 --- a/lib/playerPlugins/login.js +++ b/lib/playerPlugins/login.js @@ -16,6 +16,7 @@ function inject(serv,player) { serv.entityMaxId++; player.entity=new Entity(serv.entityMaxId); + player.username=player._client.username; serv.players.push(player); serv.uuidToPlayer[player._client.uuid] = player; } @@ -78,12 +79,12 @@ function inject(serv,player) action: 0, data: [{ UUID: transformUuid(player._client.uuid), - name: player._client.username, + name: player.username, properties: [], gamemode: 0, ping: 1, hasDisplayName: true, - displayName: player._client.username + displayName: player.username }] }); @@ -93,12 +94,12 @@ function inject(serv,player) .map(function (otherPlayer) { return { UUID: transformUuid(otherPlayer._client.uuid), - name: otherPlayer._client.username, + name: otherPlayer.username, properties: [], gamemode: 0, ping: 1, hasDisplayName: true, - displayName: otherPlayer._client.username + displayName: otherPlayer.username }; }) }); @@ -136,7 +137,7 @@ function inject(serv,player) function announceJoin() { - serv.broadcast(player._client.username + ' joined the game.', "yellow"); + serv.broadcast(player.username + ' joined the game.', "yellow"); player.emit("connected"); } @@ -162,7 +163,7 @@ function inject(serv,player) player._client.on('end', function () { - serv.broadcast(player._client.username + ' quit the game.', "yellow"); + serv.broadcast(player.username + ' quit the game.', "yellow"); player.emit('disconnect'); });