From 6d13014d1c79cb94cc697ef56bfeb882270a8f87 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Thu, 27 Aug 2015 03:46:53 +0200 Subject: [PATCH] add player.changeBlock method, make digging work in creative mode, basic block_place functionality --- doc/api.md | 5 +++++ lib/playerPlugins/blocks.js | 15 +++++++++++++++ lib/playerPlugins/digging.js | 11 +++-------- lib/playerPlugins/placeBlock.js | 16 ++++++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 lib/playerPlugins/blocks.js create mode 100644 lib/playerPlugins/placeBlock.js diff --git a/doc/api.md b/doc/api.md index 583ff6f..6c86cf7 100644 --- a/doc/api.md +++ b/doc/api.md @@ -52,6 +52,7 @@ - [player.login()](#playerlogin) - [player.others()](#playerothers) - [player.chat(message)](#playerchatmessage) + - [player.changeBlock(position,blockType)](#playerchangeblockpositionblocktype) - [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.changeBlock(position,blockType) + +change the block at position `position` to `blockType` + ### Low level properties #### player._client diff --git a/lib/playerPlugins/blocks.js b/lib/playerPlugins/blocks.js new file mode 100644 index 0000000..72a928e --- /dev/null +++ b/lib/playerPlugins/blocks.js @@ -0,0 +1,15 @@ +module.exports=inject; + +function inject(serv,player) +{ + function changeBlock(position,blockType) + { + player._writeOthers("block_change",{ + location:position, + type:blockType<<4 + }); + serv.world.setBlockType(position.x,position.y,position.z,blockType); + } + + player.changeBlock=changeBlock; +} \ No newline at end of file diff --git a/lib/playerPlugins/digging.js b/lib/playerPlugins/digging.js index a02c1f8..6a8ff15 100644 --- a/lib/playerPlugins/digging.js +++ b/lib/playerPlugins/digging.js @@ -4,13 +4,8 @@ function inject(serv,player) { // doesn't check whether the player actually wait the correct time player._client.on("block_dig",function(packet){ - if(packet.status==2) - { - player._writeOthers("block_change",{ - location:packet.location, - type:0 - }); - serv.world.setBlockType(packet.location.x,packet.location.y,packet.location.z,0); - } + if(packet.status==2 || (packet.status==0 && player.gameMode==1)) + player.changeBlock(packet.location,0); }); + } \ No newline at end of file diff --git a/lib/playerPlugins/placeBlock.js b/lib/playerPlugins/placeBlock.js new file mode 100644 index 0000000..a545b54 --- /dev/null +++ b/lib/playerPlugins/placeBlock.js @@ -0,0 +1,16 @@ +var vec3 = require("vec3"); + +module.exports=inject; + +function inject(serv,player) +{ + player._client.on("block_place",function(packet){ + if(packet.direction==-1) return; + var referencePosition=new vec3(packet.location.x,packet.location.y,packet.location.z); + var directionVector=directionToVector[packet.direction]; + var placedPosition=referencePosition.plus(directionVector); + player.changeBlock(placedPosition,packet.heldItem.blockId); + }); +} + +var directionToVector=[new vec3(0,-1,0),new vec3(0,1,0),new vec3(0,0,-1),new vec3(0,0,1),new vec3(-1,0,0),new vec3(1,0,0)];