diff --git a/lib/entity.js b/lib/entity.js index 2d3ae8b..6b057d3 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -16,6 +16,7 @@ function Entity(id) { this.equipment = new Array(5); this.heldItem = this.equipment[0]; // shortcut to equipment[0] this.isValid = true; + this.metadata = []; } Entity.prototype.setEquipment = function(index, item) { diff --git a/lib/playerPlugins/login.js b/lib/playerPlugins/login.js index 0cad3ac..4f22040 100644 --- a/lib/playerPlugins/login.js +++ b/lib/playerPlugins/login.js @@ -62,12 +62,15 @@ function inject(serv,player) function sendInitialPosition() { + player.entity.position=player.spawnPoint; + player.entity.yaw=0; + player.entity.pitch=0; player._client.write('position', { - x: player.spawnPoint.x, - y: player.spawnPoint.y, - z: player.spawnPoint.z, - yaw: 0, - pitch: 0, + x: player.entity.position.x, + y: player.entity.position.y, + z: player.entity.position.z, + yaw: player.entity.yaw, + pitch: player.entity.pitch, flags: 0x00 }); } @@ -125,18 +128,16 @@ function inject(serv,player) function spawnOthers() { 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 : spawnPoint.x, - y: pos ? pos.y : spawnPoint.y, - z: pos ? pos.z : spawnPoint.z, - yaw: 0, - pitch: 0, + x: otherPlayer.entity.position.x, + y: otherPlayer.entity.position.y, + z: otherPlayer.entity.position.z, + yaw: otherPlayer.entity.yaw, + pitch: otherPlayer.entity.pitch, currentItem: 0, - metadata: [] + metadata: otherPlayer.entity.metadata }); }); @@ -148,13 +149,13 @@ function inject(serv,player) player._writeOthers('named_entity_spawn',{ entityId: player.entity.id, playerUUID: transformUuid(player._client.uuid), - x: spawnPoint.x, - y: spawnPoint.y, - z: spawnPoint.z, - yaw: 0, - pitch: 0, + x: player.entity.position.x, + y: player.entity.position.y, + z: player.entity.position.z, + yaw: player.entity.yaw, + pitch: player.entity.pitch, currentItem: 0, - metadata: [] + metadata: player.entity.metadata }); } diff --git a/lib/playerPlugins/updatePositions.js b/lib/playerPlugins/updatePositions.js index 63ba0f6..66bac76 100644 --- a/lib/playerPlugins/updatePositions.js +++ b/lib/playerPlugins/updatePositions.js @@ -9,6 +9,37 @@ function toFixedPosition(p) function inject(serv,player) { + player._client.on('look', function(packet) { + sendLook(packet.yaw,packet.pitch,packet.onGround) + }); + + // float (degrees) --> byte (1/256 "degrees") + function conv(f){ + var b = (f % 360) * 256 / 360; + if (b < -128) b += 256; + else if (b > 127) b -= 256; + return Math.floor(b); + } + function sendLook(yaw,pitch,onGround) + { + var convYaw=conv(yaw); + var convPitch=conv(pitch); + if (convYaw == player.entity.yaw && convPitch == player.entity.pitch) return; + player._writeOthers("entity_look", { + entityId: player.entity.id, + yaw: convYaw, + pitch: convPitch, + onGround: onGround + }); + player.entity.yaw = convYaw; + player.entity.pitch = convPitch; + player.entity.onGround = onGround; + player._writeOthers("entity_head_rotation", { + entityId: player.entity.id, + headYaw: convYaw + }); + } + player._client.on('position', function (packet) { var position = new vec3(packet.x, packet.y, packet.z); var onGround = packet.onGround; @@ -19,6 +50,7 @@ function inject(serv,player) var position = new vec3(packet.x, packet.y, packet.z); var onGround = packet.onGround; sendRelativePositionChange(toFixedPosition(position), onGround); + sendLook(packet.yaw,packet.pitch,packet.onGround); }); function sendRelativePositionChange(newPosition, onGround) { @@ -31,8 +63,8 @@ function inject(serv,player) x: newPosition.x, y: newPosition.y, z: newPosition.z, - yaw: 0, - pitch: 0, + yaw: player.entity.yaw, + pitch: player.entity.pitch, onGround: onGround }); }