add autoupdate and plugin add des...

This commit is contained in:
502647092 2015-09-01 13:10:06 +08:00
parent c310706d33
commit d9c5446ecd
9 changed files with 84 additions and 35 deletions

View File

@ -24,20 +24,21 @@ public class Yum extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
CommandHandler cmdhandler = new CommandHandler(this);
this.getCommand("yum").setExecutor(cmdhandler);
this.getCommand("yum").setTabCompleter(cmdhandler);
plugman = new PluginsManager(this); plugman = new PluginsManager(this);
download = new DownloadManager(this); download = new DownloadManager(this);
repo = new RepositoryManager(this); repo = new RepositoryManager(this);
config = new FileConfig(this, "config.yml"); config = new FileConfig(this, "config.yml");
repo.jsonToCache(config.getString("cache")); repo.jsonToCache(config.getString("cache"));
CommandHandler cmdhandler = new CommandHandler(this);
this.getCommand("yum").setExecutor(cmdhandler);
this.getCommand("yum").setTabCompleter(cmdhandler);
this.getPluginLoader();
} }
@Override @Override
public void onDisable() { public void onDisable() {
if (config != null)
config.set("cache", repo.cacheToJson()); config.set("cache", repo.cacheToJson());
config.save();
} }
} }

View File

@ -4,6 +4,7 @@
package cn.citycraft.Yum.commands; package cn.citycraft.Yum.commands;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -34,7 +35,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
*/ */
public static String[] moveStrings(String[] args, int start) { public static String[] moveStrings(String[] args, int start) {
String[] ret = new String[args.length - start]; String[] ret = new String[args.length - start];
System.arraycopy(args, args.length - start, ret, 0, ret.length); System.arraycopy(args, start, ret, 0, ret.length);
return ret; return ret;
} }
@ -128,7 +129,17 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
} }
if (args.length == 2) { if (args.length == 2) {
String partialPlugin = args[1]; String partialPlugin = args[1];
List<String> plugins = main.plugman.getPluginNames(false); List<String> plugins = null;
if (args[0].equalsIgnoreCase("install"))
plugins = main.repo.getAllPluginName();
else if (args[0].equalsIgnoreCase("repo"))
plugins = Arrays.asList(new String[] {
"add",
"list",
"clean"
});
else
plugins = main.plugman.getPluginNames(false);
StringUtil.copyPartialMatches(partialPlugin, plugins, completions); StringUtil.copyPartialMatches(partialPlugin, plugins, completions);
} }
Collections.sort(completions); Collections.sort(completions);

View File

@ -30,10 +30,10 @@ public class CommandLoad 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];
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); main.plugman.load(sender, pluginname);
} else { } else {
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§c错误: 插件 " + pluginname + " 已加载到服务器!");
} }
}; };

View File

@ -34,12 +34,15 @@ public class CommandRepo extends BaseCommand {
main.repo.addRepositories(args[1]); main.repo.addRepositories(args[1]);
} }
sender.sendMessage("§6仓库: §a插件信息已缓存!"); sender.sendMessage("§6仓库: §a插件信息已缓存!");
break;
case "list": case "list":
sender.sendMessage("§6仓库: §b缓存的插件信息如下 "); sender.sendMessage("§6仓库: §b缓存的插件信息如下 ");
StringUtil.sendStringArray(sender, main.repo.getAllPluginString()); StringUtil.sendStringArray(sender, main.repo.getAllPluginString());
break;
case "clean": case "clean":
main.repo.clean(); main.repo.clean();
sender.sendMessage("§6仓库: §a缓存的插件信息已清理!"); sender.sendMessage("§6仓库: §a缓存的插件信息已清理!");
break;
} }
}; };

View File

@ -0,0 +1,34 @@
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,6 +1,3 @@
/**
*
*/
package cn.citycraft.Yum.manager; package cn.citycraft.Yum.manager;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
@ -99,9 +96,8 @@ public class DownloadManager {
* @return 是否成功 * @return 是否成功
*/ */
public boolean run(CommandSender sender, String urlstring, File file) { public boolean run(CommandSender sender, String urlstring, File file) {
URL url;
try { try {
url = new URL(urlstring); URL url = new URL(urlstring);
return run(sender, url, file); return run(sender, url, file);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
sender.sendMessage("§4错误: §c无法识别的URL地址..."); sender.sendMessage("§4错误: §c无法识别的URL地址...");

View File

@ -1,6 +1,3 @@
/**
*
*/
package cn.citycraft.Yum.manager; package cn.citycraft.Yum.manager;
import java.io.File; import java.io.File;
@ -30,7 +27,6 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredListener; import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.UnknownDependencyException; import org.bukkit.plugin.UnknownDependencyException;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.utils.StringUtil; import cn.citycraft.Yum.utils.StringUtil;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
@ -41,10 +37,10 @@ import com.google.common.base.Joiner;
* @author 蒋天蓓 2015年8月21日下午7:03:26 * @author 蒋天蓓 2015年8月21日下午7:03:26
*/ */
public class PluginsManager { public class PluginsManager {
Yum main; Plugin main;
public PluginsManager(Yum yum) { public PluginsManager(Plugin plugin) {
this.main = yum; this.main = plugin;
} }
/** /**
@ -272,26 +268,23 @@ public class PluginsManager {
*/ */
public boolean load(CommandSender sender, String name) { public boolean load(CommandSender sender, String name) {
Plugin target = null; Plugin target = null;
String filename = null;
if (sender == null) { if (sender == null) {
sender = Bukkit.getConsoleSender(); sender = Bukkit.getConsoleSender();
} }
if (!name.endsWith(".jar")) { if (!name.endsWith(".jar")) {
name = name + ".jar"; filename = name + ".jar";
} }
File pluginDir = new File("plugins"); File pluginDir = new File("plugins");
File updateDir = new File(pluginDir, "update"); File updateDir = new File(pluginDir, "update");
if (!pluginDir.isDirectory()) { if (!pluginDir.isDirectory()) {
sender.sendMessage("§c插件目录不存在或IO错误!"); sender.sendMessage("§c插件目录不存在或IO错误!");
return false; return false;
} }
File pluginFile = new File(pluginDir, name); File pluginFile = new File(pluginDir, filename);
if (!pluginFile.isFile() && !new File(updateDir, name).isFile()) { if (!pluginFile.isFile() && !new File(updateDir, filename).isFile()) {
pluginFile = null; pluginFile = null;
for (File file : pluginDir.listFiles()) { for (File file : pluginDir.listFiles()) {
if (file.getName().endsWith(".jar")) { if (file.getName().endsWith(".jar")) {
@ -306,7 +299,7 @@ public class PluginsManager {
} }
} }
if (pluginFile == null) { if (pluginFile == null) {
sender.sendMessage("§c在插件目录和更新目录均未找到 " + name + " 插件 请确认文件是否存在!"); sender.sendMessage("§6载入: §c在插件目录和更新目录均未找到 " + name + " 插件 请确认文件是否存在!");
return false; return false;
} }
} }
@ -448,13 +441,12 @@ public class PluginsManager {
if (commandMap != null) { if (commandMap != null) {
for (Iterator<Map.Entry<String, Command>> it = knownCommands.entrySet().iterator(); it.hasNext();) { for (Iterator<Map.Entry<String, Command>> it = knownCommands.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Command> entry = it.next(); Map.Entry<String, Command> entry = it.next();
if ((entry.getValue() instanceof PluginCommand)) { if ((entry.getValue() instanceof PluginCommand)) {
PluginCommand command = (PluginCommand) entry.getValue(); PluginCommand command = (PluginCommand) entry.getValue();
if (command.getPlugin() == next) { if (command.getPlugin() == next) {
command.unregister(commandMap); command.unregister(commandMap);
it.remove(); it.remove();
sender.sendMessage("§6卸载: §a插件 " + name + "" + command.getDescription() + " 命令已卸载!"); sender.sendMessage("§6卸载: §a插件 " + name + "" + command.getName() + " 命令已卸载!");
} }
} }
} }

View File

@ -12,5 +12,6 @@ package cn.citycraft.Yum.repository;
public class Plugin { public class Plugin {
public String groupId; public String groupId;
public String artifactId; public String artifactId;
public String description;
public String version; public String version;
} }

View File

@ -25,13 +25,15 @@ import com.google.gson.reflect.TypeToken;
* @author 蒋天蓓 * @author 蒋天蓓
*/ */
public class RepositoryManager { public class RepositoryManager {
Gson gson = new Gson(); Gson gson;
HashMap<String, PluginInfo> plugins = new HashMap<String, PluginInfo>(); HashMap<String, PluginInfo> plugins;
Yum main; Yum main;
public RepositoryManager(Yum yum) { public RepositoryManager(Yum yum) {
this.main = yum; this.main = yum;
gson = new Gson();
plugins = new HashMap<String, PluginInfo>();
} }
public void clean() { public void clean() {
@ -52,7 +54,8 @@ public class RepositoryManager {
} }
public boolean jsonToCache(String json) { public boolean jsonToCache(String json) {
if (json == "") { if (json == null || json == "") {
plugins = new HashMap<String, PluginInfo>();
return false; return false;
} }
try { try {
@ -127,11 +130,19 @@ public class RepositoryManager {
return li; return li;
} }
public List<String> getAllPluginName() {
List<String> li = new ArrayList<String>();
for (Entry<String, PluginInfo> plugin : plugins.entrySet()) {
li.add(plugin.getValue().plugin.artifactId);
}
return li;
}
public List<String> getAllPluginString() { public List<String> getAllPluginString() {
List<String> li = new ArrayList<String>(); List<String> li = new ArrayList<String>();
for (Entry<String, PluginInfo> plugin : plugins.entrySet()) { for (Entry<String, PluginInfo> plugin : plugins.entrySet()) {
Plugin pl = plugin.getValue().plugin; Plugin pl = plugin.getValue().plugin;
li.add(String.format("%s %s(%s)", pl.groupId, pl.artifactId, pl.version)); li.add(String.format("%s %s(%s) - %s", pl.groupId, pl.artifactId, pl.version, pl.description));
} }
return li; return li;
} }