set the spawn point in spawn player plugin, send teleport packet if the position diff is too important, fix #5

This commit is contained in:
Romain Beaumont 2015-08-26 23:08:12 +02:00
parent 34ca0aa788
commit 49e313d72b
4 changed files with 57 additions and 10 deletions

View file

@ -52,6 +52,7 @@
- [player.login()](#playerlogin) - [player.login()](#playerlogin)
- [player.others()](#playerothers) - [player.others()](#playerothers)
- [player.chat(message)](#playerchatmessage) - [player.chat(message)](#playerchatmessage)
- [player.setSpawnPoint()](#playersetspawnpoint)
- [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)
@ -242,6 +243,10 @@ return the other players than `player`
sends `message` to the player sends `message` to the player
#### player.setSpawnPoint()
set the spawn point of a player
### Low level properties ### Low level properties
#### player._client #### player._client

View file

@ -1,4 +1,5 @@
var Entity=require("../entity"); var Entity=require("../entity");
var vec3 = require("vec3");
module.exports=inject; module.exports=inject;
@ -7,6 +8,10 @@ function transformUuid(s)
return s.split("-").map(function(item) { return parseInt(item, 16); }); return s.split("-").map(function(item) { return parseInt(item, 16); });
} }
function toFixedPosition(p)
{
return new vec3(Math.floor(p.x*32),Math.floor(p.y*32),Math.floor(p.z*32))
}
function inject(serv,player) function inject(serv,player)
{ {
@ -45,12 +50,20 @@ function inject(serv,player)
}); });
} }
function sendSpawn()
{
console.log("setting spawn at "+player.spawnPoint);
player._client.write('spawn_position',{
"location":player.spawnPoint
});
}
function sendInitialPosition() function sendInitialPosition()
{ {
player._client.write('position', { player._client.write('position', {
x: 6, x: player.spawnPoint.x,
y: 53, y: player.spawnPoint.y,
z: 6, z: player.spawnPoint.z,
yaw: 0, yaw: 0,
pitch: 0, pitch: 0,
flags: 0x00 flags: 0x00
@ -108,13 +121,14 @@ function inject(serv,player)
function spawn() function spawn()
{ {
player.getOthers().forEach(function (otherPlayer) { player.getOthers().forEach(function (otherPlayer) {
var spawnPoint=toFixedPosition(otherPlayer.spawnPoint);
var pos = otherPlayer.entity.position; var pos = otherPlayer.entity.position;
player._client.write('named_entity_spawn', { player._client.write('named_entity_spawn', {
entityId: otherPlayer.entity.id, entityId: otherPlayer.entity.id,
playerUUID: transformUuid(otherPlayer._client.uuid), playerUUID: transformUuid(otherPlayer._client.uuid),
x: pos ? pos.x : 6 * 32, x: pos ? pos.x : spawnPoint.x,
y: pos ? pos.y : 53 * 32, y: pos ? pos.y : spawnPoint.y,
z: pos ? pos.z : 6 * 32, z: pos ? pos.z : spawnPoint.z,
yaw: 0, yaw: 0,
pitch: 0, pitch: 0,
currentItem: 0, currentItem: 0,
@ -122,12 +136,13 @@ function inject(serv,player)
}); });
}); });
var spawnPoint=toFixedPosition(player.spawnPoint);
player._writeOthers('named_entity_spawn',{ player._writeOthers('named_entity_spawn',{
entityId: player.entity.id, entityId: player.entity.id,
playerUUID: transformUuid(player._client.uuid), playerUUID: transformUuid(player._client.uuid),
x: 6 * 32, x: spawnPoint.x,
y: 53 * 32, y: spawnPoint.y,
z: 6 * 32, z: spawnPoint.z,
yaw: 0, yaw: 0,
pitch: 0, pitch: 0,
currentItem: 0, currentItem: 0,
@ -150,6 +165,8 @@ function inject(serv,player)
addPlayer(); addPlayer();
sendLogin(); sendLogin();
sendMap(); sendMap();
player.setSpawnPoint();
sendSpawn();
sendInitialPosition(); sendInitialPosition();
player.emit("spawned"); player.emit("spawned");

View file

@ -0,0 +1,13 @@
var vec3 = require("vec3");
module.exports=inject;
function inject(serv,player)
{
function setSpawnPoint()
{
player.spawnPoint=new vec3(6,51,6);
}
player.setSpawnPoint=setSpawnPoint;
}

View file

@ -24,7 +24,19 @@ function inject(serv,player)
function sendRelativePositionChange(newPosition, onGround) { function sendRelativePositionChange(newPosition, onGround) {
if (player.entity.position.distanceTo(new vec3(0, 0, 0)) != 0) { if (player.entity.position.distanceTo(new vec3(0, 0, 0)) != 0) {
var diff = newPosition.minus(player.entity.position); var diff = newPosition.minus(player.entity.position);
if (diff.distanceTo(new vec3(0, 0, 0)) != 0) { if(diff.abs().x>256 || diff.abs().y>256 || diff.abs().z>256)
{
player._client.write('entity_teleport', {
entityId:player.entity.id,
x: newPosition.x,
y: newPosition.y,
z: newPosition.z,
yaw: 0,
pitch: 0,
onGround: onGround
});
}
else if (diff.distanceTo(new vec3(0, 0, 0)) != 0) {
player._writeOthers('rel_entity_move', { player._writeOthers('rel_entity_move', {
entityId: player.entity.id, entityId: player.entity.id,
dX: diff.x, dX: diff.x,