feat: complate server & update spring

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-06-02 17:50:47 +08:00
parent 7e1111470c
commit 16fcbfa69c
9 changed files with 230 additions and 126 deletions

View File

@ -3,21 +3,25 @@ import '@ccms/nashorn'
import { command, plugin } from '@ccms/api'
import { inject, provideSingleton, postConstruct } from '@ccms/container'
import { CommandMap } from './internal/command'
@provideSingleton(command.Command)
export class SpringCommand extends command.Command {
@inject(plugin.PluginInstance)
private pluginInstance: any
@inject(CommandMap)
private commandMap: CommandMap = new CommandMap()
protected create(plugin: any, command: string) {
console.console('§4Spring暂不支持create命令!')
return this.commandMap.register(plugin, command)
}
protected remove(plugin: any, command: string) {
console.console('§4Spring暂不支持remove命令!')
this.commandMap.unregister(plugin, command)
}
protected onCommand(plugin: any, command: any, executor: Function) {
console.console('§4Spring暂不支持onCommand!')
command.setExecutor(super.setExecutor(plugin, command, executor))
}
protected onTabComplete(plugin: any, command: any, tabCompleter: Function) {
console.console('§4Spring暂不支持onTabComplete!')
command.setTabCompleter(super.setExecutor(plugin, command, tabCompleter))
}
}

View File

@ -1,5 +1,6 @@
import { server, plugin } from '@ccms/api'
import { server } from '@ccms/api'
import { Container } from '@ccms/container'
import '@ccms/database'
import { SpringConsole } from './console';
import './event';
@ -7,8 +8,6 @@ import './server';
import './command';
import './task';
const BeanKit = Java.type('com.sixi.micro.common.kits.BeanKit')
export default function SpringImpl(container: Container) {
container.bind(server.Console).toConstantValue(SpringConsole)
}

View File

@ -0,0 +1,59 @@
import { provideSingleton } from "@ccms/container"
import { plugin } from "@ccms/api"
type CommandExec = (sender: any, _: any, command: string, args: string[]) => boolean
type TabCompleter = (sender: any, _: any, command: string, args: string[]) => string[]
type CommandStore = { [key: string]: SpringCommand }
@provideSingleton(CommandMap)
export class CommandMap {
private commands: CommandStore = {}
private pluginCommands: { [key: string]: CommandStore } = {}
register(plugin: plugin.Plugin, command: string) {
let springCommand = new SpringCommand(plugin, command)
this.commands[command] = springCommand
if (!this.pluginCommands[plugin.description.name]) { this.pluginCommands[plugin.description.name] = {} }
this.pluginCommands[plugin.description.name][command] = springCommand
return springCommand
}
unregister(plugin: plugin.Plugin, command: string) {
delete this.commands[command]
delete this.pluginCommands[plugin.description.name][command]
}
dispatch(sender: any, command: string, args: string[]): boolean {
if (command === "help") {
sender.sendMessage('§e--------- §rHelp: Index §e---------------------------')
sender.sendMessage('Use /help [n] to get page n of help.')
for (const cmdName of Object.getOwnPropertyNames(this.commands)) {
sender.sendMessage(`§6/${cmdName}: §rA command provided by plugin §b${this.commands[cmdName].plugin.description.name}§r.`)
}
return
}
let exists = this.commands[command]
if (exists) {
return exists.executor(sender, '', command, Java.to(args))
} else {
sender.sendMessage && sender.sendMessage(`Unknown command. Type "/help" for help.`)
return false
}
}
}
export class SpringCommand {
public plugin: plugin.Plugin
public name: string
public executor: CommandExec
public tabCompleter: TabCompleter
constructor(plugin: plugin.Plugin, command: string, description: string = '暂无描述!') {
this.plugin = plugin
this.name = command
}
setExecutor = (executor: CommandExec) => this.executor = executor
setTabCompleter = (tabCompleter: TabCompleter) => this.tabCompleter = tabCompleter
toString = () => `SpringCommand(${this.name})`
}

View File

@ -1,50 +1,55 @@
import { server } from '@ccms/api'
import { provideSingleton } from '@ccms/container';
import { NativePluginManager } from '@ccms/api/dist/interfaces/server/native_plugin';
import { provideSingleton, inject } from '@ccms/container'
import { NativePluginManager } from '@ccms/api/dist/interfaces/server/native_plugin'
import { CommandMap } from './internal/command'
@provideSingleton(server.Server)
export class SpringServer implements server.Server {
@inject(CommandMap)
private commandMap: CommandMap
constructor() {
}
getVersion(): string {
return "SpringFramework"
}
getPlayer(name: string) {
throw new Error("Method not implemented.");
throw new Error("Method not implemented.")
}
getOnlinePlayers(): any[] {
throw new Error("Method not implemented.");
throw new Error("Method not implemented.")
}
getConsoleSender() {
return undefined;
return {
sendMessage: (message: string) => console.console(message)
}
}
getService(service: string) {
throw new Error("Method not implemented.");
throw new Error("Method not implemented.")
}
dispatchCommand(sender: any, command: string): boolean {
console.console('§4Spring暂不支持dispatchCommand!')
return false;
let cmd_args = command.split(" ")
return this.commandMap.dispatch(sender, cmd_args.shift(), cmd_args || [])
}
dispatchConsoleCommand(command: string): boolean {
console.console('§4Spring暂不支持dispatchConsoleCommand!')
return false;
return this.dispatchCommand(this.getConsoleSender(), command)
}
getPluginsFolder(): string {
throw new Error("Method not implemented.");
throw new Error("Method not implemented.")
}
getNativePluginManager(): NativePluginManager {
throw new Error("Method not implemented.");
throw new Error("Method not implemented.")
}
getNettyPipeline() {
return base.getInstance().getAutowireCapableBeanFactory()
}
getRootLogger() {
return global.logger
return Packages.org.slf4j.LoggerFactory.getLogger("root") || global.logger
}
sendJson(sender: any, json: string | object): void {
throw new Error("Method not implemented.");
throw new Error("Method not implemented.")
}
tabComplete(sender: any, input: string, index?: number) {
throw new Error("Method not implemented.");
throw new Error("Method not implemented.")
}
}

View File

@ -34,7 +34,7 @@ export class SpringTaskManager implements task.TaskManager {
return func()
}
disable() {
Object.values(tasks).forEach((task) => task.cancel())
Object.values(tasks).forEach((task) => task?.cancel())
executor.shutdown();
}
}
@ -43,7 +43,7 @@ export class SpringTask extends task.Task {
public id = taskId++
private running = new AtomicBoolean(true)
run() {
run(...args: any[]) {
if (this.laterTime > 0) {
try {
Thread.sleep(this.laterTime)
@ -53,7 +53,7 @@ export class SpringTask extends task.Task {
}
while (this.running.get()) {
try {
this.func()
this.func(...args)
} catch (t) {
console.error("Task exec error:", t)
console.ex(t)
@ -72,13 +72,13 @@ export class SpringTask extends task.Task {
cancel(): any {
var wasRunning = this.running.getAndSet(false)
if (wasRunning) {
tasks[this.id] = undefined
delete tasks[this.id]
}
}
submit(): task.Cancelable {
submit(...args: any[]): task.Cancelable {
tasks[this.id] = this
executor.execute(this.run.bind(this))
executor.execute(() => this.run(...args))
return {
cancel: () => {
return this.cancel()