implement digging correctly by using prismarine-block digTime function, fix #7

This commit is contained in:
Romain Beaumont 2015-08-29 04:25:26 +02:00
parent 3e8aa1f489
commit 5a668e6815
4 changed files with 88 additions and 5 deletions

View file

@ -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);
}
}

View file

@ -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) {

3
lib/version.js Normal file
View file

@ -0,0 +1,3 @@
var mc = require("minecraft-protocol");
module.exports=mc.minecraftVersion;

View file

@ -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",