add Upgrade command...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-09-06 21:06:44 +08:00
parent a8e06d5f32
commit 2ecb988e50
3 changed files with 173 additions and 9 deletions

View File

@ -0,0 +1,58 @@
/**
*
*/
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
/**
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandUpgrade extends BaseCommand {
Yum main;
/**
* @param name
*/
public CommandUpgrade(Yum main) {
super("update");
this.main = main;
}
@Override
public void execute(final CommandSender sender, String label, final String[] args) throws CommandException {
final String pluginname = args[0];
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
sender.sendMessage("§a开始更新插件: " + pluginname);
if (plugin != null)
Bukkit.getScheduler().runTaskAsynchronously(main, () -> {
if (args.length < 2)
YumManager.upgrade(sender, plugin);
else
YumManager.upgrade(sender, plugin, args[1]);
});
else
sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!");
};
@Override
public int getMinimumArguments() {
return 1;
}
@Override
public String getPossibleArguments() {
return "<插件名称> <插件版本>";
}
@Override
public boolean isOnlyPlayerExecutable() {
return false;
}
}

View File

@ -20,6 +20,7 @@ import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException; import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.UnknownDependencyException; import org.bukkit.plugin.UnknownDependencyException;
@ -312,6 +313,13 @@ public class PluginsManager {
return true; return true;
} }
/**
* 载入插件
*
* @param name
* - 插件名称
* @return 是否成功
*/
public boolean load(String name) { public boolean load(String name) {
return load(Bukkit.getConsoleSender(), name); return load(Bukkit.getConsoleSender(), name);
} }
@ -331,6 +339,21 @@ public class PluginsManager {
return false; return false;
} }
/**
* 删除重载插件
*
* @param sender
* - 命令发送者
* @param plugin
* - 插件
* @return 是否成功
*/
public boolean reload(CommandSender sender, String name) {
if (name != null)
return unload(sender, name) && load(sender, name);
return false;
}
/** /**
* 重载插件 * 重载插件
* *
@ -369,9 +392,21 @@ public class PluginsManager {
* - 插件 * - 插件
* @return 是否成功 * @return 是否成功
*/ */
@SuppressWarnings("unchecked")
public boolean unload(CommandSender sender, Plugin plugin) { public boolean unload(CommandSender sender, Plugin plugin) {
String name = plugin.getName(); return unload(sender, plugin.getName());
}
/**
* 卸载插件
*
* @param sender
* - 命令发送者
* @param name
* - 插件名称
* @return 是否成功
*/
@SuppressWarnings("unchecked")
public boolean unload(CommandSender sender, String name) {
if (sender == null) if (sender == null)
sender = Bukkit.getConsoleSender(); sender = Bukkit.getConsoleSender();
PluginManager pluginManager = Bukkit.getPluginManager(); PluginManager pluginManager = Bukkit.getPluginManager();
@ -444,4 +479,54 @@ public class PluginsManager {
public boolean unload(Plugin plugin) { public boolean unload(Plugin plugin) {
return unload(Bukkit.getConsoleSender(), plugin); return unload(Bukkit.getConsoleSender(), plugin);
} }
/**
* 重载update文件夹的插件
*
* @return 是否成功
*/
public boolean updateall(CommandSender sender) {
return updateall(sender, Bukkit.getServer().getUpdateFolderFile());
}
/**
* 重载update文件夹的插件
*
* @param sender
* - 命令发送者
* @param directory
* - 更新目录
* @return 是否成功
*/
public boolean updateall(CommandSender sender, File directory) {
PluginLoader loader = main.getPluginLoader();
File updateDirectory;
if (!directory.isDirectory())
updateDirectory = Bukkit.getServer().getUpdateFolderFile();
else
updateDirectory = directory;
for (File file : updateDirectory.listFiles()) {
PluginDescriptionFile description = null;
try {
description = loader.getPluginDescription(file);
String name = description.getName();
sender.sendMessage("§6升级: 开始升级 " + name + " 插件!");
reload(sender, name);
} catch (InvalidDescriptionException e) {
sender.sendMessage("§4异常: §c" + e.getMessage());
sender.sendMessage("§c文件: " + file.getName() + " 的plugin.yml文件存在错误!");
continue;
}
}
return false;
}
/**
* 重载update文件夹的插件
*
* @return 是否成功
*/
public boolean updateall(File directory) {
return updateall(Bukkit.getConsoleSender(), directory);
}
} }

View File

@ -1,5 +1,8 @@
package cn.citycraft.Yum.manager; package cn.citycraft.Yum.manager;
import java.io.File;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -12,11 +15,12 @@ import cn.citycraft.Yum.repository.PluginInfo;
* 2015年9月1日上午10:59:47 * 2015年9月1日上午10:59:47
*/ */
public class YumManager { public class YumManager {
Plugin plugin;
public static DownloadManager download; public static DownloadManager download;
public static PluginsManager plugman; public static PluginsManager plugman;
public static RepositoryManager repo; public static RepositoryManager repo;
Plugin plugin;
public YumManager(Plugin plugin) { public YumManager(Plugin plugin) {
this.plugin = plugin; this.plugin = plugin;
plugman = new PluginsManager(plugin); plugman = new PluginsManager(plugin);
@ -31,9 +35,8 @@ public class YumManager {
public static boolean install(CommandSender sender, String pluginname, String version) { public static boolean install(CommandSender sender, String pluginname, String version) {
PluginInfo pi = repo.getPlugin(pluginname); PluginInfo pi = repo.getPlugin(pluginname);
if (pi != null) if (pi != null)
if (download.run(sender, pi.getMavenUrl(version))) { if (download.run(sender, pi.getMavenUrl(version)))
return plugman.load(sender, pluginname); return plugman.load(sender, pluginname);
}
return false; return false;
} }
@ -44,10 +47,28 @@ public class YumManager {
public static boolean update(CommandSender sender, Plugin plugin, String version) { public static boolean update(CommandSender sender, Plugin plugin, String version) {
PluginInfo pi = repo.getPlugin(plugin.getName()); PluginInfo pi = repo.getPlugin(plugin.getName());
if (pi != null) { if (pi != null) {
if (download.run(sender, pi.getMavenUrl(version), new File(Bukkit.getUpdateFolderFile(), plugman.getPluginFile(plugin).getName()))) {
if (download.run(sender, pi.getMavenUrl(version), plugman.getPluginFile(plugin))) { sender.sendMessage("§6更新: §a已下载插件 " + plugin.getName() + " 到update文件夹 重启后自动更新(或使用upgrade直接升级)!");
return plugman.reload(sender, plugin); return true;
} }
} else
sender.sendMessage("§6更新: §c仓库缓存中未找到插件 " + plugin.getName());
return false;
}
public static boolean upgrade(CommandSender sender) {
return plugman.updateall(sender);
}
public static boolean upgrade(CommandSender sender, Plugin plugin) {
return update(sender, plugin, null);
}
public static boolean upgrade(CommandSender sender, Plugin plugin, String version) {
PluginInfo pi = repo.getPlugin(plugin.getName());
if (pi != null) {
if (download.run(sender, pi.getMavenUrl(version), plugman.getPluginFile(plugin)))
return plugman.reload(sender, plugin);
return false; return false;
} else { } else {
sender.sendMessage("§6更新: §c仓库缓存中未找到插件 " + plugin.getName()); sender.sendMessage("§6更新: §c仓库缓存中未找到插件 " + plugin.getName());