From 6538c4b7bb476ec7362c3c985976711e46b397b4 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 2 Mar 2020 22:40:18 +0800 Subject: [PATCH] feat: complate spiget and forgesvc api Signed-off-by: MiaoWoo --- packages/plugins/src/MiaoPluginManager.ts | 91 ++++++++++++++++++++--- 1 file changed, 81 insertions(+), 10 deletions(-) diff --git a/packages/plugins/src/MiaoPluginManager.ts b/packages/plugins/src/MiaoPluginManager.ts index 7a2f4ec2..07d0a6a7 100644 --- a/packages/plugins/src/MiaoPluginManager.ts +++ b/packages/plugins/src/MiaoPluginManager.ts @@ -1,26 +1,97 @@ +/// + +import { task, server } from "@ms/api"; +import { inject } from "@ms/container"; import { plugin, interfaces, cmd } from "@ms/plugin"; import http from '@ms/common/dist/http' +import * as fs from '@ms/common/dist/fs' @plugin({ name: 'MiaoPluginManager', prefix: 'MPM', version: '1.0.0', author: 'MiaoWoo', source: __filename }) export class MiaoPluginManager extends interfaces.Plugin { + @inject(server.Server) + private server: server.Server; + @inject(task.TaskManager) + private taskManager: task.TaskManager; + + private serverPluginsFolder: string; + private resourceAPIs = new Map() + + enable() { + this.resourceAPIs.set("spigot", new SpigotResourceAPI()) + this.resourceAPIs.set("bukkit", new BukkitResourceAPI()) + } + @cmd() mpman(sender: any, command: string, args: string[]) { } @cmd() bktman(sender: any, command: string, args: string[]) { - switch (args[0]) { - case "s": - case "search": - if (args[1]) { - let result = http.get('https://servermods.forgesvc.net/servermods/projects?search=' + args[1]) + this.main(sender, "bukkit", args[0], args.slice(1)) + } + @cmd() + sptman(sender: any, command: string, args: string[]) { + this.main(sender, "spigot", args[0], args.slice(1)) + } + + main(sender: any, target: string, command: string, args: string[]) { + let api = this.resourceAPIs.get(target); + this.taskManager.create(() => { + switch (command) { + case "s": + case "search": + this.logger.sender(sender, `§6正在从 §a${api.name()} §6搜索插件 §b${args[0]} §6请稍候...`) + let result = api.search(args[0]) for (let item of result) { - this.logger.sender(sender, "ID:", item.id, "名称:", item.name, JSON.stringify(item)) + this.logger.sender(sender, "ID:", item.id, "名称:", item.name) } - } - break; - default: - } + break; + case "d": + case "download": + http.download(api.download(Number(args[0])), fs.concat(this.server.getPluginsFolder(), args[1] + '.jar')) + break; + default: + } + }).async().submit(); } } + +class Resource { + id: number; + name: string; +} + +interface ResourceAPI { + name(): string; + search(name: string): Resource[]; + download(id: number): string; +} + +class BukkitResourceAPI implements ResourceAPI { + private host = "https://servermods.forgesvc.net/servermods"; + name() { + return "BukkitDev" + } + search(name: string) { + return http.get(`${this.host}/projects?search=${name}`); + } + download(id: number) { + let list = JSON.parse(http.get(`${this.host}/files?projectIds=${id}`)); + let lastest = list[list.lenght - 1]; + return lastest.downloadUrl; + } +} + +class SpigotResourceAPI implements ResourceAPI { + private host = "https://api.spiget.org/v2"; + name() { + return "SpigotMC" + } + search(name: string) { + return http.get(`${this.host}/search/resources/${name}`); + } + download(id: number) { + return `${this.host}/resources/${id}/download` + } +} \ No newline at end of file