From 5a668e681562756f481048c82f7f537abd593c8f Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sat, 29 Aug 2015 04:25:26 +0200 Subject: [PATCH] implement digging correctly by using prismarine-block digTime function, fix #7 --- lib/playerPlugins/digging.js | 85 ++++++++++++++++++++++++++++++++++-- lib/playerPlugins/logout.js | 2 +- lib/version.js | 3 ++ package.json | 3 +- 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 lib/version.js diff --git a/lib/playerPlugins/digging.js b/lib/playerPlugins/digging.js index 6a8ff15..b4567ec 100644 --- a/lib/playerPlugins/digging.js +++ b/lib/playerPlugins/digging.js @@ -1,11 +1,90 @@ +var Block=require("prismarine-block")(require("../version")); + module.exports=inject; 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 || (packet.status==0 && player.gameMode==1)) - player.changeBlock(packet.location,0); + var b=serv.world.getBlock(packet.location.x,packet.location.y,packet.location.z); + currentlyDugBlock=new Block(b.id, b.biome, b.data); + if(currentlyDugBlock.type==0) return; + if(packet.status==0 && player.gameMode!=1) + startDigging(packet.location); + else if(packet.status==2) + completeDigging(packet.location); + else if(packet.status==1) + cancelDigging(packet.location); + else if(packet.status==0 && player.gameMode==1) + creativeDigging(packet.location); }); + function diggingTime(location) + { + // assume holding nothing and usual conditions + return currentlyDugBlock.digTime(); + } + + var currentlyDugBlock; + var startDiggingTime; + var animationInterval; + var expectedDiggingTime; + var lastDestroyState; + var currentAnimationId; + function startDigging(location) + { + serv.entityMaxId++; + currentAnimationId=serv.entityMaxId; + expectedDiggingTime=diggingTime(location); + lastDestroyState=0; + startDiggingTime=new Date(); + updateAnimation(); + animationInterval=setInterval(updateAnimation,100); + function updateAnimation() + { + var currentDiggingTime=new Date()-startDiggingTime; + var newDestroyState=Math.floor(9*currentDiggingTime/expectedDiggingTime); + newDestroyState=newDestroyState>9 ? 9 : newDestroyState; + if(newDestroyState!=lastDestroyState) + { + lastDestroyState=newDestroyState; + player._writeOthers("block_break_animation",{ + "entityId":currentAnimationId, + "location":location, + "destroyStage":newDestroyState + }); + } + } + } + + function cancelDigging(location) + { + clearInterval(animationInterval); + player._writeOthers("block_break_animation",{ + "entityId":currentAnimationId, + "location":location, + "destroyStage":-1 + }); + } + + function completeDigging(location) + { + clearInterval(animationInterval); + var diggingTime=new Date()-startDiggingTime; + if(expectedDiggingTime-diggingTime<100) + player.changeBlock(location,0); + else + { + player._client.write("block_change",{ + location:location, + type:currentlyDugBlock.type<<4 + }); + } + } + + + function creativeDigging(location) + { + player.changeBlock(location,0); + } + } \ No newline at end of file diff --git a/lib/playerPlugins/logout.js b/lib/playerPlugins/logout.js index 1dad9db..443d54b 100644 --- a/lib/playerPlugins/logout.js +++ b/lib/playerPlugins/logout.js @@ -16,7 +16,7 @@ function inject(serv,player) }] }); player._writeOthers('entity_destroy', {'entityIds': [player.entity.id]}); - delete serv.entities[player.entity.id] + delete serv.entities[player.entity.id]; player.emit('disconnect'); var index = serv.players.indexOf(player); if (index > -1) { diff --git a/lib/version.js b/lib/version.js new file mode 100644 index 0000000..7de8e70 --- /dev/null +++ b/lib/version.js @@ -0,0 +1,3 @@ +var mc = require("minecraft-protocol"); + +module.exports=mc.minecraftVersion; \ No newline at end of file diff --git a/package.json b/package.json index 85ccd93..9d4c3ea 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "chai": "~3.2.0", "prismarine-chunk": "~0.1.0", "vec3": "0.1.3", - "requireindex": "~1.0.0" + "requireindex": "~1.0.0", + "prismarine-block": "0.1.0" }, "repository": { "type": "git",