Skip to content

Commit

Permalink
带坐标的召唤支持波浪线“~”相对坐标
Browse files Browse the repository at this point in the history
  • Loading branch information
xBoyMinemc committed Nov 5, 2024
1 parent c901c25 commit be10083
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
64 changes: 64 additions & 0 deletions tscripts/lib/xboyPackage/xyz_dododo.ts
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 6 additions & 2 deletions tscripts/xTerrain/plugins/chatSpawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");


Expand Down Expand Up @@ -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(' '))
}
Expand Down

0 comments on commit be10083

Please sign in to comment.