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

View File

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

View File

@ -22,7 +22,24 @@ public class DownloadManager {
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 = "https://502647092.github.io/plugins/%1$s/%1$s.jar";
BufferedInputStream in = null;
@ -31,31 +48,35 @@ public class DownloadManager {
sender = Bukkit.getConsoleSender();
}
try {
String filename = pluginname + ".jar";
sender.sendMessage("§6开始下载: §3" + 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();
sender.sendMessage("§6文件长度: §3" + fileLength);
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()) {
file.createNewFile();
sender.sendMessage("§d创建新文件: " + filename);
sender.sendMessage("§d创建新文件: " + file.getName());
}
fout = new FileOutputStream(file);
byte[] data = new byte[1024];
long downloaded = 0L;
int count;
long time = System.currentTimeMillis();
while ((count = in.read(data)) != -1) {
downloaded += count;
fout.write(data, 0, count);
double percent = downloaded / fileLength * 10000;
if (System.currentTimeMillis() - time > 1000) {
sender.sendMessage(String.format("§a已下载: §a" + getPer(percent) + " %.2f%%", percent));
time = System.currentTimeMillis();
}
sender.sendMessage(String.format("§a已下载: §a" + getPer(percent) + " %.2f%%", percent));
}
sender.sendMessage("§6已下载: §a====================> 100%");
sender.sendMessage("§a插件: " + pluginname + " 下载完成!");
@ -75,16 +96,9 @@ public class DownloadManager {
}
}
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 update(CommandSender sender, Plugin plugin) {
String pluginname = plugin.getName();
String filename = PluginsManager.getPluginFile(plugin).getName();
return run(sender, pluginname, filename);
}
}

View File

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