diff --git a/src/Entity/SuperEntity.ts b/src/Entity/SuperEntity.ts index b5f382b..607df61 100644 --- a/src/Entity/SuperEntity.ts +++ b/src/Entity/SuperEntity.ts @@ -2,7 +2,7 @@ import * as mc from "@minecraft/server"; import { Attribute } from "../Public/Attribute"; import { ComponentType, CustomComponentManager } from "../Component/CustomComponentManager"; import { Vec3 } from "../Public/Vec3"; -import { cast, enumKeyToString ,fromJSON,toJSON} from "../Public/stdlib"; +import { cast, enumKeyToString ,fromJSON,toJSON} from "../Public/Stdlib"; import { registerAsSubscribable, Super } from "../Super/Super"; import { EntitySuperComponent } from "../Component/SuperEntityComponent"; import { SuperComponentCreateOptions } from "../Component/SuperComponent"; diff --git a/src/Item/SuperItemStack.ts b/src/Item/SuperItemStack.ts index 6329476..6bd361a 100644 --- a/src/Item/SuperItemStack.ts +++ b/src/Item/SuperItemStack.ts @@ -2,7 +2,7 @@ import * as mc from "@minecraft/server"; import { Attribute } from "../Public/Attribute"; import { Super } from "../Super/Super"; import { ItemSuperComponent } from "../Component/SuperItemComponent"; -import { enumKeyToString, fromJSON, toJSON } from "../Public/stdlib"; +import { enumKeyToString, fromJSON, toJSON } from "../Public/Stdlib"; import { SuperComponentCreateOptions } from "../Component/SuperComponent"; import { ComponentType, CustomComponentManager } from "../Component/CustomComponentManager"; import { SuperPlayer } from "../Player/SuperPlayer"; diff --git a/src/Player/SuperPlayer.ts b/src/Player/SuperPlayer.ts index abbff4f..9062199 100644 --- a/src/Player/SuperPlayer.ts +++ b/src/Player/SuperPlayer.ts @@ -2,7 +2,7 @@ import * as mc from "@minecraft/server"; import { SuperEntity } from "../Entity/SuperEntity"; import { registerAsSubscribable, Super } from "../Super/Super"; import { ComponentType, CustomComponentManager } from "../Component/CustomComponentManager"; -import { enumKeyToString } from "../Public/stdlib"; +import { enumKeyToString } from "../Public/Stdlib"; import { PlayerSuperComponent } from "../Component/SuperPlayerComponent"; import { SuperWorld } from "../World/SuperWorld"; import { SuperItemStack } from "Item/SuperItemStack"; diff --git a/src/Public/Timer.ts b/src/Public/Timer.ts deleted file mode 100644 index 4bb8a4c..0000000 --- a/src/Public/Timer.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { system } from "@minecraft/server"; - -export class Timer { - private timeoutId?: number; - private intervalId?: number; - constructor() { } - - // 设置一个单次定时器 - public setTimeout(callback: () => void, delay: number): void { - system.run(()=>{ - if (this.timeoutId) { - system.clearRun(this.timeoutId); - } - this.timeoutId = system.runTimeout(callback, delay); - }) - } - - // 设置一个重复定时器 - public setInterval(callback: () => void, interval: number): void { - if (this.intervalId) { - system.clearRun(this.intervalId); - } - this.intervalId = system.runInterval(callback, interval); - } - - // 取消单次定时器 - public clearTimeout(): void { - if (this.timeoutId) { - system.clearRun(this.timeoutId); - this.timeoutId = undefined; - } - } - - // 取消重复定时器 - public clearInterval(): void { - if (this.intervalId) { - system.clearRun(this.intervalId); - this.intervalId = undefined; - } - } - //持久定时器,在持续时间内持续触发 - public setLasting(interval:number,loop_event:()=>void,duration:number,out_event:()=>void):void{ - if (interval>duration) { - throw new Error("Interval cannot be greater than duration."); - } - this.setInterval(()=>{ - loop_event(); - },interval) - this.setTimeout(()=>{ - out_event(); - this.clearAll(); - },duration); - } - // 清除所有定时器 - public clearAll(): void { - this.clearTimeout(); - this.clearInterval(); - } -} diff --git a/src/Public/attribute.ts b/src/Public/attribute.ts deleted file mode 100644 index f584a14..0000000 --- a/src/Public/attribute.ts +++ /dev/null @@ -1,97 +0,0 @@ -import { hasFun } from "./stdlib"; - -//属性类 -export class Attribute { - private properties: { [key: string]: any } = {}; - _obj:any; - constructor(obj:any){ - // 保证有这两个函数 - if (hasFun(obj,"getDynamicProperty") - &&hasFun(obj,"setDynamicProperty")) { - this._obj=obj; - let data=obj.getDynamicProperty("Attribute"); - if (data) { - this.properties=JSON.parse(data); - }else{ - this.save() - } - }else{//抛出异常 - throw new Error("SuperAPI[Attribute:constructor]:The constructor you want to pass using the class must have two functions,"); - } - } - private save(){ - let data=JSON.stringify(this.properties); - if (this._obj) { - this._obj.setDynamicProperty("Attribute",data); - } - } - init(key: string, value: any){ - let data=this._obj.getDynamicProperty("Attribute"); - let json=JSON.parse(data); - if (!json[key]) {//只有在该值不存在的时候才能初始化,避免覆盖值 - this.set(key,value); - } - } - // 添加或更新属性 - set(key: string, value: any): void { - this.properties[key] = value; - this.save(); - } - - // 获取属性值 - get(key: string): any { - return this.properties[key]; - } - - // 删除属性 - delete(key: string): void { - delete this.properties[key]; - this.save(); - } - - // 检查属性是否存在 - has(key: string): boolean { - return key in this.properties; - } - - // 获取所有属性键 - keys(): string[] { - return Object.keys(this.properties); - } - - // 获取所有属性值 - values(): any[] { - return Object.values(this.properties); - } - - // 获取所有属性键值对 - entries(): [string, any][] { - return Object.entries(this.properties); - } - - // 清空所有属性 - clear(): void { - Object.keys(this.properties).forEach(key => { - delete this.properties[key]; - }); - this.save(); - } -} - -// 使用示例 -// const dynProps = new Attribute(); -// dynProps.set('name', 'John Doe'); -// dynProps.set('age', 30); - -// console.log(dynProps.get('name')); // 输出:John Doe -// console.log(dynProps.has('age')); // 输出:true - -// dynProps.delete('age'); -// console.log(dynProps.has('age')); // 输出:false - -// console.log(dynProps.keys()); // 输出:['name'] -// console.log(dynProps.values()); // 输出:['John Doe'] -// console.log(dynProps.entries()); // 输出:[['name', 'John Doe']] - -// dynProps.clear(); -// console.log(dynProps.keys()); // 输出:[] \ No newline at end of file diff --git a/src/Public/axis.ts b/src/Public/axis.ts deleted file mode 100644 index fc97151..0000000 --- a/src/Public/axis.ts +++ /dev/null @@ -1,28 +0,0 @@ -import {Vec3} from "./Vec3"; - -export default class Axis { - x: Vec3; y: Vec3; z: Vec3; - constructor(x: Vec3, y: Vec3, z: Vec3) { - this.x = x - this.y = y - this.z = z - } - static bin(ang: Vec3, src: Vec3) { - let sx = Math.sin(ang.x) - let cx = Math.cos(ang.x) - let sy = Math.sin(ang.y) - let cy = Math.cos(ang.y) - let sz = Math.sin(ang.z) - let cz = Math.cos(ang.z) - return new Axis(new Vec3(cx * cy, sx * cy, sy).muln(src.x), - new Vec3(cx * sz * sy - sx * cz, sx * sz * sy + cx * cz, sz * cy).muln(src.y), - new Vec3(cx * cz * sy + sy * sz, sx * cz * sz + cx * sz, cz * cy).muln(src.z)) - } -//入轴向量 - dot(v: Vec3) { return this.x.muln(v.x).add(this.y.muln(v.y)).add(this.z.muln(v.z)) } -//叠加轴 - plus(a: Axis) { return new Axis(this.dot(a.x), this.dot(a.y), this.dot(a.z)) } - - toString() { return `[${this.x}] [${this.y}] [${this.z}]` } - -} diff --git a/src/Public/debug.ts b/src/Public/debug.ts deleted file mode 100644 index 12d4aab..0000000 --- a/src/Public/debug.ts +++ /dev/null @@ -1,16 +0,0 @@ - -export class Debug { - static debug:boolean=true; - constructor() { - } - static log(...args:any[]){ - if (this.debug) { - console.log("[SSAPI_DUBG] ",...args); - } - } - static debug_run(fun:()=>void){ - if (this.debug) { - fun() - } - } -} \ No newline at end of file diff --git a/src/Public/stdlib.ts b/src/Public/stdlib.ts deleted file mode 100644 index 2c16acf..0000000 --- a/src/Public/stdlib.ts +++ /dev/null @@ -1,70 +0,0 @@ -export function cast(obj: any): T {//转换类型仅ts可用于强制类型转化,例如SuperEntity转SuperPlayer - return (obj as T); -} - -export function hasFun(obj: any, funcName: string): boolean { - let currentProto = Object.getPrototypeOf(obj); - while (currentProto) { - if (typeof currentProto[funcName] === 'function') { - return true; - } - currentProto = Object.getPrototypeOf(currentProto); - } - return typeof obj[funcName] === 'function'; -} - -export function enumKeyToString(enumObj: any, enumValue: any): string { - const keys = Reflect.ownKeys(enumObj); - for (const key of keys) { - if (enumObj[key] === enumValue) { - return key.toString(); - } - } - return ''; -} - -export function toJSON(value: any): string { - let cache: any[] = []; - let index: number = 0; - let str = JSON.stringify(value, function (key, value) { - if (typeof value === 'object' && value !== null) { - if (cache.indexOf(value) !== -1) { - // 循环引用,返回特殊标记 - return `_CircularReference_${index++}`; - } - // 收集所有的值 - cache.push(value); - } - return value; - }); - // 清空变量,便于垃圾回收机制回收 - cache = null; - return str; -} - -export function fromJSON(value: string): any { - // 解析 JSON 字符串,但保留循环引用的标记 - let parsed = JSON.parse(value); - - let refs: any[] = []; - let refIndex: number = 0; - - // 用于恢复循环引用的 reviver 函数 - function revive(key, val) { - if (typeof val === 'string' && val.startsWith('_CircularReference_')) { - let refIndex = parseInt(val.split('_CircularReference_')[1], 10); - - return refs[refIndex]; - } - return val; - } - - // 使用 reviver 函数解析数据,以恢复循环引用 - return JSON.parse(JSON.stringify(parsed, function (key, val) { - if (typeof val === 'object' && val !== null) { - refs.push(val); - return refs[refIndex++]; - } - return val; - }), revive); -} diff --git a/src/Public/vec2.ts b/src/Public/vec2.ts deleted file mode 100644 index 9f26d77..0000000 --- a/src/Public/vec2.ts +++ /dev/null @@ -1,46 +0,0 @@ -import {Vec3} from "./Vec3"; -export class Vec2 { - x: number; y: number; - constructor(x: any, y: number) { - this.x = x - this.y = y - } - static fromObj(obj: any) { - return new Vec2(obj.x, obj.y) - } - //投影 - mod(v: Vec2) { return v.muln(this.dot(v) / v.dot(v)) } - //点积 - dot(v: Vec2) { return this.x * v.x + this.y * v.y } - //加 - add(v: Vec2) { return new Vec2(this.x + v.x, this.y + v.y) } - //减 - sub(v: Vec2) { return new Vec2(this.x - v.x, this.y - v.y) } - //向量乘 - mul(v: Vec2) { return new Vec2(this.x * v.x, this.y * v.y) } - //向量除 - div(v: Vec2) { return new Vec2(this.x / v.x, this.y / v.y) } - //夹角 - ang(v: Vec2) { return Math.atan2(this.x * v.y - this.y * v.x, this.dot(v)) } - //数乘 - muln(v: number) { return new Vec2(this.x * v, this.y * v) } - //数除 - divn(v: number) { return new Vec2(this.x / v, this.y / v) } - //旋转 - rotate(ang: number) { return this.muln(Math.cos(ang)).add(new Vec2(-this.y, this.x).muln(Math.sin(ang))) } - //旋转至 - rotate2(v: Vec2, t: number) { return this.rotate(this.ang(v) * t) } - //长度1 - normal() { return this.divn(this.len()) } - //长 - len() { return Math.sqrt(this.dot(this)) } - //转文本 - toString() { return `${this.x} ${this.y}` } - //角转ve3 - ator() { return new Vec3(Math.cos(this.x) * Math.cos(this.y), Math.sin(this.x) * Math.cos(this.y), Math.sin(this.y)) } - - - - - -} \ No newline at end of file diff --git a/src/Public/vec3.ts b/src/Public/vec3.ts deleted file mode 100644 index 18ed03d..0000000 --- a/src/Public/vec3.ts +++ /dev/null @@ -1,45 +0,0 @@ -import {Vec2} from "./Vec2"; -export class Vec3 { - x: number; y: number; z: number; - - constructor(x: any, y: number, z: number) { - this.x = x - this.y = y - this.z = z - } - static fromObj(obj: any) { - return new Vec3(obj.x, obj.y, obj.z) - } - //投影 - mod(v: Vec3) { return v.muln(this.dot(v) / v.dot(v)) } - //点积 - dot(v: Vec3) { return this.x * v.x + this.y * v.y + this.z * v.z } - //加 - add(v: Vec3) { return new Vec3(this.x + v.x, this.y + v.y, this.z + v.z) } - //减 - sub(v: Vec3) { return new Vec3(this.x - v.x, this.y - v.y, this.z - v.z) } - //向量乘 - mul(v: Vec3) { return new Vec3(this.x * v.x, this.y * v.y, this.z * v.z) } - //向量除 - div(v: Vec3) { return new Vec3(this.x / v.x, this.y / v.y, this.z / v.z) } - //叉乘 - pow(v: Vec3) { return new Vec3(this.y * this.z - v.y * v.z, this.z * this.x - v.z * v.x, this.x * this.y - v.x * v.y) } - //夹角 - ang(v: Vec3) { return Math.acos(this.normal().dot(v.normal())) } - //数乘 - muln(v: number) { return new Vec3(this.x * v, this.y * v, this.z * v) } - //数除 - divn(v: number) { return new Vec3(this.x / v, this.y / v, this.z / v) } - //绕轴旋转 - rotate(ax: Vec3, ang: number) { let di = this.mod(ax), rx = this.sub(di); return rx.muln(Math.cos(ang)).add(rx.pow(di.normal()).muln(Math.sin(ang))).add(di) } - //旋转至 - rotate2(v: Vec3, t: number) { return this.rotate(this.pow(v), this.ang(v) * t) } - //长度1 - normal() { return this.divn(this.len()) } - //长 - len() { return Math.sqrt(this.dot(this)) } - //转文本 - toString() { return `${this.x} ${this.y} ${this.z}` } - //转角向量 - tor() { return new Vec2(Math.atan2(this.z, this.x), Math.atan2(this.y, Math.sqrt(this.x * this.x + this.z + this.z))) } -} \ No newline at end of file diff --git a/src/Runtime.ts b/src/Runtime.ts index 8229bd6..715f644 100644 --- a/src/Runtime.ts +++ b/src/Runtime.ts @@ -6,7 +6,7 @@ import { CommandManager } from "./Command/CommandManager"; import { SuperItemStack } from "./Item/SuperItemStack"; import { Debug } from "./Public/Debug"; import { ItemStackManager } from "./Item/SuperItemManager"; -import { cast, toJSON } from "./Public/stdlib"; +import { cast, toJSON } from "./Public/Stdlib"; export enum NativeClassType {//可被替换的类和原型 diff --git a/src/SuperSAPI.ts b/src/SuperSAPI.ts index 94b7c7e..ea9109d 100644 --- a/src/SuperSAPI.ts +++ b/src/SuperSAPI.ts @@ -10,7 +10,7 @@ import * as sec from "./Component/SuperEntityComponent"; import * as spc from "./Component/SuperPlayerComponent"; import * as sic from "./Component/SuperItemComponent"; import * as si from "./Item/SuperItemStack"; -import * as std from "./Public/stdlib"; +import * as std from "./Public/Stdlib"; import * as sim from "./Item/SuperItemManager"; import * as tm from "./Public/Timer"; import * as msuper from "./Super/Super"; @@ -30,7 +30,7 @@ export const version_information={ //代导入 export const MC=await import("@minecraft/server"); -export const Std = await import("./Public/stdlib"); +export const Std = await import("./Public/Stdlib"); export const MCVD = await import("./VanillaData/VanillaData"); //原生类类型 export type MC_World=mc.World