From be100834e9712c7874f4dfb76d27106578043793 Mon Sep 17 00:00:00 2001 From: xBoyMinemc <86919167+xBoyMinemc@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:07:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=A6=E5=9D=90=E6=A0=87=E7=9A=84=E5=8F=AC?= =?UTF-8?q?=E5=94=A4=E6=94=AF=E6=8C=81=E6=B3=A2=E6=B5=AA=E7=BA=BF=E2=80=9C?= =?UTF-8?q?~=E2=80=9D=E7=9B=B8=E5=AF=B9=E5=9D=90=E6=A0=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tscripts/lib/xboyPackage/xyz_dododo.ts | 64 ++++++++++++++++++++++++++ tscripts/xTerrain/plugins/chatSpawn.ts | 8 +++- 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tscripts/lib/xboyPackage/xyz_dododo.ts 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(' ')) }