fix place block test and improve lot of awaiting in the tests, only send min(3,view) chunks initially

This commit is contained in:
Romain Beaumont 2015-12-11 16:52:48 +01:00
parent 5f96ff3581
commit addec7252d
2 changed files with 45 additions and 18 deletions

View file

@ -121,7 +121,7 @@ module.exports.player=function(player,serv,settings) {
player.sendMap = () =>
{
return player.sendNearbyChunks(3)
return player.sendNearbyChunks(Math.min(3,settings["view-distance"]))
.catch((err) => setTimeout(() => { throw err; }), 0);
};

View file

@ -6,7 +6,7 @@ var assert = require('chai').assert;
var Vec3 = require('vec3').Vec3;
function assertPosEqual(actual,expected) {
assert.isBelow(actual.distanceTo(expected),0.1,"expected: "+expected+", actual: "+actual+"\n");
assert.isBelow(actual.distanceTo(expected),1,"expected: "+expected+", actual: "+actual+"\n");
}
var once = require('event-promise');
@ -65,7 +65,7 @@ describe("Server with mineflayer connection", function() {
var options = settings;
options["online-mode"]=false;
options["port"]=25566;
options["view-distance"]=1;
options["view-distance"]=2;
serv=mcServer.createMCServer(options);
@ -82,33 +82,57 @@ describe("Server with mineflayer connection", function() {
});
await Promise.all([once(bot,'login'),once(bot2,'login')]);
bot.entity.onGround=false;
bot2.entity.onGround=false;
});
afterEach(async () => {
await serv.quit();
});
describe("actions",() => {
function waitSpawnZone(bot,view)
{
var nbChunksExpected=(view*2)*(view*2);
var c=0;
return new Promise(cb => {
var listener=() => {
c++;
if(c==nbChunksExpected)
{
bot.removeListener('chunkColumnLoad',listener);
cb();
}
};
bot.on('chunkColumnLoad',listener);
});
}
it("can dig",async function () {
await onGround(bot);
this.timeout(10 * 60 * 1000);
var pos=bot.entity.position.offset(0,-1,0);
await Promise.all([waitSpawnZone(bot,2),waitSpawnZone(bot2,2),onGround(bot),onGround(bot2)]);
var pos=bot.entity.position.offset(0,-1,0).floored();
bot.dig(bot.blockAt(pos));
let [oldBlock,newBlock]=await once(bot,'blockUpdate:'+pos,{array:true});
assert.equal(newBlock.type,0);
let [oldBlock,newBlock]=await once(bot2,'blockUpdate',{array:true});
assertPosEqual(newBlock.position,pos);
assert.equal(newBlock.type,0,"block "+pos+" should have been dug");
});
it.skip("can place a block",async function () {
await onGround(bot);
it("can place a block",async function () {
this.timeout(10 * 60 * 1000);
var pos=bot.entity.position.offset(0,-2,0);
await Promise.all([waitSpawnZone(bot,2),waitSpawnZone(bot2,2),onGround(bot),onGround(bot2)]);
var pos=bot.entity.position.offset(0,-2,0).floored();
bot.dig(bot.blockAt(pos));
let [oldBlock,newBlock]=await once(bot2,'blockUpdate:'+pos,{array:true});
assert.equal(newBlock.type,0);
let [oldBlock,newBlock]=await once(bot2,'blockUpdate',{array:true});
assertPosEqual(newBlock.position,pos);
assert.equal(newBlock.type,0,"block "+pos+" should have been dug");
bot.creative.setInventorySlot(36,new mineflayer.Item(1,1));
await new Promise((cb) => {
bot.inventory.on("windowUpdate",(slot,oldItem,newItem) => {
@ -117,9 +141,11 @@ describe("Server with mineflayer connection", function() {
});
});
bot.placeBlock(bot.blockAt(pos.offset(0,-2,0)),new Vec3(0,1,0));
[oldBlock,newBlock]=await once(bot2,'blockUpdate:'+pos.offset(0,-1,0),{array:true});
assert.equal(newBlock.type,1);
bot.placeBlock(bot.blockAt(pos.offset(0,-1,0)),new Vec3(0,1,0));
[oldBlock,newBlock]=await once(bot2,'blockUpdate',{array:true});
assertPosEqual(newBlock.position,pos);
assert.equal(newBlock.type,1,"block "+pos+" should have been placed");
});
});
@ -164,7 +190,6 @@ describe("Server with mineflayer connection", function() {
assert.equal(entity.name,"EnderDragon");
});
describe("can use /tp",() => {
beforeEach(() => Promise.all([onGround(bot),onGround(bot2)]));
it("can tp myself", async () => {
bot.chat('/tp 2 3 4');
await once(bot,'forcedMove');
@ -181,12 +206,14 @@ describe("Server with mineflayer connection", function() {
assertPosEqual(bot2.entity.position, bot.entity.position);
});
it("can tp with relative positions",async () => {
await onGround(bot);
var initialPosition=bot.entity.position.clone();
bot.chat('/tp ~1 ~-2 ~3');
await once(bot,'forcedMove');
assertPosEqual(bot.entity.position,initialPosition.offset(1,-2,3));
});
it("can tp somebody else with relative positions",async () => {
await Promise.all([onGround(bot),onGround(bot2)]);
var initialPosition=bot2.entity.position.clone();
bot.chat('/tp bot2 ~1 ~-2 ~3');
await once(bot2,'forcedMove');