feat: add plugin self method

This commit is contained in:
MiaoWoo 2023-08-12 17:14:57 +08:00
parent afd3f91a3f
commit a0866c1085
4 changed files with 61 additions and 15 deletions

View File

@ -1,7 +1,5 @@
import "@ccms/nashorn"
export * from './web'
export * from './amqp'
export * from './chat'
export * from './task'
export * from './item'

View File

@ -32,18 +32,28 @@ export const provideSingletonNamed = (identifier: interfaces.ServiceIdentifier<a
return fluentProvide(identifier).inSingletonScope().whenTargetNamed(name).done()
}
export function getJavaClass(className: string) {
try { return Java.type(className).class; return } catch (error: any) { }
try { return base.getClass(className); return } catch (error: any) { }
}
/**
* java.lang.Class
* @param className Java全类名
*/
export const JavaClass = (className: string) => {
return function (target: object, propertyKey: string, index?: number) {
try { target[propertyKey] = Java.type(className).class; return } catch (error: any) { }
try { target[propertyKey] = base.getClass(className); return } catch (error: any) { }
console.warn('JavaClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!')
_proxyGetter(target, propertyKey, () => {
return getJavaClass(className) || console.warn('JavaClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!')
}, true)
}
}
export function getJSClass(className: string) {
try { return Java.type(className) } catch (error: any) { }
try { return base.getClass(className).static } catch (error: any) { }
}
/**
* JS的Java类
* @param className Java
@ -51,9 +61,7 @@ export const JavaClass = (className: string) => {
export const JSClass = (className: string) => {
return function (target: object, propertyKey: string, index?: number) {
_proxyGetter(target, propertyKey, () => {
try { return Java.type(className) } catch (error: any) { }
try { return base.getClass(className).static } catch (error: any) { }
console.warn('JSClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!')
return getJSClass(className) || console.warn('JSClass', className, 'Inject target', target.constructor.name, 'propertyKey', propertyKey, 'failed!')
}, true)
}
}

View File

@ -51,7 +51,7 @@ export class PluginEventManager {
listener.off = off
}
private unregistryListener(pluginInstance: plugin.Plugin, listener: any = pluginInstance) {
public unregistryListener(pluginInstance: plugin.Plugin, listener: any = pluginInstance) {
if (listener && listener.off) {
listener.off()
}

View File

@ -1,6 +1,8 @@
import { server, MiaoScriptConsole, event, plugin } from "@ccms/api"
import { injectable, inject, postConstruct } from "@ccms/container"
import { server, MiaoScriptConsole, event, plugin, task, command } from "@ccms/api"
import { injectable, inject, postConstruct, Autowired } from "@ccms/container"
import { getPluginMetadata } from "./utils"
import { PluginEventManager } from "./event"
import { PluginCommandManager } from "./command"
const File = Java.type('java.io.File')
@ -11,6 +13,12 @@ export namespace interfaces {
public logger: MiaoScriptConsole
@inject(server.Console)
private Console: MiaoScriptConsole
@Autowired()
private taskManager: task.TaskManager
@Autowired()
private eventManager: PluginEventManager
@Autowired()
private commandManager: PluginCommandManager
constructor() {
this.description = getPluginMetadata(this)
@ -28,10 +36,42 @@ export namespace interfaces {
return dataFolder.getAbsolutePath()
}
public registryCommand(executor: any) { }
public unregistryCommand(executor: any) { }
public registryListener(listener: any) { }
public unregistryListener(listener: any) { }
/**
*
* @param executor
*/
public registryCommand(executor: any) {
this.commandManager.registryCommand(this, executor)
}
/**
*
* @param executor
*/
public unregistryCommand(executor: any) {
this.commandManager.unregistryCommand(this, executor)
}
/**
*
* @param listener
*/
public registryListener(listener: any) {
this.eventManager.registryListener(this, listener)
}
/**
*
* @param listener
*/
public unregistryListener(listener: any) {
this.eventManager.unregistryListener(this, listener)
}
/**
*
* @param func
* @returns
*/
public createTask(func: Function) {
return this.taskManager.create(func, this)
}
public load() { }
public enable() { }