UNPKG

@bedrock-oss/stylish

Version:

A decorator package with for developing add-ons with Script API in Minecraft Bedrock Edition

1 lines 5.28 kB
{"version":3,"sources":["../src/index.ts","../src/itemRegistry.ts","../src/blockRegistry.ts","../src/autobind.ts"],"sourcesContent":["import { BlockComponentRegistry, ItemComponentRegistry, system, world } from \"@minecraft/server\";\r\nimport { registerAllItemComponents } from \"./itemRegistry\";\r\nimport { registerAllBlockComponents } from \"./blockRegistry\";\r\n\r\nexport { BindThis } from \"./autobind\";\r\nexport { ItemComponent } from \"./itemRegistry\";\r\nexport { BlockComponent } from \"./blockRegistry\";\r\n\r\n/**\r\n * Registers V1 `worldInitialize` event and registers all components.\r\n */\r\nexport function initV1() {\r\n (world as any).beforeEvents.worldInitialize.subscribe((e: any) => {\r\n registerAllComponents(e.itemComponentRegistry, e.blockComponentRegistry);\r\n });\r\n}\r\n\r\n/**\r\n * Registers V2 `startup` event and registers all components.\r\n */\r\nexport function initV2() {\r\n (system as any).beforeEvents.startup.subscribe((e: any) => {\r\n registerAllComponents(e.itemComponentRegistry, e.blockComponentRegistry);\r\n });\r\n}\r\n\r\n/**\r\n * Registers all components.\r\n * @param itemRegistry Item component registry\r\n * @param blockRegistry Block component registry\r\n */\r\nexport function registerAllComponents(itemRegistry: ItemComponentRegistry, blockRegistry: BlockComponentRegistry) {\r\n registerAllItemComponents(itemRegistry);\r\n registerAllBlockComponents(blockRegistry);\r\n}","import type { ItemComponentRegistry } from '@minecraft/server';\r\nimport { ComponentCtor } from './shared';\r\n\r\nconst registry: ComponentCtor[] = [];\r\n\r\n/**\r\n * Decorator – attach to each component class to auto‐register it.\r\n */\r\nexport function ItemComponent<T extends ComponentCtor>(ctor: T) {\r\n if (!ctor.componentId) {\r\n throw new Error(`Component ${ctor.name} is missing static componentId`);\r\n }\r\n registry.push(ctor);\r\n}\r\n\r\n/**\r\n * Call this in worldInitialize to register all decorated components.\r\n */\r\nexport function registerAllItemComponents(\r\n itemComponentRegistry: ItemComponentRegistry\r\n) {\r\n for (const Ctor of registry) {\r\n itemComponentRegistry.registerCustomComponent(\r\n Ctor.componentId,\r\n new Ctor()\r\n );\r\n }\r\n}\r\n","import type { BlockComponentRegistry } from '@minecraft/server';\r\nimport { ComponentCtor } from './shared';\r\n\r\nconst registry: ComponentCtor[] = [];\r\n\r\n/**\r\n * Decorator – attach to each component class to auto‐register it.\r\n */\r\nexport function BlockComponent<T extends ComponentCtor>(ctor: T) {\r\n if (!ctor.componentId) {\r\n throw new Error(`Component ${ctor.name} is missing static componentId`);\r\n }\r\n registry.push(ctor);\r\n}\r\n\r\n/**\r\n * Call this in worldInitialize to register all decorated components.\r\n */\r\nexport function registerAllBlockComponents(\r\n blockComponentRegistry: BlockComponentRegistry\r\n) {\r\n for (const Ctor of registry) {\r\n blockComponentRegistry.registerCustomComponent(\r\n Ctor.componentId,\r\n new Ctor()\r\n );\r\n }\r\n}\r\n","/**\r\n * Decorator – binds the method to the instance\r\n * Equivalent to calling `this.method = this.method.bind(this);`\r\n */\r\nexport function BindThis(\r\n _target: any,\r\n propertyKey: string,\r\n descriptor: PropertyDescriptor\r\n): PropertyDescriptor {\r\n const original = descriptor.value;\r\n return {\r\n configurable: true,\r\n enumerable: descriptor.enumerable,\r\n get(): any {\r\n // bind once\r\n const bound = original.bind(this);\r\n // overwrite the property on the instance so future accesses hit the bound fn directly\r\n Object.defineProperty(this, propertyKey, {\r\n value: bound,\r\n configurable: true,\r\n writable: true,\r\n enumerable: false,\r\n });\r\n return bound;\r\n },\r\n };\r\n}"],"mappings":";AAAA,SAAwD,QAAQ,aAAa;;;ACG7E,IAAM,WAA4B,CAAC;AAK5B,SAAS,cAAuC,MAAS;AAC5D,MAAI,CAAC,KAAK,aAAa;AACnB,UAAM,IAAI,MAAM,aAAa,KAAK,IAAI,gCAAgC;AAAA,EAC1E;AACA,WAAS,KAAK,IAAI;AACtB;AAKO,SAAS,0BACZ,uBACF;AACE,aAAW,QAAQ,UAAU;AACzB,0BAAsB;AAAA,MAClB,KAAK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