mirror of
https://github.com/danbulant/flying-squid
synced 2026-06-23 08:41:48 +00:00
Fix tp and setblock, implement relative position function, throw errors instead of returning, catch errors and display error
This commit is contained in:
parent
ad7253d36d
commit
67af4a8144
5 changed files with 35 additions and 31 deletions
5
src/lib/UserError.js
Normal file
5
src/lib/UserError.js
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
class UserError extends Error {
|
||||
|
||||
}
|
||||
|
||||
module.exports = UserError;
|
||||
|
|
@ -30,24 +30,18 @@ module.exports.player=function(player,serv)
|
|||
|
||||
player.commands.add({
|
||||
base: 'setblock',
|
||||
info: 'to put a block',
|
||||
usage: '/setblock <x> <y> <z> <id> <data>',
|
||||
info: 'set a block at a position',
|
||||
usage: '/setblock <x> <y> <z> <id> [data]',
|
||||
op: true,
|
||||
parse(str) {
|
||||
var results = str.match(/^(~|~?-?[0-9]*) (~|~?-?[0-9]*) (~|~?-?[0-9]*) ([0-9]{1,3}) ([0-9]{1,3})/);
|
||||
var results = str.match(/^(~|~?-?[0-9]+) (~|~?-?[0-9]+) (~|~?-?[0-9]+) ([0-9]{1,3})(?: ([0-9]{1,3}))?/);
|
||||
if(!results) return false;
|
||||
return results;
|
||||
},
|
||||
action(params) {
|
||||
var res = params.map((num, i) => { // parseInt parameters
|
||||
if (num.indexOf('~') == 0) {
|
||||
return (player.position[['', 'x', 'y', 'z'][i]] >> 5) + parseInt(num.slice(1) || 0);
|
||||
} else {
|
||||
return parseInt(num); // return parseInt>>5 if position, not id
|
||||
}
|
||||
});
|
||||
|
||||
player.setBlock(new Vec3(res[1], res[2], res[3]), res[4],res[5]);
|
||||
var res = params.slice(1, 4);
|
||||
res = res.map((val, i) => serv.posFromString(val, player.position[['x','y','z'][i]] / 32))
|
||||
player.setBlock(new Vec3(res[0], res[1], res[2]).floored(), params[4], params[5] || 0);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
|
@ -1,3 +1,6 @@
|
|||
var Vec3 = require("vec3").Vec3;
|
||||
var UserError = require('../UserError');
|
||||
|
||||
module.exports.player=function(player, serv) {
|
||||
|
||||
player.commands.add({
|
||||
|
|
@ -83,8 +86,7 @@ module.exports.player=function(player, serv) {
|
|||
},
|
||||
action(sel) {
|
||||
var arr = serv.selectorString(sel, player.position.scaled(1/32), player.world);
|
||||
if (arr instanceof Error) return arr.toString();
|
||||
else if (arr == null) return 'Could not find player';
|
||||
if (arr == null) return 'Could not find player';
|
||||
else player.chat(JSON.stringify(arr.map(a => a.id)));
|
||||
}
|
||||
});
|
||||
|
|
@ -96,7 +98,8 @@ module.exports.player=function(player, serv) {
|
|||
if (res) player.chat('' + res);
|
||||
}
|
||||
catch(err) {
|
||||
setTimeout(() => {throw err;}, 0);
|
||||
if (err instanceof UserError) player.chat('Error: ' + err.toString());
|
||||
else setTimeout(() => {throw err;}, 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -126,7 +129,7 @@ module.exports.server = function(serv) {
|
|||
|
||||
serv.selector = (type, opt) => {
|
||||
if (['all', 'random', 'near', 'entity'].indexOf(type) == -1)
|
||||
return new Error('serv.selector(): type must be either [all, random, near, or entity]');
|
||||
throw new UserError('serv.selector(): type must be either [all, random, near, or entity]');
|
||||
|
||||
var count = typeof opt.count != 'undefined' ?
|
||||
count :
|
||||
|
|
@ -214,7 +217,7 @@ module.exports.server = function(serv) {
|
|||
var player = serv.getPlayer(str);
|
||||
if (!player && str[0] != '@') return null;
|
||||
var match = str.match(/^@([a,r,p,e])(?:\[([^\]]+)\])?$/);
|
||||
if (match == null) return new Error('Invalid selector format');
|
||||
if (match == null) throw new UserError('Invalid selector format');
|
||||
var typeConversion = {
|
||||
a: 'all',
|
||||
r: 'random',
|
||||
|
|
@ -227,10 +230,10 @@ module.exports.server = function(serv) {
|
|||
var err;
|
||||
opt.forEach(o => {
|
||||
var match = o.match(/^([^=]+)=([^=]+)$/);
|
||||
if (match == null) err = new Error('Invalid selector option format: "' + o + '"');
|
||||
if (match == null) err = new UserError('Invalid selector option format: "' + o + '"');
|
||||
else optPair.push({key: match[1], val: match[2]});
|
||||
});
|
||||
if (err) return err;
|
||||
if (err) throw err;
|
||||
|
||||
var optConversion = {
|
||||
type: 'type',
|
||||
|
|
@ -269,4 +272,11 @@ module.exports.server = function(serv) {
|
|||
|
||||
return serv.selector(type, data);
|
||||
}
|
||||
|
||||
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');
|
||||
};
|
||||
}
|
||||
|
|
@ -2,11 +2,6 @@ var Vec3 = require("vec3").Vec3;
|
|||
|
||||
module.exports.player = (player, serv) => {
|
||||
|
||||
var getPos = (num, dir='x', p=player) => {
|
||||
if (num[0] == '~') return p.position[dir] + parseInt(num.slice(1, num.length) || 0)*32;
|
||||
else return parseInt(num)*32;
|
||||
}
|
||||
|
||||
player.commands.add({
|
||||
base: 'teleport',
|
||||
aliases: ['tp'],
|
||||
|
|
@ -26,9 +21,9 @@ module.exports.player = (player, serv) => {
|
|||
|
||||
player_from.teleport(player_to.position.clone());
|
||||
} else if(args.length === 3) {
|
||||
let x = getPos(args[0], 'x');
|
||||
let y = getPos(args[1], 'y');
|
||||
let z = getPos(args[2], 'z');
|
||||
let x = serv.posFromString(args[0], player.position.x / 32);
|
||||
let y = serv.posFromString(args[1], player.position.y / 32);
|
||||
let z = serv.posFromString(args[2], player.position.z / 32);
|
||||
|
||||
player.teleport(new Vec3(x, y, z));
|
||||
} else if(args.length === 4) {
|
||||
|
|
@ -37,9 +32,9 @@ module.exports.player = (player, serv) => {
|
|||
if(!(player_from = serv.getPlayer(args[0])))
|
||||
return false;
|
||||
|
||||
let x = getPos(args[1], 'x', player_from);
|
||||
let y = getPos(args[2], 'y', player_from);
|
||||
let z = getPos(args[3], 'z', player_from);
|
||||
let x = serv.posFromString(args[1], player_from.x / 32);
|
||||
let y = serv.posFromString(args[2], player_from.y / 32);
|
||||
let z = serv.posFromString(args[3], player_from.z / 32);
|
||||
|
||||
player_from.teleport(new Vec3(x, y, z));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -99,7 +99,7 @@ module.exports.player=function(player)
|
|||
};
|
||||
|
||||
player.teleport = async (position) => {
|
||||
await player.sendRelativePositionChange(position, false);
|
||||
await player.sendRelativePositionChange(position.scaled(32).floored(), false);
|
||||
player.sendPosition();
|
||||
}
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue