refactor: chat & command tabComplete

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-09-22 15:29:12 +08:00
parent 9be9c400ad
commit e9a4f2f351
10 changed files with 85 additions and 75 deletions

View File

@ -3,6 +3,15 @@ import { injectable } from '@ccms/container'
export namespace chat {
@injectable()
export abstract class Chat {
/**
* sendJsonChat
* @param sender reciver
* @param json json
* @param type chat Type 0: chat 1: system 2: actionBar
*/
sendJson(sender: any, json: string | object, type = 0) {
throw new Error("Method not implemented.")
}
sendMessage(sender: any, message: string) {
throw new Error("Method not implemented.")
}

View File

@ -24,6 +24,9 @@ export namespace command {
this.onTabComplete(plugin, cmd, exec.tab)
}
}
public tabComplete(sender: any, input: string, index?: number): string[] {
throw new Error("Method not implemented.")
}
/**
*
* @param plugin

View File

@ -4,6 +4,8 @@ import { injectable, inject } from '@ccms/container'
import { NativePluginManager } from './native_plugin'
import { constants } from '../../constants'
export { NativePluginManager } from './native_plugin'
export namespace server {
/**
* Runtime ServerType
@ -13,10 +15,6 @@ export namespace server {
* Runtime Console
*/
export const Console = Symbol("Console")
/**
* MiaoScript Server
*/
export const Server = Symbol("Server")
/**
* Runtime Server Instance
*/
@ -24,21 +22,19 @@ export namespace server {
/**
* MiaoScript Server
*/
export interface Server {
getVersion(): string
getPlayer(name: string): any
getOnlinePlayers(): any[]
getConsoleSender(): any
getService(service: string): any
dispatchCommand(sender: string | any, command: string): boolean
dispatchConsoleCommand(command: string): boolean
getPluginsFolder(): string
getNativePluginManager(): NativePluginManager
getDedicatedServer?(): any
getNettyPipeline(): any
getRootLogger(): any
sendJson(sender: string | any, json: object | string): void
tabComplete?(sender: string | any, input: string, index?: number): string[]
export abstract class Server {
abstract getVersion(): string
abstract getPlayer(name: string): any
abstract getOnlinePlayers(): any[]
abstract getConsoleSender(): any
abstract getService(service: string): any
abstract dispatchCommand(sender: string | any, command: string): boolean
abstract dispatchConsoleCommand(command: string): boolean
abstract getPluginsFolder(): string
abstract getNativePluginManager(): NativePluginManager
abstract getDedicatedServer?(): any
abstract getNettyPipeline(): any
abstract getRootLogger(): any
}
@injectable()
export class ServerChecker {
@ -102,12 +98,6 @@ export namespace server {
getRootLogger() {
throw new Error("Method not implemented.")
}
sendJson(sender: any, json: string | object): void {
throw new Error("Method not implemented.")
}
tabComplete?(sender: any, input: string, index?: number): string[] {
throw new Error("Method not implemented.")
}
protected reflect() {
try {
let consoleServer = this.getDedicatedServer()

View File

@ -4,11 +4,14 @@ import bukkitChat from './enhance/chat'
@provideSingleton(chat.Chat)
export class BukkitChat extends chat.Chat {
sendJson(sender: any, json: string | object, type = 0) {
bukkitChat.send(sender, typeof json === "string" ? json : JSON.stringify(json), type)
}
sendMessage(sender: any, message: string) {
bukkitChat.send(sender, JSON.stringify({ text: message }), 0)
this.sendJson(sender, { text: message }, 0)
}
sendActionBar(sender: any, message: string) {
bukkitChat.send(sender, JSON.stringify({ text: message }), 2)
this.sendJson(sender, { text: message }, 2)
}
sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 20, time: number = 100, fadeOut: number = 20) {
sender.sendTitle(title, subtitle, fadeIn, time, fadeOut)

View File

@ -4,44 +4,47 @@ import { command, plugin } from '@ccms/api'
import * as reflect from '@ccms/common/dist/reflect'
import { provideSingleton, postConstruct, inject } from '@ccms/container'
let Bukkit = org.bukkit.Bukkit;
let TabCompleter = Java.type('org.bukkit.command.TabCompleter');
let PluginCommand = Java.type('org.bukkit.command.PluginCommand');
let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor');
let Bukkit = org.bukkit.Bukkit
let TabCompleter = Java.type('org.bukkit.command.TabCompleter')
let PluginCommand = Java.type('org.bukkit.command.PluginCommand')
let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor')
@provideSingleton(command.Command)
export class BukkitCommand extends command.Command {
@inject(plugin.PluginInstance)
private pluginInstance: any
private commandMap: any;
private commandMap: any
@postConstruct()
init() {
this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get();
this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get()
}
create(plugin: any, command: string) {
var cmd = this.commandMap.getCommand(command)
if (cmd && cmd instanceof PluginCommand) { return cmd };
cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get();
this.commandMap.register(plugin.description.name, cmd);
return cmd;
cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get()
this.commandMap.register(plugin.description.name, cmd)
return cmd
}
remove(plugin: any, command: string) {
var cmd = this.commandMap.getCommand(command)
if (cmd && cmd instanceof PluginCommand) {
cmd.unregister(this.commandMap);
cmd.unregister(this.commandMap)
}
}
tabComplete(sender: any, input: string, index?: number): string[] {
return Java.from(this.commandMap.tabComplete(sender, input))
}
onCommand(plugin: any, command: any, executor: Function) {
// 必须指定需要实现的接口类型 否则MOD服会报错
command.setExecutor(new CommandExecutor({
onCommand: super.setExecutor(plugin, command, executor)
}));
}))
}
onTabComplete(plugin: any, command: any, tabCompleter: Function) {
// 必须指定需要实现的接口类型 否则MOD服会报错
command.setTabCompleter(new TabCompleter({
onTabComplete: super.setTabCompleter(plugin, command, tabCompleter)
}));
}))
}
}

View File

@ -54,16 +54,4 @@ export class BukkitServer extends server.ReflectServer {
getRootLogger() {
return this.rootLogger
}
sendJson(sender: string | any, json: object | string): void {
if (typeof sender === "string") {
sender = this.getPlayer(sender)
}
let result = chat.json(sender, typeof json == "string" ? json : JSON.stringify(json))
if (result !== false) {
this.dispatchConsoleCommand(result)
}
}
tabComplete?(sender: any, input: string, index?: number): string[] {
throw new Error("Method not implemented.")
}
}

View File

@ -3,29 +3,29 @@ import { provideSingleton, inject, postConstruct } from '@ccms/container'
import * as reflect from '@ccms/common/dist/reflect'
let Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy();
let Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
@provideSingleton(server.Server)
export class BungeeServer implements server.Server {
private pluginsFolder: string;
private pipeline: any;
private rootLogger: any;
private pluginsFolder: string
private pipeline: any
private rootLogger: any
@inject(task.TaskManager)
private task: task.TaskManager
constructor() {
this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath();
this.pluginsFolder = Bungee.getPluginsFolder().getCanonicalPath()
}
@postConstruct()
initialize() {
let count = 0;
let count = 0
let wait = this.task.create(() => {
try {
// @ts-ignore
this.pipeline = reflect.on(base.getInstance().getProxy()).get('listeners').get().toArray()[0].pipeline()
wait.cancel();
wait.cancel()
} catch (ex) {
count++
if (count > 50) {
@ -44,7 +44,7 @@ export class BungeeServer implements server.Server {
}
getPlayer(name: string) {
return Bungee.getPlayer(name);
return Bungee.getPlayer(name)
}
getVersion(): string {
return Bungee.getVersion()
@ -56,7 +56,7 @@ export class BungeeServer implements server.Server {
return Bungee.getConsole()
}
getService(service: string) {
throw new Error("Method not implemented.");
throw new Error("Method not implemented.")
}
dispatchCommand(sender: string | any, command: string): boolean {
if (typeof sender === 'string') {
@ -68,18 +68,15 @@ export class BungeeServer implements server.Server {
return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command)
}
getPluginsFolder(): string {
return this.pluginsFolder;
return this.pluginsFolder
}
getNativePluginManager() {
return Bungee.getPluginManager() as any
}
getNettyPipeline() {
return this.pipeline;
return this.pipeline
}
getRootLogger() {
return this.rootLogger;
}
sendJson(sender: string | any, json: string): void {
throw new Error("Method not implemented.");
return this.rootLogger
}
}

View File

@ -0,0 +1,22 @@
import { chat } from '@ccms/api'
import { provideSingleton } from '@ccms/container'
const Sponge = org.spongepowered.api.Sponge
const ChatTypes = org.spongepowered.api.text.chat.ChatTypes
const TextSerializers = org.spongepowered.api.text.serializer.TextSerializers
@provideSingleton(chat.Chat)
export class SpongeChat extends chat.Chat {
sendJson(sender: any, json: string | object) {
sender.sendMessage(TextSerializers.JSON.deserialize(typeof json === "string" ? json : JSON.stringify(json)))
}
sendMessage(sender: any, message: string) {
sender.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(message))
}
sendActionBar(sender: any, message: string) {
sender.sendMessage(ChatTypes.ACTION_BAR, TextSerializers.FORMATTING_CODE.deserialize(message))
}
sendTitle(sender: any, title: string, subtitle: string = '', fadeIn: number = 20, time: number = 100, fadeOut: number = 20) {
sender.sendTitle(title, subtitle, fadeIn, time, fadeOut)
}
}

View File

@ -13,6 +13,9 @@ export class SpringCommand extends command.Command {
protected create(plugin: any, command: string) {
return this.commandMap.register(plugin, command)
}
public tabComplete(sender: any, input: string, index?: number): string[] {
return this.commandMap.tabComplate(sender, input, index)
}
protected remove(plugin: any, command: string) {
this.commandMap.unregister(plugin, command)
}

View File

@ -1,6 +1,6 @@
import { server } from '@ccms/api'
import { provideSingleton, inject } from '@ccms/container'
import { NativePluginManager } from '@ccms/api/dist/interfaces/server/native_plugin'
import { NativePluginManager } from '@ccms/api'
import { CommandMap } from './internal/command'
@provideSingleton(server.Server)
@ -8,8 +8,6 @@ export class SpringServer implements server.Server {
@inject(CommandMap)
private commandMap: CommandMap
constructor() {
}
getVersion(): string {
return "SpringFramework"
}
@ -47,10 +45,4 @@ export class SpringServer implements server.Server {
getRootLogger() {
return Packages.org.slf4j.LoggerFactory.getLogger("root") || global.logger
}
sendJson(sender: any, json: string | object): void {
throw new Error("Method not implemented.")
}
tabComplete(sender: any, input: string, index?: number) {
return this.commandMap.tabComplate(sender, input, index)
}
}