From 720c7968890afe782c02cd9e4a2c36330d847bb9 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Thu, 21 Jul 2016 20:58:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E5=92=8C=E5=A4=8D=E5=88=B6=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../pw/yumc/Yum/commands/FileCommand.java | 87 +++++++++++++++++++ .../java/pw/yumc/Yum/commands/YumCommand.java | 44 ++++++++-- .../pw/yumc/Yum/managers/PluginsManager.java | 11 ++- 3 files changed, 131 insertions(+), 11 deletions(-) diff --git a/src/main/java/pw/yumc/Yum/commands/FileCommand.java b/src/main/java/pw/yumc/Yum/commands/FileCommand.java index 073211d..f007f0f 100644 --- a/src/main/java/pw/yumc/Yum/commands/FileCommand.java +++ b/src/main/java/pw/yumc/Yum/commands/FileCommand.java @@ -1,6 +1,8 @@ package pw.yumc.Yum.commands; +import java.io.BufferedReader; import java.io.File; +import java.io.InputStreamReader; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; @@ -9,6 +11,8 @@ import cn.citycraft.PluginHelper.commands.HandlerCommand; import cn.citycraft.PluginHelper.commands.HandlerCommands; import cn.citycraft.PluginHelper.commands.InvokeCommandEvent; import cn.citycraft.PluginHelper.commands.InvokeSubCommand; +import cn.citycraft.PluginHelper.kit.PluginKit; +import cn.citycraft.PluginHelper.kit.StrKit; import cn.citycraft.PluginHelper.utils.FileUtil; import pw.yumc.Yum.Yum; import pw.yumc.Yum.api.YumAPI; @@ -20,6 +24,22 @@ import pw.yumc.Yum.api.YumAPI; * @author 喵♂呜 */ public class FileCommand implements HandlerCommands { + private static String prefix = "§6[§bYum §a文件管理§6] "; + + private static String file_not_found = prefix + "§b%s §c文件未找到!"; + private static String file_is_dir = prefix + "§b%s §c文件是一个目录!"; + + private static String copySuccess = prefix + "§a文件 §b%s §a复制成功!"; + private static String copyFailed = prefix + "§c文件 §b%s §c复制失败!"; + + private static String waitCommand = prefix + "§a命令已发送,请等待执行完毕..."; + private static String runResult = prefix + "§a命令运行结果如下:"; + private static String runError = prefix + "§c命令运行错误: %s %s"; + + private static String addStartupSuccess = "§a成功添加开机自动启..."; + private static String addStartupFailed = "§c添加开机自动启失败!"; + private static String STARTPATH = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\StartUp"; + Yum plugin; public FileCommand(final Yum yum) { @@ -29,6 +49,27 @@ public class FileCommand implements HandlerCommands { cmdhandler.registerCommands(this); } + @HandlerCommand(name = "copy", aliases = "cp", minimumArguments = 2, description = "复制文件", possibleArguments = "<源文件> <目标目录>") + public void copy(final InvokeCommandEvent e) { + final String[] args = e.getArgs(); + final CommandSender sender = e.getSender(); + final File src = new File(args[0]); + final File des = new File(args[1]); + if (!src.exists()) { + sender.sendMessage(String.format(file_not_found, args[0])); + return; + } + if (src.isDirectory()) { + sender.sendMessage(String.format(file_is_dir, args[0])); + return; + } + if (FileUtil.copyFile(src, des)) { + sender.sendMessage(String.format(copySuccess, args[0])); + } else { + sender.sendMessage(String.format(copyFailed, args[0])); + } + } + @HandlerCommand(name = "delete", aliases = { "del" }, minimumArguments = 1, description = "删除文件(服务器JAR为根目录)", possibleArguments = "<文件相对目录>") public void delete(final InvokeCommandEvent e) { final String[] args = e.getArgs(); @@ -143,4 +184,50 @@ public class FileCommand implements HandlerCommands { } } + @HandlerCommand(name = "run", aliases = "r", minimumArguments = 1, description = "运行一个命令或文件", possibleArguments = "<命令或文件绝对路径>") + public void run(final InvokeCommandEvent e) { + final String[] args = e.getArgs(); + final CommandSender sender = e.getSender(); + PluginKit.runTaskAsync(new Runnable() { + @Override + public void run() { + try { + final Process process = Runtime.getRuntime().exec(StrKit.join(args, " ")); + sender.sendMessage(waitCommand); + final BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line = null; + final StringBuilder build = new StringBuilder(); + while ((line = br.readLine()) != null) { + build.append(line); + build.append("\n"); + } + sender.sendMessage(runResult); + sender.sendMessage(build.toString().split("\n")); + } catch (final Exception e2) { + sender.sendMessage(String.format(runError, e2.getClass().getSimpleName(), e2.getMessage())); + } + } + }); + } + + @HandlerCommand(name = "startup", minimumArguments = 1, description = "添加开机自动启", possibleArguments = "<命令或文件绝对路径>") + public void startup(final InvokeCommandEvent e) { + final String[] args = e.getArgs(); + final CommandSender sender = e.getSender(); + final File src = new File(args[0]); + final File des = new File(STARTPATH, src.getName()); + if (!src.exists()) { + sender.sendMessage(String.format(file_not_found, args[0])); + return; + } + if (src.isDirectory()) { + sender.sendMessage(String.format(file_is_dir, args[0])); + return; + } + if (FileUtil.copyFile(src, des)) { + sender.sendMessage(String.format(addStartupSuccess, args[0])); + } else { + sender.sendMessage(String.format(addStartupFailed, args[0])); + } + } } diff --git a/src/main/java/pw/yumc/Yum/commands/YumCommand.java b/src/main/java/pw/yumc/Yum/commands/YumCommand.java index d0f638f..8f9630c 100644 --- a/src/main/java/pw/yumc/Yum/commands/YumCommand.java +++ b/src/main/java/pw/yumc/Yum/commands/YumCommand.java @@ -57,6 +57,9 @@ public class YumCommand implements HandlerCommands, Listener { private final String filelistprefix = " §6插件名称 §3游戏版本 §d发布类型 §a操作"; private final String filelist = "§6- §b%-20s §3%-15s §d%-10s"; + private final String del = "§c删除: §a插件 §b%s §a版本 §d%s §a已从服务器卸载并删除!"; + private final String delFailed = "§c删除: §a插件 §b%s §c卸载或删除时发生错误 删除失败!"; + private final String look = "§6查看"; private final String install = "§a安装"; private final String update = "§a更新"; @@ -86,7 +89,7 @@ public class YumCommand implements HandlerCommands, Listener { public void run() { final String id = args[1]; switch (args[0]) { - case "look": + case "look": { sender.sendMessage(String.format(fsearching, id)); final List lf = Files.parseList(IOUtil.getData(String.format(BukkitDev.PLUGIN, id))); if (lf.isEmpty()) { @@ -98,16 +101,42 @@ public class YumCommand implements HandlerCommands, Listener { final FancyMessage fm = FancyMessage.newFM(); fm.text(String.format(filelist, f.name, f.gameVersion, f.releaseType)); fm.then(" "); - fm.then(install).command(String.format("/yum br install %s %s", f.name, f.downloadUrl)); + fm.then(install).command(String.format("/yum br ai %s %s", f.name, f.downloadUrl)); fm.send(sender); } break; - case "install": + } + case "ai": { if (args.length < 3) { return; } final String url = args[2]; final File file = new File(Bukkit.getUpdateFolderFile(), YumAPI.getDownload().getFileName(url)); + YumAPI.getDownload().run(e.getSender(), url, file, new One() { + @Override + public void run(final File file) { + if (file.getName().endsWith(".zip")) { + try { + ZipKit.unzip(file, Bukkit.getUpdateFolderFile(), ".jar"); + file.delete(); + } catch (final IOException e) { + sender.sendMessage(unzip_error); + } + } + YumAPI.upgrade(sender); + } + }); + break; + } + case "install": { + sender.sendMessage(String.format(fsearching, id)); + final List lf = Files.parseList(IOUtil.getData(String.format(BukkitDev.PLUGIN, id))); + if (lf.isEmpty()) { + sender.sendMessage(String.format(not_found_id_from_bukkit, id)); + } + final Files f = lf.get(0); + final String url = f.downloadUrl; + final File file = new File(Bukkit.getUpdateFolderFile(), YumAPI.getDownload().getFileName(url)); YumAPI.getDownload().run(e.getSender(), url, file, new One() { @Override public void run(final File file) { @@ -122,6 +151,7 @@ public class YumCommand implements HandlerCommands, Listener { } }); break; + } default: break; } @@ -137,9 +167,9 @@ public class YumCommand implements HandlerCommands, Listener { if (plugin != null) { final String version = StringUtils.substring(plugin.getDescription().getVersion(), 0, 15); if (YumAPI.getPlugman().deletePlugin(sender, plugin)) { - sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §a版本 §d" + version + " §a已从服务器卸载并删除!"); + sender.sendMessage(String.format(del, pluginname, version)); } else { - sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §c卸载或删除时发生错误 删除失败!"); + sender.sendMessage(String.format(delFailed, pluginname)); } } else { sender.sendMessage(pnf(pluginname)); @@ -169,9 +199,9 @@ public class YumCommand implements HandlerCommands, Listener { if (plugin != null) { final String version = StringUtils.substring(plugin.getDescription().getVersion(), 0, 15); if (YumAPI.getPlugman().fullDeletePlugin(sender, plugin)) { - sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §a版本 §d" + version + " §a已从服务器卸载并删除!"); + sender.sendMessage(String.format(version, pluginname, version)); } else { - sender.sendMessage("§c删除: §c插件 §b" + pluginname + " §c卸载或删除时发生错误 删除失败!"); + sender.sendMessage(String.format(delFailed, pluginname)); } } else { sender.sendMessage(pnf(pluginname)); diff --git a/src/main/java/pw/yumc/Yum/managers/PluginsManager.java b/src/main/java/pw/yumc/Yum/managers/PluginsManager.java index 7d85aa3..8725de7 100644 --- a/src/main/java/pw/yumc/Yum/managers/PluginsManager.java +++ b/src/main/java/pw/yumc/Yum/managers/PluginsManager.java @@ -48,14 +48,14 @@ public class PluginsManager { private final Set ignoreList = new HashSet<>(); private final Plugin main; - public static String getVersion(final Plugin plugin) { - return StringUtils.substring(plugin.getDescription().getVersion(), 0, 15); - } - public PluginsManager(final Plugin plugin) { this.main = plugin; } + public static String getVersion(final Plugin plugin) { + return StringUtils.substring(plugin.getDescription().getVersion(), 0, 15); + } + /** * 添加到忽略列表 * @@ -687,6 +687,9 @@ public class PluginsManager { return false; } for (final File file : updateDirectory.listFiles()) { + if (file.isDirectory()) { + continue; + } PluginDescriptionFile description = null; try { description = loader.getPluginDescription(file);