feat: add download and getPluginsFolder
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
		@@ -4,7 +4,7 @@ export namespace plugin {
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    export const Plugin = Symbol("Plugin");
 | 
					    export const Plugin = Symbol("Plugin");
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * MiaoScript Plugin
 | 
					     * MiaoScript Plugin Folder
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    export const PluginFolder = Symbol("PluginFolder");
 | 
					    export const PluginFolder = Symbol("PluginFolder");
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -26,6 +26,7 @@ export namespace server {
 | 
				
			|||||||
        getService(service: string): any;
 | 
					        getService(service: string): any;
 | 
				
			||||||
        dispatchCommand(sender: string | any, command: string): boolean;
 | 
					        dispatchCommand(sender: string | any, command: string): boolean;
 | 
				
			||||||
        dispatchConsoleCommand(command: string): boolean;
 | 
					        dispatchConsoleCommand(command: string): boolean;
 | 
				
			||||||
 | 
					        getPluginsFolder(): string;
 | 
				
			||||||
        sendJson(sender: string | any, json: object | string): void;
 | 
					        sendJson(sender: string | any, json: object | string): void;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,6 +31,9 @@ export class BukkitServer implements server.Server {
 | 
				
			|||||||
    dispatchConsoleCommand(command: string): boolean {
 | 
					    dispatchConsoleCommand(command: string): boolean {
 | 
				
			||||||
        return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command)
 | 
					        return Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    getPluginsFolder(): string {
 | 
				
			||||||
 | 
					        return Bukkit.getUpdateFolderFile().getParentFile().getCanonicalPath();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    sendJson(sender: string | any, json: object | string): void {
 | 
					    sendJson(sender: string | any, json: object | string): void {
 | 
				
			||||||
        if (typeof sender === "string") {
 | 
					        if (typeof sender === "string") {
 | 
				
			||||||
            sender = this.getPlayer(sender)
 | 
					            sender = this.getPlayer(sender)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,6 +29,9 @@ export class BungeeServer implements server.Server {
 | 
				
			|||||||
    dispatchConsoleCommand(command: string): boolean {
 | 
					    dispatchConsoleCommand(command: string): boolean {
 | 
				
			||||||
        return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command)
 | 
					        return Bungee.getPluginManager().dispatchCommand(Bungee.getConsole(), command)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    getPluginsFolder(): string {
 | 
				
			||||||
 | 
					        return Bungee.getPluginsFolder().getCanonicalPath();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    sendJson(sender: string | any, json: string): void {
 | 
					    sendJson(sender: string | any, json: string): void {
 | 
				
			||||||
        throw new Error("Method not implemented.");
 | 
					        throw new Error("Method not implemented.");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,10 @@
 | 
				
			|||||||
import '@ms/api'
 | 
					import '@ms/api'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const URL = Java.type('java.net.URL')
 | 
				
			||||||
 | 
					const Paths = Java.type('java.nio.file.Paths');
 | 
				
			||||||
 | 
					const Files = Java.type('java.nio.file.Files');
 | 
				
			||||||
 | 
					const StandardCopyOption = Java.type('java.nio.file.StandardCopyOption');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export type Method =
 | 
					export type Method =
 | 
				
			||||||
    | 'get' | 'GET'
 | 
					    | 'get' | 'GET'
 | 
				
			||||||
    | 'delete' | 'DELETE'
 | 
					    | 'delete' | 'DELETE'
 | 
				
			||||||
@@ -31,8 +36,12 @@ function request(config: RequestConfig) {
 | 
				
			|||||||
    return xhr.get();
 | 
					    return xhr.get();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function download(url: string, target: string) {
 | 
				
			||||||
 | 
					    Files.copy(new URL(url).openStream(), Paths.get(target), StandardCopyOption.REPLACE_EXISTING);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function _proxy(method: Method) {
 | 
					function _proxy(method: Method) {
 | 
				
			||||||
    return function(url: string, data?: any, config?: RequestConfig) {
 | 
					    return function (url: string, data?: any, config?: RequestConfig) {
 | 
				
			||||||
        return request({ url, method, data, ...config });
 | 
					        return request({ url, method, data, ...config });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -40,5 +49,6 @@ function _proxy(method: Method) {
 | 
				
			|||||||
export default {
 | 
					export default {
 | 
				
			||||||
    get: _proxy('GET'),
 | 
					    get: _proxy('GET'),
 | 
				
			||||||
    post: _proxy('POST'),
 | 
					    post: _proxy('POST'),
 | 
				
			||||||
    request
 | 
					    request,
 | 
				
			||||||
 | 
					    download
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,8 +1,12 @@
 | 
				
			|||||||
import { server } from '@ms/api'
 | 
					import { server } from '@ms/api'
 | 
				
			||||||
import { provideSingleton } from '@ms/container';
 | 
					import { provideSingleton } from '@ms/container';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let Sponge = org.spongepowered.api.Sponge;
 | 
					import * as fs from '@ms/common/dist/fs'
 | 
				
			||||||
let TextSerializers = org.spongepowered.api.text.serializer.TextSerializers;
 | 
					
 | 
				
			||||||
 | 
					const Sponge = org.spongepowered.api.Sponge;
 | 
				
			||||||
 | 
					const TextSerializers = org.spongepowered.api.text.serializer.TextSerializers;
 | 
				
			||||||
 | 
					const URL = Java.type("java.net.URL");
 | 
				
			||||||
 | 
					const File = Java.type("java.io.File");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@provideSingleton(server.Server)
 | 
					@provideSingleton(server.Server)
 | 
				
			||||||
export class SpongeServer implements server.Server {
 | 
					export class SpongeServer implements server.Server {
 | 
				
			||||||
@@ -30,6 +34,10 @@ export class SpongeServer implements server.Server {
 | 
				
			|||||||
    dispatchConsoleCommand(command: string): boolean {
 | 
					    dispatchConsoleCommand(command: string): boolean {
 | 
				
			||||||
        return Sponge.getCommandManager().process(Sponge.getServer().getConsole(), command).getQueryResult()
 | 
					        return Sponge.getCommandManager().process(Sponge.getServer().getConsole(), command).getQueryResult()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    getPluginsFolder(): string {
 | 
				
			||||||
 | 
					        let pluginFile = new URL(base.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().getPath().split("!")[0]).path
 | 
				
			||||||
 | 
					        return new File(pluginFile).getParentFile().getCanonicalPath()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    sendJson(sender: string | any, json: string): void {
 | 
					    sendJson(sender: string | any, json: string): void {
 | 
				
			||||||
        if (typeof sender === "string") {
 | 
					        if (typeof sender === "string") {
 | 
				
			||||||
            sender = this.getPlayer(sender)
 | 
					            sender = this.getPlayer(sender)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user