From 8107333391a74f5b9db4257f0a58d3903cba5633 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 1 Sep 2015 19:09:29 +0800 Subject: [PATCH] add yummanager and modify plugininfo... --- src/main/java/cn/citycraft/Yum/Yum.java | 17 ++---- .../citycraft/Yum/commands/CommandDelete.java | 3 +- .../Yum/commands/CommandHandler.java | 5 +- .../citycraft/Yum/commands/CommandInfo.java | 3 +- .../Yum/commands/CommandInstall.java | 7 +-- .../citycraft/Yum/commands/CommandList.java | 3 +- .../citycraft/Yum/commands/CommandLoad.java | 3 +- .../citycraft/Yum/commands/CommandReload.java | 5 +- .../citycraft/Yum/commands/CommandRepo.java | 9 +-- .../citycraft/Yum/commands/CommandUnload.java | 3 +- .../citycraft/Yum/commands/CommandUpdate.java | 21 ++----- .../Yum/manager/AutoUpdateManager.java | 34 ----------- .../RepositoryManager.java | 17 +++--- .../cn/citycraft/Yum/manager/YumManager.java | 57 +++++++++++++++++++ .../citycraft/Yum/repository/PluginInfo.java | 5 +- 15 files changed, 101 insertions(+), 91 deletions(-) delete mode 100644 src/main/java/cn/citycraft/Yum/manager/AutoUpdateManager.java rename src/main/java/cn/citycraft/Yum/{repository => manager}/RepositoryManager.java (92%) create mode 100644 src/main/java/cn/citycraft/Yum/manager/YumManager.java diff --git a/src/main/java/cn/citycraft/Yum/Yum.java b/src/main/java/cn/citycraft/Yum/Yum.java index 6bb093b..b329fe6 100644 --- a/src/main/java/cn/citycraft/Yum/Yum.java +++ b/src/main/java/cn/citycraft/Yum/Yum.java @@ -7,9 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin; import cn.citycraft.Yum.commands.CommandHandler; import cn.citycraft.Yum.config.FileConfig; -import cn.citycraft.Yum.manager.DownloadManager; -import cn.citycraft.Yum.manager.PluginsManager; -import cn.citycraft.Yum.repository.RepositoryManager; +import cn.citycraft.Yum.manager.YumManager; /** * MC插件仓库 @@ -17,9 +15,7 @@ import cn.citycraft.Yum.repository.RepositoryManager; * @author 蒋天蓓 2015年8月21日下午5:14:39 */ public class Yum extends JavaPlugin { - public DownloadManager download; - public PluginsManager plugman; - public RepositoryManager repo; + public YumManager yumgr; public FileConfig config; @Override @@ -32,16 +28,13 @@ public class Yum extends JavaPlugin { CommandHandler cmdhandler = new CommandHandler(this); this.getCommand("yum").setExecutor(cmdhandler); this.getCommand("yum").setTabCompleter(cmdhandler); - - plugman = new PluginsManager(this); - download = new DownloadManager(this); - repo = new RepositoryManager(this); - repo.jsonToCache(config); + yumgr = new YumManager(this); + YumManager.repo.jsonToCache(config); } @Override public void onDisable() { - repo.cacheToJson(config); + YumManager.repo.cacheToJson(config); config.save(); } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java b/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java index fcc5def..c5cb0ad 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandDelete.java @@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; /** * 插件删除命令类 @@ -31,7 +32,7 @@ public class CommandDelete extends BaseCommand { String pluginname = args[0]; Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); if (plugin != null) { - main.plugman.deletePlugin(sender, plugin); + YumManager.plugman.deletePlugin(sender, plugin); } else { sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java b/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java index edd55fd..67646a1 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java @@ -17,6 +17,7 @@ import org.bukkit.entity.Player; import org.bukkit.util.StringUtil; import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; /** * 子命令处理类 @@ -131,7 +132,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter { String partialPlugin = args[1]; List plugins = null; if (args[0].equalsIgnoreCase("install")) - plugins = main.repo.getAllPluginName(); + plugins = YumManager.repo.getAllPluginName(); else if (args[0].equalsIgnoreCase("repo")) plugins = Arrays.asList(new String[] { "add", @@ -140,7 +141,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter { "update" }); else - plugins = main.plugman.getPluginNames(false); + plugins = YumManager.plugman.getPluginNames(false); StringUtil.copyPartialMatches(partialPlugin, plugins, completions); } Collections.sort(completions); diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandInfo.java b/src/main/java/cn/citycraft/Yum/commands/CommandInfo.java index 040ebfa..28b1025 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandInfo.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandInfo.java @@ -10,6 +10,7 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; import cn.citycraft.Yum.utils.StringUtil; /** @@ -50,7 +51,7 @@ public class CommandInfo extends BaseCommand { StringUtil.sendStringArray(sender, desc.getDepend()); sender.sendMessage("§6插件软依赖: §3" + (desc.getSoftDepend().size() == 0 ? "无" : "")); StringUtil.sendStringArray(sender, desc.getSoftDepend()); - sender.sendMessage("§6插件物理路径: §3" + main.plugman.getPluginFile(plugin).getAbsolutePath()); + sender.sendMessage("§6插件物理路径: §3" + YumManager.plugman.getPluginFile(plugin).getAbsolutePath()); } 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 23b7899..db5bbab 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandInstall.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandInstall.java @@ -9,7 +9,6 @@ import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import cn.citycraft.Yum.Yum; -import cn.citycraft.Yum.repository.PluginInfo; /** * 插件安装命令类 @@ -35,11 +34,7 @@ public class CommandInstall extends BaseCommand { Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() { @Override public void run() { - PluginInfo pi = main.repo.getPlugin(pluginname); - if (pi != null) - if (main.download.run(sender, pi.getMavenUrl())) { - main.plugman.load(sender, pluginname); - } + } }); } else { diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandList.java b/src/main/java/cn/citycraft/Yum/commands/CommandList.java index 4d2a925..44096ef 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandList.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandList.java @@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; /** * 插件查看命令类 @@ -35,7 +36,7 @@ public class CommandList extends BaseCommand { public void execute(CommandSender sender, String label, String[] args) throws CommandException { sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: "); for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { - sender.sendMessage("§6 - " + main.plugman.getFormattedName(plugin, true)); + 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 1ecc190..c6915df 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandLoad.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandLoad.java @@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; /** * 插件删除命令类 @@ -31,7 +32,7 @@ public class CommandLoad extends BaseCommand { String pluginname = args[0]; Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); if (plugin == null) { - main.plugman.load(sender, pluginname); + YumManager.plugman.load(sender, pluginname); } 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 fe024e3..f505599 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandReload.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandReload.java @@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; /** * 插件删除命令类 @@ -30,12 +31,12 @@ public class CommandReload extends BaseCommand { public void execute(CommandSender sender, String label, String[] args) throws CommandException { String pluginname = args[0]; if (pluginname.equalsIgnoreCase("all") || pluginname.equalsIgnoreCase("*")) { - main.plugman.reloadAll(sender); + YumManager.plugman.reloadAll(sender); return; } Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); if (plugin != null) { - main.plugman.reload(sender, plugin); + YumManager.plugman.reload(sender, plugin); } 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 66b1a95..439bfad 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java @@ -7,6 +7,7 @@ import org.bukkit.command.CommandException; import org.bukkit.command.CommandSender; import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; import cn.citycraft.Yum.utils.StringUtil; /** @@ -31,7 +32,7 @@ public class CommandRepo extends BaseCommand { switch (cmd) { case "add": if (args.length == 2) { - if (main.repo.addRepositories(args[1])) { + if (YumManager.repo.addRepositories(args[1])) { sender.sendMessage("§6仓库: §a插件信息已缓存!"); } else { sender.sendMessage("§6仓库: §c源地址未找到仓库信息或无法访问!"); @@ -42,14 +43,14 @@ public class CommandRepo extends BaseCommand { break; case "list": sender.sendMessage("§6仓库: §b缓存的插件信息如下 "); - StringUtil.sendStringArray(sender, main.repo.getAllPluginsInfo()); + StringUtil.sendStringArray(sender, YumManager.repo.getAllPluginsInfo()); break; case "clean": - main.repo.clean(); + YumManager.repo.clean(); sender.sendMessage("§6仓库: §a缓存的插件信息已清理!"); break; case "update": - main.repo.updateRepositories(sender); + YumManager.repo.updateRepositories(sender); sender.sendMessage("§6仓库: §a仓库缓存数据已更新!"); break; } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandUnload.java b/src/main/java/cn/citycraft/Yum/commands/CommandUnload.java index 9b55254..fd24b4a 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandUnload.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandUnload.java @@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.manager.YumManager; /** * 插件删除命令类 @@ -31,7 +32,7 @@ public class CommandUnload extends BaseCommand { String pluginname = args[0]; Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); if (plugin != null) { - main.plugman.unload(sender, plugin); + YumManager.plugman.unload(sender, plugin); } 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 68bafb0..a0a28c1 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java @@ -9,7 +9,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import cn.citycraft.Yum.Yum; -import cn.citycraft.Yum.repository.PluginInfo; +import cn.citycraft.Yum.manager.YumManager; /** * @author 蒋天蓓 2015年8月12日下午2:04:05 @@ -34,21 +34,10 @@ public class CommandUpdate extends BaseCommand { Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() { @Override public void run() { - PluginInfo pi = main.repo.getPlugin(pluginname); - if (pi != null) { - String fileurl; - if (args.length < 2) { - fileurl = pi.getMavenUrl(); - } else { - fileurl = pi.getMavenUrl(args[1]); - } - if (main.download.run(sender, fileurl, main.plugman.getPluginFile(plugin))) { - if (main.plugman.unload(sender, plugin)) { - main.plugman.load(sender, pluginname); - } - } - } - + if (args.length < 2) + YumManager.update(sender, plugin); + else + YumManager.update(sender, plugin, args[1]); } }); } else { diff --git a/src/main/java/cn/citycraft/Yum/manager/AutoUpdateManager.java b/src/main/java/cn/citycraft/Yum/manager/AutoUpdateManager.java deleted file mode 100644 index d048504..0000000 --- a/src/main/java/cn/citycraft/Yum/manager/AutoUpdateManager.java +++ /dev/null @@ -1,34 +0,0 @@ -package cn.citycraft.Yum.manager; - -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.plugin.Plugin; - -/** - * 自动更新类 - * - * @author 蒋天蓓 - * 2015年9月1日上午10:59:47 - */ -public class AutoUpdateManager { - Plugin plugin; - DownloadManager download; - PluginsManager plugman; - - public AutoUpdateManager(Plugin plugin) { - this.plugin = plugin; - plugman = new PluginsManager(plugin); - download = new DownloadManager(plugin); - } - - public boolean run(CommandSender sender) { - if (download.run(sender, "下载地址", plugman.getPluginFile(plugin))) { - plugman.reload(sender, plugin); - } - return false; - } - - public boolean run() { - return run(Bukkit.getConsoleSender()); - } -} diff --git a/src/main/java/cn/citycraft/Yum/repository/RepositoryManager.java b/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java similarity index 92% rename from src/main/java/cn/citycraft/Yum/repository/RepositoryManager.java rename to src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java index 13cfa66..15e9694 100644 --- a/src/main/java/cn/citycraft/Yum/repository/RepositoryManager.java +++ b/src/main/java/cn/citycraft/Yum/manager/RepositoryManager.java @@ -1,7 +1,7 @@ /** * */ -package cn.citycraft.Yum.repository; +package cn.citycraft.Yum.manager; import java.io.BufferedReader; import java.io.IOException; @@ -15,7 +15,10 @@ import java.util.Map.Entry; import org.bukkit.command.CommandSender; import org.bukkit.configuration.file.FileConfiguration; -import cn.citycraft.Yum.Yum; +import cn.citycraft.Yum.repository.Package; +import cn.citycraft.Yum.repository.Plugin; +import cn.citycraft.Yum.repository.PluginInfo; +import cn.citycraft.Yum.repository.Repository; import com.google.common.base.Charsets; import com.google.gson.Gson; @@ -32,10 +35,10 @@ public class RepositoryManager { List repos; HashMap plugins; - Yum main; + org.bukkit.plugin.Plugin main; - public RepositoryManager(Yum yum) { - this.main = yum; + public RepositoryManager(org.bukkit.plugin.Plugin plugin) { + this.main = plugin; gson = new Gson(); plugins = new HashMap(); repos = new ArrayList(); @@ -45,7 +48,7 @@ public class RepositoryManager { plugins.clear(); } - public void update(Package pkg) { + public void updatePackage(Package pkg) { for (Plugin plugin : pkg.plugins) { PluginInfo pi = new PluginInfo(); pi.plugin = plugin; @@ -113,7 +116,7 @@ public class RepositoryManager { if (pkg == null) { return false; } - update(pkg); + updatePackage(pkg); return true; } diff --git a/src/main/java/cn/citycraft/Yum/manager/YumManager.java b/src/main/java/cn/citycraft/Yum/manager/YumManager.java new file mode 100644 index 0000000..4dfc6c5 --- /dev/null +++ b/src/main/java/cn/citycraft/Yum/manager/YumManager.java @@ -0,0 +1,57 @@ +package cn.citycraft.Yum.manager; + +import org.bukkit.command.CommandSender; +import org.bukkit.plugin.Plugin; + +import cn.citycraft.Yum.repository.PluginInfo; + +/** + * 自动更新类 + * + * @author 蒋天蓓 + * 2015年9月1日上午10:59:47 + */ +public class YumManager { + Plugin plugin; + public static DownloadManager download; + public static PluginsManager plugman; + public static RepositoryManager repo; + + public YumManager(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) { + 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))) { + return plugman.load(sender, pluginname); + } + return false; + } + + public static boolean update(CommandSender sender, Plugin plugin) { + return update(sender, plugin, null); + } + + public static boolean update(CommandSender sender, Plugin plugin, String version) { + PluginInfo pi = repo.getPlugin(plugin.getName()); + if (pi != null) { + + if (download.run(sender, pi.getMavenUrl(version), plugman.getPluginFile(plugin))) { + return plugman.reload(sender, plugin); + } + return false; + } else { + sender.sendMessage("§6更新: §c仓库缓存中未找到插件 " + plugin.getName()); + return false; + } + } +} diff --git a/src/main/java/cn/citycraft/Yum/repository/PluginInfo.java b/src/main/java/cn/citycraft/Yum/repository/PluginInfo.java index 976b65f..280d482 100644 --- a/src/main/java/cn/citycraft/Yum/repository/PluginInfo.java +++ b/src/main/java/cn/citycraft/Yum/repository/PluginInfo.java @@ -12,13 +12,12 @@ public class PluginInfo { public String repo; public String getMavenUrl() { - return String.format(url + (url.endsWith("/") ? "" : "/") + "%1$s/%2$s/%3$s/%2$s-%3$s.jar", plugin.groupId.replace(".", "/"), - plugin.artifactId, plugin.version); + return getMavenUrl(null); } public String getMavenUrl(String version) { return String.format(url + (url.endsWith("/") ? "" : "/") + "%1$s/%2$s/%3$s/%2$s-%3$s.jar", plugin.groupId.replace(".", "/"), - plugin.artifactId, version); + plugin.artifactId, version == null ? plugin.version : version); } public String getFileName() {