add yummanager and modify plugininfo...

This commit is contained in:
502647092 2015-09-01 19:09:29 +08:00
parent 8f8e3b3465
commit 8107333391
15 changed files with 101 additions and 91 deletions

View File

@ -7,9 +7,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.Yum.commands.CommandHandler; import cn.citycraft.Yum.commands.CommandHandler;
import cn.citycraft.Yum.config.FileConfig; import cn.citycraft.Yum.config.FileConfig;
import cn.citycraft.Yum.manager.DownloadManager; import cn.citycraft.Yum.manager.YumManager;
import cn.citycraft.Yum.manager.PluginsManager;
import cn.citycraft.Yum.repository.RepositoryManager;
/** /**
* MC插件仓库 * MC插件仓库
@ -17,9 +15,7 @@ import cn.citycraft.Yum.repository.RepositoryManager;
* @author 蒋天蓓 2015年8月21日下午5:14:39 * @author 蒋天蓓 2015年8月21日下午5:14:39
*/ */
public class Yum extends JavaPlugin { public class Yum extends JavaPlugin {
public DownloadManager download; public YumManager yumgr;
public PluginsManager plugman;
public RepositoryManager repo;
public FileConfig config; public FileConfig config;
@Override @Override
@ -32,16 +28,13 @@ public class Yum extends JavaPlugin {
CommandHandler cmdhandler = new CommandHandler(this); CommandHandler cmdhandler = new CommandHandler(this);
this.getCommand("yum").setExecutor(cmdhandler); this.getCommand("yum").setExecutor(cmdhandler);
this.getCommand("yum").setTabCompleter(cmdhandler); this.getCommand("yum").setTabCompleter(cmdhandler);
yumgr = new YumManager(this);
plugman = new PluginsManager(this); YumManager.repo.jsonToCache(config);
download = new DownloadManager(this);
repo = new RepositoryManager(this);
repo.jsonToCache(config);
} }
@Override @Override
public void onDisable() { public void onDisable() {
repo.cacheToJson(config); YumManager.repo.cacheToJson(config);
config.save(); config.save();
} }

View File

@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum; import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
/** /**
* 插件删除命令类 * 插件删除命令类
@ -31,7 +32,7 @@ public class CommandDelete extends BaseCommand {
String pluginname = args[0]; String pluginname = args[0];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) { if (plugin != null) {
main.plugman.deletePlugin(sender, plugin); YumManager.plugman.deletePlugin(sender, plugin);
} else { } else {
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
} }

View File

@ -17,6 +17,7 @@ import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil; import org.bukkit.util.StringUtil;
import cn.citycraft.Yum.Yum; 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]; String partialPlugin = args[1];
List<String> plugins = null; List<String> plugins = null;
if (args[0].equalsIgnoreCase("install")) if (args[0].equalsIgnoreCase("install"))
plugins = main.repo.getAllPluginName(); plugins = YumManager.repo.getAllPluginName();
else if (args[0].equalsIgnoreCase("repo")) else if (args[0].equalsIgnoreCase("repo"))
plugins = Arrays.asList(new String[] { plugins = Arrays.asList(new String[] {
"add", "add",
@ -140,7 +141,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
"update" "update"
}); });
else else
plugins = main.plugman.getPluginNames(false); plugins = YumManager.plugman.getPluginNames(false);
StringUtil.copyPartialMatches(partialPlugin, plugins, completions); StringUtil.copyPartialMatches(partialPlugin, plugins, completions);
} }
Collections.sort(completions); Collections.sort(completions);

View File

@ -10,6 +10,7 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import cn.citycraft.Yum.Yum; import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
import cn.citycraft.Yum.utils.StringUtil; import cn.citycraft.Yum.utils.StringUtil;
/** /**
@ -50,7 +51,7 @@ public class CommandInfo extends BaseCommand {
StringUtil.sendStringArray(sender, desc.getDepend()); StringUtil.sendStringArray(sender, desc.getDepend());
sender.sendMessage("§6插件软依赖: §3" + (desc.getSoftDepend().size() == 0 ? "" : "")); sender.sendMessage("§6插件软依赖: §3" + (desc.getSoftDepend().size() == 0 ? "" : ""));
StringUtil.sendStringArray(sender, desc.getSoftDepend()); StringUtil.sendStringArray(sender, desc.getSoftDepend());
sender.sendMessage("§6插件物理路径: §3" + main.plugman.getPluginFile(plugin).getAbsolutePath()); sender.sendMessage("§6插件物理路径: §3" + YumManager.plugman.getPluginFile(plugin).getAbsolutePath());
} else { } else {
sender.sendMessage("§4错误: §c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§4错误: §c插件 " + pluginname + " 不存在或已卸载!");
} }

View File

@ -9,7 +9,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum; 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() { Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
@Override @Override
public void run() { 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 { } else {

View File

@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum; 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 { public void execute(CommandSender sender, String label, String[] args) throws CommandException {
sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: "); sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: ");
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) { for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
sender.sendMessage("§6 - " + main.plugman.getFormattedName(plugin, true)); sender.sendMessage("§6 - " + YumManager.plugman.getFormattedName(plugin, true));
} }
} }

View File

@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum; import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
/** /**
* 插件删除命令类 * 插件删除命令类
@ -31,7 +32,7 @@ public class CommandLoad extends BaseCommand {
String pluginname = args[0]; String pluginname = args[0];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin == null) { if (plugin == null) {
main.plugman.load(sender, pluginname); YumManager.plugman.load(sender, pluginname);
} else { } else {
sender.sendMessage("§c错误: 插件 " + pluginname + " 已加载到服务器!"); sender.sendMessage("§c错误: 插件 " + pluginname + " 已加载到服务器!");
} }

View File

@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum; 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 { public void execute(CommandSender sender, String label, String[] args) throws CommandException {
String pluginname = args[0]; String pluginname = args[0];
if (pluginname.equalsIgnoreCase("all") || pluginname.equalsIgnoreCase("*")) { if (pluginname.equalsIgnoreCase("all") || pluginname.equalsIgnoreCase("*")) {
main.plugman.reloadAll(sender); YumManager.plugman.reloadAll(sender);
return; return;
} }
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) { if (plugin != null) {
main.plugman.reload(sender, plugin); YumManager.plugman.reload(sender, plugin);
} else { } else {
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
} }

View File

@ -7,6 +7,7 @@ import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import cn.citycraft.Yum.Yum; import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
import cn.citycraft.Yum.utils.StringUtil; import cn.citycraft.Yum.utils.StringUtil;
/** /**
@ -31,7 +32,7 @@ public class CommandRepo extends BaseCommand {
switch (cmd) { switch (cmd) {
case "add": case "add":
if (args.length == 2) { if (args.length == 2) {
if (main.repo.addRepositories(args[1])) { if (YumManager.repo.addRepositories(args[1])) {
sender.sendMessage("§6仓库: §a插件信息已缓存!"); sender.sendMessage("§6仓库: §a插件信息已缓存!");
} else { } else {
sender.sendMessage("§6仓库: §c源地址未找到仓库信息或无法访问!"); sender.sendMessage("§6仓库: §c源地址未找到仓库信息或无法访问!");
@ -42,14 +43,14 @@ public class CommandRepo extends BaseCommand {
break; break;
case "list": case "list":
sender.sendMessage("§6仓库: §b缓存的插件信息如下 "); sender.sendMessage("§6仓库: §b缓存的插件信息如下 ");
StringUtil.sendStringArray(sender, main.repo.getAllPluginsInfo()); StringUtil.sendStringArray(sender, YumManager.repo.getAllPluginsInfo());
break; break;
case "clean": case "clean":
main.repo.clean(); YumManager.repo.clean();
sender.sendMessage("§6仓库: §a缓存的插件信息已清理!"); sender.sendMessage("§6仓库: §a缓存的插件信息已清理!");
break; break;
case "update": case "update":
main.repo.updateRepositories(sender); YumManager.repo.updateRepositories(sender);
sender.sendMessage("§6仓库: §a仓库缓存数据已更新!"); sender.sendMessage("§6仓库: §a仓库缓存数据已更新!");
break; break;
} }

View File

@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum; import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
/** /**
* 插件删除命令类 * 插件删除命令类
@ -31,7 +32,7 @@ public class CommandUnload extends BaseCommand {
String pluginname = args[0]; String pluginname = args[0];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) { if (plugin != null) {
main.plugman.unload(sender, plugin); YumManager.plugman.unload(sender, plugin);
} else { } else {
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
} }

View File

@ -9,7 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum; import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.repository.PluginInfo; import cn.citycraft.Yum.manager.YumManager;
/** /**
* @author 蒋天蓓 2015年8月12日下午2:04:05 * @author 蒋天蓓 2015年8月12日下午2:04:05
@ -34,21 +34,10 @@ public class CommandUpdate extends BaseCommand {
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
@Override @Override
public void run() { public void run() {
PluginInfo pi = main.repo.getPlugin(pluginname); if (args.length < 2)
if (pi != null) { YumManager.update(sender, plugin);
String fileurl; else
if (args.length < 2) { YumManager.update(sender, plugin, args[1]);
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);
}
}
}
} }
}); });
} else { } else {

View File

@ -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());
}
}

View File

@ -1,7 +1,7 @@
/** /**
* *
*/ */
package cn.citycraft.Yum.repository; package cn.citycraft.Yum.manager;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
@ -15,7 +15,10 @@ import java.util.Map.Entry;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration; 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.common.base.Charsets;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -32,10 +35,10 @@ public class RepositoryManager {
List<String> repos; List<String> repos;
HashMap<String, PluginInfo> plugins; HashMap<String, PluginInfo> plugins;
Yum main; org.bukkit.plugin.Plugin main;
public RepositoryManager(Yum yum) { public RepositoryManager(org.bukkit.plugin.Plugin plugin) {
this.main = yum; this.main = plugin;
gson = new Gson(); gson = new Gson();
plugins = new HashMap<String, PluginInfo>(); plugins = new HashMap<String, PluginInfo>();
repos = new ArrayList<String>(); repos = new ArrayList<String>();
@ -45,7 +48,7 @@ public class RepositoryManager {
plugins.clear(); plugins.clear();
} }
public void update(Package pkg) { public void updatePackage(Package pkg) {
for (Plugin plugin : pkg.plugins) { for (Plugin plugin : pkg.plugins) {
PluginInfo pi = new PluginInfo(); PluginInfo pi = new PluginInfo();
pi.plugin = plugin; pi.plugin = plugin;
@ -113,7 +116,7 @@ public class RepositoryManager {
if (pkg == null) { if (pkg == null) {
return false; return false;
} }
update(pkg); updatePackage(pkg);
return true; return true;
} }

View File

@ -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;
}
}
}

View File

@ -12,13 +12,12 @@ public class PluginInfo {
public String repo; public String repo;
public String getMavenUrl() { public String getMavenUrl() {
return String.format(url + (url.endsWith("/") ? "" : "/") + "%1$s/%2$s/%3$s/%2$s-%3$s.jar", plugin.groupId.replace(".", "/"), return getMavenUrl(null);
plugin.artifactId, plugin.version);
} }
public String getMavenUrl(String version) { 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(".", "/"), 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() { public String getFileName() {