How to use the flying-squid.UserError function in flying-squid

To help you get started, we’ve selected a few flying-squid examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PrismarineJS / flying-squid / src / lib / plugins / spawn.js View on Github external
parse (str) {
      const args = str.split(' ')
      if (args.length !== 2) { return false }

      let carrier = player.selectorString(args[0])
      if (carrier.length === 0) throw new UserError('one carrier')
      let attached = player.selectorString(args[1])
      if (attached.length === 0) throw new UserError('one attached')

      return { carrier: carrier[0], attached: attached[0] }
    },
    action ({ carrier, attached }) {
github PrismarineJS / flying-squid / src / lib / plugins / commands.js View on Github external
serv.selectorString = (str, pos, world, allowUser = true) => {
    pos = pos.clone()
    const player = serv.getPlayer(str)
    if (!player && str[0] !== '@') return []
    else if (player) return allowUser ? [player] : []
    const match = str.match(/^@([arpe])(?:\[([^\]]+)\])?$/)
    if (match === null) throw new UserError('Invalid selector format')
    const typeConversion = {
      a: 'all',
      r: 'random',
      p: 'near',
      e: 'entity'
    }
    const type = typeConversion[match[1]]
    const opt = match[2] ? match[2].split(',') : []
    const optPair = []
    let err
    opt.forEach(o => {
      const match = o.match(/^([^=]+)=([^=]+)$/)
      if (match === null) err = new UserError('Invalid selector option format: "' + o + '"')
      else optPair.push({ key: match[1], val: match[2] })
    })
    if (err) throw err
github PrismarineJS / flying-squid / src / lib / plugins / portal.js View on Github external
parse (str) {
      const pars = str.split(' ')
      if (pars.length !== 6) { return false }
      let [x, y, z, direction, width, height] = pars;
      [x, y, z] = [x, y, z].map((val, i) => serv.posFromString(val, player.position[['x', 'y', 'z'][i]]))
      const bottomLeft = new Vec3(x, y, z)
      if (direction !== 'x' && direction !== 'z') { throw new UserError('Wrong Direction') }
      direction = direction === 'x' ? new Vec3(1, 0, 0) : new Vec3(0, 0, 1)
      return { bottomLeft, direction, width, height }
    },
    async action ({ bottomLeft, direction, width, height }) {
github PrismarineJS / flying-squid / src / lib / plugins / spawn.js View on Github external
parse (str) {
      const args = str.split(' ')
      if (args.length !== 2) { return false }

      let carrier = player.selectorString(args[0])
      if (carrier.length === 0) throw new UserError('one carrier')
      let attached = player.selectorString(args[1])
      if (attached.length === 0) throw new UserError('one attached')

      return { carrier: carrier[0], attached: attached[0] }
    },
    action ({ carrier, attached }) {
github PrismarineJS / flying-squid / src / lib / plugins / commands.js View on Github external
serv.selectorString = (str, pos, world, allowUser=true) => {
    pos = pos.clone();
    var player = serv.getPlayer(str);
    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 = {
      a: 'all',
      r: 'random',
      p: 'near',
      e: 'entity'
    };
    var type = typeConversion[match[1]];
    var opt = match[2] ? match[2].split(',') : [];
    var optPair = [];
    var err;
    opt.forEach(o => {
      var match = o.match(/^([^=]+)=([^=]+)$/);
      if (match == null) err = new UserError('Invalid selector option format: "' + o + '"');
      else optPair.push({key: match[1], val: match[2]});
    });
    if (err) throw err;
github PrismarineJS / flying-squid / src / lib / plugins / portal.js View on Github external
async action ({ bottomLeft, direction, width, height }) {
      if (width > 21 || height > 21) { throw new UserError('Portals can only be 21x21!') }
      const portal = generatePortal(bottomLeft, direction, width, height)
      await addPortalToWorld(player.world, portal, [], [], async (pos, type) => {
        await serv.setBlock(player.world, pos, type, 0)
      })
    }
  })
github PrismarineJS / flying-squid / src / lib / plugins / commands.js View on Github external
serv.selector = (type, opt) => {
    if (['all', 'random', 'near', 'entity'].indexOf(type) == -1)
      throw new UserError('serv.selector(): type must be either [all, random, near, or entity]');

    var count = typeof opt.count != 'undefined' ?
                  count :
                  (type == 'all' || type == 'entity' ?serv.entities.length : 1);
    var pos = opt.pos;
    var sample;
    if (type == 'all') sample = serv.players;
    else if (type == 'random' || type == 'near') sample = serv.players.filter(p => p.health != 0);
    else if (type == 'entity') sample = Object.keys(serv.entities).map(k => serv.entities[k]);

    var checkOption = (val, compare) => {
      if (!val) return true;
      var not = val[0] == '!';
      var v = val;
      if (not) v = v.slice(1, v.length);
      if (not && compare == v) return false;
github PrismarineJS / flying-squid / src / lib / plugins / commands.js View on Github external
serv.selector = (type, opt) => {
    if (['all', 'random', 'near', 'entity'].indexOf(type) === -1) { throw new UserError('serv.selector(): type must be either [all, random, near, or entity]') }

    const count = opt.count !== undefined
      ? opt.count
      : (type === 'all' || type === 'entity' ? serv.entities.length : 1)

    const pos = opt.pos
    let sample
    if (type === 'all') sample = serv.players
    else if (type === 'random' || type === 'near') sample = serv.players.filter(p => p.health !== 0)
    else if (type === 'entity') sample = Object.keys(serv.entities).map(k => serv.entities[k])

    const checkOption = (val, compare) => {
      if (!val) return true
      const not = val[0] === '!'
      let v = val
      if (not) v = v.slice(1, v.length)
github PrismarineJS / flying-squid / src / lib / plugins / tp.js View on Github external
action (args) {
      if (args.length === 2) {
        let entitiesFrom = player.selectorString(args[0])
        let entityTo = player.selectorString(args[1])
        if (entityTo.length === 0) throw new UserError('Invalid target')
        entityTo = entityTo[0]

        entitiesFrom.forEach(e => e.teleport(entityTo.position))
      } else if (args.length === 3) {
        let x = serv.posFromString(args[0], player.position.x)
        let y = serv.posFromString(args[1], player.position.y)
        let z = serv.posFromString(args[2], player.position.z)

        player.teleport(new Vec3(x, y, z))
      } else if (args.length === 4) {
        let entitiesFrom = player.selectorString(args[0])

        entitiesFrom.forEach(e => e.teleport(new Vec3(
          serv.posFromString(args[1], e.position.x),
          serv.posFromString(args[2], e.position.y),
          serv.posFromString(args[3], e.position.z)
github PrismarineJS / flying-squid / src / lib / plugins / commands.js View on Github external
serv.posFromString = (str, pos) => {
    if (parseInt(str)) return parseInt(str);
    if (str.match(/~-?\d+/)) return parseInt(str.slice(1)) + pos;
    else if (str == '~') return pos;
    else throw new UserError('Invalid position');
  };
};