UNPKG

yoni-mcscripts-lib

Version:

为 Minecraft Script API 中的部分接口创建了 wrapper,并提供简单的事件管理器和任务管理器,另附有一些便于代码编写的一些小工具。

37 lines (36 loc) 1.49 kB
export class EntityWraps { static map = new WeakMap(); static classWraps = new Map(); static prototypeWraps = new Map(); static srcClassWraps = new Map(); static srcPrototypeWraps = new Map(); static fromSourceEntity(entity) { const srcProto = Object.getPrototypeOf(entity); const wrappedClass = this.prototypeWraps.get(srcProto); if (!wrappedClass) return null; let wrappedEntity = this.map.get(entity); if (!wrappedEntity) { wrappedEntity = new wrappedClass(entity); this.map.set(entity, wrappedEntity); } return wrappedEntity; } static registerWrap(entityClass, originalEntityClass) { const srcProto = originalEntityClass.prototype; const proto = entityClass.prototype; this.classWraps.set(originalEntityClass, entityClass); this.prototypeWraps.set(srcProto, entityClass); this.srcClassWraps.set(entityClass, originalEntityClass); this.srcPrototypeWraps.set(proto, originalEntityClass); } static unregisterWrap(originalEntityClass) { const srcProto = originalEntityClass.prototype; const entityClass = this.classWraps.get(originalEntityClass); const proto = entityClass.prototype; this.classWraps.delete(originalEntityClass); this.prototypeWraps.delete(srcProto); this.srcClassWraps.delete(entityClass); this.srcPrototypeWraps.delete(proto); } }