45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { Client } from 'minecraft-protocol'
|
|
import { Logger } from 'pino'
|
|
|
|
type ModuleLoader = <T extends Module<{}>>(module: IModuleType) => T
|
|
|
|
/** Typeof Module */
|
|
export interface IModuleType {
|
|
name: string
|
|
new(client: Client, logger: Logger, conf: object, getModule: ModuleLoader): Module<{}>
|
|
}
|
|
|
|
/** Management unit */
|
|
export default abstract class Module<T extends object> {
|
|
|
|
protected logger: Logger
|
|
protected client: Client
|
|
protected conf: T
|
|
|
|
private readonly getModule: ModuleLoader
|
|
|
|
constructor(client: Client, logger: Logger, conf: object, getModule: ModuleLoader) {
|
|
this.client = client
|
|
this.logger = logger
|
|
this.conf = {...this.getConf(), ...conf}
|
|
this.getModule = getModule
|
|
this.mount()
|
|
}
|
|
|
|
/** Mostly to clean timers */
|
|
public umount() {
|
|
// nothing
|
|
}
|
|
|
|
/** Default values of config */
|
|
protected abstract getConf(): T
|
|
|
|
/** Setup listener on client */
|
|
protected abstract mount(): void
|
|
|
|
/** Get reference to another module */
|
|
protected load<M extends Module<{}>>(module: IModuleType): M {
|
|
return this.getModule<M>(module)
|
|
}
|
|
}
|