@bedrock-oss/stylish
Version:
A decorator package with for developing add-ons with Script API in Minecraft Bedrock Edition
82 lines (73 loc) • 3.26 kB
text/typescript
import { StartupEvent, WorldLoadAfterEvent, ItemUseBeforeEvent, CustomCommand, ItemComponentRegistry, BlockComponentRegistry } from '@minecraft/server';
/**
* Decorator – binds the method to the instance
* Equivalent to calling `this.method = this.method.bind(this);`
*/
declare function BindThis(_target: any, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor;
type ComponentCtor = {
componentId: string;
new (): any;
};
/**
* Decorator – attach to each component class to auto‐register it.
*/
declare function ItemComponent<T extends ComponentCtor>(ctor: T): void;
/**
* Decorator – attach to each component class to auto‐register it.
*/
declare function BlockComponent<T extends ComponentCtor>(ctor: T): void;
interface EventMap {
startup: StartupEvent;
worldLoad: WorldLoadAfterEvent;
beforeItemUse: ItemUseBeforeEvent;
}
type EventName = keyof EventMap;
type Handler<E extends EventName> = (e: EventMap[E]) => void | Promise<void>;
/**
* Creates a decorator function for the given event name.
* The decorator can be used in two ways:
* 1. As a method decorator on static or instance methods.
* 2. As a direct function call to register a standalone function.
*
* @param event The name of the event to register for.
* @returns A decorator function.
*/
type EventDecorator<E extends EventName> = ((fn: Handler<E>) => void) & ((target: any, propertyKey: string | symbol, descriptor: any) => any);
declare function createEventDecorator<E extends EventName>(event: E): EventDecorator<E>;
declare const OnStartup: EventDecorator<"startup">;
declare const OnWorldLoad: EventDecorator<"worldLoad">;
declare const OnBeforeItemUse: EventDecorator<"beforeItemUse">;
/**
* Class decorator – wraps the constructor so that any time an instance is created,
* its instance event methods are automatically registered.
*/
declare function RegisterEvents<T extends new (...args: any[]) => any>(Ctor: T): T;
/**
* Constructor type for Custom Commands. Constructors must not have parameters.
*/
type CustomCommandCtor = new () => CustomCommand & Record<string, any>;
/**
* Decorator – attach to each Custom Command class to auto-register it.
*/
declare function CustomCmd<T extends CustomCommandCtor>(ctor: T): void;
/**
* Initializes the library
*/
declare function init(): void;
/**
* Registers V1 `worldInitialize` event and registers all components.
* @deprecated Use {@link init() `init()`} instead. **Please note that Minecraft Script API v1.X is no longer supported and this will initialize for Minecraft Script API V2.X and later!**
*/
declare function initV1(): void;
/**
* Registers V1 `worldInitialize` event and registers all components.
* @deprecated Use {@link init() `init()`} instead.
*/
declare function initV2(): void;
/**
* Registers all components.
* @param itemRegistry Item component registry
* @param blockRegistry Block component registry
*/
declare function registerAllComponents(itemRegistry: ItemComponentRegistry, blockRegistry: BlockComponentRegistry): void;
export { BindThis, BlockComponent, CustomCmd, ItemComponent, OnBeforeItemUse, OnStartup, OnWorldLoad, RegisterEvents, createEventDecorator, init, initV1, initV2, registerAllComponents };