From 464093aade89f436d54e8734015926c2ed660aa2 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Thu, 24 Sep 2015 19:02:44 +0800 Subject: [PATCH] clean up and close classloader while unload plugin... Signed-off-by: 502647092 --- src/main/java/cn/citycraft/Yum/Yum.java | 8 +- .../java/cn/citycraft/Yum/api/YumApi.java | 1 - .../citycraft/Yum/commands/BaseCommand.java | 45 ++-- .../citycraft/Yum/commands/CommandDelete.java | 18 +- .../citycraft/Yum/commands/CommandInfo.java | 16 +- .../Yum/commands/CommandInstall.java | 16 +- .../citycraft/Yum/commands/CommandList.java | 7 +- .../citycraft/Yum/commands/CommandLoad.java | 13 +- .../citycraft/Yum/commands/CommandReload.java | 13 +- .../citycraft/Yum/commands/CommandRepo.java | 14 +- .../citycraft/Yum/commands/CommandUnload.java | 13 +- .../citycraft/Yum/commands/CommandUpdate.java | 14 +- .../Yum/commands/CommandUpgrade.java | 17 +- .../Yum/commands/HandlerCommand.java | 79 +++---- .../Yum/manager/DownloadManager.java | 75 ++++--- .../cn/citycraft/Yum/manager/PluginInfo.java | 10 +- .../citycraft/Yum/manager/PluginsManager.java | 207 ++++++++++-------- .../citycraft/Yum/manager/Repositories.java | 8 +- .../Yum/manager/RepositoryManager.java | 129 ++++++----- .../cn/citycraft/Yum/manager/YumManager.java | 23 +- 20 files changed, 406 insertions(+), 320 deletions(-) diff --git a/src/main/java/cn/citycraft/Yum/Yum.java b/src/main/java/cn/citycraft/Yum/Yum.java index e82d8c9..3829cde 100644 --- a/src/main/java/cn/citycraft/Yum/Yum.java +++ b/src/main/java/cn/citycraft/Yum/Yum.java @@ -19,8 +19,8 @@ import cn.citycraft.Yum.manager.YumManager; * @author 蒋天蓓 2015年8月21日下午5:14:39 */ public class Yum extends JavaPlugin { - public YumManager yumgr; public FileConfig config; + public YumManager yumgr; @Override public void onDisable() { @@ -30,16 +30,16 @@ public class Yum extends JavaPlugin { @Override public void onEnable() { - HandlerCommand cmdhandler = new HandlerCommand(this); + final HandlerCommand cmdhandler = new HandlerCommand(this); this.getCommand("yum").setExecutor(cmdhandler); this.getCommand("yum").setTabCompleter(cmdhandler); yumgr = new YumManager(this); YumManager.repo.jsonToCache(config); new VersionChecker(this); try { - Metrics metrics = new Metrics(this); + final Metrics metrics = new Metrics(this); metrics.start(); - } catch (IOException e) { + } catch (final IOException e) { } } diff --git a/src/main/java/cn/citycraft/Yum/api/YumApi.java b/src/main/java/cn/citycraft/Yum/api/YumApi.java index 1b27644..d3e251a 100644 --- a/src/main/java/cn/citycraft/Yum/api/YumApi.java +++ b/src/main/java/cn/citycraft/Yum/api/YumApi.java @@ -3,7 +3,6 @@ */ package cn.citycraft.Yum.api; - /** * Yum仓库插件API * diff --git a/src/main/java/cn/citycraft/Yum/commands/BaseCommand.java b/src/main/java/cn/citycraft/Yum/commands/BaseCommand.java index 72c452e..14d4429 100644 --- a/src/main/java/cn/citycraft/Yum/commands/BaseCommand.java +++ b/src/main/java/cn/citycraft/Yum/commands/BaseCommand.java @@ -17,19 +17,19 @@ import org.bukkit.command.CommandSender; * 2015年8月12日下午12:49:34 */ public abstract class BaseCommand { - private String name; - private String permission; - private String[] aliases; - private String description; + private final String[] aliases; + private final String description; private int minimumArguments = 0; - private String possibleArguments = ""; + private final String name; private boolean onlyPlayerExecutable = false; + private String permission; + private String possibleArguments = ""; - public BaseCommand(String name, String description) { + public BaseCommand(final String name, final String description) { this(name, description, new String[0]); } - public BaseCommand(String name, String description, String... aliases) { + public BaseCommand(final String name, final String description, final String... aliases) { this.name = name; this.description = description; this.aliases = aliases; @@ -51,11 +51,11 @@ public abstract class BaseCommand { /** * 获得当前命令的别名 - * + * * @return 当前命令的别名 */ public List getCommandList() { - List cmds = new ArrayList(); + final List cmds = new ArrayList(); cmds.add(name); cmds.addAll(Arrays.asList(aliases)); return cmds; @@ -113,9 +113,10 @@ public abstract class BaseCommand { * - 命令发送者 * @return 是否有权限执行命令 */ - public final boolean hasPermission(CommandSender sender) { - if (permission == null) + public final boolean hasPermission(final CommandSender sender) { + if (permission == null) { return true; + } return sender.hasPermission(permission); } @@ -135,13 +136,17 @@ public abstract class BaseCommand { * - 命令 * @return 是否匹配 */ - public final boolean isValidTrigger(String name) { - if (this.name.equalsIgnoreCase(name)) + public final boolean isValidTrigger(final String name) { + if (this.name.equalsIgnoreCase(name)) { return true; - if (aliases != null) - for (String alias : aliases) - if (alias.equalsIgnoreCase(name)) + } + if (aliases != null) { + for (final String alias : aliases) { + if (alias.equalsIgnoreCase(name)) { return true; + } + } + } return false; } @@ -151,7 +156,7 @@ public abstract class BaseCommand { * @param minimumArguments * - 最小参数个数 */ - public void setMinimumArguments(int minimumArguments) { + public void setMinimumArguments(final int minimumArguments) { this.minimumArguments = minimumArguments; } @@ -161,7 +166,7 @@ public abstract class BaseCommand { * @param onlyPlayerExecutable * - 是否只允许玩家执行 */ - public void setOnlyPlayerExecutable(boolean onlyPlayerExecutable) { + public void setOnlyPlayerExecutable(final boolean onlyPlayerExecutable) { this.onlyPlayerExecutable = onlyPlayerExecutable; } @@ -171,7 +176,7 @@ public abstract class BaseCommand { * @param permission * - 命令权限 */ - public void setPermission(String permission) { + public void setPermission(final String permission) { this.permission = permission; } @@ -181,7 +186,7 @@ public abstract class BaseCommand { * @param possibleArguments * - 可能的命令参数 */ - public void setPossibleArguments(String possibleArguments) { + public void setPossibleArguments(final String possibleArguments) { this.possibleArguments = possibleArguments; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java b/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java index 26382c9..fb23202 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 { /** * @param name */ - public CommandDelete(Yum main) { + public CommandDelete(final Yum main) { super("delete", "删除插件"); this.main = main; setMinimumArguments(1); @@ -30,15 +30,17 @@ public class CommandDelete extends BaseCommand { } @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - String pluginname = args[0]; - Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); - if (plugin != null) - if (YumManager.plugman.deletePlugin(sender, plugin)) + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { + final String pluginname = args[0]; + final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); + if (plugin != null) { + if (YumManager.plugman.deletePlugin(sender, plugin)) { sender.sendMessage("§c删除: §a插件 " + pluginname + " 已从服务器卸载并删除!"); - else + } else { sender.sendMessage("§c删除: §c插件 " + pluginname + " 卸载或删除时发生错误 删除失败!"); - else + } + } else { sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); + } }; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandInfo.java b/src/main/java/cn/citycraft/Yum/commands/CommandInfo.java index 22b3681..586c165 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandInfo.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandInfo.java @@ -24,20 +24,21 @@ public class CommandInfo extends BaseCommand { /** * @param name */ - public CommandInfo(Yum main) { + public CommandInfo(final Yum main) { super("info", "查看插件详情"); this.main = main; setPossibleArguments("<插件名称>"); } @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - if (args.length == 0) + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { + if (args.length == 0) { return; - String pluginname = args[0]; - Plugin plugin = main.getServer().getPluginManager().getPlugin(pluginname); + } + final String pluginname = args[0]; + final Plugin plugin = main.getServer().getPluginManager().getPlugin(pluginname); if (plugin != null) { - PluginDescriptionFile desc = plugin.getDescription(); + final PluginDescriptionFile desc = plugin.getDescription(); sender.sendMessage("§6插件名称: §3" + plugin.getName()); sender.sendMessage("§6插件版本: §3" + desc.getVersion()); sender.sendMessage("§6插件作者: §3" + StringUtils.join(desc.getAuthors(), " ")); @@ -47,7 +48,8 @@ public class CommandInfo extends BaseCommand { sender.sendMessage("§6插件软依赖: §3" + (desc.getSoftDepend().size() == 0 ? "无" : "")); StringUtil.sendStringArray(sender, desc.getSoftDepend()); sender.sendMessage("§6插件物理路径: §3" + YumManager.plugman.getPluginFile(plugin).getAbsolutePath()); - } else + } else { sender.sendMessage("§4错误: §c插件 " + pluginname + " 不存在或已卸载!"); + } }; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandInstall.java b/src/main/java/cn/citycraft/Yum/commands/CommandInstall.java index bcc5b08..7678dae 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandInstall.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandInstall.java @@ -22,7 +22,7 @@ public class CommandInstall extends BaseCommand { /** * @param name */ - public CommandInstall(Yum main) { + public CommandInstall(final Yum main) { super("install", "安装插件"); this.main = main; setMinimumArguments(1); @@ -30,18 +30,20 @@ public class CommandInstall extends BaseCommand { } @Override - public void execute(final CommandSender sender, String label, final String[] args) throws CommandException { + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { final String pluginname = args[0]; - Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); - if (plugin == null) + final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); + if (plugin == null) { Bukkit.getScheduler().runTaskAsynchronously(main, () -> { - if (args.length < 2) + if (args.length < 2) { YumManager.install(sender, pluginname); - else + } else { YumManager.install(sender, pluginname, args[1]); + } }); - else + } else { sender.sendMessage("§c插件已安装在服务器 需要更新请使用yum update " + pluginname + "!"); + } }; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandList.java b/src/main/java/cn/citycraft/Yum/commands/CommandList.java index ee14010..6acf2f0 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandList.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandList.java @@ -22,15 +22,16 @@ public class CommandList extends BaseCommand { /** * @param name */ - public CommandList(Yum main) { + public CommandList(final Yum main) { super("list", "列出已安装插件列表"); this.main = main; } @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: "); - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) + for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { sender.sendMessage("§6 - " + YumManager.plugman.getFormattedName(plugin, true)); + } }; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandLoad.java b/src/main/java/cn/citycraft/Yum/commands/CommandLoad.java index a60c2ae..a1effbe 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandLoad.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandLoad.java @@ -22,7 +22,7 @@ public class CommandLoad extends BaseCommand { /** * @param name */ - public CommandLoad(Yum main) { + public CommandLoad(final Yum main) { super("load", "载入插件"); this.main = main; setMinimumArguments(1); @@ -30,12 +30,13 @@ public class CommandLoad extends BaseCommand { } @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - String pluginname = args[0]; - Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); - if (plugin == null) + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { + final String pluginname = args[0]; + final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); + if (plugin == null) { YumManager.plugman.load(sender, pluginname); - else + } else { sender.sendMessage("§c错误: 插件 " + pluginname + " 已加载到服务器!"); + } }; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandReload.java b/src/main/java/cn/citycraft/Yum/commands/CommandReload.java index 4b3e474..4a34be4 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandReload.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandReload.java @@ -22,7 +22,7 @@ public class CommandReload extends BaseCommand { /** * @param name */ - public CommandReload(Yum main) { + public CommandReload(final Yum main) { super("reload", "重载插件"); this.main = main; setMinimumArguments(1); @@ -30,16 +30,17 @@ public class CommandReload extends BaseCommand { } @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - String pluginname = args[0]; + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { + final String pluginname = args[0]; if (pluginname.equalsIgnoreCase("all") || pluginname.equalsIgnoreCase("*")) { YumManager.plugman.reloadAll(sender); return; } - Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); - if (plugin != null) + final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); + if (plugin != null) { YumManager.plugman.reload(sender, plugin); - else + } else { sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); + } }; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java b/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java index 50283fd..cd75433 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java @@ -21,7 +21,7 @@ public class CommandRepo extends BaseCommand { /** * @param name */ - public CommandRepo(Yum main) { + public CommandRepo(final Yum main) { super("repo", "插件源命令"); this.main = main; setMinimumArguments(1); @@ -29,17 +29,19 @@ public class CommandRepo extends BaseCommand { } @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - String cmd = args[0]; + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { + final String cmd = args[0]; switch (cmd) { case "add": if (args.length == 2) { - if (YumManager.repo.addRepositories(sender, args[1])) + if (YumManager.repo.addRepositories(sender, args[1])) { sender.sendMessage("§6仓库: §a插件信息已缓存!"); - else + } else { sender.sendMessage("§6仓库: §c源地址未找到仓库信息或无法访问!"); - } else + } + } else { sender.sendMessage("§6仓库: §c请输入源地址!"); + } break; case "list": sender.sendMessage("§6仓库: §b缓存的插件信息如下 "); diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandUnload.java b/src/main/java/cn/citycraft/Yum/commands/CommandUnload.java index 5fef251..7e925bb 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandUnload.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandUnload.java @@ -22,7 +22,7 @@ public class CommandUnload extends BaseCommand { /** * @param name */ - public CommandUnload(Yum main) { + public CommandUnload(final Yum main) { super("unload", "卸载插件"); this.main = main; setMinimumArguments(1); @@ -30,12 +30,13 @@ public class CommandUnload extends BaseCommand { } @Override - public void execute(CommandSender sender, String label, String[] args) throws CommandException { - String pluginname = args[0]; - Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); - if (plugin != null) + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { + final String pluginname = args[0]; + final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); + if (plugin != null) { YumManager.plugman.unload(sender, plugin); - else + } else { sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); + } }; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java b/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java index 058b583..9eb1b8d 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java @@ -20,7 +20,7 @@ public class CommandUpdate extends BaseCommand { /** * @param name */ - public CommandUpdate(Yum main) { + public CommandUpdate(final Yum main) { super("update", "更新插件"); this.main = main; setMinimumArguments(1); @@ -28,18 +28,20 @@ public class CommandUpdate extends BaseCommand { } @Override - public void execute(final CommandSender sender, String label, final String[] args) throws CommandException { + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { final String pluginname = args[0]; final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); sender.sendMessage("§a开始更新插件: " + pluginname); - if (plugin != null) + if (plugin != null) { Bukkit.getScheduler().runTaskAsynchronously(main, () -> { - if (args.length < 2) + if (args.length < 2) { YumManager.update(sender, plugin); - else + } else { YumManager.update(sender, plugin, args[1]); + } }); - else + } else { sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!"); + } }; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java b/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java index ae3bd83..242f07a 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandUpgrade.java @@ -20,7 +20,7 @@ public class CommandUpgrade extends BaseCommand { /** * @param name */ - public CommandUpgrade(Yum main) { + public CommandUpgrade(final Yum main) { super("upgrade", "升级插件"); this.main = main; setMinimumArguments(1); @@ -28,18 +28,19 @@ public class CommandUpgrade extends BaseCommand { } @Override - public void execute(final CommandSender sender, String label, final String[] args) throws CommandException { + public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException { Bukkit.getScheduler().runTaskAsynchronously(main, () -> { - if (args.length == 0) + if (args.length == 0) { YumManager.plugman.upgrade(sender); - else { - String pluginname = args[0]; - Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); + } else { + final String pluginname = args[0]; + final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); sender.sendMessage("§a开始升级插件: " + pluginname); - if (plugin != null) + if (plugin != null) { YumManager.plugman.upgrade(sender, plugin); - else + } else { sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!"); + } } }); }; diff --git a/src/main/java/cn/citycraft/Yum/commands/HandlerCommand.java b/src/main/java/cn/citycraft/Yum/commands/HandlerCommand.java index dcf19d5..70f8b95 100644 --- a/src/main/java/cn/citycraft/Yum/commands/HandlerCommand.java +++ b/src/main/java/cn/citycraft/Yum/commands/HandlerCommand.java @@ -25,19 +25,19 @@ import cn.citycraft.Yum.manager.YumManager; * @author 蒋天蓓 2015年8月22日上午8:29:44 */ public class HandlerCommand implements CommandExecutor, TabCompleter { - /** - * 已注册命令列表(包括别名) - */ - List RegisterCommandList = new ArrayList(); - /** * 命令监听类列表 */ - private List commandlist = new ArrayList();; + private final List commandlist = new ArrayList(); + /** * 插件主类 */ - Yum main; + Yum main;; + /** + * 已注册命令列表(包括别名) + */ + List RegisterCommandList = new ArrayList(); /** * 注册子命令 @@ -45,7 +45,7 @@ public class HandlerCommand implements CommandExecutor, TabCompleter { * @param yum * - 插件主类 */ - public HandlerCommand(Yum yum) { + public HandlerCommand(final Yum yum) { this.main = yum; registerCommand(new CommandList(yum)); registerCommand(new CommandInstall(yum)); @@ -70,8 +70,8 @@ public class HandlerCommand implements CommandExecutor, TabCompleter { * - 数组开始位置 * @return 转移后的数组字符串 */ - public static String[] moveStrings(String[] args, int start) { - String[] ret = new String[args.length - start]; + public static String[] moveStrings(final String[] args, final int start) { + final String[] ret = new String[args.length - start]; System.arraycopy(args, start, ret, 0, ret.length); return ret; } @@ -82,25 +82,28 @@ public class HandlerCommand implements CommandExecutor, TabCompleter { * @return - 返回已注册的命令List */ public List getRegisterCommands() { - List cmds = new ArrayList(); - for (BaseCommand command : commandlist) + final List cmds = new ArrayList(); + for (final BaseCommand command : commandlist) { cmds.addAll(command.getCommandList()); + } return cmds; } @Override - public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { - if (args.length == 0) - return true; - String subcmd = args[0]; - if (subcmd.equalsIgnoreCase("help")) { - sender.sendMessage("§6=========YUM插件帮助列表========="); - for (BaseCommand command : commandlist) - sender.sendMessage(String.format("§6/yum §a%1$s %2$s §6- §b%3$s", command.getName(), command.getPossibleArguments(), command.getDescription())); + public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) { + if (args.length == 0) { return true; } - String[] subargs = moveStrings(args, 1); - for (BaseCommand command : commandlist) + final String subcmd = args[0]; + if (subcmd.equalsIgnoreCase("help")) { + sender.sendMessage("§6=========YUM插件帮助列表========="); + for (final BaseCommand command : commandlist) { + sender.sendMessage(String.format("§6/yum §a%1$s %2$s §6- §b%3$s", command.getName(), command.getPossibleArguments(), command.getDescription())); + } + return true; + } + final String[] subargs = moveStrings(args, 1); + for (final BaseCommand command : commandlist) { if (command.isValidTrigger(subcmd)) { if (!command.hasPermission(sender)) { sender.sendMessage("§c你没有此命令的权限!"); @@ -110,40 +113,40 @@ public class HandlerCommand implements CommandExecutor, TabCompleter { sender.sendMessage("§c控制台无法使用此命令!"); return true; } - if (subargs.length >= command.getMinimumArguments()) + if (subargs.length >= command.getMinimumArguments()) { try { command.execute(sender, subcmd, subargs); return true; - } catch (CommandException e) { + } catch (final CommandException e) { sender.sendMessage(e.getMessage()); } - else + } else { sender.sendMessage("§c错误的参数 §e使用方法 /yum " + command.getName() + command.getPossibleArguments()); + } } + } return false; } @Override - public List onTabComplete(CommandSender sender, Command command, String label, String[] args) { + public List onTabComplete(final CommandSender sender, final Command command, final String label, final String[] args) { if (sender.isOp() || sender.hasPermission("yum.admin") || sender.hasPermission("yum." + args[0])) { - List completions = new ArrayList<>(); + final List completions = new ArrayList<>(); if (args.length == 1) { - String partialCommand = args[0]; - List commands = RegisterCommandList; + final String partialCommand = args[0]; + final List commands = RegisterCommandList; StringUtil.copyPartialMatches(partialCommand, commands, completions); } if (args.length == 2) { - String partialPlugin = args[1]; + final String partialPlugin = args[1]; List plugins = null; - if (args[0].equalsIgnoreCase("install")) + if (args[0].equalsIgnoreCase("install")) { plugins = YumManager.repo.getAllPluginName(); - else if (args[0].equalsIgnoreCase("repo")) - plugins = Arrays.asList(new String[] { "add", - "list", - "clean", - "update" }); - else + } else if (args[0].equalsIgnoreCase("repo")) { + plugins = Arrays.asList(new String[] { "add", "list", "clean", "update" }); + } else { plugins = YumManager.plugman.getPluginNames(false); + } StringUtil.copyPartialMatches(partialPlugin, plugins, completions); } Collections.sort(completions); @@ -158,7 +161,7 @@ public class HandlerCommand implements CommandExecutor, TabCompleter { * @param command * - 被注册的命令类 */ - public void registerCommand(BaseCommand command) { + public void registerCommand(final BaseCommand command) { commandlist.add(command); } diff --git a/src/main/java/cn/citycraft/Yum/manager/DownloadManager.java b/src/main/java/cn/citycraft/Yum/manager/DownloadManager.java index a6ee204..a54f6d9 100644 --- a/src/main/java/cn/citycraft/Yum/manager/DownloadManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/DownloadManager.java @@ -19,7 +19,7 @@ import org.bukkit.plugin.Plugin; public class DownloadManager { Plugin plugin; - public DownloadManager(Plugin main) { + public DownloadManager(final Plugin main) { this.plugin = main; } @@ -30,8 +30,8 @@ public class DownloadManager { * - 地址 * @return 文件名称 */ - public String getFileName(String url) { - int end = url.lastIndexOf('/'); + public String getFileName(final String url) { + final int end = url.lastIndexOf('/'); return url.substring(end + 1); } @@ -42,22 +42,10 @@ public class DownloadManager { * - 地址 * @return 文件名称 */ - public String getFileName(URL url) { + public String getFileName(final URL url) { return getFileName(url.getFile()); } - private String getPer(int per) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < 11; i++) - if (per > i) - sb.append("=="); - else if (per == i) - sb.append("> "); - else - sb.append(" "); - return sb.toString(); - } - /** * 从网络下载文件 * @@ -67,7 +55,7 @@ public class DownloadManager { * - 下载地址 * @return 是否成功 */ - public boolean run(CommandSender sender, String urlstring) { + public boolean run(final CommandSender sender, final String urlstring) { return run(sender, urlstring, new File("plugins", getFileName(urlstring))); } @@ -82,11 +70,11 @@ public class DownloadManager { * - 保存文件 * @return 是否成功 */ - public boolean run(CommandSender sender, String urlstring, File file) { + public boolean run(final CommandSender sender, final String urlstring, final File file) { try { - URL url = new URL(urlstring); + final URL url = new URL(urlstring); return run(sender, url, file); - } catch (MalformedURLException e) { + } catch (final MalformedURLException e) { sender.sendMessage("§4错误: §c无法识别的URL地址..."); sender.sendMessage("§4地址: §c" + urlstring); return false; @@ -104,15 +92,16 @@ public class DownloadManager { * - 保存文件 * @return 是否成功 */ - public boolean run(CommandSender sender, URL url, File file) { + public boolean run(CommandSender sender, final URL url, final File file) { BufferedInputStream in = null; FileOutputStream fout = null; - if (sender == null) + if (sender == null) { sender = Bukkit.getConsoleSender(); + } try { sender.sendMessage("§6开始下载: §3" + getFileName(url)); sender.sendMessage("§6下载地址: §3" + url.toString()); - int fileLength = url.openConnection().getContentLength(); + final int fileLength = url.openConnection().getContentLength(); if (fileLength < 0) { sender.sendMessage("§6下载: §c文件 " + file.getName() + " 获取长度错误(可能是网络问题)!"); sender.sendMessage("§6文件: §c " + file.getName() + " 下载失败!"); @@ -124,38 +113,42 @@ public class DownloadManager { file.getParentFile().mkdirs(); sender.sendMessage("§6创建新目录: §d" + file.getParentFile().getAbsolutePath()); } - if (file.exists()) + if (file.exists()) { file.delete(); + } file.createNewFile(); sender.sendMessage("§6创建新文件: §d" + file.getAbsolutePath()); fout = new FileOutputStream(file); - byte[] data = new byte[1024]; + final byte[] data = new byte[1024]; long downloaded = 0L; int count; long time = System.currentTimeMillis(); while ((count = in.read(data)) != -1) { downloaded += count; fout.write(data, 0, count); - int percent = (int) (downloaded * 100L / fileLength); - if (percent % 10 == 0) + final int percent = (int) (downloaded * 100L / fileLength); + if (percent % 10 == 0) { if (fileLength < 102400 || System.currentTimeMillis() - time > 1000) { sender.sendMessage(String.format("§6已下载: §a" + getPer(percent / 10) + " %s%%", percent)); time = System.currentTimeMillis(); } + } } sender.sendMessage("§6文件: §a " + file.getName() + " 下载完成!"); return true; - } catch (Exception ex) { + } catch (final Exception ex) { sender.sendMessage("§6异常: §c" + ex.getMessage()); sender.sendMessage("§6文件: §c" + file.getName() + " 下载失败!"); return false; } finally { try { - if (in != null) + if (in != null) { in.close(); - if (fout != null) + } + if (fout != null) { fout.close(); - } catch (Exception ex) { + } + } catch (final Exception ex) { } } } @@ -167,7 +160,7 @@ public class DownloadManager { * - 下载地址 * @return 是否成功 */ - public boolean run(String urlstring) { + public boolean run(final String urlstring) { return run(null, urlstring); } @@ -180,7 +173,7 @@ public class DownloadManager { * - 保存文件 * @return 是否成功 */ - public boolean run(String urlstring, File file) { + public boolean run(final String urlstring, final File file) { return run(null, urlstring, file); } @@ -193,8 +186,22 @@ public class DownloadManager { * - 保存文件 * @return 是否成功 */ - public boolean run(URL url, File file) { + public boolean run(final URL url, final File file) { return run(null, url, file); } + private String getPer(final int per) { + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < 11; i++) { + if (per > i) { + sb.append("=="); + } else if (per == i) { + sb.append("> "); + } else { + sb.append(" "); + } + } + return sb.toString(); + } + } diff --git a/src/main/java/cn/citycraft/Yum/manager/PluginInfo.java b/src/main/java/cn/citycraft/Yum/manager/PluginInfo.java index ea9ce5f..0dd5d9e 100644 --- a/src/main/java/cn/citycraft/Yum/manager/PluginInfo.java +++ b/src/main/java/cn/citycraft/Yum/manager/PluginInfo.java @@ -7,9 +7,9 @@ import cn.citycraft.Yum.manager.Repositories.TagInfo; public class PluginInfo { public Plugin plugin; - public String url; public String repo; public List tags; + public String url; public String getFileName() { return String.format("%1$s-%2$s.jar", plugin.artifactId, plugin.version); @@ -19,14 +19,16 @@ public class PluginInfo { return getMavenUrl(null); } - public String getMavenUrl(String version) { + public String getMavenUrl(final String version) { String ver = version; - if (ver == null && tags != null) - for (TagInfo tagInfo : tags) + if (ver == null && tags != null) { + for (final TagInfo tagInfo : tags) { if (tagInfo.tag.equalsIgnoreCase("1.7.10")) { ver = tagInfo.version; break; } + } + } return String.format(url + (url.endsWith("/") ? "" : "/") + "%1$s/%2$s/%3$s/%2$s-%3$s.jar", plugin.groupId.replace(".", "/"), plugin.artifactId, ver == null ? plugin.version : ver); } } \ No newline at end of file diff --git a/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java b/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java index 7fa9d14..eb03427 100644 --- a/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/PluginsManager.java @@ -37,7 +37,7 @@ import cn.citycraft.PluginHelper.utils.StringUtil; public class PluginsManager { Plugin main; - public PluginsManager(Plugin plugin) { + public PluginsManager(final Plugin plugin) { this.main = plugin; } @@ -50,7 +50,7 @@ public class PluginsManager { * - 插件 * @return 是否成功 */ - public boolean deletePlugin(CommandSender sender, Plugin plugin) { + public boolean deletePlugin(final CommandSender sender, final Plugin plugin) { return unload(sender, plugin) && getPluginFile(plugin).delete(); } @@ -61,7 +61,7 @@ public class PluginsManager { * - 插件 * @return 是否成功 */ - public boolean deletePlugin(Plugin plugin) { + public boolean deletePlugin(final Plugin plugin) { return deletePlugin(Bukkit.getConsoleSender(), plugin); } @@ -71,18 +71,21 @@ public class PluginsManager { * @param plugin * - 插件 */ - public void disable(Plugin plugin) { - if ((plugin != null) && (plugin.isEnabled())) + public void disable(final Plugin plugin) { + if ((plugin != null) && (plugin.isEnabled())) { Bukkit.getPluginManager().disablePlugin(plugin); + } } /** * 关闭所有插件 */ public void disableAll() { - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) - if (!isIgnored(plugin)) + for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { + if (!isIgnored(plugin)) { disable(plugin); + } + } } /** @@ -91,18 +94,21 @@ public class PluginsManager { * @param plugin * - 插件 */ - public void enable(Plugin plugin) { - if ((plugin != null) && (!plugin.isEnabled())) + public void enable(final Plugin plugin) { + if ((plugin != null) && (!plugin.isEnabled())) { Bukkit.getPluginManager().enablePlugin(plugin); + } } /** * 启用所有插件 */ public void enableAll() { - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) - if (!isIgnored(plugin)) + for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { + if (!isIgnored(plugin)) { enable(plugin); + } + } } /** @@ -112,7 +118,7 @@ public class PluginsManager { * - 插件 * @return 格式化的插件名称 */ - public String getFormattedName(Plugin plugin) { + public String getFormattedName(final Plugin plugin) { return getFormattedName(plugin, false); } @@ -125,11 +131,12 @@ public class PluginsManager { * - 是否包括版本 * @return 格式化的插件名称 */ - public String getFormattedName(Plugin plugin, boolean includeVersions) { - ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED; + public String getFormattedName(final Plugin plugin, final boolean includeVersions) { + final ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED; String pluginName = color + plugin.getName(); - if (includeVersions) + if (includeVersions) { pluginName = pluginName + " (" + plugin.getDescription().getVersion() + ")"; + } return pluginName; } @@ -140,7 +147,7 @@ public class PluginsManager { * - 名称 * @return 插件 */ - public Plugin getPluginByName(String name) { + public Plugin getPluginByName(final String name) { return Bukkit.getPluginManager().getPlugin(name); } @@ -151,7 +158,7 @@ public class PluginsManager { * - 名称 * @return 插件 */ - public Plugin getPluginByName(String[] args, int start) { + public Plugin getPluginByName(final String[] args, final int start) { return getPluginByName(StringUtil.consolidateStrings(args, start)); } @@ -162,22 +169,23 @@ public class PluginsManager { * - 插件 * @return 插件的绝对路径 */ - public File getPluginFile(Plugin plugin) { + public File getPluginFile(final Plugin plugin) { File file = null; - ClassLoader cl = plugin.getClass().getClassLoader(); + final ClassLoader cl = plugin.getClass().getClassLoader(); if ((cl instanceof URLClassLoader)) { @SuppressWarnings("resource") - URLClassLoader ucl = (URLClassLoader) cl; - URL url = ucl.getURLs()[0]; + final URLClassLoader ucl = (URLClassLoader) cl; + final URL url = ucl.getURLs()[0]; file = new File(url.getFile()); } return file; } - public List getPluginNames(boolean fullName) { - List plugins = new ArrayList(); - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) + public List getPluginNames(final boolean fullName) { + final List plugins = new ArrayList(); + for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName()); + } return plugins; } @@ -188,10 +196,11 @@ public class PluginsManager { * - 插件名称 * @return 插件版本 */ - public String getPluginVersion(String name) { - Plugin plugin = getPluginByName(name); - if ((plugin != null) && (plugin.getDescription() != null)) + public String getPluginVersion(final String name) { + final Plugin plugin = getPluginByName(name); + if ((plugin != null) && (plugin.getDescription() != null)) { return plugin.getDescription().getVersion(); + } return null; } @@ -202,21 +211,23 @@ public class PluginsManager { * - 插件 * @return 插件命令 */ - public String getUsages(Plugin plugin) { - List parsedCommands = new ArrayList(); + public String getUsages(final Plugin plugin) { + final List parsedCommands = new ArrayList(); - Map> commands = plugin.getDescription().getCommands(); + final Map> commands = plugin.getDescription().getCommands(); if (commands != null) { - Iterator>> commandsIt = commands.entrySet().iterator(); + final Iterator>> commandsIt = commands.entrySet().iterator(); while (commandsIt.hasNext()) { - Entry> thisEntry = commandsIt.next(); - if (thisEntry != null) + final Entry> thisEntry = commandsIt.next(); + if (thisEntry != null) { parsedCommands.add(thisEntry.getKey()); + } } } - if (parsedCommands.isEmpty()) + if (parsedCommands.isEmpty()) { return null; + } return Joiner.on(", ").join(parsedCommands); } @@ -227,7 +238,7 @@ public class PluginsManager { * - 插件 * @return 是否 */ - public boolean isIgnored(Plugin plugin) { + public boolean isIgnored(final Plugin plugin) { return isIgnored(plugin.getName()); } @@ -238,10 +249,12 @@ public class PluginsManager { * - 插件名称 * @return 是否 */ - public boolean isIgnored(String plugin) { - for (String name : new ArrayList()) - if (name.equalsIgnoreCase(plugin)) + public boolean isIgnored(final String plugin) { + for (final String name : new ArrayList()) { + if (name.equalsIgnoreCase(plugin)) { return true; + } + } return false; } @@ -254,15 +267,17 @@ public class PluginsManager { * - 插件名称 * @return 是否成功 */ - public boolean load(CommandSender sender, String name) { + public boolean load(CommandSender sender, final String name) { Plugin target = null; String filename = null; - if (sender == null) + if (sender == null) { sender = Bukkit.getConsoleSender(); - if (!name.endsWith(".jar")) + } + if (!name.endsWith(".jar")) { filename = name + ".jar"; - File pluginDir = new File("plugins"); - File updateDir = new File(pluginDir, "update"); + } + final File pluginDir = new File("plugins"); + final File updateDir = new File(pluginDir, "update"); if (!pluginDir.isDirectory()) { sender.sendMessage("§c插件目录不存在或IO错误!"); return false; @@ -272,16 +287,18 @@ public class PluginsManager { if (!pluginFile.isFile() && !new File(updateDir, filename).isFile()) { pluginFile = null; - for (File file : pluginDir.listFiles()) - if (file.getName().endsWith(".jar")) + for (final File file : pluginDir.listFiles()) { + if (file.getName().endsWith(".jar")) { try { - PluginDescriptionFile desc = main.getPluginLoader().getPluginDescription(file); + final PluginDescriptionFile desc = main.getPluginLoader().getPluginDescription(file); if (desc.getName().equalsIgnoreCase(name)) { pluginFile = file; break; } - } catch (InvalidDescriptionException e) { + } catch (final InvalidDescriptionException e) { } + } + } if (pluginFile == null) { sender.sendMessage("§6载入: §c在插件目录和更新目录均未找到 " + name + " 插件 请确认文件是否存在!"); return false; @@ -290,19 +307,19 @@ public class PluginsManager { try { target = Bukkit.getPluginManager().loadPlugin(pluginFile); - } catch (InvalidDescriptionException e) { + } catch (final InvalidDescriptionException e) { sender.sendMessage("§4异常: §c" + e.getMessage()); sender.sendMessage("§c插件: " + name + " 的plugin.yml文件存在错误!"); return false; - } catch (UnsupportedClassVersionError e) { + } catch (final UnsupportedClassVersionError e) { sender.sendMessage("§4异常: §c" + e.getMessage()); sender.sendMessage("§c服务器或JAVA的版本低于插件: " + name + " 所需要的版本!!"); return false; - } catch (InvalidPluginException e) { + } catch (final InvalidPluginException e) { sender.sendMessage("§4异常: §c" + e.getMessage()); sender.sendMessage("§c文件: " + name + " 不是一个可载入的插件!"); return false; - } catch (UnknownDependencyException e) { + } catch (final UnknownDependencyException e) { sender.sendMessage("§4异常: §c" + e.getMessage()); sender.sendMessage("§c插件: " + name + " 缺少部分依赖项目!"); return false; @@ -321,7 +338,7 @@ public class PluginsManager { * - 插件名称 * @return 是否成功 */ - public boolean load(String name) { + public boolean load(final String name) { return load(Bukkit.getConsoleSender(), name); } @@ -334,9 +351,10 @@ public class PluginsManager { * - 插件 * @return 是否成功 */ - public boolean reload(CommandSender sender, Plugin plugin) { - if (plugin != null) + public boolean reload(final CommandSender sender, final Plugin plugin) { + if (plugin != null) { return unload(sender, plugin) && load(sender, plugin.getName()); + } return false; } @@ -349,9 +367,10 @@ public class PluginsManager { * - 插件 * @return 是否成功 */ - public boolean reload(CommandSender sender, String name) { - if (name != null) + public boolean reload(final CommandSender sender, final String name) { + if (name != null) { return unload(sender, name) && load(sender, name); + } return false; } @@ -362,7 +381,7 @@ public class PluginsManager { * - 插件 * @return 是否成功 */ - public boolean reload(Plugin plugin) { + public boolean reload(final Plugin plugin) { return reload(Bukkit.getConsoleSender(), plugin); } @@ -370,18 +389,22 @@ public class PluginsManager { * 重载所有插件 */ public void reloadAll() { - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) - if (!isIgnored(plugin)) + for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { + if (!isIgnored(plugin)) { reload(plugin); + } + } } /** * 重载所有插件 */ - public void reloadAll(CommandSender sender) { - for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) - if (!isIgnored(plugin)) + public void reloadAll(final CommandSender sender) { + for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) { + if (!isIgnored(plugin)) { reload(sender, plugin); + } + } } /** @@ -393,7 +416,7 @@ public class PluginsManager { * - 插件 * @return 是否成功 */ - public boolean unload(CommandSender sender, Plugin plugin) { + public boolean unload(final CommandSender sender, final Plugin plugin) { return unload(sender, plugin.getName()); } @@ -407,10 +430,11 @@ public class PluginsManager { * @return 是否成功 */ @SuppressWarnings("unchecked") - public boolean unload(CommandSender sender, String name) { - if (sender == null) + public boolean unload(CommandSender sender, final String name) { + if (sender == null) { sender = Bukkit.getConsoleSender(); - PluginManager pluginManager = Bukkit.getPluginManager(); + } + final PluginManager pluginManager = Bukkit.getPluginManager(); SimpleCommandMap commandMap = null; List plugins = null; Map lookupNames = null; @@ -420,26 +444,26 @@ public class PluginsManager { return false; } try { - Field pluginsField = pluginManager.getClass().getDeclaredField("plugins"); + final Field pluginsField = pluginManager.getClass().getDeclaredField("plugins"); pluginsField.setAccessible(true); plugins = (List) pluginsField.get(pluginManager); - Field lookupNamesField = pluginManager.getClass().getDeclaredField("lookupNames"); + final Field lookupNamesField = pluginManager.getClass().getDeclaredField("lookupNames"); lookupNamesField.setAccessible(true); lookupNames = (Map) lookupNamesField.get(pluginManager); - Field commandMapField = pluginManager.getClass().getDeclaredField("commandMap"); + final Field commandMapField = pluginManager.getClass().getDeclaredField("commandMap"); commandMapField.setAccessible(true); commandMap = (SimpleCommandMap) commandMapField.get(pluginManager); - Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands"); + final Field knownCommandsField = SimpleCommandMap.class.getDeclaredField("knownCommands"); knownCommandsField.setAccessible(true); knownCommands = (Map) knownCommandsField.get(commandMap); - } catch (Exception e) { + } catch (final Exception e) { sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件 " + name + " 卸载失败!"); return false; } - for (Plugin next : pluginManager.getPlugins()) + for (final Plugin next : pluginManager.getPlugins()) { if (next.getName().equals(name)) { pluginManager.disablePlugin(next); if ((plugins != null) && (plugins.contains(next))) { @@ -453,10 +477,10 @@ public class PluginsManager { } if (commandMap != null) { - for (Iterator> it = knownCommands.entrySet().iterator(); it.hasNext();) { - Map.Entry entry = it.next(); + for (final Iterator> it = knownCommands.entrySet().iterator(); it.hasNext();) { + final Map.Entry entry = it.next(); if ((entry.getValue() instanceof PluginCommand)) { - PluginCommand command = (PluginCommand) entry.getValue(); + final PluginCommand command = (PluginCommand) entry.getValue(); if (command.getPlugin() == next) { command.unregister(commandMap); it.remove(); @@ -465,7 +489,14 @@ public class PluginsManager { } sender.sendMessage("§6卸载: §a注销插件 " + name + " 的所有命令!"); } + final ClassLoader cl = next.getClass().getClassLoader(); + try { + ((URLClassLoader) cl).close(); + } catch (final IOException ex) { + } + System.gc(); } + } sender.sendMessage("§6卸载: §a插件 " + name + " 已成功卸载!"); return true; } @@ -477,7 +508,7 @@ public class PluginsManager { * - 卸载插件 * @return 是否成功 */ - public boolean unload(Plugin plugin) { + public boolean unload(final Plugin plugin) { return unload(Bukkit.getConsoleSender(), plugin); } @@ -486,7 +517,7 @@ public class PluginsManager { * * @return 是否成功 */ - public boolean upgrade(CommandSender sender) { + public boolean upgrade(final CommandSender sender) { sender.sendMessage("§6升级: §a开始升级 服务器更新 目录下的所有插件!"); return upgrade(sender, null, null); } @@ -500,31 +531,33 @@ public class PluginsManager { * - 更新目录 * @return 是否成功 */ - public boolean upgrade(CommandSender sender, File directory, Plugin plugin) { + public boolean upgrade(final CommandSender sender, final File directory, final Plugin plugin) { boolean result = false; - PluginLoader loader = main.getPluginLoader(); + final PluginLoader loader = main.getPluginLoader(); File updateDirectory; - if (directory == null || !directory.isDirectory()) + if (directory == null || !directory.isDirectory()) { updateDirectory = Bukkit.getServer().getUpdateFolderFile(); - else + } else { updateDirectory = directory; + } try { sender.sendMessage("§6升级: §b从 " + updateDirectory.getCanonicalPath() + " 文件夹检索插件插件!"); } catch (SecurityException | IOException e1) { sender.sendMessage("§4异常: §c文件夹 " + updateDirectory.getName() + " 权限不足或IO错误!"); return false; } - for (File file : updateDirectory.listFiles()) { + for (final File file : updateDirectory.listFiles()) { PluginDescriptionFile description = null; try { description = loader.getPluginDescription(file); - String name = description.getName(); - if (plugin != null && !name.equals(plugin.getName())) + final String name = description.getName(); + if (plugin != null && !name.equals(plugin.getName())) { continue; + } result = true; sender.sendMessage("§6升级: §a开始升级 " + name + " 插件!"); reload(sender, name); - } catch (InvalidDescriptionException e) { + } catch (final InvalidDescriptionException e) { sender.sendMessage("§4异常: §c" + e.getMessage()); sender.sendMessage("§4文件: §c" + file.getName() + " 的plugin.yml文件存在错误!"); } @@ -538,7 +571,7 @@ public class PluginsManager { * * @return 是否成功 */ - public boolean upgrade(CommandSender sender, Plugin plugin) { + public boolean upgrade(final CommandSender sender, final Plugin plugin) { return upgrade(sender, null, plugin); } @@ -547,7 +580,7 @@ public class PluginsManager { * * @return 是否成功 */ - public boolean upgrade(File directory) { + public boolean upgrade(final File directory) { Bukkit.getConsoleSender().sendMessage("§6升级: §a开始升级 " + directory.getName() + " 目录下的所有插件!"); return upgrade(Bukkit.getConsoleSender(), directory, null); } diff --git a/src/main/java/cn/citycraft/Yum/manager/Repositories.java b/src/main/java/cn/citycraft/Yum/manager/Repositories.java index e0e5495..43b5401 100644 --- a/src/main/java/cn/citycraft/Yum/manager/Repositories.java +++ b/src/main/java/cn/citycraft/Yum/manager/Repositories.java @@ -15,22 +15,22 @@ import java.util.List; public class Repositories { public class PackageInfo { public String name; - public String url; public List plugins = new ArrayList<>(); + public String url; } public class Plugin { - public String groupId; public String artifactId; public String description; - public String version; + public String groupId; public List tags; + public String version; } public class Repository { public String id; - public String url; public String type; + public String url; } public class TagInfo { diff --git a/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java b/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java index 07d45ef..53a97d4 100644 --- a/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java @@ -32,37 +32,40 @@ import cn.citycraft.Yum.manager.Repositories.Repository;; */ public class RepositoryManager { Gson gson; - List repos; + org.bukkit.plugin.Plugin main; HashMap plugins; - org.bukkit.plugin.Plugin main; + List repos; - public RepositoryManager(org.bukkit.plugin.Plugin plugin) { + public RepositoryManager(final org.bukkit.plugin.Plugin plugin) { this.main = plugin; gson = new Gson(); plugins = new HashMap(); repos = new ArrayList(); } - public boolean addPackage(CommandSender sender, String urlstring) { - String json = getHtml(urlstring); - if (json.isEmpty()) + public boolean addPackage(final CommandSender sender, final String urlstring) { + final String json = getHtml(urlstring); + if (json.isEmpty()) { return false; - PackageInfo pkg = jsonToPackage(json); - if (pkg == null) + } + final PackageInfo pkg = jsonToPackage(json); + if (pkg == null) { return false; + } updatePackage(sender, pkg); return true; } - public boolean addRepositories(CommandSender sender, String urlstring) { - if (urlstring.isEmpty()) + public boolean addRepositories(final CommandSender sender, final String urlstring) { + if (urlstring.isEmpty()) { return false; + } repos.add(urlstring); return updateRepositories(sender, urlstring); } - public void cacheToJson(FileConfiguration config) { + public void cacheToJson(final FileConfiguration config) { config.set("repocache", gson.toJson(repos)); config.set("plugincache", gson.toJson(plugins)); } @@ -72,97 +75,106 @@ public class RepositoryManager { } public List getAllPlugin() { - List li = new ArrayList(); - for (Entry plugin : plugins.entrySet()) + final List li = new ArrayList(); + for (final Entry plugin : plugins.entrySet()) { li.add(plugin.getValue()); + } return li; } public List getAllPluginName() { - List li = new ArrayList(); - for (Entry plugin : plugins.entrySet()) + final List li = new ArrayList(); + for (final Entry plugin : plugins.entrySet()) { li.add(plugin.getValue().plugin.artifactId); + } return li; } public List getAllPluginsInfo() { - List li = new ArrayList(); - for (Entry plugin : plugins.entrySet()) { - Plugin pl = plugin.getValue().plugin; + final List li = new ArrayList(); + for (final Entry plugin : plugins.entrySet()) { + final Plugin pl = plugin.getValue().plugin; li.add(String.format("§d%s §a%s(%s) §6- §e%s", plugin.getValue().repo, pl.artifactId, pl.version, pl.description)); } return li; } - public String getHtml(String urlstring) { + public String getHtml(final String urlstring) { String html = ""; try { - URL url = new URL(urlstring); - BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8)); + final URL url = new URL(urlstring); + final BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8)); String line; - while ((line = br.readLine()) != null) + while ((line = br.readLine()) != null) { html += line; + } return html; - } catch (IOException e) { + } catch (final IOException e) { return null; } } - public PluginInfo getPlugin(String name) { - for (Entry plugin : plugins.entrySet()) - if (plugin.getValue().plugin.artifactId.equalsIgnoreCase(name)) + public PluginInfo getPlugin(final String name) { + for (final Entry plugin : plugins.entrySet()) { + if (plugin.getValue().plugin.artifactId.equalsIgnoreCase(name)) { return plugin.getValue(); + } + } return null; } - public List getPluginInfo(String name) { - List li = new ArrayList(); - for (Entry plugin : plugins.entrySet()) - if (plugin.getValue().plugin.artifactId.equalsIgnoreCase(name)) + public List getPluginInfo(final String name) { + final List li = new ArrayList(); + for (final Entry plugin : plugins.entrySet()) { + if (plugin.getValue().plugin.artifactId.equalsIgnoreCase(name)) { li.add(plugin.getValue()); + } + } return li; } - public PluginInfo getPluginInfo(String groupId, String artifactId) { + public PluginInfo getPluginInfo(final String groupId, final String artifactId) { return plugins.get(groupId + "." + artifactId); } - public boolean jsonToCache(FileConfiguration config) { - String repocache = config.getString("repocache"); - String plugincache = config.getString("plugincache"); + public boolean jsonToCache(final FileConfiguration config) { + final String repocache = config.getString("repocache"); + final String plugincache = config.getString("plugincache"); try { - if (!repocache.isEmpty()) + if (!repocache.isEmpty()) { repos = gson.fromJson(repocache, new TypeToken>() { }.getType()); - if (!plugincache.isEmpty()) + } + if (!plugincache.isEmpty()) { plugins = gson.fromJson(plugincache, new TypeToken>() { }.getType()); + } return true; - } catch (JsonSyntaxException e) { + } catch (final JsonSyntaxException e) { return false; } } - public PackageInfo jsonToPackage(String json) { + public PackageInfo jsonToPackage(final String json) { try { return gson.fromJson(json, PackageInfo.class); - } catch (JsonSyntaxException e) { + } catch (final JsonSyntaxException e) { return null; } } - public List jsonToRepositories(String json) { + public List jsonToRepositories(final String json) { try { return gson.fromJson(json, new TypeToken>() { }.getType()); - } catch (JsonSyntaxException e) { + } catch (final JsonSyntaxException e) { return new ArrayList(); } } - public void updatePackage(CommandSender sender, PackageInfo pkg) { - for (Plugin plugin : pkg.plugins) { - PluginInfo pi = new PluginInfo(); + public void updatePackage(final CommandSender sender, final PackageInfo pkg) { + for (final Plugin plugin : pkg.plugins) { + final PluginInfo pi = new PluginInfo(); pi.plugin = plugin; pi.url = pkg.url; pi.repo = pkg.name; @@ -171,29 +183,36 @@ public class RepositoryManager { sender.sendMessage("§6仓库: §e" + pkg.name + " §a更新成功!"); } - public boolean updateRepositories(CommandSender sender) { + public boolean updateRepositories(final CommandSender sender) { plugins.clear(); - for (String string : repos) - if (updateRepositories(sender, string)) + for (final String string : repos) { + if (updateRepositories(sender, string)) { sender.sendMessage("§6源: §e" + string + " §a更新成功!"); - else + } else { sender.sendMessage("§6源: §e" + string + " §c更新失败!"); + } + } return true; } public boolean updateRepositories(CommandSender sender, String urlstring) { - if (sender == null) + if (sender == null) { sender = Bukkit.getConsoleSender(); - if (!urlstring.endsWith("repo.info")) + } + if (!urlstring.endsWith("repo.info")) { urlstring = urlstring + "/repo.info"; - String json = getHtml(urlstring); - if (json.isEmpty()) + } + final String json = getHtml(urlstring); + if (json.isEmpty()) { return false; - List lrepo = jsonToRepositories(json); - if (lrepo.isEmpty()) + } + final List lrepo = jsonToRepositories(json); + if (lrepo.isEmpty()) { return false; - for (Repository repository : lrepo) + } + for (final Repository repository : lrepo) { addPackage(sender, repository.url); + } return true; } } diff --git a/src/main/java/cn/citycraft/Yum/manager/YumManager.java b/src/main/java/cn/citycraft/Yum/manager/YumManager.java index d5f221d..daf290a 100644 --- a/src/main/java/cn/citycraft/Yum/manager/YumManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/YumManager.java @@ -19,38 +19,41 @@ public class YumManager { Plugin plugin; - public YumManager(Plugin plugin) { + public YumManager(final Plugin plugin) { this.plugin = plugin; plugman = new PluginsManager(plugin); download = new DownloadManager(plugin); repo = new RepositoryManager(plugin); } - public static boolean install(CommandSender sender, String pluginname) { + public static boolean install(final CommandSender sender, final String pluginname) { return install(sender, pluginname, null); } - public static boolean install(CommandSender sender, String pluginname, String version) { - PluginInfo pi = repo.getPlugin(pluginname); - if (pi != null) - if (download.run(sender, pi.getMavenUrl(version))) + public static boolean install(final CommandSender sender, final String pluginname, final String version) { + final PluginInfo pi = repo.getPlugin(pluginname); + if (pi != null) { + if (download.run(sender, pi.getMavenUrl(version))) { return plugman.load(sender, pluginname); + } + } return false; } - public static boolean update(CommandSender sender, Plugin plugin) { + public static boolean update(final CommandSender sender, final Plugin plugin) { return update(sender, plugin, null); } - public static boolean update(CommandSender sender, Plugin plugin, String version) { - PluginInfo pi = repo.getPlugin(plugin.getName()); + public static boolean update(final CommandSender sender, final Plugin plugin, final String version) { + final PluginInfo pi = repo.getPlugin(plugin.getName()); if (pi != null) { if (download.run(sender, pi.getMavenUrl(version), new File(Bukkit.getUpdateFolderFile(), plugman.getPluginFile(plugin).getName()))) { sender.sendMessage("§6更新: §a已下载插件 " + plugin.getName() + " 到update文件夹 重启后自动更新(或使用upgrade直接升级)!"); return true; } - } else + } else { sender.sendMessage("§6更新: §c仓库缓存中未找到插件 " + plugin.getName()); + } return false; } }