From 0adfc20a1f555f7db20e13d269162fe51b1c8ccb Mon Sep 17 00:00:00 2001 From: DemiPixel Date: Fri, 11 Dec 2015 01:03:21 -0800 Subject: [PATCH] Continuing attempt to implement abilities --- doc/API.md | 2 +- src/lib/plugins/chest.js | 9 ++++---- src/lib/plugins/commands.js | 2 -- src/lib/plugins/communication.js | 3 +++ src/lib/plugins/login.js | 3 ++- src/lib/plugins/physics.js | 35 +++++++++++++++++++++++++++++- src/lib/plugins/updatePositions.js | 26 ++++++---------------- 7 files changed, 52 insertions(+), 28 deletions(-) diff --git a/doc/API.md b/doc/API.md index ea64af8..9bdea62 100644 --- a/doc/API.md +++ b/doc/API.md @@ -840,7 +840,7 @@ Emitted when a player places a block - position: Position they're attempting to place the block - id: Id of block being placed - damage: Data of block being placed -- reference (u): Reference block that was placed on +- reference (u): Reference block (position) that was placed on - direction (u): Direction vector from reference to position - playSound: Which sound to play (Default: true) - sound: Sound to play (Default: default sound for that material) diff --git a/src/lib/plugins/chest.js b/src/lib/plugins/chest.js index 18a91b7..c3072a8 100644 --- a/src/lib/plugins/chest.js +++ b/src/lib/plugins/chest.js @@ -2,13 +2,13 @@ var Vec3 = require("vec3").Vec3; module.exports.player=function(player) { - player._client.on('block_place', async ({location} = {}) => { - var referencePosition=new Vec3(location.x,location.y,location.z); + player.on('placeBlock_cancel', async (opt, cancel) => { if (player.crouching) return; try { - var id = await player.world.getBlockType(referencePosition); - var blockAbove = await player.world.getBlockType(referencePosition.clone().add(new Vec3(0, 1, 0))); + var id = await player.world.getBlockType(opt.reference); + var blockAbove = await player.world.getBlockType(opt.reference.plus(new Vec3(0, 1, 0))); if (id == 54) { + opt.playSound = false; if (blockAbove) { return; } @@ -18,6 +18,7 @@ module.exports.player=function(player) windowTitle: JSON.stringify("Chest"), slotCount: 9 * 3 + 8 // 3 rows, make nicer later }); + cancel(); } } catch(err) { diff --git a/src/lib/plugins/commands.js b/src/lib/plugins/commands.js index 9fb3258..41d44ce 100644 --- a/src/lib/plugins/commands.js +++ b/src/lib/plugins/commands.js @@ -121,8 +121,6 @@ module.exports.player=function(player, serv) { else setTimeout(() => {throw err;}, 0); } } - - player.selectorString = (str) => serv.selectorString(str, player.position.scaled(1/32), player.world); }; module.exports.entity = function(entity, serv) { diff --git a/src/lib/plugins/communication.js b/src/lib/plugins/communication.js index 7df5fc0..f5871a4 100644 --- a/src/lib/plugins/communication.js +++ b/src/lib/plugins/communication.js @@ -48,4 +48,7 @@ module.exports.entity=function(entity,serv) entity._writeOthersNearby = (packetName, packetFields) => serv._writeArray(packetName, packetFields, entity.getNearbyPlayers()); + + entity._writeNearby = (packetName, packetFields) => + serv._writeArray(packetName, packetFields, entity.getNearbyPlayers().concat(entity.type == 'player' ? [entity] : [])); }; \ No newline at end of file diff --git a/src/lib/plugins/login.js b/src/lib/plugins/login.js index 880b58c..9898f4d 100644 --- a/src/lib/plugins/login.js +++ b/src/lib/plugins/login.js @@ -94,6 +94,7 @@ module.exports.player=function(player,serv) gamemode: player.gameMode }] }); + player.sendAbilities(); }; function fillTabList() @@ -161,6 +162,7 @@ module.exports.player=function(player,serv) player.sendSpawnPosition(); player.sendSelfPosition(); player.updateHealth(player.health); + player.sendAbilities(); updateTime(); @@ -173,6 +175,5 @@ module.exports.player=function(player,serv) await player.waitPlayerLogin(); player.sendRestMap(); sendChunkWhenMove(); - player.sendAbilities(); }; }; \ No newline at end of file diff --git a/src/lib/plugins/physics.js b/src/lib/plugins/physics.js index 1a1a70d..076ed84 100644 --- a/src/lib/plugins/physics.js +++ b/src/lib/plugins/physics.js @@ -37,6 +37,22 @@ module.exports.entity=function(entity){ return { position: newPos, onGround: yBlock} }; + entity.sendVelocity = (vel, maxVel) => { + var velocity = vel.scaled(32).floored(); // Make fixed point + var maxVelocity = maxVel.scaled(32).floored(); + var scaledVelocity = velocity.scaled(8000/32/20).floored(); // from fixed-position/second to unit => 1/8000 blocks per tick + entity._writeNearby('entity_velocity', { + entityId: entity.id, + velocityX: scaledVelocity.x, + velocityY: scaledVelocity.y, + velocityZ: scaledVelocity.z + }); + if (entity.type != 'player') { + if (maxVelocity) entity.velocity = addVelocityWithMax(entity.velocity, velocity, maxVelocity); + else entity.velocity.add(velocity); + } + }; + function getMoveAmount(dir, block, entity, delta, sizeSigned) { if (block) { @@ -70,4 +86,21 @@ module.exports.entity=function(entity){ function clamp(a, b, c) { return Math.max(a, Math.min(b, c)); } -}; \ No newline at end of file +}; + +module.exports.player = function(player, serv) { + player.commands.add({ + base: 'velocity', + info: 'Push velocity on player(s)', + usage: '/velocity ', + op: true, + parse(str) { + return str.match(/(.+?) (\d+) (\d+) (\d+)/) || false; + }, + action(params) { + var selector = player.selectorString(params[1]); + var vec = new Vec3(parseInt(params[2]), parseInt(params[3]), parseInt(params[4])); + selector.forEach(e => e.sendVelocity(vec, vec.scaled(5))); + } + }) +} \ No newline at end of file diff --git a/src/lib/plugins/updatePositions.js b/src/lib/plugins/updatePositions.js index 3a63266..686f4d0 100644 --- a/src/lib/plugins/updatePositions.js +++ b/src/lib/plugins/updatePositions.js @@ -69,9 +69,13 @@ module.exports.player=function(player) } player.sendAbilities = () => { - var f = (+(player.gameMode == 1)*1) + (+(player.gameMode == 1 || player.gameMode == 3)*2) + (+(player.gameMode == 1 || player.gamemode == 3)*4); - var walkingSpeed = 1.0 + ((player.effects[1] != null ? (player.effects[1].amplifier + 1) : 0) * 0.2) - var flyingSpeed = 0.2; + var godmode = player.gameMode == 1 || player.gameMode == 3; + var canFly = player.gameMode == 1 || player.gameMode == 3; + var isFlying = !player.onGround && canFly; + var creativeMode = player.gameMode == 1; + var f = (+godmode*8) + (+canFly*4) + (+isFlying*2) + (+creativeMode*1); + var walkingSpeed = 4.3/20 * (1 + (player.effects[1] != null ? (player.effects[1].amplifier + 1) : 0) * 0.2) + var flyingSpeed = 1.0/20; console.log(walkingSpeed, flyingSpeed); player._client.write('abilities', { flags: f, @@ -116,22 +120,6 @@ module.exports.entity=function(entity,serv){ }); }; - entity.sendVelocity = (vel, maxVel) => { - var velocity = vel.scaled(32).floored(); // Make fixed point - var maxVelocity = maxVel.scaled(32).floored(); - var scaledVelocity = velocity.scaled(8000/32/20).floored(); // from fixed-position/second to unit => 1/8000 blocks per tick - entity._writeOthersNearby('entity_velocity', { - entityId: entity.id, - velocityX: scaledVelocity.x, - velocityY: scaledVelocity.y, - velocityZ: scaledVelocity.z - }); - if (entity.type != 'player') { - if (maxVelocity) entity.velocity = addVelocityWithMax(entity.velocity, velocity, maxVelocity); - else entity.velocity.add(velocity); - } - }; - entity.teleport = (pos) => { // Overwritten in players inject above entity.sendPosition(pos.scaled(32), false, true); }