From 3f52c0462ad2b1f2cf78fbf66eece758fb93e646 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Fri, 18 Dec 2015 13:36:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0File=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=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 --- pom.xml | 4 +- src/main/java/cn/citycraft/Yum/Yum.java | 4 +- .../java/cn/citycraft/Yum/api/YumAPI.java | 123 ++++++++++-------- .../citycraft/Yum/commands/CommandDelete.java | 2 +- .../citycraft/Yum/commands/CommandList.java | 2 +- .../Yum/file/commands/CommandDelete.java | 49 +++++++ .../Yum/file/commands/CommandDownLoad.java | 52 ++++++++ .../Yum/file/commands/CommandLs.java | 55 ++++++++ .../Yum/file/commands/CommandRm.java | 53 ++++++++ .../Yum/file/commands/FileCommand.java | 15 +++ src/main/resources/plugin.yml | 5 + 11 files changed, 305 insertions(+), 59 deletions(-) create mode 100644 src/main/java/cn/citycraft/Yum/file/commands/CommandDelete.java create mode 100644 src/main/java/cn/citycraft/Yum/file/commands/CommandDownLoad.java create mode 100644 src/main/java/cn/citycraft/Yum/file/commands/CommandLs.java create mode 100644 src/main/java/cn/citycraft/Yum/file/commands/CommandRm.java create mode 100644 src/main/java/cn/citycraft/Yum/file/commands/FileCommand.java diff --git a/pom.xml b/pom.xml index 214cf11..6c5bfae 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 cn.citycraft Yum - 1.8.9 + 1.9 Yum Minecraft 服务器插件管理系统 @@ -55,7 +55,7 @@ http://ci.citycraft.cn:8080 - &a通过检测NMS版本实现多端兼容处理... + &c添加File文件操作命令... Debug UTF-8 diff --git a/src/main/java/cn/citycraft/Yum/Yum.java b/src/main/java/cn/citycraft/Yum/Yum.java index dfc88c9..fda2702 100644 --- a/src/main/java/cn/citycraft/Yum/Yum.java +++ b/src/main/java/cn/citycraft/Yum/Yum.java @@ -9,6 +9,7 @@ import cn.citycraft.PluginHelper.config.FileConfig; import cn.citycraft.PluginHelper.utils.VersionChecker; import cn.citycraft.Yum.api.YumAPI; import cn.citycraft.Yum.commands.YumCommand; +import cn.citycraft.Yum.file.commands.FileCommand; /** * MC插件仓库 @@ -25,8 +26,9 @@ public class Yum extends JavaPlugin { @Override public void onEnable() { - new YumCommand(this); new YumAPI(this); + new YumCommand(this); + new FileCommand(this); YumAPI.getRepo().jsonToCache(config); YumAPI.updaterepo(); new VersionChecker(this); diff --git a/src/main/java/cn/citycraft/Yum/api/YumAPI.java b/src/main/java/cn/citycraft/Yum/api/YumAPI.java index c31f148..f8d3398 100644 --- a/src/main/java/cn/citycraft/Yum/api/YumAPI.java +++ b/src/main/java/cn/citycraft/Yum/api/YumAPI.java @@ -2,6 +2,8 @@ package cn.citycraft.Yum.api; import java.io.File; import java.net.URL; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -34,7 +36,7 @@ public class YumAPI { * 初始化Yum管理中心 * * @param plugin - * - 插件实体 + * 插件实体 */ public YumAPI(final Plugin plugin) { YumAPI.main = plugin; @@ -47,7 +49,7 @@ public class YumAPI { * 删除插件 * * @param plugin - * - 插件实体 + * 插件实体 */ public static void delete(final Plugin plugin) { plugman.deletePlugin(plugin); @@ -84,11 +86,11 @@ public class YumAPI { * 安装新插件 * * @param sender - * - 命令发送者 + * 命令发送者 * @param pluginname - * - 插件名称 + * 插件名称 * @param version - * - 插件版本 + * 插件版本 * @return 是否安装成功 */ public static boolean install(final CommandSender sender, final String pluginname, final String url) { @@ -103,9 +105,9 @@ public class YumAPI { * 安装新插件 * * @param pluginname - * - 插件名称 + * 插件名称 * @param version - * - 插件版本 + * 插件版本 * @return 是否安装成功 */ public static boolean install(final String pluginname, final String url) { @@ -116,9 +118,9 @@ public class YumAPI { * 安装新插件 * * @param sender - * - 命令发送者 + * 命令发送者 * @param pluginname - * - 插件名称 + * 插件名称 * @return 是否安装成功 */ public static boolean installfromyum(final CommandSender sender, final String pluginname) { @@ -129,11 +131,11 @@ public class YumAPI { * 安装新插件 * * @param sender - * - 命令发送者 + * 命令发送者 * @param pluginname - * - 插件名称 + * 插件名称 * @param version - * - 插件版本 + * 插件版本 * @return 是否安装成功 */ public static boolean installfromyum(final CommandSender sender, final String pluginname, final String version) { @@ -148,7 +150,7 @@ public class YumAPI { * 载入插件 * * @param pluginname - * - 插件名称 + * 插件名称 */ public static void load(final File pluginFile) { plugman.load(pluginFile); @@ -158,7 +160,7 @@ public class YumAPI { * 载入插件 * * @param pluginname - * - 插件名称 + * 插件名称 */ public static void load(final String pluginname) { plugman.load(pluginname); @@ -168,7 +170,7 @@ public class YumAPI { * 重载插件 * * @param plugin - * - 插件实体 + * 插件实体 */ public static void reload(final Plugin plugin) { plugman.reload(plugin); @@ -178,7 +180,7 @@ public class YumAPI { * 卸载插件 * * @param plugin - * - 插件实体 + * 插件实体 */ public static void unload(final Plugin plugin) { plugman.unload(plugin); @@ -188,11 +190,11 @@ public class YumAPI { * 更新插件 * * @param sender - * - 命令发送者 + * 命令发送者 * @param plugin - * - 插件实体 + * 插件实体 * @param url - * - 新插件的下载地址 + * 新插件的下载地址 * @return 是否更新成功 */ public static boolean update(final CommandSender sender, final Plugin plugin, final URL url) { @@ -208,9 +210,9 @@ public class YumAPI { * 更新插件 * * @param plugin - * - 插件实体 + * 插件实体 * @param url - * - 新插件的下载地址 + * 新插件的下载地址 * @return 是否更新成功 */ public static boolean update(final Plugin plugin, final URL url) { @@ -221,51 +223,45 @@ public class YumAPI { * 更新支持Yum的插件 * * @param sender - * - 命令发送者 + * 命令发送者 */ public static void updateall(final CommandSender sender) { + final List ulist = new ArrayList<>(); try { - - final Map updatelist = UpdatePlugin.getUpdateList(); - if (updatelist.size() > 0) { - for (final Entry updateplugin : UpdatePlugin.getUpdateList().entrySet()) { - sender.sendMessage("§d一键更新: §a开始更新" + updateplugin.getKey() + "!"); - updatefromyum(sender, updateplugin.getValue(), null); - } - UpdatePlugin.clearList(); - sender.sendMessage("§d一键更新: §e已下载所有需要升级的插件到 服务器更新 文件夹"); - sender.sendMessage("§d一键更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!"); - } else { - sender.sendMessage("§6更新: §e未找到需要更新且可以用Yum处理的插件!"); + for (final Entry updateplugin : UpdatePlugin.getUpdateList().entrySet()) { + ulist.add(updateplugin.getValue()); } + UpdatePlugin.clearList(); } catch (final Exception | Error e) { try { final Map updatelist = UpdatePlugin.getList(); - if (updatelist.size() > 0) { - for (final Entry updateplugin : UpdatePlugin.getList().entrySet()) { - sender.sendMessage("§d一键更新: §a开始更新" + updateplugin.getKey().getName() + "!"); - updatefromyum(sender, updateplugin.getKey(), null); - } - UpdatePlugin.getList().clear(); - sender.sendMessage("§d一键更新: §e已下载所有需要升级的插件到 服务器更新 文件夹"); - sender.sendMessage("§d一键更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!"); - } else { - sender.sendMessage("§6更新: §e未找到需要更新且可以用Yum处理的插件!"); - } + ulist.addAll(updatelist.keySet()); + UpdatePlugin.getList().clear(); } catch (final Exception | Error e2) { sender.sendMessage("§4错误: §c无法检索全体更新列表!"); sender.sendMessage("§4异常: §c" + e2.getMessage()); } } + if (ulist.size() > 0) { + for (final Plugin updateplugin : ulist) { + sender.sendMessage("§d一键更新: §a开始更新" + updateplugin.getName() + "!"); + updatefromyum(sender, updateplugin, null, true); + } + UpdatePlugin.getList().clear(); + sender.sendMessage("§d一键更新: §e已下载所有需要升级的插件到 服务器更新 文件夹"); + sender.sendMessage("§d一键更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!"); + } else { + sender.sendMessage("§6更新: §e未找到需要更新且可以用Yum处理的插件!"); + } } /** * 更新插件 * * @param sender - * - 命令发送者 + * 命令发送者 * @param plugin - * - 插件实体 + * 插件实体 * @return 是否更新成功 */ public static boolean updatefromyum(final CommandSender sender, final Plugin plugin) { @@ -276,19 +272,38 @@ public class YumAPI { * 从Yum内部更新插件 * * @param sender - * - 命令发送者 + * 命令发送者 * @param plugin - * - 插件实体 + * 插件实体 * @param version - * - 插件版本(null则自动获取) + * 插件版本(null则自动获取) * @return */ public static boolean updatefromyum(final CommandSender sender, final Plugin plugin, final String version) { + return updatefromyum(sender, plugin, version, false); + } + + /** + * 从Yum内部更新插件 + * + * @param sender + * 命令发送者 + * @param plugin + * 插件实体 + * @param version + * 插件版本(null则自动获取) + * @param oneKeyUpdate + * 是否一键更新 + * @return + */ + public static boolean updatefromyum(final CommandSender sender, final Plugin plugin, final String version, final boolean oneKeyUpdate) { final PluginInfo pi = repo.getPlugin(plugin.getName()); if (pi != null) { if (download.run(sender, pi.getUrl(sender, version), new File(Bukkit.getUpdateFolderFile(), plugman.getPluginFile(plugin).getName()))) { - sender.sendMessage("§6更新: §e已下载 " + plugin.getName() + " 插件到服务器更新文件夹"); - sender.sendMessage("§6更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!"); + if (!oneKeyUpdate) { + sender.sendMessage("§6更新: §e已下载 " + plugin.getName() + " 插件到服务器更新文件夹"); + sender.sendMessage("§6更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!"); + } return true; } } else { @@ -311,9 +326,9 @@ public class YumAPI { /** * @param sender - * - 命令发送者 + * 命令发送者 * @param plugin - * - 插件实体 + * 插件实体 */ public static void upgrade(final CommandSender sender, final Plugin plugin) { plugman.upgrade(sender, plugin); diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java b/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java index 92595e4..6c03bd3 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java @@ -22,7 +22,7 @@ public class CommandDelete extends BaseCommand { Yum main; public CommandDelete(final Yum main) { - super("d"); + super("del"); this.main = main; setMinimumArguments(1); setDescription("删除插件"); diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandList.java b/src/main/java/cn/citycraft/Yum/commands/CommandList.java index 90fc5f1..e1d9a72 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandList.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandList.java @@ -34,7 +34,7 @@ public class CommandList extends BaseCommand { public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException { sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: "); for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - sender.sendMessage("§6 - " + YumAPI.getPlugman().getFormattedName(plugin, true)); + sender.sendMessage("§6- " + YumAPI.getPlugman().getFormattedName(plugin, true)); } }; } diff --git a/src/main/java/cn/citycraft/Yum/file/commands/CommandDelete.java b/src/main/java/cn/citycraft/Yum/file/commands/CommandDelete.java new file mode 100644 index 0000000..f66143b --- /dev/null +++ b/src/main/java/cn/citycraft/Yum/file/commands/CommandDelete.java @@ -0,0 +1,49 @@ +/** + * + */ +package cn.citycraft.Yum.file.commands; + +import java.io.File; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; + +import cn.citycraft.PluginHelper.commands.BaseCommand; +import cn.citycraft.Yum.Yum; + +/** + * 插件删除命令类 + * + * @author 蒋天蓓 2015年8月12日下午2:04:05 + */ +public class CommandDelete extends BaseCommand { + Yum main; + + public CommandDelete(final Yum main) { + super("del"); + this.main = main; + setMinimumArguments(1); + setDescription("删除文件(服务器JAR为根目录)"); + setPossibleArguments("<文件相对目录>"); + } + + @Override + public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException { + final String fpath = args[0]; + final File file = new File(fpath); + if (!file.exists()) { + sendMessage(sender, "§c文件 " + file.getAbsolutePath() + " 不存在!"); + } else { + if (file.isDirectory()) { + sendMessage(sender, "§e" + file.getAbsolutePath() + " §c是一个目录 请使用file rm!"); + return; + } + try { + sendMessage(sender, "§d文件 §e" + file.getAbsolutePath() + " " + (file.delete() ? "§a删除成功!" : "§c删除失败!")); + } catch (final Exception e) { + sendMessage(sender, "§d文件 §e" + file.getAbsolutePath() + " 删除失败: " + e.getMessage()); + } + } + } +} diff --git a/src/main/java/cn/citycraft/Yum/file/commands/CommandDownLoad.java b/src/main/java/cn/citycraft/Yum/file/commands/CommandDownLoad.java new file mode 100644 index 0000000..e20222e --- /dev/null +++ b/src/main/java/cn/citycraft/Yum/file/commands/CommandDownLoad.java @@ -0,0 +1,52 @@ +/** + * + */ +package cn.citycraft.Yum.file.commands; + +import java.io.File; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; + +import cn.citycraft.PluginHelper.commands.BaseCommand; +import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.api.YumAPI; +import cn.citycraft.Yum.manager.DownloadManager; + +/** + * 插件删除命令类 + * + * @author 蒋天蓓 2015年8月12日下午2:04:05 + */ +public class CommandDownLoad extends BaseCommand { + Yum main; + DownloadManager dl; + + public CommandDownLoad(final Yum main) { + super("d"); + this.main = main; + dl = YumAPI.getDownload(); + setMinimumArguments(1); + setDescription("下载文件(默认保存到服务器更新文件夹)"); + setPossibleArguments("<下载地址> [保存文件路径]"); + } + + @Override + public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException { + String urlstr = args[0]; + if (!urlstr.startsWith("http")) { + urlstr = "http://" + urlstr; + } + File file = null; + if (args.length == 2) { + file = new File(args[1]); + } else { + file = new File(Bukkit.getUpdateFolderFile(), dl.getFileName(urlstr)); + } + if (args.length == 1) { + dl.run(sender, urlstr, file); + } + } +} diff --git a/src/main/java/cn/citycraft/Yum/file/commands/CommandLs.java b/src/main/java/cn/citycraft/Yum/file/commands/CommandLs.java new file mode 100644 index 0000000..2da840c --- /dev/null +++ b/src/main/java/cn/citycraft/Yum/file/commands/CommandLs.java @@ -0,0 +1,55 @@ +/** + * + */ +package cn.citycraft.Yum.file.commands; + +import java.io.File; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; + +import cn.citycraft.PluginHelper.commands.BaseCommand; +import cn.citycraft.Yum.Yum; + +/** + * 插件删除命令类 + * + * @author 蒋天蓓 2015年8月12日下午2:04:05 + */ +public class CommandLs extends BaseCommand { + + public CommandLs(final Yum main) { + super(); + setDescription("列出当前目录(服务器JAR为根目录)"); + setPossibleArguments("<相对目录>"); + } + + @Override + public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException { + File dir = new File("."); + if (args.length == 1) { + dir = new File(args[0]); + } + if (!dir.isDirectory()) { + sendMessage(sender, "§6路径: §e " + dir.getAbsolutePath() + " §c不是一个目录!"); + return; + } + final StringBuffer sb = new StringBuffer(); + for (final File file : dir.listFiles()) { + if (file.isDirectory()) { + sb.append("§b"); + } else { + sb.append("§a"); + } + sb.append(file.getName() + " "); + } + final String filelist = sb.toString(); + if (filelist.isEmpty()) { + sendMessage(sender, "§6目录: §e" + dir.getAbsolutePath() + " §c下没有文件或文件夹!"); + } else { + sendMessage(sender, "§6目录: §e" + dir.getAbsolutePath() + " §a存在如下文件!"); + sendMessage(sender, sb.toString()); + } + } +} diff --git a/src/main/java/cn/citycraft/Yum/file/commands/CommandRm.java b/src/main/java/cn/citycraft/Yum/file/commands/CommandRm.java new file mode 100644 index 0000000..b6c2e9a --- /dev/null +++ b/src/main/java/cn/citycraft/Yum/file/commands/CommandRm.java @@ -0,0 +1,53 @@ +/** + * + */ +package cn.citycraft.Yum.file.commands; + +import java.io.File; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandException; +import org.bukkit.command.CommandSender; + +import cn.citycraft.PluginHelper.commands.BaseCommand; +import cn.citycraft.PluginHelper.utils.FileUtil; +import cn.citycraft.Yum.Yum; + +/** + * 插件删除命令类 + * + * @author 蒋天蓓 2015年8月12日下午2:04:05 + */ +public class CommandRm extends BaseCommand { + + public CommandRm(final Yum main) { + super(); + setMinimumArguments(1); + setDescription("删除文件夹(服务器JAR为根目录)"); + setPossibleArguments("<文件相对目录>"); + } + + @Override + public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException { + final String fpath = args[0]; + final File file = new File(fpath); + if (!file.exists()) { + sendMessage(sender, "§c目录 " + file.getAbsolutePath() + " 不存在!"); + } else { + if (!file.isDirectory()) { + sendMessage(sender, "§d路径 §e" + file.getAbsolutePath() + " §c是一个文件 请使用file delete!"); + return; + } + if (file.getAbsolutePath().toLowerCase().endsWith("plugins") || file.getAbsolutePath().toLowerCase().endsWith("world")) { + sendMessage(sender, "§d路径 §e" + file.getAbsolutePath() + " §c不允许被删除!"); + return; + } + if (file.listFiles().length != 0 && !(args.length > 1 && args[1].equalsIgnoreCase("-rf"))) { + sendMessage(sender, "§d目录 §e" + file.getAbsolutePath() + " §c不为空!"); + sendMessage(sender, "§c请使用 §a/file rm " + fpath + " -rf §c强行删除!"); + return; + } + sendMessage(sender, "§d目录 §e" + file.getAbsolutePath() + " " + (FileUtil.deleteDir(sender, file) ? "§a删除成功!" : "§c删除失败!")); + } + } +} diff --git a/src/main/java/cn/citycraft/Yum/file/commands/FileCommand.java b/src/main/java/cn/citycraft/Yum/file/commands/FileCommand.java new file mode 100644 index 0000000..2246397 --- /dev/null +++ b/src/main/java/cn/citycraft/Yum/file/commands/FileCommand.java @@ -0,0 +1,15 @@ +package cn.citycraft.Yum.file.commands; + +import cn.citycraft.PluginHelper.commands.HandlerSubCommand; +import cn.citycraft.Yum.Yum; + +public class FileCommand { + public FileCommand(final Yum yum) { + final HandlerSubCommand cmdhandler = new HandlerSubCommand(yum, "file"); + cmdhandler.setAllCommandOnlyConsole(yum.config.getBoolean("onlyCommandConsole", false)); + cmdhandler.registerCommand(new CommandDownLoad(yum)); + cmdhandler.registerCommand(new CommandDelete(yum)); + cmdhandler.registerCommand(new CommandRm(yum)); + cmdhandler.registerCommand(new CommandLs(yum)); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8f8ee2b..a2ef2b6 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -10,6 +10,11 @@ commands: usage: §6使用§a/yum help§6查看帮助! permission: yum.use permission-message: §c你没有 的权限来执行此命令! + file: + description: MC文件管理命令 + usage: §6使用§a/yum help§6查看帮助! + permission: yum.use + permission-message: §c你没有 的权限来执行此命令! permissions: yum.use: description: 插件管理系统使用权限!