create an entity.attach, some more selector fixes, and add a fun /pile <entities> command (that will go in a plugin later)

This commit is contained in:
Romain Beaumont 2015-12-11 05:20:10 +01:00
parent ce1ffe9c4f
commit 47c75671c4
2 changed files with 46 additions and 14 deletions

View file

@ -106,8 +106,7 @@ module.exports.player=function(player, serv) {
},
action(sel) {
var arr = serv.selectorString(sel, player.position.scaled(1/32), player.world);
if (arr == null) return 'Could not find player';
else player.chat(JSON.stringify(arr.map(a => a.id)));
player.chat(JSON.stringify(arr.map(a => a.id)));
}
});
@ -234,13 +233,13 @@ module.exports.server = function(serv) {
if (count > 0) return sample.slice(0, count);
else return sample.slice(count); // Negative, returns from end
}
};
serv.selectorString = (str, pos, world, allowUser=true) => {
pos = pos.clone();
var player = serv.getPlayer(str);
if (!player && str[0] != '@') return null;
else if (player) return allowUser ? [player] : null;
if (!player && str[0] != '@') return [];
else if (player) return allowUser ? [player] : [];
var match = str.match(/^@([a,r,p,e])(?:\[([^\]]+)\])?$/);
if (match == null) throw new UserError('Invalid selector format');
var typeConversion = {
@ -296,7 +295,7 @@ module.exports.server = function(serv) {
});
return serv.selector(type, data);
}
};
serv.posFromString = (str, pos) => {
if (parseInt(str)) return parseInt(str);
@ -304,4 +303,4 @@ module.exports.server = function(serv) {
else if (str == '~') return pos;
else throw new UserError('Invalid position');
};
}
};

View file

@ -59,6 +59,7 @@ module.exports.server=function(serv,options) {
mob.metadata = metadata;
mob.updateAndSpawn();
return mob;
};
serv.destroyEntity = entity => {
@ -126,6 +127,32 @@ module.exports.player=function(player,serv){
}
});
player.commands.add({
base: 'pile',
info: 'make a pile of entities',
usage: '/pile <entities types>',
op: true,
parse(str) {
var args=str.split(' ');
if(args.length==0)
return false;
return args
.map(name => entitiesByName[name])
.filter(entity => !!entity);
},
action(entityTypes) {
entityTypes.map(entity =>
serv.spawnMob(entity.id, player.world, player.position.scaled(1/32), {
velocity: Vec3((Math.random() - 0.5) * 10, Math.random()*10 + 10, (Math.random() - 0.5) * 10)
}))
.reduce((prec,entity) => {
if(prec!=null)
prec.attach(entity);
return entity;
},null);
}
});
player.commands.add({
base: 'attach',
info: 'attach an entity on an other entity',
@ -144,13 +171,7 @@ module.exports.player=function(player,serv){
return {carrier:carrier[0],attached:attached[0]};
},
action({carrier,attached}) {
var p={
entityId:attached.id,
vehicleId:carrier.id,
leash:false
};
player._client.write('attach_entity',p);
player._writeOthersNearby('attach_entity',p);
carrier.attach(attached);
}
});
@ -281,4 +302,16 @@ module.exports.entity=function(entity,serv) {
serv.destroyEntity(entity);
};
entity.attach= (attachedEntity,leash=false) =>
{
var p={
entityId:attachedEntity.id,
vehicleId:entity.id,
leash:leash
};
if(entity.type=='player')
entity._client.write('attach_entity',p);
entity._writeOthersNearby('attach_entity',p);
}
};