feat: complate command event and task

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-21 15:04:47 +08:00
parent 500384326f
commit 5ea4b6bdaf
6 changed files with 166 additions and 5 deletions

View File

@ -0,0 +1,72 @@
import { command, plugin } from "@ms/api";
import { inject, injectable } from "@ms/container";
let Sponge = Java.type('org.spongepowered.api.Sponge');
var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable');
var CommandResult = Java.type('org.spongepowered.api.command.CommandResult');
let Text = Java.type('org.spongepowered.api.text.Text');
var Optional = Java.type('java.util.Optional');
class SimpleCommandCallable {
public callable: any;
private executor: Function;
private tabCompleter: Function;
constructor(command: string, description: string = '暂无描述!') {
this.callable = new CommandCallable({
//CommandResult process(CommandSource source, String arguments) throws CommandException;
process: (sender: any, args) => {
return this.executor(sender, command, args.length === 0 ? [] : args.split(" ").filter(e => e)) ? CommandResult.success() : CommandResult.empty();
},
//List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException;
getSuggestions: (sender: any, args, target) => {
return this.tabCompleter(sender, command, args.length === 0 ? [] : args.split(" ").filter(e => e));
},
//boolean testPermission(CommandSource source);
testPermission: () => {
return true;
},
//Optional<Text> getShortDescription(CommandSource source);
getShortDescription: () => {
return Optional.of(Text.of(description));
},
//Optional<Text> getHelp(CommandSource source);
getHelp: () => {
return Optional.of(Text.of(""));
},
//Text getUsage(CommandSource source);
getUsage: () => {
return Text.of('');
}
})
}
setExecutor = (executor: Function) => this.executor = executor;
setTabCompleter = (tabCompleter: Function) => this.tabCompleter = tabCompleter;
}
@injectable()
export class SpongeCommand extends command.Command {
@inject(plugin.PluginInstance)
private pluginInstance: any
private commandMap: any[] = [];
create(plugin: any, command: string) {
var commandKey = plugin.description.name.toLowerCase() + ":" + command;
if (!this.commandMap[commandKey]) {
this.commandMap[commandKey] = new SimpleCommandCallable(command);
Sponge.getCommandManager().register(this.pluginInstance, this.commandMap[commandKey].callable, command, commandKey);
}
return this.commandMap[commandKey];
}
onCommand(plugin: any, command: any, executor: Function) {
command.setExecutor(super.setExecutor(plugin, command, executor));
}
onTabComplete(plugin: any, command: any, tabCompleter: Function) {
command.setTabCompleter(super.setTabCompleter(plugin, command, tabCompleter));
}
}

View File

@ -11,7 +11,7 @@ export class SpongeConsole extends MiaoScriptConsole {
this.error('第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!')
return
}
if (args[0].toString() === '[object Array]') {
if (Object.prototype.toString.call(args[0]) === '[object Array]') {
args[0].forEach(line => sender.sendMessage(Text.of(this.prefix + line)))
} else {
sender.sendMessage(Text.of(this.prefix + args.join(' ')));

View File

@ -0,0 +1,52 @@
import { event, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container';
let Modifier = Java.type("java.lang.reflect.Modifier");
let Order = Java.type("org.spongepowered.api.event.Order");
let Event = Java.type("org.spongepowered.api.event.Event");
let EventListener = Java.type("org.spongepowered.api.event.EventListener");
let Sponge = Java.type("org.spongepowered.api.Sponge");
let priorityMap = {
'LOWEST': 'PRE',
'LOW': 'FIRST',
'NORMAL': 'DEFAULT',
'HIGH': 'LATE',
'HIGHEST': 'LAST',
'MONITOR': 'POST'
};
/**
* Sponge Event Impl
*/
@injectable()
export class SpongeEvent extends event.Event {
@inject(plugin.PluginInstance)
private pluginInstance: any;
constructor() {
super('org/spongepowered/api/event');
}
isValidEvent(clazz: any): boolean {
//继承于 org.spongepowered.api.event.Event
return Event.class.isAssignableFrom(clazz) &&
// 访问符为Public
Modifier.isPublic(clazz.getModifiers()) &&
// Sponge的事件都是接口
Modifier.isAbstract(clazz.getModifiers());
}
class2Name(clazz: any) {
return clazz.canonicalName.substring(clazz.name.lastIndexOf(".") + 1);
}
register(eventCls: any, exec: Function, priority: any = 'NORMAL', ignoreCancel: boolean = true) {
var listener = new EventListener({
handle: exec
});
Sponge.getEventManager().registerListener(this.pluginInstance, eventCls, Order[priorityMap[priority]], listener);
return listener;
}
unregister(event: any, listener: any): void {
Sponge.getEventManager().unregisterListeners(listener);
}
}

View File

@ -1,13 +1,22 @@
import { server, plugin } from '@ms/api'
import './typings'
import { server, plugin, command, event, task } from '@ms/api'
import { DefaultContainer as container } from '@ms/container'
import { SpongeConsole } from './console'
import { SpongeConsole } from './console';
import { SpongeEvent } from './event';
import { SpongeCommand } from './command';
import { SpongeTaskManager } from './task';
let SpongeServerType = 'sponge';
let Sponge = Java.type("org.spongepowered.api.Sponge");
console.debug(`Detect Sponge Compatible set ServerType to ${SpongeServerType} ...`)
container.bind(server.Console).toConstantValue(SpongeConsole);
container.bind(server.ServerType).toConstantValue(SpongeServerType);
container.bind(plugin.PluginInstance).toConstantValue(Sponge.pluginManager.getPlugin('MiaoScript').orElse(null));
container.bind(event.Event).to(SpongeEvent).inSingletonScope();
container.bind(command.Command).to(SpongeCommand).inSingletonScope();
container.bind(task.TaskManager).to(SpongeTaskManager).inSingletonScope();
console.debug(`Detect Sponge Compatible set ServerType to ${SpongeServerType} ...`)

View File

@ -0,0 +1,28 @@
import { task, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container';
var Consumer = Java.type('java.util.function.Consumer');
var Task = Java.type("org.spongepowered.api.scheduler.Task");
@injectable()
export class SpongeTaskManager implements task.TaskManager {
@inject(plugin.PluginInstance)
private pluginInstance: any;
create(func: Function): task.Task {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return new SpongeTask(this.pluginInstance, func);
}
}
export class SpongeTask extends task.Task {
submit(): task.Cancelable {
let run = Task.builder().execute(new Consumer({
accept: () => this.run()
}));
if (this.isAsync) { run.async() }
if (this.laterTime) { run.delayTicks(this.laterTime) }
if (this.interval) { run.intervalTicks(this.interval) }
return run.submit(this.plugin)
}
}

View File