feat: complate bukkit comsole and command

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-10 17:21:27 +08:00
parent eb6a2a32db
commit 7cbaf3f42b
3 changed files with 22 additions and 24 deletions

View File

@ -28,6 +28,7 @@
}, },
"dependencies": { "dependencies": {
"@ms/api": "^0.0.0", "@ms/api": "^0.0.0",
"inversify": "^5.0.1" "@ms/common": "^0.0.0",
"@ms/container": "^0.0.0"
} }
} }

View File

@ -1,26 +1,24 @@
import '@ms/nashorn' import '@ms/nashorn'
import { injectable, postConstruct, inject } from 'inversify' import { command, plugin } from '@ms/api'
import { command } from '@ms/api' import * as reflect from '@ms/common/dist/reflect'
import * as ref from '@ms/common/dist/reflect' import { injectable, postConstruct, inject } from '@ms/container'
var bukkit = require('./server'); let Bukkit = Java.type("org.bukkit.Bukkit");
var plugin = bukkit.plugin.self; let Arrays = Java.type('java.util.Arrays');
var commandMap = ref.on(bukkit.plugin.manager).get('commandMap').get(); let TabCompleter = Java.type('org.bukkit.command.TabCompleter');
var PluginCommand = Java.type('org.bukkit.command.PluginCommand'); let PluginCommand = Java.type('org.bukkit.command.PluginCommand');
let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor');
var Arrays = Java.type('java.util.Arrays');
var TabCompleter = Java.type("org.bukkit.command.TabCompleter");
var CommandExecutor = Java.type("org.bukkit.command.CommandExecutor");
@injectable() @injectable()
export class BukkitCommand extends command.Command { export class BukkitCommand extends command.Command {
@inject("Plugin.Self") @inject(plugin.PluginInstance)
private plugin: any private pluginInstance: any
private commandMap: any;
@postConstruct() @postConstruct()
init() { init() {
this.commandMap = reflect.on(Bukkit.pluginManager).get('commandMap').get();
} }
enable(jsp) { enable(jsp) {
@ -28,23 +26,19 @@ export class BukkitCommand extends command.Command {
if (commands) { if (commands) {
var pluginCommands = []; var pluginCommands = [];
for (var name in commands) { for (var name in commands) {
// noinspection JSUnfilteredForInLoop
var command = commands[name]; var command = commands[name];
if (typeof command !== 'object') continue; if (typeof command !== 'object') continue;
command.name = name; command.name = name;
// noinspection JSUnfilteredForInLoop
var newCmd = this.create(jsp, command); var newCmd = this.create(jsp, command);
if (command.description) newCmd.setDescription(command.description); if (command.description) newCmd.setDescription(command.description);
if (command.usage) newCmd.setUsage(command.usage); if (command.usage) newCmd.setUsage(command.usage);
/** @namespace command.aliases */
if (command.aliases) newCmd.setAliases(Arrays.asList(command.aliases)); if (command.aliases) newCmd.setAliases(Arrays.asList(command.aliases));
if (command.permission) newCmd.setPermission(command.permission); if (command.permission) newCmd.setPermission(command.permission);
if (command['permission-message']) newCmd.setPermissionMessage(command['permission-message']); if (command['permission-message']) newCmd.setPermissionMessage(command['permission-message']);
pluginCommands.push(newCmd); pluginCommands.push(newCmd);
// noinspection JSUnfilteredForInLoop
console.debug(`插件 ${jsp.description.name} 注册命令 ${name} ...`); console.debug(`插件 ${jsp.description.name} 注册命令 ${name} ...`);
} }
commandMap.registerAll(jsp.description.name, Arrays.asList(pluginCommands)); this.commandMap.registerAll(jsp.description.name, Arrays.asList(pluginCommands));
} }
} }
@ -58,10 +52,10 @@ export class BukkitCommand extends command.Command {
} }
create(jsp, command) { create(jsp, command) {
var cmd = commandMap.getCommand(command.name) var cmd = this.commandMap.getCommand(command.name)
if (cmd && cmd instanceof PluginCommand) { return cmd }; if (cmd && cmd instanceof PluginCommand) { return cmd };
cmd = ref.on(PluginCommand).create(command.name, plugin).get(); cmd = reflect.on(PluginCommand).create(command.name, this.pluginInstance).get();
commandMap.register(jsp.description.name, cmd); this.commandMap.register(jsp.description.name, cmd);
return cmd; return cmd;
} }

View File

@ -1,7 +1,8 @@
import { server, plugin } from '@ms/api' import { server, plugin, command } from '@ms/api'
import { DefaultContainer as container } from '@ms/container' import { DefaultContainer as container } from '@ms/container'
import { BukkitConsole } from './console' import { BukkitConsole } from './console'
import { BukkitCommand } from './command';
let BukkitServerType = 'bukkit'; let BukkitServerType = 'bukkit';
let Bukkit = Java.type("org.bukkit.Bukkit"); let Bukkit = Java.type("org.bukkit.Bukkit");
@ -9,3 +10,5 @@ let Bukkit = Java.type("org.bukkit.Bukkit");
container.bind(server.Console).toConstantValue(BukkitConsole); container.bind(server.Console).toConstantValue(BukkitConsole);
container.bind(server.ServerType).toConstantValue(BukkitServerType); container.bind(server.ServerType).toConstantValue(BukkitServerType);
container.bind(plugin.PluginInstance).toConstantValue(Bukkit.pluginManager.getPlugin('MiaoScript')); container.bind(plugin.PluginInstance).toConstantValue(Bukkit.pluginManager.getPlugin('MiaoScript'));
container.bind(command.Command).to(BukkitCommand).inSingletonScope();