From cf7368da66250757bdac9fa527a8b1d3af40824e Mon Sep 17 00:00:00 2001 From: 502647092 Date: Tue, 1 Sep 2015 15:36:54 +0800 Subject: [PATCH] add update repo... Signed-off-by: 502647092 --- src/main/java/cn/citycraft/Yum/Yum.java | 12 ++++-- .../Yum/commands/CommandHandler.java | 3 +- .../citycraft/Yum/commands/CommandRepo.java | 3 ++ .../Yum/repository/RepositoryManager.java | 40 ++++++++++++++----- 4 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/main/java/cn/citycraft/Yum/Yum.java b/src/main/java/cn/citycraft/Yum/Yum.java index a6f86b8..6bb093b 100644 --- a/src/main/java/cn/citycraft/Yum/Yum.java +++ b/src/main/java/cn/citycraft/Yum/Yum.java @@ -22,22 +22,26 @@ public class Yum extends JavaPlugin { public RepositoryManager repo; public FileConfig config; + @Override + public void onLoad() { + config = new FileConfig(this, "config.yml"); + } + @Override public void onEnable() { 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); - config = new FileConfig(this, "config.yml"); - repo.jsonToCache(config.getString("cache")); + repo.jsonToCache(config); } @Override public void onDisable() { - if (config != null) - config.set("cache", repo.cacheToJson()); + repo.cacheToJson(config); config.save(); } diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java b/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java index 8ac332d..edd55fd 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandHandler.java @@ -136,7 +136,8 @@ public class CommandHandler implements CommandExecutor, TabCompleter { plugins = Arrays.asList(new String[] { "add", "list", - "clean" + "clean", + "update" }); else plugins = main.plugman.getPluginNames(false); diff --git a/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java b/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java index b02de54..458493f 100644 --- a/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java +++ b/src/main/java/cn/citycraft/Yum/commands/CommandRepo.java @@ -48,6 +48,9 @@ public class CommandRepo extends BaseCommand { main.repo.clean(); sender.sendMessage("§6仓库: §a缓存的插件信息已清理!"); break; + case "update": + main.repo.updateRepositories(sender); + break; } }; diff --git a/src/main/java/cn/citycraft/Yum/repository/RepositoryManager.java b/src/main/java/cn/citycraft/Yum/repository/RepositoryManager.java index 450b9d5..dbf1d96 100644 --- a/src/main/java/cn/citycraft/Yum/repository/RepositoryManager.java +++ b/src/main/java/cn/citycraft/Yum/repository/RepositoryManager.java @@ -12,6 +12,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map.Entry; +import org.bukkit.command.CommandSender; +import org.bukkit.configuration.file.FileConfiguration; + import cn.citycraft.Yum.Yum; import com.google.common.base.Charsets; @@ -26,6 +29,7 @@ import com.google.gson.reflect.TypeToken; */ public class RepositoryManager { Gson gson; + List repos; HashMap plugins; Yum main; @@ -34,6 +38,7 @@ public class RepositoryManager { this.main = yum; gson = new Gson(); plugins = new HashMap(); + repos = new ArrayList(); } public void clean() { @@ -50,24 +55,40 @@ public class RepositoryManager { } } - public String cacheToJson() { - return gson.toJson(plugins); + public void cacheToJson(FileConfiguration config) { + config.set("repocache", gson.toJson(repos)); + config.set("plugincache", gson.toJson(plugins)); } - public boolean jsonToCache(String json) { - if (json == null || json == "") { - plugins = new HashMap(); - return false; - } + public boolean jsonToCache(FileConfiguration config) { + String repocache = config.getString("repocache"); + String plugincache = config.getString("plugincache"); try { - plugins = gson.fromJson(json, new TypeToken>() { - }.getType()); + if (repocache != null && repocache != "") + repos = gson.fromJson(repocache, new TypeToken>() { + }.getType()); + if (plugincache != null && plugincache != "") + plugins = gson.fromJson(plugincache, new TypeToken>() { + }.getType()); return true; } catch (JsonSyntaxException e) { return false; } } + public boolean updateRepositories(CommandSender sender) { + plugins.clear(); + for (String string : repos) { + if (addRepositories(string)) { + sender.sendMessage("§6源: §e" + string + " §a更新成功!"); + } else { + sender.sendMessage("§6源: §e" + string + " §c更新失败!"); + } + } + sender.sendMessage("§6源: §a所有的源已更新完成!"); + return true; + } + public boolean addRepositories(String urlstring) { String json = getHtml(urlstring); if (json == "") { @@ -80,6 +101,7 @@ public class RepositoryManager { for (Repository repository : lrepo) { addPackage(repository.url); } + repos.add(urlstring); return true; }