feat: use inversify autoProvide inject

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
2020-02-26 10:15:33 +08:00
parent 7e4c44eadd
commit 3143851969
26 changed files with 128 additions and 153 deletions

View File

@ -1,12 +1,12 @@
import { channel, plugin } from '@ms/api'
import { inject, injectable } from '@ms/container'
import { inject, provideSingleton } from '@ms/container'
const Sponge = org.spongepowered.api.Sponge
const RawDataListener = Java.type("org.spongepowered.api.network.RawDataListener")
const ChannelRegistrar = Sponge.getChannelRegistrar()
const Consumer = Java.type("java.util.function.Consumer");
@injectable()
@provideSingleton(channel.Channel)
export class SpongeChannel extends channel.Channel {
@inject(plugin.PluginInstance)
private pluginInstance: any;

View File

@ -1,15 +1,43 @@
import { command, plugin } from "@ms/api";
import { inject, injectable } from "@ms/container";
import { inject, provideSingleton } from "@ms/container";
let Sponge = Java.type('org.spongepowered.api.Sponge');
let Text = Java.type('org.spongepowered.api.text.Text');
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');
@provideSingleton(command.Command)
export class SpongeCommand extends command.Command {
@inject(plugin.PluginInstance)
private pluginInstance: any
private commandMapping: any[] = [];
create(plugin: any, command: string) {
let commandKey = this.getCommandKey(plugin, command);
let commandCallable = new SimpleCommandCallable(command);
this.commandMapping[commandKey] = Sponge.getCommandManager().register(this.pluginInstance, commandCallable.callable, command, commandKey).orElse(null);
return commandCallable;
}
remove(plugin: any, command: string) {
var commandKey = this.getCommandKey(plugin, command);
if (this.commandMapping[commandKey]) {
Sponge.getCommandManager().removeMapping(this.commandMapping[commandKey]);
delete this.commandMapping[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));
}
private getCommandKey(plugin: any, command: string) {
return plugin.description.name.toLowerCase() + ":" + command;
}
}
class SimpleCommandCallable {
public callable: any;
private name: string;
@ -49,35 +77,4 @@ class SimpleCommandCallable {
setExecutor = (executor: Function) => this.executor = executor;
setTabCompleter = (tabCompleter: Function) => this.tabCompleter = tabCompleter;
toString = () => `Sponge SimpleCommandCallable(${this.name})`
}
@injectable()
export class SpongeCommand extends command.Command {
@inject(plugin.PluginInstance)
private pluginInstance: any
private commandMapping: any[] = [];
create(plugin: any, command: string) {
let commandKey = this.getCommandKey(plugin, command);
let commandCallable = new SimpleCommandCallable(command);
this.commandMapping[commandKey] = Sponge.getCommandManager().register(this.pluginInstance, commandCallable.callable, command, commandKey).orElse(null);
return commandCallable;
}
remove(plugin: any, command: string) {
var commandKey = this.getCommandKey(plugin, command);
if (this.commandMapping[commandKey]) {
Sponge.getCommandManager().removeMapping(this.commandMapping[commandKey]);
delete this.commandMapping[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));
}
private getCommandKey(plugin: any, command: string) {
return plugin.description.name.toLowerCase() + ":" + command;
}
}
}

View File

@ -1,5 +1,5 @@
import { event, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container';
import { inject, provideSingleton } from '@ms/container';
let Modifier = Java.type("java.lang.reflect.Modifier");
let Order = Java.type("org.spongepowered.api.event.Order");
@ -19,7 +19,7 @@ let priorityMap = {
/**
* Sponge Event Impl
*/
@injectable()
@provideSingleton(event.Event)
export class SpongeEvent extends event.Event {
@inject(plugin.PluginInstance)
private pluginInstance: any;

View File

@ -1,20 +1,15 @@
/// <reference types="@ms/types/dist/typings/sponge" />
import { server, command, event, channel, task } from '@ms/api'
import { server } from '@ms/api'
import { Container } from '@ms/container'
import { SpongeConsole } from './console';
import { SpongeEvent } from './event';
import { SpongeServer } from './server';
import { SpongeCommand } from './command';
import { SpongeChannel } from './channel';
import { SpongeTaskManager } from './task';
import './event';
import './server';
import './command';
import './channel';
import './task';
export default function SpongeImpl(container: Container) {
container.bind(server.Console).toConstantValue(SpongeConsole);
container.bind(event.Event).to(SpongeEvent).inSingletonScope();
container.bind(server.Server).to(SpongeServer).inSingletonScope();
container.bind(command.Command).to(SpongeCommand).inSingletonScope();
container.bind(channel.Channel).to(SpongeChannel).inSingletonScope();
container.bind(task.TaskManager).to(SpongeTaskManager).inSingletonScope();
}

View File

@ -1,10 +1,10 @@
import { server } from '@ms/api'
import { injectable } from '@ms/container';
import { provideSingleton } from '@ms/container';
let Sponge = org.spongepowered.api.Sponge;
let TextSerializers = org.spongepowered.api.text.serializer.TextSerializers;
@injectable()
@provideSingleton(server.Server)
export class SpongeServer implements server.Server {
getPlayer(name: string) {
return Sponge.getServer().getPlayer(name).orElse(null)

View File

@ -1,5 +1,5 @@
import { task, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container';
import { inject, provideSingleton } from '@ms/container';
const Sponge = Java.type("org.spongepowered.api.Sponge");
const Task = Java.type("org.spongepowered.api.scheduler.Task");
@ -7,7 +7,7 @@ const Consumer = Java.type('java.util.function.Consumer');
const Callable = Java.type('java.util.concurrent.Callable');
const TimeUnit = Java.type('java.util.concurrent.TimeUnit');
@injectable()
@provideSingleton(task.TaskManager)
export class SpongeTaskManager implements task.TaskManager {
@inject(plugin.PluginInstance)
private pluginInstance: any;