From 47c75671c44460a667ccdf536ec471503d8e6dd9 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Fri, 11 Dec 2015 05:20:10 +0100 Subject: [PATCH] create an entity.attach, some more selector fixes, and add a fun /pile command (that will go in a plugin later) --- src/lib/plugins/commands.js | 13 +++++----- src/lib/plugins/spawn.js | 47 +++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/lib/plugins/commands.js b/src/lib/plugins/commands.js index 22529d6..41d44ce 100644 --- a/src/lib/plugins/commands.js +++ b/src/lib/plugins/commands.js @@ -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'); }; -} \ No newline at end of file +}; \ No newline at end of file diff --git a/src/lib/plugins/spawn.js b/src/lib/plugins/spawn.js index 742c683..d935681 100644 --- a/src/lib/plugins/spawn.js +++ b/src/lib/plugins/spawn.js @@ -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 ', + 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); + } + }; \ No newline at end of file