feat: 新增命令执行和复制命令

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-07-21 20:58:26 +08:00
parent b066e8a81e
commit 720c796889
3 changed files with 131 additions and 11 deletions

View File

@ -1,6 +1,8 @@
package pw.yumc.Yum.commands; package pw.yumc.Yum.commands;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.InputStreamReader;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; 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.HandlerCommands;
import cn.citycraft.PluginHelper.commands.InvokeCommandEvent; import cn.citycraft.PluginHelper.commands.InvokeCommandEvent;
import cn.citycraft.PluginHelper.commands.InvokeSubCommand; 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 cn.citycraft.PluginHelper.utils.FileUtil;
import pw.yumc.Yum.Yum; import pw.yumc.Yum.Yum;
import pw.yumc.Yum.api.YumAPI; import pw.yumc.Yum.api.YumAPI;
@ -20,6 +24,22 @@ import pw.yumc.Yum.api.YumAPI;
* @author * @author
*/ */
public class FileCommand implements HandlerCommands { 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; Yum plugin;
public FileCommand(final Yum yum) { public FileCommand(final Yum yum) {
@ -29,6 +49,27 @@ public class FileCommand implements HandlerCommands {
cmdhandler.registerCommands(this); 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 = "<文件相对目录>") @HandlerCommand(name = "delete", aliases = { "del" }, minimumArguments = 1, description = "删除文件(服务器JAR为根目录)", possibleArguments = "<文件相对目录>")
public void delete(final InvokeCommandEvent e) { public void delete(final InvokeCommandEvent e) {
final String[] args = e.getArgs(); 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]));
}
}
} }

View File

@ -57,6 +57,9 @@ public class YumCommand implements HandlerCommands, Listener {
private final String filelistprefix = " §6插件名称 §3游戏版本 §d发布类型 §a操作"; private final String filelistprefix = " §6插件名称 §3游戏版本 §d发布类型 §a操作";
private final String filelist = "§6- §b%-20s §3%-15s §d%-10s"; 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 look = "§6查看";
private final String install = "§a安装"; private final String install = "§a安装";
private final String update = "§a更新"; private final String update = "§a更新";
@ -86,7 +89,7 @@ public class YumCommand implements HandlerCommands, Listener {
public void run() { public void run() {
final String id = args[1]; final String id = args[1];
switch (args[0]) { switch (args[0]) {
case "look": case "look": {
sender.sendMessage(String.format(fsearching, id)); sender.sendMessage(String.format(fsearching, id));
final List<Files> lf = Files.parseList(IOUtil.getData(String.format(BukkitDev.PLUGIN, id))); final List<Files> lf = Files.parseList(IOUtil.getData(String.format(BukkitDev.PLUGIN, id)));
if (lf.isEmpty()) { if (lf.isEmpty()) {
@ -98,16 +101,42 @@ public class YumCommand implements HandlerCommands, Listener {
final FancyMessage fm = FancyMessage.newFM(); final FancyMessage fm = FancyMessage.newFM();
fm.text(String.format(filelist, f.name, f.gameVersion, f.releaseType)); fm.text(String.format(filelist, f.name, f.gameVersion, f.releaseType));
fm.then(" "); 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); fm.send(sender);
} }
break; break;
case "install": }
case "ai": {
if (args.length < 3) { if (args.length < 3) {
return; return;
} }
final String url = args[2]; final String url = args[2];
final File file = new File(Bukkit.getUpdateFolderFile(), YumAPI.getDownload().getFileName(url)); final File file = new File(Bukkit.getUpdateFolderFile(), YumAPI.getDownload().getFileName(url));
YumAPI.getDownload().run(e.getSender(), url, file, new One<File>() {
@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<Files> 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<File>() { YumAPI.getDownload().run(e.getSender(), url, file, new One<File>() {
@Override @Override
public void run(final File file) { public void run(final File file) {
@ -122,6 +151,7 @@ public class YumCommand implements HandlerCommands, Listener {
} }
}); });
break; break;
}
default: default:
break; break;
} }
@ -137,9 +167,9 @@ public class YumCommand implements HandlerCommands, Listener {
if (plugin != null) { if (plugin != null) {
final String version = StringUtils.substring(plugin.getDescription().getVersion(), 0, 15); final String version = StringUtils.substring(plugin.getDescription().getVersion(), 0, 15);
if (YumAPI.getPlugman().deletePlugin(sender, plugin)) { if (YumAPI.getPlugman().deletePlugin(sender, plugin)) {
sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §a版本 §d" + version + " §a已从服务器卸载并删除!"); sender.sendMessage(String.format(del, pluginname, version));
} else { } else {
sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §c卸载或删除时发生错误 删除失败!"); sender.sendMessage(String.format(delFailed, pluginname));
} }
} else { } else {
sender.sendMessage(pnf(pluginname)); sender.sendMessage(pnf(pluginname));
@ -169,9 +199,9 @@ public class YumCommand implements HandlerCommands, Listener {
if (plugin != null) { if (plugin != null) {
final String version = StringUtils.substring(plugin.getDescription().getVersion(), 0, 15); final String version = StringUtils.substring(plugin.getDescription().getVersion(), 0, 15);
if (YumAPI.getPlugman().fullDeletePlugin(sender, plugin)) { if (YumAPI.getPlugman().fullDeletePlugin(sender, plugin)) {
sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §a版本 §d" + version + " §a已从服务器卸载并删除!"); sender.sendMessage(String.format(version, pluginname, version));
} else { } else {
sender.sendMessage("§c删除: §c插件 §b" + pluginname + " §c卸载或删除时发生错误 删除失败!"); sender.sendMessage(String.format(delFailed, pluginname));
} }
} else { } else {
sender.sendMessage(pnf(pluginname)); sender.sendMessage(pnf(pluginname));

View File

@ -48,14 +48,14 @@ public class PluginsManager {
private final Set<String> ignoreList = new HashSet<>(); private final Set<String> ignoreList = new HashSet<>();
private final Plugin main; private final Plugin main;
public static String getVersion(final Plugin plugin) {
return StringUtils.substring(plugin.getDescription().getVersion(), 0, 15);
}
public PluginsManager(final Plugin plugin) { public PluginsManager(final Plugin plugin) {
this.main = 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; return false;
} }
for (final File file : updateDirectory.listFiles()) { for (final File file : updateDirectory.listFiles()) {
if (file.isDirectory()) {
continue;
}
PluginDescriptionFile description = null; PluginDescriptionFile description = null;
try { try {
description = loader.getPluginDescription(file); description = loader.getPluginDescription(file);