update install and update command...

This commit is contained in:
j502647092 2015-08-23 10:10:13 +08:00
parent e661249388
commit 91cbc7fc7e
5 changed files with 82 additions and 114 deletions

View File

@ -1,65 +1,14 @@
/** /**
* *
*/ */
package cn.citycraft.Yum.api; package cn.citycraft.Yum.api;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.utils.DownloadManager;
import cn.citycraft.Yum.utils.PluginsManager;
/** /**
* Yum仓库插件API * Yum仓库插件API
* *
* @author 蒋天蓓 * @author 蒋天蓓 2015年8月22日下午4:43:41
* 2015年8月22日下午4:43:41
*/ */
public class YumApi { public class YumApi {
public static void install(Plugin yum, final String pluginname) {
install(yum, Bukkit.getConsoleSender(), pluginname);
}
public static void install(Plugin yum, final CommandSender sender, final String pluginname) {
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
final DownloadManager download = new DownloadManager(yum);
if (plugin == null) {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
@Override
public void run() {
if (download.run(sender, pluginname)) {
sender.sendMessage(PluginsManager.load(pluginname));
}
}
});
} else {
sender.sendMessage("§c插件已安装在服务器 需要更新请使用yum update " + pluginname + "!");
}
}
public static void update(Plugin yum, final String pluginname) {
update(yum, Bukkit.getConsoleSender(), pluginname);
}
public static void update(Plugin yum, final CommandSender sender, final String pluginname) {
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
final DownloadManager download = new DownloadManager(yum);
sender.sendMessage("§a开始更新插件: " + pluginname);
if (plugin != null) {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
@Override
public void run() {
sender.sendMessage(PluginsManager.unload(plugin));
PluginsManager.getPluginFile(plugin).delete();
if (download.run(sender, pluginname)) {
sender.sendMessage(PluginsManager.load(pluginname));
}
}
});
} else {
sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!");
}
}
} }

View File

@ -13,7 +13,7 @@ import cn.citycraft.Yum.utils.PluginsManager;
/** /**
* 插件安装命令类 * 插件安装命令类
* *
* @author 蒋天蓓 2015年8月12日下午2:04:05 * @author 蒋天蓓 2015年8月12日下午2:04:05
*/ */
public class CommandInstall extends BaseCommand { public class CommandInstall extends BaseCommand {
@ -27,11 +27,6 @@ public class CommandInstall extends BaseCommand {
this.yum = main; this.yum = main;
} }
@Override
public boolean isOnlyPlayerExecutable() {
return false;
};
@Override @Override
public void execute(final CommandSender sender, String label, String[] args) throws CommandException { public void execute(final CommandSender sender, String label, String[] args) throws CommandException {
final String pluginname = args[0]; final String pluginname = args[0];
@ -40,7 +35,7 @@ public class CommandInstall extends BaseCommand {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
@Override @Override
public void run() { public void run() {
if (yum.download.run(sender, pluginname)) { if (yum.download.install(sender, pluginname)) {
sender.sendMessage(PluginsManager.load(pluginname)); sender.sendMessage(PluginsManager.load(pluginname));
} }
} }
@ -48,7 +43,7 @@ public class CommandInstall extends BaseCommand {
} else { } else {
sender.sendMessage("§c插件已安装在服务器 需要更新请使用yum update " + pluginname + "!"); sender.sendMessage("§c插件已安装在服务器 需要更新请使用yum update " + pluginname + "!");
} }
} };
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
@ -59,4 +54,9 @@ public class CommandInstall extends BaseCommand {
public String getPossibleArguments() { public String getPossibleArguments() {
return "<插件名称>"; return "<插件名称>";
} }
@Override
public boolean isOnlyPlayerExecutable() {
return false;
}
} }

View File

@ -25,11 +25,6 @@ public class CommandUpdate extends BaseCommand {
this.yum = main; this.yum = main;
} }
@Override
public boolean isOnlyPlayerExecutable() {
return false;
};
@Override @Override
public void execute(final CommandSender sender, String label, String[] args) throws CommandException { public void execute(final CommandSender sender, String label, String[] args) throws CommandException {
final String pluginname = args[0]; final String pluginname = args[0];
@ -39,17 +34,17 @@ public class CommandUpdate extends BaseCommand {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
@Override @Override
public void run() { public void run() {
sender.sendMessage(PluginsManager.unload(plugin)); if (yum.download.update(sender, plugin)) {
PluginsManager.getPluginFile(plugin).delete(); sender.sendMessage(PluginsManager.unload(plugin));
if (yum.download.run(sender, pluginname)) { // PluginsManager.getPluginFile(plugin).delete();
sender.sendMessage(PluginsManager.load(pluginname)); sender.sendMessage(PluginsManager.load(plugin));
} }
} }
}); });
} else { } else {
sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!"); sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!");
} }
} };
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
@ -60,4 +55,9 @@ public class CommandUpdate extends BaseCommand {
public String getPossibleArguments() { public String getPossibleArguments() {
return "<插件名称>"; return "<插件名称>";
} }
@Override
public boolean isOnlyPlayerExecutable() {
return false;
}
} }

View File

@ -22,7 +22,24 @@ public class DownloadManager {
this.plugin = main; this.plugin = main;
} }
public boolean run(CommandSender sender, String pluginname) { private String getPer(double per) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
if (per > i) {
sb.append(" ");
} else {
sb.append("==");
}
}
sb.append(">");
return sb.toString();
}
public boolean install(CommandSender sender, String pluginname) {
return run(sender, pluginname, null);
}
public boolean run(CommandSender sender, String pluginname, String filename) {
String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar"; String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
// String url = "https://502647092.github.io/plugins/%1$s/%1$s.jar"; // String url = "https://502647092.github.io/plugins/%1$s/%1$s.jar";
BufferedInputStream in = null; BufferedInputStream in = null;
@ -31,31 +48,35 @@ public class DownloadManager {
sender = Bukkit.getConsoleSender(); sender = Bukkit.getConsoleSender();
} }
try { try {
String filename = pluginname + ".jar";
sender.sendMessage("§6开始下载: §3" + pluginname); sender.sendMessage("§6开始下载: §3" + pluginname);
URL fileUrl = new URL(String.format(url, pluginname)); URL fileUrl = new URL(String.format(url, pluginname));
sender.sendMessage("§6下载地址: §3http://********/" + filename); sender.sendMessage("§6下载地址: §3" + fileUrl.getPath());
int fileLength = fileUrl.openConnection().getContentLength(); int fileLength = fileUrl.openConnection().getContentLength();
sender.sendMessage("§6文件长度: §3" + fileLength); sender.sendMessage("§6文件长度: §3" + fileLength);
in = new BufferedInputStream(fileUrl.openStream()); in = new BufferedInputStream(fileUrl.openStream());
File file = new File(new File("plugins"), filename); File file = null;
if (filename == null) {
file = new File(new File("plugins"), pluginname + ".jar");
} else {
file = new File(new File("plugins/update"), filename);
}
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
sender.sendMessage("§d创建新目录: " + file.getParentFile().getAbsolutePath());
}
if (!file.exists()) { if (!file.exists()) {
file.createNewFile(); file.createNewFile();
sender.sendMessage("§d创建新文件: " + filename); sender.sendMessage("§d创建新文件: " + file.getName());
} }
fout = new FileOutputStream(file); fout = new FileOutputStream(file);
byte[] data = new byte[1024]; byte[] data = new byte[1024];
long downloaded = 0L; long downloaded = 0L;
int count; int count;
long time = System.currentTimeMillis();
while ((count = in.read(data)) != -1) { while ((count = in.read(data)) != -1) {
downloaded += count; downloaded += count;
fout.write(data, 0, count); fout.write(data, 0, count);
double percent = downloaded / fileLength * 10000; double percent = downloaded / fileLength * 10000;
if (System.currentTimeMillis() - time > 1000) { sender.sendMessage(String.format("§a已下载: §a" + getPer(percent) + " %.2f%%", percent));
sender.sendMessage(String.format("§a已下载: §a" + getPer(percent) + " %.2f%%", percent));
time = System.currentTimeMillis();
}
} }
sender.sendMessage("§6已下载: §a====================> 100%"); sender.sendMessage("§6已下载: §a====================> 100%");
sender.sendMessage("§a插件: " + pluginname + " 下载完成!"); sender.sendMessage("§a插件: " + pluginname + " 下载完成!");
@ -75,16 +96,9 @@ public class DownloadManager {
} }
} }
private String getPer(double per) { public boolean update(CommandSender sender, Plugin plugin) {
StringBuilder sb = new StringBuilder(); String pluginname = plugin.getName();
for (int i = 0; i < 10; i++) { String filename = PluginsManager.getPluginFile(plugin).getName();
if (per > i) { return run(sender, pluginname, filename);
sb.append(" ");
} else {
sb.append("==");
}
}
sb.append(">");
return sb.toString();
} }
} }

View File

@ -37,6 +37,14 @@ import com.google.common.base.Joiner;
*/ */
public class PluginsManager { public class PluginsManager {
public static boolean deletePlugin(Plugin plugin) {
ClassLoader cl = plugin.getClass().getClassLoader();
if ((cl instanceof URLClassLoader)) {
} else {
}
return false;
}
public static void disable(Plugin plugin) { public static void disable(Plugin plugin) {
if ((plugin.isEnabled()) && (plugin != null)) { if ((plugin.isEnabled()) && (plugin != null)) {
Bukkit.getPluginManager().disablePlugin(plugin); Bukkit.getPluginManager().disablePlugin(plugin);
@ -90,14 +98,6 @@ public class PluginsManager {
return getPluginByName(StringUtil.consolidateStrings(args, start)); return getPluginByName(StringUtil.consolidateStrings(args, start));
} }
public static List<String> getPluginNames(boolean fullName) {
List<String> plugins = new ArrayList<String>();
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName());
}
return plugins;
}
public static File getPluginFile(Plugin plugin) { public static File getPluginFile(Plugin plugin) {
File file = null; File file = null;
ClassLoader cl = plugin.getClass().getClassLoader(); ClassLoader cl = plugin.getClass().getClassLoader();
@ -110,12 +110,12 @@ public class PluginsManager {
return file; return file;
} }
public static boolean deletePlugin(Plugin plugin) { public static List<String> getPluginNames(boolean fullName) {
ClassLoader cl = plugin.getClass().getClassLoader(); List<String> plugins = new ArrayList<String>();
if ((cl instanceof URLClassLoader)) { for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
} else { plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName());
} }
return false; return plugins;
} }
public static String getPluginVersion(String name) { public static String getPluginVersion(String name) {
@ -157,22 +157,27 @@ public class PluginsManager {
} }
public static String load(Plugin plugin) { public static String load(Plugin plugin) {
return load(plugin.getName()); String filename = getPluginFile(plugin).getName();
return load(filename);
} }
public static String load(String name) { public static String load(String name) {
Plugin target = null; Plugin target = null;
File pluginDir = new File("plugins"); if (!name.endsWith(".jar")) {
name = name + ".jar";
if (!pluginDir.isDirectory()) {
return "§c插件目录不存在或IO错误!";
} }
File pluginFile = new File(pluginDir, name + ".jar"); File pluginDir = new File("plugins");
File updateDir = new File(pluginDir, "update");
if (!pluginFile.isFile()) if (!pluginDir.isDirectory())
return "§c在plugins目录未找到 " + name + " 插件 请确认文件是否存在!"; return "§c插件目录不存在或IO错误!";
File pluginFile = new File(pluginDir, name);
if (!pluginFile.isFile() && !new File(updateDir, name).isFile())
return "§c在插件目录和更新目录未找到 " + name + " 插件 请确认文件是否存在!";
try { try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile); target = Bukkit.getPluginManager().loadPlugin(pluginFile);