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"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </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"> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>

View File

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

View File

@ -12,29 +12,28 @@ import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
/** /**
* @author 蒋天蓓 * @author 蒋天蓓 2015年8月21日下午6:08:09 TODO
* 2015年8月21日下午6:08:09
* TODO
*/ */
public class DownloadUtils { 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"; String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
BufferedInputStream in = null; BufferedInputStream in = null;
FileOutputStream fout = null; FileOutputStream fout = null;
if (sender == null) if (sender == null) {
sender = Bukkit.getConsoleSender(); sender = Bukkit.getConsoleSender();
}
try { try {
String filename = pluginname + ".jar"; String filename = pluginname + ".jar";
sender.sendMessage("开始下载: " + pluginname); sender.sendMessage("§3开始下载: " + pluginname);
URL fileUrl = new URL(String.format(url, pluginname)); URL fileUrl = new URL(String.format(url, pluginname));
sender.sendMessage("下载地址: http://********/" + filename); sender.sendMessage("§3下载地址: http://********/" + filename);
int fileLength = fileUrl.openConnection().getContentLength(); int fileLength = fileUrl.openConnection().getContentLength();
sender.sendMessage("文件长度: " + fileLength); sender.sendMessage("§3文件长度: " + fileLength);
in = new BufferedInputStream(fileUrl.openStream()); in = new BufferedInputStream(fileUrl.openStream());
File file = new File(dir, filename); File file = new File(new File("plugins"), filename);
if (!file.exists()) { if (!file.exists()) {
file.createNewFile(); file.createNewFile();
sender.sendMessage("创建新文件: " + filename); sender.sendMessage("§d创建新文件: " + filename);
} }
fout = new FileOutputStream(file); fout = new FileOutputStream(file);
byte[] data = new byte[1024]; byte[] data = new byte[1024];
@ -46,13 +45,15 @@ public class DownloadUtils {
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) { if (System.currentTimeMillis() - time > 1000) {
sender.sendMessage(String.format("已下载: ====================> %.2f%%", percent)); sender.sendMessage(String.format("§a已下载: " + getPer(percent) + " %.2f%%", percent));
time = System.currentTimeMillis(); time = System.currentTimeMillis();
} }
} }
sender.sendMessage("§a已下载: ====================> 100%");
sender.sendMessage("§a插件: " + pluginname + "下载完成!");
return true; return true;
} catch (Exception ex) { } catch (Exception ex) {
sender.sendMessage("插件下载失败!"); sender.sendMessage("§c插件下载失败!");
ex.printStackTrace(); ex.printStackTrace();
return false; return false;
} finally { } 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 蒋天蓓 * @author 蒋天蓓 2015年8月21日下午7:03:26
* 2015年8月21日下午7:03:26
*/ */
public class PluginUtil { 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) { public static void disable(Plugin plugin) {
if ((plugin.isEnabled()) && (plugin != null)) { if ((plugin.isEnabled()) && (plugin != null)) {
Bukkit.getPluginManager().disablePlugin(plugin); 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) { public static String getFormattedName(Plugin plugin) {
return getFormattedName(plugin, false); return getFormattedName(plugin, false);
} }
@ -73,13 +72,10 @@ public class PluginUtil {
public static String getFormattedName(Plugin plugin, boolean includeVersions) { public static String getFormattedName(Plugin plugin, boolean includeVersions) {
ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED; ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED;
String pluginName = color + plugin.getName(); String pluginName = color + plugin.getName();
if (includeVersions) if (includeVersions) {
pluginName = pluginName + " (" + plugin.getDescription().getVersion() + ")"; pluginName = pluginName + " (" + plugin.getDescription().getVersion() + ")";
return pluginName;
} }
return pluginName;
public static Plugin getPluginByName(String[] args, int start) {
return getPluginByName(StringUtil.consolidateStrings(args, start));
} }
public static Plugin getPluginByName(String name) { public static Plugin getPluginByName(String name) {
@ -90,10 +86,15 @@ public class PluginUtil {
return null; return null;
} }
public static Plugin getPluginByName(String[] args, int start) {
return getPluginByName(StringUtil.consolidateStrings(args, start));
}
public static List<String> getPluginNames(boolean fullName) { public static List<String> getPluginNames(boolean fullName) {
List<String> plugins = new ArrayList<String>(); 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()); plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName());
}
return plugins; return plugins;
} }
@ -118,9 +119,8 @@ public class PluginUtil {
} }
} }
} }
if (parsedCommands.isEmpty()) { if (parsedCommands.isEmpty())
return "没有注册命令!"; return "§c没有注册命令!";
}
return Joiner.on(", ").join(parsedCommands); return Joiner.on(", ").join(parsedCommands);
} }
@ -150,23 +150,22 @@ public class PluginUtil {
} }
File pluginFile = new File(pluginDir, name + ".jar"); File pluginFile = new File(pluginDir, name + ".jar");
if (!pluginFile.isFile()) { if (!pluginFile.isFile())
return "插件不存在!"; return "§c在plugins目录未找到 " + name + " 插件 请确认文件是否存在!";
}
try { try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile); target = Bukkit.getPluginManager().loadPlugin(pluginFile);
} catch (InvalidDescriptionException e) { } catch (InvalidDescriptionException e) {
e.printStackTrace(); e.printStackTrace();
return "错误的插件信息!"; return "§c插件 " + name + " 的plugin.yml文件存在错误!";
} catch (InvalidPluginException e) { } catch (InvalidPluginException e) {
e.printStackTrace(); e.printStackTrace();
return "错误的插件!"; return "§c文件 " + name + " 不是一个可载入的插件!";
} }
target.onLoad(); target.onLoad();
Bukkit.getPluginManager().enablePlugin(target); Bukkit.getPluginManager().enablePlugin(target);
return "插件已载入!"; return "§a插件 " + name + " 成功载入到服务器!";
} }
public static void reload(Plugin plugin) { public static void reload(Plugin plugin) {
@ -226,41 +225,42 @@ public class PluginUtil {
commands = (Map<String, Command>) knownCommandsField.get(commandMap); commands = (Map<String, Command>) knownCommandsField.get(commandMap);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
e.printStackTrace(); e.printStackTrace();
return "插件卸载失败!"; return "§c插件 " + name + " 卸载失败!";
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
return "插件已失败!"; return "§c插件 " + name + " 卸载失败!";
} }
} }
pluginManager.disablePlugin(plugin); pluginManager.disablePlugin(plugin);
if ((plugins != null) && (plugins.contains(plugin))) { if (plugins != null && plugins.contains(plugin)) {
plugins.remove(plugin); plugins.remove(plugin);
} }
if ((names != null) && (names.containsKey(name))) {
if (names != null && names.containsKey(name)) {
names.remove(name); names.remove(name);
} }
Iterator<RegisteredListener> it;
if ((listeners != null) && (reloadlisteners)) { if (listeners != null && reloadlisteners) {
for (SortedSet<RegisteredListener> set : listeners.values()) { for (SortedSet<RegisteredListener> set : listeners.values()) {
for (it = set.iterator(); it.hasNext();) { for (Iterator<RegisteredListener> it = set.iterator(); it.hasNext();) {
RegisteredListener value = it.next(); RegisteredListener value = it.next();
if (value.getPlugin() == plugin) if (value.getPlugin() == plugin) {
it.remove(); it.remove();
} }
} }
} }
}
Iterator<Map.Entry<String, Command>> cmdit;
if (commandMap != null) { if (commandMap != null) {
for (cmdit = commands.entrySet().iterator(); cmdit.hasNext();) { for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, Command> entry = cmdit.next(); Map.Entry<String, Command> entry = it.next();
if ((entry.getValue() instanceof PluginCommand)) { if (entry.getValue() instanceof PluginCommand) {
PluginCommand c = (PluginCommand) entry.getValue(); PluginCommand c = (PluginCommand) entry.getValue();
if (c.getPlugin() == plugin) { if (c.getPlugin() == plugin) {
c.unregister(commandMap); c.unregister(commandMap);
cmdit.remove(); it.remove();
} }
} }
} }
@ -277,6 +277,6 @@ public class PluginUtil {
} }
System.gc(); System.gc();
return "插件已卸载!"; return "§a插件 " + name + " 成功卸载!";
} }
} }