From caedd146322428d69d6b2b3a57cd2f82a0f7f811 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Thu, 27 Aug 2015 23:26:12 +0200 Subject: [PATCH] add logout to its own file, expose sendInitialPosition and spawn, implement respawn, fix #16 --- doc/api.md | 11 +++++++++ lib/playerPlugins/login.js | 44 ++++++++++++------------------------ lib/playerPlugins/logout.js | 32 ++++++++++++++++++++++++++ lib/playerPlugins/respawn.js | 9 +++++++- 4 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 lib/playerPlugins/logout.js diff --git a/doc/api.md b/doc/api.md index dab09df..d99b011 100644 --- a/doc/api.md +++ b/doc/api.md @@ -58,6 +58,8 @@ - [player.others()](#playerothers) - [player.chat(message)](#playerchatmessage) - [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype) + - [player.sendInitialPosition()](#playersendinitialposition) + - [player.spawn()](#playerspawn) - [Low level properties](#low-level-properties) - [player._client](#player_client) - [Low level methods](#low-level-methods) @@ -268,6 +270,15 @@ sends `message` to the player change the block at position `position` to `blockType` +#### player.sendInitialPosition() + +send its initial position to the player + +#### player.spawn() + +tell everybody else that the player spawned + + ### Low level properties #### player._client diff --git a/lib/playerPlugins/login.js b/lib/playerPlugins/login.js index 8769c2e..434f40c 100644 --- a/lib/playerPlugins/login.js +++ b/lib/playerPlugins/login.js @@ -15,8 +15,6 @@ function toFixedPosition(p) function inject(serv,player) { - player.login=login; - function addPlayer() { serv.entityMaxId++; @@ -54,7 +52,7 @@ function inject(serv,player) }); } - function sendSpawn() + function sendSpawnPosition() { console.log("setting spawn at "+player.spawnPoint); player._client.write('spawn_position',{ @@ -122,7 +120,8 @@ function inject(serv,player) }); } - function spawn() + + function spawnOthers() { player.getOthers().forEach(function (otherPlayer) { var spawnPoint=toFixedPosition(otherPlayer.spawnPoint); @@ -140,6 +139,10 @@ function inject(serv,player) }); }); + } + + function spawn() + { var spawnPoint=toFixedPosition(player.spawnPoint); player._writeOthers('named_entity_spawn',{ entityId: player.entity.id, @@ -154,6 +157,7 @@ function inject(serv,player) }); } + function announceJoin() { serv.broadcast(player.username + ' joined the game.', "yellow"); @@ -169,7 +173,7 @@ function inject(serv,player) addPlayer(); sendLogin(); sendMap(); - sendSpawn(); + sendSpawnPosition(); sendInitialPosition(); player.emit("spawned"); @@ -177,32 +181,14 @@ function inject(serv,player) updateTime(); updateGameState(); fillTabList(); + spawnOthers(); spawn(); announceJoin(); - - - player._client.on('end', function () { - serv.broadcast(player.username + ' quit the game.', "yellow"); - player._writeOthers('player_info', { - action: 4, - data: [{ - UUID: transformUuid(player._client.uuid) - }] - }); - player._writeOthers('entity_destroy', {'entityIds': [player.entity.id]}); - delete serv.entities[player.entity.id] - player.emit('disconnect'); - var index = serv.players.indexOf(player); - if (index > -1) { - serv.players.splice(index,1); - } - delete serv.uuidToPlayer[player._client.uuid]; - }); - - - player._client.on('error', function (error) { - player.emit('error',error); - }); } + + + player.login=login; + player.sendInitialPosition=sendInitialPosition; + player.spawn=spawn; } \ No newline at end of file diff --git a/lib/playerPlugins/logout.js b/lib/playerPlugins/logout.js new file mode 100644 index 0000000..1dad9db --- /dev/null +++ b/lib/playerPlugins/logout.js @@ -0,0 +1,32 @@ +module.exports=inject; + +function transformUuid(s) +{ + return s.split("-").map(function(item) { return parseInt(item, 16); }); +} + +function inject(serv,player) +{ + player._client.on('end', function () { + serv.broadcast(player.username + ' quit the game.', "yellow"); + player._writeOthers('player_info', { + action: 4, + data: [{ + UUID: transformUuid(player._client.uuid) + }] + }); + player._writeOthers('entity_destroy', {'entityIds': [player.entity.id]}); + delete serv.entities[player.entity.id] + player.emit('disconnect'); + var index = serv.players.indexOf(player); + if (index > -1) { + serv.players.splice(index,1); + } + delete serv.uuidToPlayer[player._client.uuid]; + }); + + + player._client.on('error', function (error) { + player.emit('error',error); + }); +} \ No newline at end of file diff --git a/lib/playerPlugins/respawn.js b/lib/playerPlugins/respawn.js index aaf524a..2b80f7e 100644 --- a/lib/playerPlugins/respawn.js +++ b/lib/playerPlugins/respawn.js @@ -4,7 +4,14 @@ function inject(serv, player) { player._client.on("client_command", function(packet) { if(packet.payload == 0) { - // respawn the player + player._client.write("respawn",{ + dimension:0, + difficulty:0, + gamemode:player.gameMode, + levelType:'default' + }); + player.sendInitialPosition(); + player.spawn(); } }); } \ No newline at end of file