add Download info...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092 2015-08-21 22:52:20 +08:00
parent 0661c52fb2
commit 14e6281a56
4 changed files with 119 additions and 105 deletions

View File

@ -17,11 +17,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>

View File

@ -15,8 +15,7 @@ import cn.citycraft.Yum.utils.PluginUtil;
/**
* MC插件仓库
*
* @author 蒋天蓓
* 2015年8月21日下午5:14:39
* @author 蒋天蓓 2015年8月21日下午5:14:39
*/
public class Yum extends JavaPlugin {
@Override
@ -38,22 +37,28 @@ public class Yum extends JavaPlugin {
Plugin plugin = this.getServer().getPluginManager().getPlugin(args[1]);
switch (args[0]) {
case "install":
if (DownloadUtils.download(sender, getDataFolder().getParent(), args[1]))
sender.sendMessage(PluginUtil.load(args[1]));
if (plugin != null) {
if (DownloadUtils.download(sender, args[1])) {
sender.sendMessage(PluginUtil.load(args[1]));
}
} else {
sender.sendMessage("§c插件已安装在服务器!");
}
break;
case "remove":
if (plugin != null) {
sender.sendMessage(PluginUtil.unload(plugin));
} else {
sender.sendMessage("插件不存在或已卸载!");
sender.sendMessage("§c插件不存在或已卸载!");
}
break;
case "update":
if (plugin != null) {
if (DownloadUtils.download(sender, getDataFolder().getParent(), args[1]))
if (DownloadUtils.download(sender, args[1])) {
sender.sendMessage(PluginUtil.load(args[1]));
}
} else {
sender.sendMessage("插件不存在或已卸载!");
sender.sendMessage("§c插件不存在或已卸载!");
}
break;
}

View File

@ -12,29 +12,28 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
/**
* @author 蒋天蓓
* 2015年8月21日下午6:08:09
* TODO
* @author 蒋天蓓 2015年8月21日下午6:08:09 TODO
*/
public class DownloadUtils {
public static boolean download(CommandSender sender, String dir, String pluginname) {
public static boolean download(CommandSender sender, String pluginname) {
String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
BufferedInputStream in = null;
FileOutputStream fout = null;
if (sender == null)
if (sender == null) {
sender = Bukkit.getConsoleSender();
}
try {
String filename = pluginname + ".jar";
sender.sendMessage("开始下载: " + pluginname);
sender.sendMessage("§3开始下载: " + pluginname);
URL fileUrl = new URL(String.format(url, pluginname));
sender.sendMessage("下载地址: http://********/" + filename);
sender.sendMessage("§3下载地址: http://********/" + filename);
int fileLength = fileUrl.openConnection().getContentLength();
sender.sendMessage("文件长度: " + fileLength);
sender.sendMessage("§3文件长度: " + fileLength);
in = new BufferedInputStream(fileUrl.openStream());
File file = new File(dir, filename);
File file = new File(new File("plugins"), filename);
if (!file.exists()) {
file.createNewFile();
sender.sendMessage("创建新文件: " + filename);
sender.sendMessage("§d创建新文件: " + filename);
}
fout = new FileOutputStream(file);
byte[] data = new byte[1024];
@ -46,13 +45,15 @@ public class DownloadUtils {
fout.write(data, 0, count);
double percent = downloaded / fileLength * 10000;
if (System.currentTimeMillis() - time > 1000) {
sender.sendMessage(String.format("已下载: ====================> %.2f%%", percent));
sender.sendMessage(String.format("§a已下载: " + getPer(percent) + " %.2f%%", percent));
time = System.currentTimeMillis();
}
}
sender.sendMessage("§a已下载: ====================> 100%");
sender.sendMessage("§a插件: " + pluginname + "下载完成!");
return true;
} catch (Exception ex) {
sender.sendMessage("插件下载失败!");
sender.sendMessage("§c插件下载失败!");
ex.printStackTrace();
return false;
} finally {
@ -65,4 +66,17 @@ public class DownloadUtils {
}
}
}
private static 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();
}
}

View File

@ -33,25 +33,10 @@ import com.google.common.base.Joiner;
/**
* 插件管理类
*
* @author 蒋天蓓
* 2015年8月21日下午7:03:26
* @author 蒋天蓓 2015年8月21日下午7:03:26
*/
public class PluginUtil {
public static void enable(Plugin plugin) {
if ((!plugin.isEnabled()) && (plugin != null)) {
Bukkit.getPluginManager().enablePlugin(plugin);
}
}
public static void enableAll() {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (!isIgnored(plugin)) {
enable(plugin);
}
}
}
public static void disable(Plugin plugin) {
if ((plugin.isEnabled()) && (plugin != null)) {
Bukkit.getPluginManager().disablePlugin(plugin);
@ -66,6 +51,20 @@ public class PluginUtil {
}
}
public static void enable(Plugin plugin) {
if ((!plugin.isEnabled()) && (plugin != null)) {
Bukkit.getPluginManager().enablePlugin(plugin);
}
}
public static void enableAll() {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (!isIgnored(plugin)) {
enable(plugin);
}
}
}
public static String getFormattedName(Plugin plugin) {
return getFormattedName(plugin, false);
}
@ -73,15 +72,12 @@ public class PluginUtil {
public static String getFormattedName(Plugin plugin, boolean includeVersions) {
ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED;
String pluginName = color + plugin.getName();
if (includeVersions)
if (includeVersions) {
pluginName = pluginName + " (" + plugin.getDescription().getVersion() + ")";
}
return pluginName;
}
public static Plugin getPluginByName(String[] args, int start) {
return getPluginByName(StringUtil.consolidateStrings(args, start));
}
public static Plugin getPluginByName(String name) {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (name.equalsIgnoreCase(plugin.getName()))
@ -90,10 +86,15 @@ public class PluginUtil {
return null;
}
public static Plugin getPluginByName(String[] args, int 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())
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName());
}
return plugins;
}
@ -118,9 +119,8 @@ public class PluginUtil {
}
}
}
if (parsedCommands.isEmpty()) {
return "没有注册命令!";
}
if (parsedCommands.isEmpty())
return "§c没有注册命令!";
return Joiner.on(", ").join(parsedCommands);
}
@ -150,23 +150,22 @@ public class PluginUtil {
}
File pluginFile = new File(pluginDir, name + ".jar");
if (!pluginFile.isFile()) {
return "插件不存在!";
}
if (!pluginFile.isFile())
return "§c在plugins目录未找到 " + name + " 插件 请确认文件是否存在!";
try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile);
} catch (InvalidDescriptionException e) {
e.printStackTrace();
return "错误的插件信息!";
return "§c插件 " + name + " 的plugin.yml文件存在错误!";
} catch (InvalidPluginException e) {
e.printStackTrace();
return "错误的插件!";
return "§c文件 " + name + " 不是一个可载入的插件!";
}
target.onLoad();
Bukkit.getPluginManager().enablePlugin(target);
return "插件已载入!";
return "§a插件 " + name + " 成功载入到服务器!";
}
public static void reload(Plugin plugin) {
@ -226,41 +225,42 @@ public class PluginUtil {
commands = (Map<String, Command>) knownCommandsField.get(commandMap);
} catch (NoSuchFieldException e) {
e.printStackTrace();
return "插件卸载失败!";
return "§c插件 " + name + " 卸载失败!";
} catch (IllegalAccessException e) {
e.printStackTrace();
return "插件已失败!";
return "§c插件 " + name + " 卸载失败!";
}
}
pluginManager.disablePlugin(plugin);
if ((plugins != null) && (plugins.contains(plugin))) {
if (plugins != null && plugins.contains(plugin)) {
plugins.remove(plugin);
}
if ((names != null) && (names.containsKey(name))) {
if (names != null && names.containsKey(name)) {
names.remove(name);
}
Iterator<RegisteredListener> it;
if ((listeners != null) && (reloadlisteners)) {
if (listeners != null && reloadlisteners) {
for (SortedSet<RegisteredListener> set : listeners.values()) {
for (it = set.iterator(); it.hasNext();) {
for (Iterator<RegisteredListener> it = set.iterator(); it.hasNext();) {
RegisteredListener value = it.next();
if (value.getPlugin() == plugin)
if (value.getPlugin() == plugin) {
it.remove();
}
}
}
}
Iterator<Map.Entry<String, Command>> cmdit;
if (commandMap != null) {
for (cmdit = commands.entrySet().iterator(); cmdit.hasNext();) {
Map.Entry<String, Command> entry = cmdit.next();
if ((entry.getValue() instanceof PluginCommand)) {
for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Command> entry = it.next();
if (entry.getValue() instanceof PluginCommand) {
PluginCommand c = (PluginCommand) entry.getValue();
if (c.getPlugin() == plugin) {
c.unregister(commandMap);
cmdit.remove();
it.remove();
}
}
}
@ -277,6 +277,6 @@ public class PluginUtil {
}
System.gc();
return "插件已卸载!";
return "§a插件 " + name + " 成功卸载!";
}
}