feat: add chat api

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-07-28 09:39:15 +08:00
parent f301d130f9
commit 2990cdc4aa
3 changed files with 73 additions and 52 deletions

19
packages/api/src/chat.ts Normal file
View File

@ -0,0 +1,19 @@
export namespace chat {
export abstract class Chat {
sendMessage(sender: any, message: string) {
throw new Error("Method not implemented.")
}
sendActionBar(sender: any, message: string) {
throw new Error("Method not implemented.")
}
clearActionBar(sender: any) {
this.sendActionBar(sender, '')
}
sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 1, time: number = 5, fadeOut: number = 1) {
throw new Error("Method not implemented.")
}
clearTitle(sender: any) {
this.sendTitle(sender, '', '', 0, 0, 0)
}
}
}

View File

@ -4,7 +4,7 @@
import i18n from '@ccms/i18n' import i18n from '@ccms/i18n'
import { injectable, unmanaged } from '@ccms/container' import { injectable, unmanaged } from '@ccms/container'
const Thread = Java.type('java.lang.Thread'); const Thread = Java.type('java.lang.Thread')
export namespace event { export namespace event {
/** /**
@ -30,7 +30,7 @@ export namespace event {
protected baseEventDir = ''; protected baseEventDir = '';
constructor(@unmanaged() baseEventDir: string) { constructor(@unmanaged() baseEventDir: string) {
this.baseEventDir = baseEventDir; this.baseEventDir = baseEventDir
} }
/** /**
@ -39,74 +39,74 @@ export namespace event {
* org.spongepowered.api.event.game.GameRegistryEvent.Register => gameregistryevent$register * org.spongepowered.api.event.game.GameRegistryEvent.Register => gameregistryevent$register
*/ */
mapEventName() { mapEventName() {
if (this.baseEventDir === "") { throw new Error(i18n.translate('ms.api.event.empty.event.dir')); } if (this.baseEventDir === "") { throw new Error(i18n.translate('ms.api.event.empty.event.dir')) }
let count = 0; let count = 0
let jar = this.getJarFile(this.baseEventDir); let jar = this.getJarFile(this.baseEventDir)
let entries = jar.entries(); let entries = jar.entries()
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
let entry = entries.nextElement(); let entry = entries.nextElement()
let name = entry.name; let name = entry.name
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) { if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
// replace name to qualifiedName // replace name to qualifiedName
let qualifiedName = name.replaceAll('/', '.'); let qualifiedName = name.replaceAll('/', '.')
try { try {
let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6)); let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6))
if (this.isValidEvent(clazz)) { if (this.isValidEvent(clazz)) {
let simpleName = this.class2Name(clazz).toLowerCase(); let simpleName = this.class2Name(clazz).toLowerCase()
console.trace(i18n.translate("ms.api.event.mapping", { canonicalName: clazz.canonicalName, simpleName })); console.trace(i18n.translate("ms.api.event.mapping", { canonicalName: clazz.canonicalName, simpleName }))
this.mapEvent[simpleName] = clazz; this.mapEvent[simpleName] = clazz
count++; count++
} }
} catch (ex) { } catch (ex) {
//ignore already loaded class //ignore already loaded class
} }
} }
} }
return count; return count
} }
getJarFile(resource: string, loader?: any) { getJarFile(resource: string, loader?: any) {
let dirs = (loader || Thread.currentThread().getContextClassLoader()).getResources(resource); let dirs = (loader || Thread.currentThread().getContextClassLoader()).getResources(resource)
if (dirs.hasMoreElements()) { if (dirs.hasMoreElements()) {
let url = dirs.nextElement(); let url = dirs.nextElement()
if (url.protocol === "jar") { return url.openConnection().jarFile; } if (url.protocol === "jar") { return url.openConnection().jarFile }
} }
throw new Error(i18n.translate("ms.api.event.resource.not.found", { resource })) throw new Error(i18n.translate("ms.api.event.resource.not.found", { resource }))
} }
class2Name(clazz: any) { class2Name(clazz: any) {
return clazz.simpleName; return clazz.simpleName
} }
name2Class(name: any, event: string) { name2Class(name: any, event: string) {
let eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event']; let eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event']
if (!eventCls) { if (!eventCls) {
try { try {
eventCls = base.getClass(eventCls); eventCls = base.getClass(eventCls)
this.mapEvent[event] = eventCls; this.mapEvent[event] = eventCls
} catch (ex) { } catch (ex) {
console.i18n("ms.api.event.not.found", { name, event }) console.i18n("ms.api.event.not.found", { name, event })
return; return
} }
} }
return eventCls; return eventCls
} }
execute(name, exec, eventCls) { execute(name, exec, eventCls) {
return (...args: any[]) => { return (...args: any[]) => {
try { try {
let event = args[args.length - 1]; let event = args[args.length - 1]
if (eventCls.isAssignableFrom(event.getClass())) { if (eventCls.isAssignableFrom(event.getClass())) {
let time = Date.now() let time = Date.now()
exec(event); exec(event)
let cost = Date.now() - time; let cost = Date.now() - time
if (cost > 20) { if (cost > 20) {
console.i18n("ms.api.event.execute.slow", { name, event: this.class2Name(eventCls), cost }) console.i18n("ms.api.event.execute.slow", { name, event: this.class2Name(eventCls), cost })
} }
} }
} catch (ex) { } catch (ex) {
console.i18n("ms.api.event.execute.error", { name, event: this.class2Name(eventCls), ex }) console.i18n("ms.api.event.execute.error", { name, event: this.class2Name(eventCls), ex })
console.ex(ex); console.ex(ex)
} }
} }
} }
@ -120,34 +120,34 @@ export namespace event {
* @param ignoreCancel * @param ignoreCancel
*/ */
listen(plugin: any, event: string, exec: (event: any) => void, priority: EventPriority = EventPriority.NORMAL, ignoreCancel = false) { listen(plugin: any, event: string, exec: (event: any) => void, priority: EventPriority = EventPriority.NORMAL, ignoreCancel = false) {
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError(i18n.translate("ms.api.event.listen.plugin.name.empty")); if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError(i18n.translate("ms.api.event.listen.plugin.name.empty"))
var name = plugin.description.name; var name = plugin.description.name
var eventCls = this.name2Class(name, event); var eventCls = this.name2Class(name, event)
if (!eventCls) { return; } if (!eventCls) { return }
if (typeof priority === 'boolean') { if (typeof priority === 'boolean') {
ignoreCancel = priority; ignoreCancel = priority
priority = EventPriority.NORMAL; priority = EventPriority.NORMAL
} }
priority = priority || EventPriority.NORMAL; priority = priority || EventPriority.NORMAL
ignoreCancel = ignoreCancel || false; ignoreCancel = ignoreCancel || false
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
var listener = this.register(eventCls, this.execute(name, exec, eventCls), priority, ignoreCancel); var listener = this.register(eventCls, this.execute(name, exec, eventCls), priority, ignoreCancel)
var listenerMap = this.listenerMap; var listenerMap = this.listenerMap
// add to cache Be used for close plugin to close event // add to cache Be used for close plugin to close event
if (!listenerMap[name]) listenerMap[name] = []; if (!listenerMap[name]) listenerMap[name] = []
var offExec = () => { var offExec = () => {
this.unregister(eventCls, listener); this.unregister(eventCls, listener)
console.debug(i18n.translate("ms.api.event.unregister", { name, event: this.class2Name(eventCls) })); console.debug(i18n.translate("ms.api.event.unregister", { name, event: this.class2Name(eventCls), exec: exec.name || '[anonymous]' }))
}; }
var off = { var off = {
event: eventCls, event: eventCls,
listener: listener, listener: listener,
off: offExec off: offExec
}; }
listenerMap[name].push(off); listenerMap[name].push(off)
// noinspection JSUnresolvedVariable // noinspection JSUnresolvedVariable
console.debug(i18n.translate("ms.api.event.register", { name, event: this.class2Name(eventCls) })); console.debug(i18n.translate("ms.api.event.register", { name, event: this.class2Name(eventCls), exec: exec.name || '[anonymous]' }))
return off; return off
} }
/** /**
@ -155,10 +155,10 @@ export namespace event {
* @param plugin * @param plugin
*/ */
disable(plugin: any) { disable(plugin: any) {
var eventCache = this.listenerMap[plugin.description.name]; var eventCache = this.listenerMap[plugin.description.name]
if (eventCache) { if (eventCache) {
eventCache.forEach(t => t.off()); eventCache.forEach(t => t.off())
delete this.listenerMap[plugin.description.name]; delete this.listenerMap[plugin.description.name]
} }
} }
@ -166,7 +166,7 @@ export namespace event {
* *
* @param clazz * @param clazz
*/ */
abstract isValidEvent(clazz: any): boolean; abstract isValidEvent(clazz: any): boolean
/** /**
* *
* @param eventCls * @param eventCls
@ -174,12 +174,12 @@ export namespace event {
* @param priority * @param priority
* @param ignoreCancel * @param ignoreCancel
*/ */
abstract register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean): any; abstract register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean): any
/** /**
* *
* @param event * @param event
* @param listener * @param listener
*/ */
abstract unregister(event: any, listener: any): void; abstract unregister(event: any, listener: any): void
} }
} }

View File

@ -1,4 +1,6 @@
import "@ccms/nashorn" import "@ccms/nashorn"
export * from './chat'
export * from './task' export * from './task'
export * from './event' export * from './event'
export * from './console' export * from './console'