diff --git a/doc/api.md b/doc/api.md index 583ff6f..94c8898 100644 --- a/doc/api.md +++ b/doc/api.md @@ -52,6 +52,7 @@ - [player.login()](#playerlogin) - [player.others()](#playerothers) - [player.chat(message)](#playerchatmessage) + - [player.setSpawnPoint()](#playersetspawnpoint) - [Low level properties](#low-level-properties) - [player._client](#player_client) - [Low level methods](#low-level-methods) @@ -242,6 +243,10 @@ return the other players than `player` sends `message` to the player +#### player.setSpawnPoint() + +set the spawn point of a player + ### Low level properties #### player._client diff --git a/lib/playerPlugins/login.js b/lib/playerPlugins/login.js index 8158f07..8e45ff5 100644 --- a/lib/playerPlugins/login.js +++ b/lib/playerPlugins/login.js @@ -1,4 +1,5 @@ var Entity=require("../entity"); +var vec3 = require("vec3"); module.exports=inject; @@ -7,6 +8,10 @@ function transformUuid(s) 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) { @@ -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() { player._client.write('position', { - x: 6, - y: 53, - z: 6, + x: player.spawnPoint.x, + y: player.spawnPoint.y, + z: player.spawnPoint.z, yaw: 0, pitch: 0, flags: 0x00 @@ -108,13 +121,14 @@ function inject(serv,player) function spawn() { player.getOthers().forEach(function (otherPlayer) { + var spawnPoint=toFixedPosition(otherPlayer.spawnPoint); var pos = otherPlayer.entity.position; player._client.write('named_entity_spawn', { entityId: otherPlayer.entity.id, playerUUID: transformUuid(otherPlayer._client.uuid), - x: pos ? pos.x : 6 * 32, - y: pos ? pos.y : 53 * 32, - z: pos ? pos.z : 6 * 32, + x: pos ? pos.x : spawnPoint.x, + y: pos ? pos.y : spawnPoint.y, + z: pos ? pos.z : spawnPoint.z, yaw: 0, pitch: 0, currentItem: 0, @@ -122,12 +136,13 @@ function inject(serv,player) }); }); + var spawnPoint=toFixedPosition(player.spawnPoint); player._writeOthers('named_entity_spawn',{ entityId: player.entity.id, playerUUID: transformUuid(player._client.uuid), - x: 6 * 32, - y: 53 * 32, - z: 6 * 32, + x: spawnPoint.x, + y: spawnPoint.y, + z: spawnPoint.z, yaw: 0, pitch: 0, currentItem: 0, @@ -150,6 +165,8 @@ function inject(serv,player) addPlayer(); sendLogin(); sendMap(); + player.setSpawnPoint(); + sendSpawn(); sendInitialPosition(); player.emit("spawned"); diff --git a/lib/playerPlugins/spawn.js b/lib/playerPlugins/spawn.js new file mode 100644 index 0000000..4c03439 --- /dev/null +++ b/lib/playerPlugins/spawn.js @@ -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; +} \ No newline at end of file diff --git a/lib/playerPlugins/updatePositions.js b/lib/playerPlugins/updatePositions.js index 4991fec..58b3c6a 100644 --- a/lib/playerPlugins/updatePositions.js +++ b/lib/playerPlugins/updatePositions.js @@ -24,7 +24,19 @@ function inject(serv,player) function sendRelativePositionChange(newPosition, onGround) { if (player.entity.position.distanceTo(new vec3(0, 0, 0)) != 0) { 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', { entityId: player.entity.id, dX: diff.x,