mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-04 18:50:46 +00:00
add logout to its own file, expose sendInitialPosition and spawn, implement respawn, fix #16
This commit is contained in:
parent
ec2ba6696d
commit
caedd14632
4 changed files with 66 additions and 30 deletions
11
doc/api.md
11
doc/api.md
|
|
@ -58,6 +58,8 @@
|
||||||
- [player.others()](#playerothers)
|
- [player.others()](#playerothers)
|
||||||
- [player.chat(message)](#playerchatmessage)
|
- [player.chat(message)](#playerchatmessage)
|
||||||
- [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype)
|
- [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype)
|
||||||
|
- [player.sendInitialPosition()](#playersendinitialposition)
|
||||||
|
- [player.spawn()](#playerspawn)
|
||||||
- [Low level properties](#low-level-properties)
|
- [Low level properties](#low-level-properties)
|
||||||
- [player._client](#player_client)
|
- [player._client](#player_client)
|
||||||
- [Low level methods](#low-level-methods)
|
- [Low level methods](#low-level-methods)
|
||||||
|
|
@ -268,6 +270,15 @@ sends `message` to the player
|
||||||
|
|
||||||
change the block at position `position` to `blockType`
|
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
|
### Low level properties
|
||||||
|
|
||||||
#### player._client
|
#### player._client
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,6 @@ function toFixedPosition(p)
|
||||||
|
|
||||||
function inject(serv,player)
|
function inject(serv,player)
|
||||||
{
|
{
|
||||||
player.login=login;
|
|
||||||
|
|
||||||
function addPlayer()
|
function addPlayer()
|
||||||
{
|
{
|
||||||
serv.entityMaxId++;
|
serv.entityMaxId++;
|
||||||
|
|
@ -54,7 +52,7 @@ function inject(serv,player)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendSpawn()
|
function sendSpawnPosition()
|
||||||
{
|
{
|
||||||
console.log("setting spawn at "+player.spawnPoint);
|
console.log("setting spawn at "+player.spawnPoint);
|
||||||
player._client.write('spawn_position',{
|
player._client.write('spawn_position',{
|
||||||
|
|
@ -122,7 +120,8 @@ function inject(serv,player)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function spawn()
|
|
||||||
|
function spawnOthers()
|
||||||
{
|
{
|
||||||
player.getOthers().forEach(function (otherPlayer) {
|
player.getOthers().forEach(function (otherPlayer) {
|
||||||
var spawnPoint=toFixedPosition(otherPlayer.spawnPoint);
|
var spawnPoint=toFixedPosition(otherPlayer.spawnPoint);
|
||||||
|
|
@ -140,6 +139,10 @@ function inject(serv,player)
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function spawn()
|
||||||
|
{
|
||||||
var spawnPoint=toFixedPosition(player.spawnPoint);
|
var spawnPoint=toFixedPosition(player.spawnPoint);
|
||||||
player._writeOthers('named_entity_spawn',{
|
player._writeOthers('named_entity_spawn',{
|
||||||
entityId: player.entity.id,
|
entityId: player.entity.id,
|
||||||
|
|
@ -154,6 +157,7 @@ function inject(serv,player)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function announceJoin()
|
function announceJoin()
|
||||||
{
|
{
|
||||||
serv.broadcast(player.username + ' joined the game.', "yellow");
|
serv.broadcast(player.username + ' joined the game.', "yellow");
|
||||||
|
|
@ -169,7 +173,7 @@ function inject(serv,player)
|
||||||
addPlayer();
|
addPlayer();
|
||||||
sendLogin();
|
sendLogin();
|
||||||
sendMap();
|
sendMap();
|
||||||
sendSpawn();
|
sendSpawnPosition();
|
||||||
sendInitialPosition();
|
sendInitialPosition();
|
||||||
|
|
||||||
player.emit("spawned");
|
player.emit("spawned");
|
||||||
|
|
@ -177,32 +181,14 @@ function inject(serv,player)
|
||||||
updateTime();
|
updateTime();
|
||||||
updateGameState();
|
updateGameState();
|
||||||
fillTabList();
|
fillTabList();
|
||||||
|
spawnOthers();
|
||||||
spawn();
|
spawn();
|
||||||
|
|
||||||
announceJoin();
|
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;
|
||||||
}
|
}
|
||||||
32
lib/playerPlugins/logout.js
Normal file
32
lib/playerPlugins/logout.js
Normal file
|
|
@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
@ -4,7 +4,14 @@ function inject(serv, player)
|
||||||
{
|
{
|
||||||
player._client.on("client_command", function(packet) {
|
player._client.on("client_command", function(packet) {
|
||||||
if(packet.payload == 0) {
|
if(packet.payload == 0) {
|
||||||
// respawn the player
|
player._client.write("respawn",{
|
||||||
|
dimension:0,
|
||||||
|
difficulty:0,
|
||||||
|
gamemode:player.gameMode,
|
||||||
|
levelType:'default'
|
||||||
|
});
|
||||||
|
player.sendInitialPosition();
|
||||||
|
player.spawn();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue