diff --git a/tscripts/lib/xboyPackage/xyz_dododo.ts b/tscripts/lib/xboyPackage/xyz_dododo.ts new file mode 100644 index 0000000..5228753 --- /dev/null +++ b/tscripts/lib/xboyPackage/xyz_dododo.ts @@ -0,0 +1,64 @@ +// 将1 1 1解析为x y z +// 将1.1 -20 -30.2解析为x y z +// 将~1 ~1 ~1解析为x y z,解析时假设xyz上的~分别是一个全局变量 +// 解析字符串为x, y, z + +const ops = {} +ops['+'] = '+' +ops['-'] = '-' + +export function xyz_dododo(xyz:string[],playerLocation=[0,0,0]) : number[] { + // 初始化x, y, z + let new_xyz = [0,0,0] + + // 遍历分割后的数组 + xyz.forEach((part, index) => { + // 否则直接解析为数字 + if (!part.startsWith('~')) + return new_xyz[index] = Number(part); + + let data: number|string = part.slice(1) // 去掉~ + let op = ops[data[0]] // 存在风险 + // 如果以~开头,假设它是一个全局变量 + if (op === undefined) { + op = '+' + } else { + data = data.slice(1) // 去掉运算符 + } + + data = Number(data) + if(isNaN(data)) + throw new Error(['x','y','z'][index] + ' not a number') + + + if(op === '+') + data += playerLocation[index] + if(op === '-') + data -= playerLocation[index] + + new_xyz[index] = data + }); + + return new_xyz; +} + +function xyz_dododo_test() { + // 示例用法 + const result1 = xyz_dododo(["1", "1", "1"]); + console.log(result1); // [ 1, 1, 1 ] + + const result2 = xyz_dododo(["1.1", "-20" ,"-30.2"]); + console.log(result2); // [ 1.1, -20, -30.2 ] + + + const result3 = xyz_dododo(["~2.2", "~+3" ,"~-4.5"]); + console.log(result3); // [ 102.2, 203, -295.5 ] + + + const result4 = xyz_dododo(["~/2.2", "~+0" ,"~-"]); + console.log(result4); + // throw new Error(['x','y','z'][index] + ' not a number') + // ^ + // + // Error: x not a number +} \ No newline at end of file diff --git a/tscripts/xTerrain/plugins/chatSpawn.ts b/tscripts/xTerrain/plugins/chatSpawn.ts index b39a1e2..73f8748 100644 --- a/tscripts/xTerrain/plugins/chatSpawn.ts +++ b/tscripts/xTerrain/plugins/chatSpawn.ts @@ -10,6 +10,7 @@ import { } from '../main' import { commandInfo, CommandRegistry } from '../../lib/yumeCommand/CommandRegistry' import { Dimension, Vector3, world } from '@minecraft/server' +import {xyz_dododo} from "../../lib/xboyPackage/xyz_dododo"; const overworld = world.getDimension("overworld"); @@ -114,8 +115,11 @@ const withArgs_xyz_name = ({args,entity}:commandInfo)=>{ if(args.length>=2 && args.length<=3) return entity?.sendMessage('[模拟玩家] 命令错误,期待三个坐标数字,得到个数为'+(args.length-1)) try { - const [x,y,z] = args.slice(1,4).map(Number) - location = {x,y,z} + const [x,y,z] = args.slice(1,4) + const {x:_x,y:_y,z:_z} = entity.location + const [__x,__y,__z] = xyz_dododo([x,y,z],[_x,_y,_z]) + location = {x:__x,y:__y,z:__z} + // 好烂,谁来改改 }catch (e) { return entity?.sendMessage('[模拟玩家] 命令错误,期待三个却得到错误的信息 '+args.join(' ')) }