mirror of
https://e.coding.net/circlecloud/Yum.git
synced 2025-09-07 15:06:56 +00:00
41
src/main/java/pw/yumc/Yum/Yum.java
Normal file
41
src/main/java/pw/yumc/Yum/Yum.java
Normal file
@ -0,0 +1,41 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package pw.yumc.Yum;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import cn.citycraft.CommonData.UpdatePlugin;
|
||||
import cn.citycraft.PluginHelper.config.FileConfig;
|
||||
import cn.citycraft.PluginHelper.utils.VersionChecker;
|
||||
import pw.yumc.Yum.api.YumAPI;
|
||||
import pw.yumc.Yum.commands.FileCommand;
|
||||
import pw.yumc.Yum.commands.YumCommand;
|
||||
|
||||
/**
|
||||
* MC插件仓库
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2015年8月21日下午5:14:39
|
||||
*/
|
||||
public class Yum extends JavaPlugin {
|
||||
public FileConfig config;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
new YumAPI(this);
|
||||
new YumCommand(this);
|
||||
new FileCommand(this);
|
||||
new VersionChecker(this);
|
||||
YumAPI.updaterepo(Bukkit.getConsoleSender());
|
||||
YumAPI.updatecheck(Bukkit.getConsoleSender());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
config = new FileConfig(this);
|
||||
// 初始化更新列
|
||||
UpdatePlugin.getUpdateList();
|
||||
}
|
||||
}
|
369
src/main/java/pw/yumc/Yum/api/YumAPI.java
Normal file
369
src/main/java/pw/yumc/Yum/api/YumAPI.java
Normal file
@ -0,0 +1,369 @@
|
||||
package pw.yumc.Yum.api;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import cn.citycraft.CommonData.UpdatePlugin;
|
||||
import pw.yumc.Yum.manager.DownloadManager;
|
||||
import pw.yumc.Yum.manager.PluginInfo;
|
||||
import pw.yumc.Yum.manager.PluginsManager;
|
||||
import pw.yumc.Yum.manager.RepositoryManager;
|
||||
|
||||
/**
|
||||
* Yum管理中心
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2015年9月1日上午10:59:47
|
||||
*/
|
||||
public class YumAPI {
|
||||
private static DownloadManager download;
|
||||
private static Plugin main;
|
||||
private static PluginsManager plugman;
|
||||
private static RepositoryManager repo;
|
||||
private static boolean runlock = false;
|
||||
|
||||
/**
|
||||
* 初始化Yum管理中心
|
||||
*
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
*/
|
||||
public YumAPI(final Plugin plugin) {
|
||||
YumAPI.main = plugin;
|
||||
plugman = new PluginsManager(main);
|
||||
download = new DownloadManager(main);
|
||||
repo = new RepositoryManager(main);
|
||||
plugman.addIgnore(main.getConfig().getStringList("ignorelist"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除插件
|
||||
*
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
*/
|
||||
public static void delete(final Plugin plugin) {
|
||||
plugman.deletePlugin(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得下载管理器
|
||||
*
|
||||
* @return {@link DownloadManager}
|
||||
*/
|
||||
public static DownloadManager getDownload() {
|
||||
return download;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得插件管理器
|
||||
*
|
||||
* @return {@link PluginsManager}
|
||||
*/
|
||||
public static PluginsManager getPlugman() {
|
||||
return plugman;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得仓库管理器
|
||||
*
|
||||
* @return {@link RepositoryManager}
|
||||
*/
|
||||
public static RepositoryManager getRepo() {
|
||||
return repo;
|
||||
}
|
||||
|
||||
public static List<Plugin> getUpdateList(final CommandSender sender) {
|
||||
final List<Plugin> ulist = new ArrayList<>();
|
||||
try {
|
||||
for (final Entry<String, Plugin> updateplugin : UpdatePlugin.getUpdateList().entrySet()) {
|
||||
ulist.add(updateplugin.getValue());
|
||||
}
|
||||
} catch (final Exception | Error e) {
|
||||
sender.sendMessage("§4错误: §c无法检索全体更新列表!");
|
||||
sender.sendMessage("§4异常: §c" + e.getMessage());
|
||||
}
|
||||
return ulist;
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装新插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param pluginname
|
||||
* 插件名称
|
||||
* @param version
|
||||
* 插件版本
|
||||
* @return 是否安装成功
|
||||
*/
|
||||
public static boolean install(final CommandSender sender, final String pluginname, final String url) {
|
||||
final File pluginFile = new File(Bukkit.getUpdateFolderFile().getParentFile(), pluginname + ".jar");
|
||||
if (download.run(sender, url, pluginFile)) {
|
||||
return plugman.load(sender, pluginFile);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装新插件
|
||||
*
|
||||
* @param pluginname
|
||||
* 插件名称
|
||||
* @param version
|
||||
* 插件版本
|
||||
* @return 是否安装成功
|
||||
*/
|
||||
public static boolean install(final String pluginname, final String url) {
|
||||
return install(null, pluginname, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装新插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param pluginname
|
||||
* 插件名称
|
||||
* @return 是否安装成功
|
||||
*/
|
||||
public static boolean installfromyum(final CommandSender sender, final String pluginname) {
|
||||
return installfromyum(sender, pluginname, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 安装新插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param pluginname
|
||||
* 插件名称
|
||||
* @param version
|
||||
* 插件版本
|
||||
* @return 是否安装成功
|
||||
*/
|
||||
public static boolean installfromyum(final CommandSender sender, final String pluginname, final String version) {
|
||||
final PluginInfo pi = repo.getPlugin(pluginname);
|
||||
if (pi != null) {
|
||||
return install(sender, pi.name, pi.getUrl(sender, version));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件
|
||||
*
|
||||
* @param pluginname
|
||||
* 插件名称
|
||||
*/
|
||||
public static void load(final File pluginFile) {
|
||||
plugman.load(pluginFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件
|
||||
*
|
||||
* @param pluginname
|
||||
* 插件名称
|
||||
*/
|
||||
public static void load(final String pluginname) {
|
||||
plugman.load(pluginname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载插件
|
||||
*
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
*/
|
||||
public static void reload(final Plugin plugin) {
|
||||
plugman.reload(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载插件
|
||||
*
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
*/
|
||||
public static void unload(final Plugin plugin) {
|
||||
plugman.unload(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
* @param url
|
||||
* 新插件的下载地址
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
public static boolean update(final CommandSender sender, final Plugin plugin, final URL url) {
|
||||
if (download.run(sender, url, new File(Bukkit.getUpdateFolderFile(), plugman.getPluginFile(plugin).getName()))) {
|
||||
sender.sendMessage("§6更新: §e已下载 " + plugin.getName() + " 插件到服务器更新文件夹");
|
||||
sender.sendMessage("§6更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新插件
|
||||
*
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
* @param url
|
||||
* 新插件的下载地址
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
public static boolean update(final Plugin plugin, final URL url) {
|
||||
return update(null, plugin, url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新支持Yum的插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
*/
|
||||
public static void updateall(final CommandSender sender) {
|
||||
main.getServer().getScheduler().runTaskAsynchronously(main, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (runlock) {
|
||||
sender.sendMessage("§d一键更新: §c一键更新运行中 请稍候重试...");
|
||||
return;
|
||||
}
|
||||
runlock = true;
|
||||
int failed = 0;
|
||||
final List<Plugin> ulist = getUpdateList(sender);
|
||||
if (ulist.size() > 0) {
|
||||
sender.sendMessage("§d开始更新服务器可更新插件");
|
||||
for (final Plugin updateplugin : ulist) {
|
||||
sender.sendMessage("§d一键更新: §a开始更新" + updateplugin.getName() + "!");
|
||||
if (!updatefromyum(sender, updateplugin, null, true))
|
||||
failed++;
|
||||
}
|
||||
if (failed != 0) {
|
||||
sender.sendMessage("§d一键更新: §c升级过程中 §4" + failed + " §c个插件更新失败!");
|
||||
}
|
||||
sender.sendMessage("§d一键更新: §e已下载所有需要升级的插件到 服务器更新 文件夹");
|
||||
sender.sendMessage("§d一键更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!");
|
||||
updatecheck(sender);
|
||||
} else {
|
||||
sender.sendMessage("§6更新: §e未找到需要更新且可以用Yum处理的插件!");
|
||||
}
|
||||
runlock = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有可更新插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
*/
|
||||
public static void updatecheck(final CommandSender sender) {
|
||||
main.getServer().getScheduler().runTaskLaterAsynchronously(main, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final List<Plugin> ulist = getUpdateList(sender);
|
||||
if (ulist.size() > 0) {
|
||||
sender.sendMessage("§6[§bYum§6]§e自动更新: §a发现 §e" + ulist.size() + " §a个可更新插件 请使用 §b/yum ua §a更新所有插件!");
|
||||
}
|
||||
}
|
||||
}, 60);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
public static boolean updatefromyum(final CommandSender sender, final Plugin plugin) {
|
||||
return updatefromyum(sender, plugin, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Yum内部更新插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
* @param version
|
||||
* 插件版本(null则自动获取)
|
||||
* @return
|
||||
*/
|
||||
public static boolean updatefromyum(final CommandSender sender, final Plugin plugin, final String version) {
|
||||
return updatefromyum(sender, plugin, version, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Yum内部更新插件
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
* @param version
|
||||
* 插件版本(null则自动获取)
|
||||
* @param oneKeyUpdate
|
||||
* 是否一键更新
|
||||
* @return
|
||||
*/
|
||||
public static boolean updatefromyum(final CommandSender sender, final Plugin plugin, final String version, final boolean oneKeyUpdate) {
|
||||
final PluginInfo pi = repo.getPlugin(plugin.getName());
|
||||
if (pi != null) {
|
||||
final File pFile = new File(Bukkit.getUpdateFolderFile(), plugman.getPluginFile(plugin).getName());
|
||||
if (download.run(sender, pi.getUrl(sender, version), pFile)) {
|
||||
if (!oneKeyUpdate) {
|
||||
sender.sendMessage("§6更新: §e已下载 " + plugin.getName() + " 插件到服务器更新文件夹");
|
||||
sender.sendMessage("§6更新: §e插件将在重启后自动更新(或使用§b/yum upgrade§e直接升级)!");
|
||||
}
|
||||
UpdatePlugin.getUpdateList().remove(plugin.getName());
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§6更新: §c仓库缓存中未找到插件 " + plugin.getName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Yum源数据
|
||||
*/
|
||||
public static void updaterepo(final CommandSender sender) {
|
||||
main.getServer().getScheduler().runTaskAsynchronously(main, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
repo.updateRepositories(sender);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param plugin
|
||||
* 插件实体
|
||||
*/
|
||||
public static void upgrade(final CommandSender sender, final Plugin plugin) {
|
||||
plugman.upgrade(sender, plugin);
|
||||
}
|
||||
}
|
149
src/main/java/pw/yumc/Yum/commands/FileCommand.java
Normal file
149
src/main/java/pw/yumc/Yum/commands/FileCommand.java
Normal file
@ -0,0 +1,149 @@
|
||||
package pw.yumc.Yum.commands;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import cn.citycraft.PluginHelper.commands.HandlerCommand;
|
||||
import cn.citycraft.PluginHelper.commands.HandlerCommands;
|
||||
import cn.citycraft.PluginHelper.commands.InvokeCommandEvent;
|
||||
import cn.citycraft.PluginHelper.commands.InvokeSubCommand;
|
||||
import cn.citycraft.PluginHelper.utils.FileUtil;
|
||||
import pw.yumc.Yum.Yum;
|
||||
import pw.yumc.Yum.api.YumAPI;
|
||||
import pw.yumc.Yum.manager.DownloadManager;
|
||||
|
||||
/**
|
||||
* File命令基类
|
||||
*
|
||||
* @since 2016年1月9日 上午10:02:39
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class FileCommand implements HandlerCommands {
|
||||
Yum plugin;
|
||||
DownloadManager dl;
|
||||
|
||||
public FileCommand(final Yum yum) {
|
||||
plugin = yum;
|
||||
dl = YumAPI.getDownload();
|
||||
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "file");
|
||||
cmdhandler.setAllCommandOnlyConsole(yum.config.getBoolean("onlyFileCommandConsole", true));
|
||||
cmdhandler.registerCommands(this);
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "delete", aliases = { "del" }, minimumArguments = 1, description = "删除文件(服务器JAR为根目录)", possibleArguments = "<文件相对目录>")
|
||||
public void delete(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
final String fpath = args[0];
|
||||
final File file = new File(fpath);
|
||||
final CommandSender sender = e.getSender();
|
||||
if (!file.exists()) {
|
||||
sender.sendMessage("§c文件 " + file.getAbsolutePath() + " 不存在!");
|
||||
} else {
|
||||
if (file.isDirectory()) {
|
||||
sender.sendMessage("§e" + file.getAbsolutePath() + " §c是一个目录 请使用file rm!");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
sender.sendMessage("§d文件 §e" + file.getAbsolutePath() + " " + (file.delete() ? "§a删除成功!" : "§c删除失败!"));
|
||||
} catch (final Exception ex) {
|
||||
sender.sendMessage("§d文件 §e" + file.getAbsolutePath() + " 删除失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "download", aliases = { "d" }, minimumArguments = 1, description = "下载文件(默认保存到服务器更新文件夹)", possibleArguments = "<下载地址> [保存文件路径]")
|
||||
public void download(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
String urlstr = args[0];
|
||||
if (!urlstr.startsWith("http")) {
|
||||
urlstr = "http://" + urlstr;
|
||||
}
|
||||
File file = null;
|
||||
if (args.length == 2) {
|
||||
file = new File(args[1]);
|
||||
} else {
|
||||
file = new File(Bukkit.getUpdateFolderFile(), dl.getFileName(urlstr));
|
||||
}
|
||||
dl.run(e.getSender(), urlstr, file);
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "ls", aliases = { "l" }, description = "列出当前目录(服务器JAR为根目录)", possibleArguments = "<相对目录>")
|
||||
public void ls(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
final CommandSender sender = e.getSender();
|
||||
File dir = new File(".");
|
||||
if (args.length == 1) {
|
||||
dir = new File(args[0]);
|
||||
}
|
||||
if (!dir.isDirectory()) {
|
||||
sender.sendMessage("§6路径: §e " + dir.getAbsolutePath() + " §c不是一个目录!");
|
||||
return;
|
||||
}
|
||||
final StringBuffer sb = new StringBuffer();
|
||||
for (final File file : dir.listFiles()) {
|
||||
if (file.isDirectory()) {
|
||||
sb.append("§b");
|
||||
} else {
|
||||
sb.append("§a");
|
||||
}
|
||||
sb.append(file.getName() + " ");
|
||||
}
|
||||
final String filelist = sb.toString();
|
||||
if (filelist.isEmpty()) {
|
||||
sender.sendMessage("§6目录: §e" + dir.getAbsolutePath() + " §c下没有文件或文件夹!");
|
||||
} else {
|
||||
sender.sendMessage("§6目录: §e" + dir.getAbsolutePath() + " §a存在如下文件!");
|
||||
sender.sendMessage(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "rename", aliases = { "rn" }, minimumArguments = 2, description = "重命名文件(服务器JAR为根目录)", possibleArguments = "<文件相对路径> <文件名称>")
|
||||
public void rename(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
final CommandSender sender = e.getSender();
|
||||
final String fpath = args[0];
|
||||
final File file = new File(fpath);
|
||||
if (!file.exists()) {
|
||||
sender.sendMessage("§c文件 " + file.getAbsolutePath() + " 不存在!");
|
||||
} else {
|
||||
try {
|
||||
final File newFile = new File(file.getParentFile(), args[1]);
|
||||
file.renameTo(newFile);
|
||||
sender.sendMessage("§a文件 §e" + file.getAbsolutePath() + " §a重命名为 §d" + newFile.getAbsolutePath());
|
||||
} catch (final Exception ex) {
|
||||
sender.sendMessage("§c文件 §e" + file.getAbsolutePath() + " §c重命名失败: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "rm", minimumArguments = 1, description = "删除文件夹(服务器JAR为根目录)", possibleArguments = "<相对目录>")
|
||||
public void rm(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
final CommandSender sender = e.getSender();
|
||||
final String fpath = args[0];
|
||||
final File file = new File(fpath);
|
||||
if (!file.exists()) {
|
||||
sender.sendMessage("§c目录 " + file.getAbsolutePath() + " 不存在!");
|
||||
} else {
|
||||
if (!file.isDirectory()) {
|
||||
sender.sendMessage("§d路径 §e" + file.getAbsolutePath() + " §c是一个文件 请使用file delete!");
|
||||
return;
|
||||
}
|
||||
for (final String name : plugin.config.getStringList("blacklist")) {
|
||||
if (file.getAbsolutePath().toLowerCase().endsWith(name)) {
|
||||
sender.sendMessage("§d路径 §e" + file.getAbsolutePath() + " §c不允许被删除!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (file.listFiles().length != 0 && !(args.length > 1 && args[1].equalsIgnoreCase("-rf"))) {
|
||||
sender.sendMessage("§d目录 §e" + file.getAbsolutePath() + " §c不为空!");
|
||||
sender.sendMessage("§c请使用 §a/file rm " + fpath + " -rf §c强行删除!");
|
||||
return;
|
||||
}
|
||||
sender.sendMessage("§d目录 §e" + file.getAbsolutePath() + " " + (FileUtil.deleteDir(sender, file) ? "§a删除成功!" : "§c删除失败!"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
371
src/main/java/pw/yumc/Yum/commands/YumCommand.java
Normal file
371
src/main/java/pw/yumc/Yum/commands/YumCommand.java
Normal file
@ -0,0 +1,371 @@
|
||||
package pw.yumc.Yum.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
|
||||
import cn.citycraft.PluginHelper.commands.HandlerCommand;
|
||||
import cn.citycraft.PluginHelper.commands.HandlerCommands;
|
||||
import cn.citycraft.PluginHelper.commands.HandlerTabComplete;
|
||||
import cn.citycraft.PluginHelper.commands.InvokeCommandEvent;
|
||||
import cn.citycraft.PluginHelper.commands.InvokeSubCommand;
|
||||
import cn.citycraft.PluginHelper.utils.StringUtil;
|
||||
import pw.yumc.Yum.Yum;
|
||||
import pw.yumc.Yum.api.YumAPI;
|
||||
import pw.yumc.Yum.manager.PluginsManager;
|
||||
import pw.yumc.Yum.manager.RepositoryManager;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.Repositories;
|
||||
|
||||
/**
|
||||
* Yum命令基类
|
||||
*
|
||||
* @since 2016年1月9日 上午10:02:24
|
||||
* @author 喵♂呜
|
||||
*/
|
||||
public class YumCommand implements HandlerCommands, Listener {
|
||||
Yum main;
|
||||
RepositoryManager repo;
|
||||
PluginsManager plugman;
|
||||
|
||||
public YumCommand(final Yum yum) {
|
||||
main = yum;
|
||||
repo = YumAPI.getRepo();
|
||||
plugman = YumAPI.getPlugman();
|
||||
Bukkit.getPluginManager().registerEvents(this, yum);
|
||||
final InvokeSubCommand cmdhandler = new InvokeSubCommand(yum, "yum");
|
||||
cmdhandler.setAllCommandOnlyConsole(yum.config.getBoolean("onlyCommandConsole", false));
|
||||
cmdhandler.registerCommands(this);
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "delete", aliases = { "del" }, minimumArguments = 1, description = "删除插件", possibleArguments = "<插件名称>")
|
||||
public void delete(final InvokeCommandEvent e) {
|
||||
final String pluginname = e.getArgs()[0];
|
||||
final CommandSender sender = e.getSender();
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin != null) {
|
||||
final String version = StringUtils.substring(plugin.getDescription().getVersion(), 0, 15);
|
||||
if (plugman.deletePlugin(sender, plugin)) {
|
||||
sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §a版本 §d" + version + " §a已从服务器卸载并删除!");
|
||||
} else {
|
||||
sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §c卸载或删除时发生错误 删除失败!");
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(pnf(pluginname));
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "fulldelete", aliases = { "fdel" }, minimumArguments = 1, description = "删除插件以及数据文件夹", possibleArguments = "<插件名称>")
|
||||
public void fulldelete(final InvokeCommandEvent e) {
|
||||
final String pluginname = e.getArgs()[0];
|
||||
final CommandSender sender = e.getSender();
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin != null) {
|
||||
final String version = StringUtils.substring(plugin.getDescription().getVersion(), 0, 15);
|
||||
if (plugman.fullDeletePlugin(sender, plugin)) {
|
||||
sender.sendMessage("§c删除: §a插件 §b" + pluginname + " §a版本 §d" + version + " §a已从服务器卸载并删除!");
|
||||
} else {
|
||||
sender.sendMessage("§c删除: §c插件 §b" + pluginname + " §c卸载或删除时发生错误 删除失败!");
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage(pnf(pluginname));
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "info", minimumArguments = 1, description = "查看插件详情", possibleArguments = "<插件名称>")
|
||||
public void info(final InvokeCommandEvent e) {
|
||||
final String pluginname = e.getArgs()[0];
|
||||
final CommandSender sender = e.getSender();
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin != null) {
|
||||
final PluginDescriptionFile desc = plugin.getDescription();
|
||||
sender.sendMessage("§6插件名称: §3" + plugin.getName());
|
||||
sender.sendMessage("§6插件版本: §3" + StringUtils.substring(plugin.getDescription().getVersion(), 0, 15));
|
||||
sender.sendMessage("§6插件作者: §3" + StringUtils.join(desc.getAuthors(), " "));
|
||||
sender.sendMessage("§6插件描述: §3" + (desc.getDescription() == null ? "无" : desc.getDescription()));
|
||||
sender.sendMessage("§6插件依赖: §3" + (desc.getDepend().isEmpty() ? "无" : ""));
|
||||
StringUtil.sendStringArray(sender, desc.getDepend(), "§6 - §a");
|
||||
sender.sendMessage("§6插件软依赖: §3" + (desc.getSoftDepend().isEmpty() ? "无" : ""));
|
||||
StringUtil.sendStringArray(sender, desc.getSoftDepend(), "§6 - §a");
|
||||
final Map<String, Map<String, Object>> clist = desc.getCommands();
|
||||
if (clist != null) {
|
||||
sender.sendMessage("§6插件注册命令: §3" + (clist.isEmpty() ? "无" : ""));
|
||||
for (final Entry<String, Map<String, Object>> entry : clist.entrySet()) {
|
||||
sender.sendMessage("§6 - §a" + entry.getKey());
|
||||
sendEntry(sender, "§6 描述: §a", entry.getValue(), "description");
|
||||
sendEntry(sender, "§6 权限: §a", entry.getValue(), "permission");
|
||||
sendEntry(sender, "§6 用法: §a", entry.getValue(), "usage");
|
||||
}
|
||||
}
|
||||
final List<Permission> plist = desc.getPermissions();
|
||||
if (plist != null) {
|
||||
sender.sendMessage("§6插件注册权限: " + (plist.isEmpty() ? "无" : ""));
|
||||
for (final Permission perm : plist) {
|
||||
sender.sendMessage("§6 - §a" + perm.getName() + "§6 - §e" + (perm.getDescription().isEmpty() ? "无描述" : perm.getDescription()));
|
||||
}
|
||||
}
|
||||
sender.sendMessage("§6插件物理路径: §3" + plugman.getPluginFile(plugin).getAbsolutePath());
|
||||
} else {
|
||||
sender.sendMessage(pnf(pluginname));
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "install", aliases = { "i" }, minimumArguments = 1, description = "安装插件", possibleArguments = "<插件名称>")
|
||||
public void install(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
final CommandSender sender = e.getSender();
|
||||
final String pluginname = args[0];
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin == null) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (args.length < 2) {
|
||||
YumAPI.installfromyum(sender, pluginname);
|
||||
} else {
|
||||
YumAPI.installfromyum(sender, pluginname, args[1]);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
sender.sendMessage("§4错误: §c插件 §b" + pluginname + " §c已安装在服务器 需要更新请使用 §b/yum update " + pluginname + "!");
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "list", aliases = { "l" }, description = "列出已安装插件列表")
|
||||
public void list(final InvokeCommandEvent e) {
|
||||
final CommandSender sender = e.getSender();
|
||||
sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: ");
|
||||
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
sender.sendMessage("§6- " + plugman.getFormattedName(plugin, true));
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerTabComplete()
|
||||
public List<String> listtab(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
if (!args[0].equalsIgnoreCase("install") && !args[0].equalsIgnoreCase("repo")) {
|
||||
return StringUtil.copyPartialMatches(args[1], plugman.getPluginNames(false), new ArrayList<String>());
|
||||
}
|
||||
if (args[0].equalsIgnoreCase("install")) {
|
||||
return StringUtil.copyPartialMatches(args[1], repo.getAllPluginName(), new ArrayList<String>());
|
||||
}
|
||||
if (args[0].equalsIgnoreCase("repo")) {
|
||||
if (args.length == 2) {
|
||||
return StringUtil.copyPartialMatches(args[1], Arrays.asList(new String[] { "add", "all", "list", "delall", "clean", "update", "del" }), new ArrayList<String>());
|
||||
}
|
||||
if (args.length == 3 && (args[1] == "add" || args[1] == "del")) {
|
||||
return StringUtil.copyPartialMatches(args[2], repo.getRepos().keySet(), new ArrayList<String>());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "load", minimumArguments = 1, description = "载入插件", possibleArguments = "<插件名称>")
|
||||
public void load(final InvokeCommandEvent e) {
|
||||
final CommandSender sender = e.getSender();
|
||||
final String pluginname = e.getArgs()[0];
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin == null) {
|
||||
plugman.load(sender, pluginname);
|
||||
} else {
|
||||
sender.sendMessage("§c错误: §b插件 " + pluginname + " §c已加载到服务器!");
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onAdminJoin(final PlayerJoinEvent e) {
|
||||
if (e.getPlayer().isOp()) {
|
||||
YumAPI.updatecheck(e.getPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "reload", aliases = { "re" }, minimumArguments = 1, description = "重载插件", possibleArguments = "<插件名称|all|*>")
|
||||
public void reload(final InvokeCommandEvent e) {
|
||||
final CommandSender sender = e.getSender();
|
||||
final String pluginname = e.getArgs()[0];
|
||||
if (pluginname.equalsIgnoreCase("all") || pluginname.equalsIgnoreCase("*")) {
|
||||
plugman.reloadAll(sender);
|
||||
return;
|
||||
}
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin != null) {
|
||||
plugman.reload(sender, plugin);
|
||||
} else {
|
||||
sender.sendMessage(pnf(pluginname));
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "repo", aliases = { "r" }, minimumArguments = 1, description = "插件源命令", possibleArguments = "<add|del|all|clean|list> <仓库名称>")
|
||||
public void repo(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
final CommandSender sender = e.getSender();
|
||||
main.getServer().getScheduler().runTaskAsynchronously(main, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
final String cmd = args[0];
|
||||
switch (cmd) {
|
||||
case "add":
|
||||
if (args.length == 2) {
|
||||
if (repo.addRepositories(sender, args[1])) {
|
||||
final String reponame = repo.getRepoCache(args[1]).name;
|
||||
sender.sendMessage("§6仓库: §a源仓库 §e" + reponame + " §a的插件信息已缓存!");
|
||||
} else {
|
||||
sender.sendMessage("§6仓库: §c源地址未找到仓库信息或当前地址已缓存!");
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§6仓库: §c请输入需要添加的源地址!");
|
||||
}
|
||||
break;
|
||||
case "del":
|
||||
if (args.length == 2) {
|
||||
final Repositories delrepo = repo.getRepoCache(args[1]);
|
||||
if (delrepo != null) {
|
||||
repo.delRepositories(sender, args[1]);
|
||||
sender.sendMessage("§6仓库: §a源仓库 §e" + delrepo.name + " §c已删除 §a请使用 §b/yum repo update §a更新缓存!");
|
||||
} else {
|
||||
sender.sendMessage("§6仓库: §c源地址未找到!");
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§6仓库: §c请输入需要删除的源地址!");
|
||||
}
|
||||
break;
|
||||
case "delall":
|
||||
repo.getRepoCache().getRepos().clear();
|
||||
sender.sendMessage("§6仓库: §a缓存的仓库信息已清理!");
|
||||
break;
|
||||
case "list":
|
||||
sender.sendMessage("§6仓库: §b缓存的插件信息如下 ");
|
||||
StringUtil.sendStringArray(sender, repo.getAllPluginsInfo());
|
||||
break;
|
||||
case "all":
|
||||
sender.sendMessage("§6仓库: §b缓存的仓库信息如下 ");
|
||||
StringUtil.sendStringArray(sender, repo.getRepoCache().getAllRepoInfo());
|
||||
break;
|
||||
case "clean":
|
||||
repo.clean();
|
||||
sender.sendMessage("§6仓库: §a缓存的插件信息已清理!");
|
||||
break;
|
||||
case "update":
|
||||
repo.updateRepositories(sender);
|
||||
sender.sendMessage("§6仓库: §a仓库缓存数据已更新!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 发生实体消息
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param prefix
|
||||
* 实体前缀
|
||||
* @param map
|
||||
* 实体
|
||||
* @param key
|
||||
* 实体Key
|
||||
*/
|
||||
public void sendEntry(final CommandSender sender, final String prefix, final Map<String, Object> map, final String key) {
|
||||
final Object value = map.get(key);
|
||||
if (value != null) {
|
||||
sender.sendMessage(prefix + (String) value);
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "unload", minimumArguments = 1, description = "卸载插件", possibleArguments = "<插件名称>")
|
||||
public void unload(final InvokeCommandEvent e) {
|
||||
final String pluginname = e.getArgs()[0];
|
||||
final CommandSender sender = e.getSender();
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin != null) {
|
||||
plugman.unload(sender, plugin);
|
||||
} else {
|
||||
sender.sendMessage(pnf(pluginname));
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "update", aliases = { "u" }, description = "更新插件或缓存", possibleArguments = "[插件名称] [插件版本]")
|
||||
public void update(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
final CommandSender sender = e.getSender();
|
||||
switch (args.length) {
|
||||
case 0:
|
||||
repo.updateRepositories(sender);
|
||||
sender.sendMessage("§6仓库: §a仓库缓存数据已更新!");
|
||||
break;
|
||||
case 1:
|
||||
case 2:
|
||||
final String pluginname = args[0];
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
sender.sendMessage("§a开始更新插件: " + pluginname);
|
||||
if (plugin != null) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (args.length < 2) {
|
||||
YumAPI.updatefromyum(sender, plugin);
|
||||
} else {
|
||||
YumAPI.updatefromyum(sender, plugin, args[1]);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
sender.sendMessage("§c插件" + pluginname + "未安装或已卸载 需要安装请使用 §b/yum install " + pluginname + "!");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sender.sendMessage("§c命令参数错误!");
|
||||
}
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "updateall", aliases = { "ua" }, description = "更新所有可更新插件")
|
||||
public void updateall(final InvokeCommandEvent e) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
YumAPI.updateall(e.getSender());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@HandlerCommand(name = "upgrade", aliases = { "ug" }, description = "升级或载入插件", possibleArguments = "[插件名称]")
|
||||
public void upgrade(final InvokeCommandEvent e) {
|
||||
final String[] args = e.getArgs();
|
||||
final CommandSender sender = e.getSender();
|
||||
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (args.length == 0) {
|
||||
plugman.upgrade(sender);
|
||||
} else {
|
||||
final String pluginname = args[0];
|
||||
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
sender.sendMessage("§a开始升级插件: §b" + pluginname);
|
||||
if (plugin != null) {
|
||||
YumAPI.upgrade(sender, plugin);
|
||||
} else {
|
||||
sender.sendMessage("§c错误: §b插件 " + pluginname + " §c未安装或已卸载 需要安装请使用 §b/yum install " + pluginname + "!");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private String pnf(final String pname) {
|
||||
return String.format("§4错误: §c插件 §b %s §c不存在或已卸载!", pname);
|
||||
}
|
||||
|
||||
}
|
216
src/main/java/pw/yumc/Yum/manager/DownloadManager.java
Normal file
216
src/main/java/pw/yumc/Yum/manager/DownloadManager.java
Normal file
@ -0,0 +1,216 @@
|
||||
package pw.yumc.Yum.manager;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
|
||||
/**
|
||||
* 下载管理类
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2015年8月21日下午6:08:09
|
||||
*/
|
||||
public class DownloadManager {
|
||||
Plugin plugin;
|
||||
|
||||
public DownloadManager(final Plugin main) {
|
||||
this.plugin = main;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从地址获得文件名称
|
||||
*
|
||||
* @param url
|
||||
* - 地址
|
||||
* @return 文件名称
|
||||
*/
|
||||
public String getFileName(final String url) {
|
||||
final int end = url.lastIndexOf('/');
|
||||
return url.substring(end + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从地址获得文件名称
|
||||
*
|
||||
* @param url
|
||||
* - 地址
|
||||
* @return 文件名称
|
||||
*/
|
||||
public String getFileName(final URL url) {
|
||||
return getFileName(url.getFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* 从网络下载文件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param urlstring
|
||||
* - 下载地址
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean run(final CommandSender sender, final String urlstring) {
|
||||
return run(sender, urlstring, new File("plugins", getFileName(urlstring)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 从网络下载文件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param urlstring
|
||||
* - 下载地址
|
||||
* @param file
|
||||
* - 保存文件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean run(final CommandSender sender, final String urlstring, final File file) {
|
||||
try {
|
||||
final URL url = new URL(urlstring);
|
||||
return run(sender, url, file);
|
||||
} catch (final MalformedURLException e) {
|
||||
sender.sendMessage("§4错误: §c无法识别的URL地址...");
|
||||
sender.sendMessage("§4地址: §c" + urlstring);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从网络下载文件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param url
|
||||
* - 下载地址
|
||||
* @param file
|
||||
* - 保存文件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean run(CommandSender sender, final URL url, final File file) {
|
||||
BufferedInputStream in = null;
|
||||
FileOutputStream fout = null;
|
||||
if (sender == null) {
|
||||
sender = Bukkit.getConsoleSender();
|
||||
}
|
||||
try {
|
||||
sender.sendMessage("§6开始下载: §3" + getFileName(url));
|
||||
sender.sendMessage("§6下载地址: §3" + url.toString());
|
||||
final int fileLength = url.openConnection().getContentLength();
|
||||
if (fileLength < 0) {
|
||||
sender.sendMessage("§6下载: §c文件 " + file.getName() + " 获取长度错误(可能是网络问题)!");
|
||||
sender.sendMessage("§6文件: §c" + file.getName() + " 下载失败!");
|
||||
return false;
|
||||
}
|
||||
sender.sendMessage("§6文件长度: §3" + fileLength);
|
||||
in = new BufferedInputStream(url.openStream());
|
||||
if (!file.getParentFile().exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
sender.sendMessage("§6创建新目录: §d" + file.getParentFile().getAbsolutePath());
|
||||
}
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
file.createNewFile();
|
||||
sender.sendMessage("§6创建新文件: §d" + file.getAbsolutePath());
|
||||
fout = new FileOutputStream(file);
|
||||
final 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);
|
||||
final int percent = (int) (downloaded * 100L / fileLength);
|
||||
if (percent % 10 == 0) {
|
||||
if (System.currentTimeMillis() - time > 500) {
|
||||
sender.sendMessage(String.format("§6已下载: §a" + getPer(percent / 10) + " %s%%", percent));
|
||||
time = System.currentTimeMillis();
|
||||
}
|
||||
}
|
||||
}
|
||||
String pVer = null;
|
||||
try {
|
||||
final PluginDescriptionFile desc = plugin.getPluginLoader().getPluginDescription(file);
|
||||
pVer = StringUtils.substring(desc.getVersion(), 0, 15);
|
||||
} catch (final Exception e) {
|
||||
pVer = "";
|
||||
}
|
||||
sender.sendMessage("§6" + (pVer.isEmpty() ? "文件" : "插件") + ": §b" + file.getName() + (pVer.isEmpty() ? "" : " §a版本 §e" + pVer) + " §a下载完成!");
|
||||
return true;
|
||||
} catch (final Exception ex) {
|
||||
sender.sendMessage("§6异常: §c" + ex.getMessage());
|
||||
sender.sendMessage("§6文件: §c" + file.getName() + " 下载失败!");
|
||||
return false;
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
if (fout != null) {
|
||||
fout.close();
|
||||
}
|
||||
} catch (final Exception ex) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从网络下载文件
|
||||
*
|
||||
* @param urlstring
|
||||
* - 下载地址
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean run(final String urlstring) {
|
||||
return run(null, urlstring);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从网络下载文件
|
||||
*
|
||||
* @param urlstring
|
||||
* - 下载地址
|
||||
* @param file
|
||||
* - 保存文件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean run(final String urlstring, final File file) {
|
||||
return run(null, urlstring, file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从网络下载文件
|
||||
*
|
||||
* @param url
|
||||
* - 下载地址
|
||||
* @param file
|
||||
* - 保存文件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean run(final URL url, final File file) {
|
||||
return run(null, url, file);
|
||||
}
|
||||
|
||||
private String getPer(final int per) {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < 11; i++) {
|
||||
if (per > i) {
|
||||
sb.append("==");
|
||||
} else if (per == i) {
|
||||
sb.append("> ");
|
||||
} else {
|
||||
sb.append(" ");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
124
src/main/java/pw/yumc/Yum/manager/PluginInfo.java
Normal file
124
src/main/java/pw/yumc/Yum/manager/PluginInfo.java
Normal file
@ -0,0 +1,124 @@
|
||||
package pw.yumc.Yum.manager;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import cn.citycraft.PluginHelper.PluginHelperLogger;
|
||||
import cn.citycraft.PluginHelper.utils.IOUtil;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.Plugin;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.TagInfo;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.URLType;
|
||||
|
||||
public class PluginInfo implements Serializable {
|
||||
public static final String NMSVersion = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
|
||||
public String branch;
|
||||
public String name;
|
||||
public Plugin plugin;
|
||||
public String pom;
|
||||
public URLType type;
|
||||
public String repo;
|
||||
public String url;
|
||||
|
||||
/**
|
||||
* 获得下载直链
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param version
|
||||
* 版本
|
||||
* @return 下载直链
|
||||
*/
|
||||
public String getDirectUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得下载直链
|
||||
*
|
||||
* @param sender
|
||||
* 命令发送者
|
||||
* @param version
|
||||
* 版本
|
||||
* @return 下载直链
|
||||
*/
|
||||
public String getDirectUrl(final String version) {
|
||||
return url.replace("[version]", version);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得文件名称
|
||||
*
|
||||
* @return 文件名称
|
||||
*/
|
||||
public String getFileName() {
|
||||
return getFileName(plugin.version);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得文件名称
|
||||
*
|
||||
* @param version
|
||||
* 插件版本
|
||||
* @return 文件名称
|
||||
*/
|
||||
public String getFileName(final String version) {
|
||||
return String.format("%1$s-%2$s.jar", plugin.artifactId, version);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Maven仓库指定插件的下载地址
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param version
|
||||
* - 需要更新的版本
|
||||
* @return 更新地址
|
||||
*/
|
||||
public String getMavenUrl(final String ver) {
|
||||
return String.format(url + (url.endsWith("/") ? "" : "/") + "%1$s/%2$s/%3$s/%2$s-%3$s.jar",
|
||||
plugin.groupId.replace(".", "/"),
|
||||
plugin.artifactId,
|
||||
(ver == null || ver.isEmpty()) ? plugin.version : ver);
|
||||
}
|
||||
|
||||
public String getUrl(final CommandSender sender, final String version) {
|
||||
String ver = version;
|
||||
if (ver == null) {
|
||||
if (plugin.tags != null) {
|
||||
PluginHelperLogger.getLogger().debug("发现存在TAG标签 开始检索: " + NMSVersion);
|
||||
for (final TagInfo tagInfo : plugin.tags) {
|
||||
if (tagInfo.tag.equalsIgnoreCase(NMSVersion)) {
|
||||
sender.sendMessage("§6版本: §b从Tag标签中获取 §e" + NMSVersion + " §b的最新版本...");
|
||||
ver = tagInfo.version;
|
||||
if (tagInfo.type == URLType.DirectUrl) {
|
||||
return tagInfo.url;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if (pom != null && !pom.isEmpty()) {
|
||||
pom = pom.replace("[name]", name).replace("[branch]", branch);
|
||||
sender.sendMessage("§6版本: §b尝试从在线POM文件获取最新版本...");
|
||||
ver = IOUtil.getXMLTag(pom, "version", null);
|
||||
if (ver != null) {
|
||||
sender.sendMessage("§6版本: §a成功获取到最新版本 §e" + ver + " §a...");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ver == null) {
|
||||
ver = plugin.version;
|
||||
sender.sendMessage("§6版本: §d使用缓存的版本 §e" + ver + " §a...");
|
||||
}
|
||||
switch (type) {
|
||||
case DirectUrl:
|
||||
return getDirectUrl(ver);
|
||||
case Maven:
|
||||
return getMavenUrl(ver);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
732
src/main/java/pw/yumc/Yum/manager/PluginsManager.java
Normal file
732
src/main/java/pw/yumc/Yum/manager/PluginsManager.java
Normal file
@ -0,0 +1,732 @@
|
||||
package pw.yumc.Yum.manager;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.plugin.InvalidDescriptionException;
|
||||
import org.bukkit.plugin.InvalidPluginException;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginLoader;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.UnknownDependencyException;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
import cn.citycraft.PluginHelper.utils.FileUtil;
|
||||
import cn.citycraft.PluginHelper.utils.StringUtil;
|
||||
|
||||
/**
|
||||
* 插件管理类
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2015年8月21日下午7:03:26
|
||||
*/
|
||||
public class PluginsManager {
|
||||
private final Set<String> ignoreList = new HashSet<>();
|
||||
private final Plugin main;
|
||||
|
||||
public PluginsManager(final Plugin plugin) {
|
||||
this.main = plugin;
|
||||
}
|
||||
|
||||
public static String getVersion(final Plugin plugin) {
|
||||
return StringUtils.substring(plugin.getDescription().getVersion(), 0, 15);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加到忽略列表
|
||||
*
|
||||
* @param name
|
||||
* - 插件名称
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean addIgnore(final Collection<? extends String> name) {
|
||||
return ignoreList.addAll(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加到忽略列表
|
||||
*
|
||||
* @param name
|
||||
* - 插件名称
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean addIgnore(final String name) {
|
||||
return ignoreList.add(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除插件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean deletePlugin(final CommandSender sender, final Plugin plugin) {
|
||||
return unload(sender, plugin) && getPluginFile(plugin).delete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除插件
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean deletePlugin(final Plugin plugin) {
|
||||
return deletePlugin(Bukkit.getConsoleSender(), plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭插件
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
*/
|
||||
public void disable(final Plugin plugin) {
|
||||
if ((plugin != null) && (plugin.isEnabled())) {
|
||||
Bukkit.getPluginManager().disablePlugin(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭所有插件
|
||||
*/
|
||||
public void disableAll() {
|
||||
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
if (!isIgnored(plugin)) {
|
||||
disable(plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用插件
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
*/
|
||||
public void enable(final Plugin plugin) {
|
||||
if ((plugin != null) && (!plugin.isEnabled())) {
|
||||
Bukkit.getPluginManager().enablePlugin(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启用所有插件
|
||||
*/
|
||||
public void enableAll() {
|
||||
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
if (!isIgnored(plugin)) {
|
||||
enable(plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除插件(包括数据)
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean fullDeletePlugin(final CommandSender sender, final Plugin plugin) {
|
||||
return unload(sender, plugin) && getPluginFile(plugin).delete() && FileUtil.deleteDir(sender, plugin.getDataFolder());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得格式化的插件名称
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 格式化的插件名称
|
||||
*/
|
||||
public String getFormattedName(final Plugin plugin) {
|
||||
return getFormattedName(plugin, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得格式化的插件名称(可带版本)
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @param includeVersions
|
||||
* - 是否包括版本
|
||||
* @return 格式化的插件名称
|
||||
*/
|
||||
public String getFormattedName(final Plugin plugin, final boolean includeVersions) {
|
||||
final ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED;
|
||||
String pluginName = color + plugin.getName();
|
||||
if (includeVersions) {
|
||||
pluginName = pluginName + " (" + getVersion(plugin) + ")";
|
||||
}
|
||||
return pluginName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过名称获得插件
|
||||
*
|
||||
* @param name
|
||||
* - 名称
|
||||
* @return 插件
|
||||
*/
|
||||
public Plugin getPluginByName(final String name) {
|
||||
return Bukkit.getPluginManager().getPlugin(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过名称获得插件(处理带空格的插件)
|
||||
*
|
||||
* @param name
|
||||
* - 名称
|
||||
* @return 插件
|
||||
*/
|
||||
public Plugin getPluginByName(final String[] args, final int start) {
|
||||
return getPluginByName(StringUtil.consolidateStrings(args, start));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得插件绝对路径
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 插件的绝对路径
|
||||
*/
|
||||
public File getPluginFile(final Plugin plugin) {
|
||||
File file = null;
|
||||
final ClassLoader cl = plugin.getClass().getClassLoader();
|
||||
if ((cl instanceof URLClassLoader)) {
|
||||
@SuppressWarnings("resource")
|
||||
final URLClassLoader ucl = (URLClassLoader) cl;
|
||||
final URL url = ucl.getURLs()[0];
|
||||
try {
|
||||
file = new File(URLDecoder.decode(url.getFile(), "UTF-8"));
|
||||
} catch (final UnsupportedEncodingException e) {
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public List<String> getPluginNames(final boolean fullName) {
|
||||
final List<String> plugins = new ArrayList<String>();
|
||||
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName());
|
||||
}
|
||||
return plugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得插件版本
|
||||
*
|
||||
* @param name
|
||||
* - 插件名称
|
||||
* @return 插件版本
|
||||
*/
|
||||
public String getPluginVersion(final String name) {
|
||||
final Plugin plugin = getPluginByName(name);
|
||||
if ((plugin != null) && (plugin.getDescription() != null)) {
|
||||
return getVersion(plugin);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得插件命令
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 插件命令
|
||||
*/
|
||||
public String getUsages(final Plugin plugin) {
|
||||
final List<String> parsedCommands = new ArrayList<String>();
|
||||
|
||||
final Map<String, Map<String, Object>> commands = plugin.getDescription().getCommands();
|
||||
|
||||
if (commands != null) {
|
||||
final Iterator<Entry<String, Map<String, Object>>> commandsIt = commands.entrySet().iterator();
|
||||
while (commandsIt.hasNext()) {
|
||||
final Entry<String, Map<String, Object>> thisEntry = commandsIt.next();
|
||||
if (thisEntry != null) {
|
||||
parsedCommands.add(thisEntry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (parsedCommands.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
return Joiner.on(", ").join(parsedCommands);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断插件是否在忽略列表
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 是否
|
||||
*/
|
||||
public boolean isIgnored(final Plugin plugin) {
|
||||
return isIgnored(plugin.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断插件是否在忽略列表
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件名称
|
||||
* @return 是否
|
||||
*/
|
||||
public boolean isIgnored(final String plugin) {
|
||||
for (final String name : ignoreList) {
|
||||
if (name.equalsIgnoreCase(plugin)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param pluginFile
|
||||
* - 插件文件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean load(final CommandSender sender, final File pluginFile) {
|
||||
Plugin target = null;
|
||||
final String name = pluginFile.getName();
|
||||
try {
|
||||
target = Bukkit.getPluginManager().loadPlugin(pluginFile);
|
||||
} catch (final InvalidDescriptionException e) {
|
||||
sender.sendMessage("§4异常: §c" + e.getMessage());
|
||||
sender.sendMessage("§4插件: §c" + name + " 的 plugin.yml 文件存在错误!");
|
||||
return false;
|
||||
} catch (final UnsupportedClassVersionError e) {
|
||||
sender.sendMessage("§4异常: §c" + e.getMessage());
|
||||
sender.sendMessage("§c服务器或JAVA的版本低于插件: " + name + " 所需要的版本!!");
|
||||
return false;
|
||||
} catch (final InvalidPluginException e) {
|
||||
sender.sendMessage("§4异常: §c" + e.getMessage());
|
||||
sender.sendMessage("§4文件: §c" + name + " 不是一个可载入的插件!");
|
||||
sender.sendMessage("§4注意: §cMOD服重载插件3次以上需重启服务器");
|
||||
return false;
|
||||
} catch (final UnknownDependencyException e) {
|
||||
sender.sendMessage("§4异常: §c服务器未安装必须依赖: " + e.getMessage());
|
||||
sender.sendMessage("§4插件: §c" + name + " 载入失败 缺少部分依赖项目!");
|
||||
return false;
|
||||
}
|
||||
if (target == null) {
|
||||
sender.sendMessage("§4异常: §c服务器类加载器载入插件失败!");
|
||||
return false;
|
||||
}
|
||||
target.onLoad();
|
||||
Bukkit.getPluginManager().enablePlugin(target);
|
||||
sender.sendMessage("§6载入: §a插件 §b" + target.getName() + " §a版本 §d" + getVersion(target) + " §a已成功载入到服务器!");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param name
|
||||
* - 插件名称
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean load(CommandSender sender, final String name) {
|
||||
String filename = null;
|
||||
if (sender == null) {
|
||||
sender = Bukkit.getConsoleSender();
|
||||
}
|
||||
if (!name.endsWith(".jar")) {
|
||||
filename = name + ".jar";
|
||||
}
|
||||
final File pluginDir = new File("plugins");
|
||||
final File updateDir = new File(pluginDir, "update");
|
||||
if (!pluginDir.isDirectory()) {
|
||||
sender.sendMessage("§6载入: §c插件目录不存在或IO错误!");
|
||||
return false;
|
||||
}
|
||||
|
||||
File pluginFile = new File(pluginDir, filename);
|
||||
|
||||
if (!pluginFile.isFile() && !new File(updateDir, filename).isFile()) {
|
||||
pluginFile = null;
|
||||
for (final File file : pluginDir.listFiles()) {
|
||||
if (file.getName().endsWith(".jar")) {
|
||||
try {
|
||||
final PluginDescriptionFile desc = main.getPluginLoader().getPluginDescription(file);
|
||||
if (desc.getName().equalsIgnoreCase(name)) {
|
||||
pluginFile = file;
|
||||
break;
|
||||
}
|
||||
} catch (final InvalidDescriptionException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (pluginFile == null) {
|
||||
sender.sendMessage("§6载入: §c在插件目录和更新目录均未找到 §b" + name + " §c插件 请确认文件是否存在!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return load(sender, pluginFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件
|
||||
*
|
||||
* @param pluginFile
|
||||
* - 插件名称
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean load(final File pluginFile) {
|
||||
return load(Bukkit.getConsoleSender(), pluginFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 载入插件
|
||||
*
|
||||
* @param name
|
||||
* - 插件名称
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean load(final String name) {
|
||||
return load(Bukkit.getConsoleSender(), name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除重载插件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean reload(final CommandSender sender, final Plugin plugin) {
|
||||
if (plugin != null) {
|
||||
return unload(sender, plugin) && load(sender, plugin.getName());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除重载插件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param main
|
||||
* - 插件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean reload(final CommandSender sender, final String name) {
|
||||
if (name != null) {
|
||||
return unload(sender, name) && load(sender, name);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载插件
|
||||
*
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean reload(final Plugin plugin) {
|
||||
return reload(Bukkit.getConsoleSender(), plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载所有插件
|
||||
*/
|
||||
public void reloadAll() {
|
||||
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
if (!isIgnored(plugin)) {
|
||||
unload(plugin);
|
||||
}
|
||||
}
|
||||
Bukkit.getPluginManager().loadPlugins(Bukkit.getUpdateFolderFile().getParentFile());
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载所有插件
|
||||
*/
|
||||
public void reloadAll(final CommandSender sender) {
|
||||
final Plugin[] plist = Bukkit.getPluginManager().getPlugins();
|
||||
for (final Plugin plugin : plist) {
|
||||
if (!isIgnored(plugin)) {
|
||||
unload(sender, plugin);
|
||||
}
|
||||
}
|
||||
for (final Plugin plugin : plist) {
|
||||
if (!isIgnored(plugin)) {
|
||||
load(sender, plugin.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从忽略列表移除
|
||||
*
|
||||
* @param name
|
||||
* - 插件名称
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean removeIgnore(final String name) {
|
||||
return ignoreList.remove(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载插件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param plugin
|
||||
* - 插件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean unload(final CommandSender sender, final Plugin plugin) {
|
||||
return unload(sender, plugin.getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载插件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param name
|
||||
* - 插件名称
|
||||
* @return 是否成功
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean unload(CommandSender sender, final String name) {
|
||||
if (sender == null) {
|
||||
sender = Bukkit.getConsoleSender();
|
||||
}
|
||||
final PluginManager pluginManager = Bukkit.getPluginManager();
|
||||
SimpleCommandMap commandMap = null;
|
||||
List<Plugin> plugins = null;
|
||||
Map<String, Plugin> lookupNames = null;
|
||||
Map<String, Command> knownCommands = null;
|
||||
// ###移除类加载器后会导致插件无法载入###
|
||||
// Map<Pattern, PluginLoader> fileAssociations = null;
|
||||
if (pluginManager == null) {
|
||||
sender.sendMessage("§4异常: §c插件管理类反射获取失败!");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
final Field pluginsField = pluginManager.getClass().getDeclaredField("plugins");
|
||||
pluginsField.setAccessible(true);
|
||||
plugins = (List<Plugin>) pluginsField.get(pluginManager);
|
||||
|
||||
final Field lookupNamesField = pluginManager.getClass().getDeclaredField("lookupNames");
|
||||
lookupNamesField.setAccessible(true);
|
||||
lookupNames = (Map<String, Plugin>) lookupNamesField.get(pluginManager);
|
||||
|
||||
final Field commandMapField = pluginManager.getClass().getDeclaredField("commandMap");
|
||||
commandMapField.setAccessible(true);
|
||||
commandMap = (SimpleCommandMap) commandMapField.get(pluginManager);
|
||||
|
||||
final Field knownCommandsField = commandMap.getClass().getDeclaredField("knownCommands");
|
||||
knownCommandsField.setAccessible(true);
|
||||
knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
|
||||
|
||||
// ###移除类加载器后会导致插件无法载入###
|
||||
// final Field fileAssociationsField = pluginManager.getClass().getDeclaredField("fileAssociations");
|
||||
// fileAssociationsField.setAccessible(true);
|
||||
// fileAssociations = (Map<Pattern, PluginLoader>) fileAssociationsField.get(pluginManager);
|
||||
} catch (final Exception e) {
|
||||
sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件 §b" + name + " §c卸载失败!");
|
||||
return false;
|
||||
}
|
||||
String pluginVersion = "";
|
||||
for (final Plugin next : pluginManager.getPlugins()) {
|
||||
if (next.getName().equals(name)) {
|
||||
pluginManager.disablePlugin(next);
|
||||
if ((plugins != null) && (plugins.contains(next))) {
|
||||
pluginVersion = getVersion(next);
|
||||
plugins.remove(next);
|
||||
sender.sendMessage("§6卸载: §a从服务器插件列表删除 §b" + name + " §a的实例!");
|
||||
}
|
||||
|
||||
if ((lookupNames != null) && (lookupNames.containsKey(name))) {
|
||||
lookupNames.remove(name);
|
||||
sender.sendMessage("§6卸载: §a从插件查找列表删除 §b" + name + " §a的实例!");
|
||||
}
|
||||
|
||||
if (commandMap != null) {
|
||||
for (final Iterator<Map.Entry<String, Command>> it = knownCommands.entrySet().iterator(); it.hasNext();) {
|
||||
final Map.Entry<String, Command> entry = it.next();
|
||||
if ((entry.getValue() instanceof PluginCommand)) {
|
||||
final PluginCommand command = (PluginCommand) entry.getValue();
|
||||
if (command.getPlugin() == next) {
|
||||
command.unregister(commandMap);
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
sender.sendMessage("§6卸载: §a注销插件 §b" + name + " §a的所有命令!");
|
||||
}
|
||||
final ClassLoader cl = next.getClass().getClassLoader();
|
||||
try {
|
||||
((URLClassLoader) cl).close();
|
||||
} catch (final IOException ex) {
|
||||
}
|
||||
// ###移除类加载器后会导致插件无法载入###
|
||||
// if (fileAssociations != null) {
|
||||
// for (final Iterator<Entry<Pattern, PluginLoader>> filter = fileAssociations.entrySet().iterator(); filter.hasNext();) {
|
||||
// final Entry<Pattern, PluginLoader> entry = filter.next();
|
||||
// final Matcher match = entry.getKey().matcher(getPluginFile(next).getName());
|
||||
// if (match.find()) {
|
||||
// filter.remove();
|
||||
// sender.sendMessage("§6卸载: §a移除插件 §b" + name + " §a的类加载器!");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
System.gc();
|
||||
}
|
||||
}
|
||||
if (!pluginVersion.isEmpty()) {
|
||||
sender.sendMessage("§6卸载: §a插件 §b" + name + " §a版本 §d" + pluginVersion + " §a已成功卸载!");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 卸载插件
|
||||
*
|
||||
* @param plugin
|
||||
* - 卸载插件
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean unload(final Plugin plugin) {
|
||||
return unload(Bukkit.getConsoleSender(), plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载update文件夹的插件
|
||||
*
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean upgrade(final CommandSender sender) {
|
||||
sender.sendMessage("§6升级: §a开始升级 服务器更新 目录下的所有插件!");
|
||||
return upgrade(sender, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载update文件夹的插件
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param directory
|
||||
* - 更新目录
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean upgrade(final CommandSender sender, final File directory, final Plugin plugin) {
|
||||
boolean result = false;
|
||||
final PluginLoader loader = main.getPluginLoader();
|
||||
File updateDirectory;
|
||||
if (directory == null || !directory.isDirectory()) {
|
||||
updateDirectory = Bukkit.getServer().getUpdateFolderFile();
|
||||
} else {
|
||||
updateDirectory = directory;
|
||||
}
|
||||
if (updateDirectory == null) {
|
||||
sender.sendMessage("§4异常: §c文件夹 §d服务器更新文件夹 §c未找到或IO错误!");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
sender.sendMessage("§6升级: §b从 §d" + updateDirectory.getCanonicalPath() + " §b文件夹检索插件插件!");
|
||||
} catch (SecurityException | IOException e1) {
|
||||
sender.sendMessage("§4异常: §c文件夹 §d" + updateDirectory.getName() + " §c权限不足或IO错误!");
|
||||
return false;
|
||||
}
|
||||
final File[] plugins = updateDirectory.listFiles();
|
||||
if (plugins.length == 0) {
|
||||
sender.sendMessage("§6升级: §d更新文件夹未找到插件!");
|
||||
return false;
|
||||
}
|
||||
for (final File file : updateDirectory.listFiles()) {
|
||||
PluginDescriptionFile description = null;
|
||||
try {
|
||||
description = loader.getPluginDescription(file);
|
||||
final String name = description.getName();
|
||||
if (plugin != null && !name.equals(plugin.getName())) {
|
||||
continue;
|
||||
}
|
||||
final Plugin oldplugin = Bukkit.getPluginManager().getPlugin(name);
|
||||
result = true;
|
||||
File dest = null;
|
||||
if (!unload(sender, name)) {
|
||||
sender.sendMessage("§6升级: §d开始安装 §b" + name + " §d插件!");
|
||||
dest = new File(Bukkit.getUpdateFolderFile().getParentFile(), File.separatorChar + file.getName());
|
||||
} else {
|
||||
if (oldplugin != null) {
|
||||
dest = new File(Bukkit.getUpdateFolderFile(), File.separatorChar + getPluginFile(oldplugin).getName());
|
||||
}
|
||||
sender.sendMessage("§6升级: §a开始升级 §b" + name + " §a插件!");
|
||||
}
|
||||
if (dest != null) {
|
||||
file.renameTo(dest);
|
||||
}
|
||||
load(sender, name);
|
||||
} catch (final InvalidDescriptionException e) {
|
||||
sender.sendMessage("§4异常: §c" + e.getMessage());
|
||||
sender.sendMessage("§4文件: §c" + file.getName() + " 的plugin.yml文件存在错误!");
|
||||
}
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
sender.sendMessage("§6升级: §a所有插件升级完毕!");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载update文件夹的插件
|
||||
*
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean upgrade(final CommandSender sender, final Plugin plugin) {
|
||||
return upgrade(sender, null, plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重载update文件夹的插件
|
||||
*
|
||||
* @return 是否成功
|
||||
*/
|
||||
public boolean upgrade(final File directory) {
|
||||
Bukkit.getConsoleSender().sendMessage("§6升级: §a开始升级 §d" + directory.getName() + " §a目录下的所有插件!");
|
||||
return upgrade(Bukkit.getConsoleSender(), directory, null);
|
||||
}
|
||||
}
|
83
src/main/java/pw/yumc/Yum/manager/RepoCache.java
Normal file
83
src/main/java/pw/yumc/Yum/manager/RepoCache.java
Normal file
@ -0,0 +1,83 @@
|
||||
package pw.yumc.Yum.manager;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import cn.citycraft.PluginHelper.PluginHelperLogger;
|
||||
import cn.citycraft.PluginHelper.jsonresult.JsonHandle;
|
||||
import cn.citycraft.PluginHelper.utils.IOUtil;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.Repositories;
|
||||
|
||||
public class RepoCache implements Serializable {
|
||||
PluginHelperLogger logger = PluginHelperLogger.getLogger();
|
||||
Map<String, PluginInfo> plugins = new HashMap<String, PluginInfo>();
|
||||
Map<String, Repositories> repos = new HashMap<String, Repositories>();
|
||||
|
||||
public static RepoCache fromJson(final String json) {
|
||||
return JsonHandle.fromJson(json, RepoCache.class);
|
||||
}
|
||||
|
||||
public void addPlugins(final String name, final PluginInfo info) {
|
||||
plugins.put(name, info);
|
||||
}
|
||||
|
||||
public Repositories addRepo(final String repo) {
|
||||
if (repos.containsKey(repo) || repo.isEmpty()) {
|
||||
logger.debug("源地址为空或已存在 " + repo);
|
||||
return null;
|
||||
}
|
||||
final Repositories reposes = getRepo(repo);
|
||||
if (reposes == null) {
|
||||
return null;
|
||||
}
|
||||
repos.put(repo, reposes);
|
||||
return reposes;
|
||||
}
|
||||
|
||||
public List<String> getAllRepoInfo() {
|
||||
final List<String> repoinfo = new ArrayList<String>();
|
||||
for (final Entry<String, Repositories> repo : repos.entrySet()) {
|
||||
repoinfo.add(String.format("§d仓库: §e%s §6- §3%s", repo.getValue().name, repo.getKey()));
|
||||
}
|
||||
return repoinfo;
|
||||
}
|
||||
|
||||
public Map<String, PluginInfo> getPlugins() {
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public Repositories getRepo(final String repo) {
|
||||
final String json = IOUtil.getData(repo);
|
||||
if (json == null || json.isEmpty()) {
|
||||
logger.debug("源地址获取数据为空 " + repo);
|
||||
return null;
|
||||
}
|
||||
final Repositories reposes = JsonHandle.fromJson(json, Repositories.class);
|
||||
if (reposes == null || reposes.repos.isEmpty()) {
|
||||
logger.debug("源地址解析Json为空 " + repo);
|
||||
return null;
|
||||
}
|
||||
return reposes;
|
||||
}
|
||||
|
||||
public Map<String, Repositories> getRepos() {
|
||||
return repos;
|
||||
}
|
||||
|
||||
public boolean removeRepo(final String repo) {
|
||||
if (repo.isEmpty() || !repos.containsKey(repo)) {
|
||||
return false;
|
||||
}
|
||||
repos.remove(repo);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonHandle.toJson(this);
|
||||
}
|
||||
}
|
60
src/main/java/pw/yumc/Yum/manager/RepoSerialization.java
Normal file
60
src/main/java/pw/yumc/Yum/manager/RepoSerialization.java
Normal file
@ -0,0 +1,60 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package pw.yumc.Yum.manager;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 源仓库序列化类
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2015年8月31日下午7:41:53
|
||||
*/
|
||||
public class RepoSerialization {
|
||||
public class PackageInfo implements Serializable {
|
||||
public String name;
|
||||
public List<Plugin> plugins = new ArrayList<>();
|
||||
public String pom;
|
||||
public String url;
|
||||
public URLType type;
|
||||
}
|
||||
|
||||
public class Plugin implements Serializable {
|
||||
public String artifactId;
|
||||
public String branch;
|
||||
public String description;
|
||||
public String groupId;
|
||||
public String name;
|
||||
public String url;
|
||||
public String pom;
|
||||
public List<TagInfo> tags;
|
||||
public String version;
|
||||
public URLType type;
|
||||
}
|
||||
|
||||
public class Repositories implements Serializable {
|
||||
public String name;
|
||||
public List<Repository> repos;
|
||||
}
|
||||
|
||||
public class Repository implements Serializable {
|
||||
public String id;
|
||||
public URLType type;
|
||||
public String url;
|
||||
}
|
||||
|
||||
public class TagInfo implements Serializable {
|
||||
public String tag;
|
||||
public String version;
|
||||
public URLType type;
|
||||
public String url;
|
||||
}
|
||||
|
||||
public enum URLType {
|
||||
Maven,
|
||||
DirectUrl;
|
||||
}
|
||||
}
|
229
src/main/java/pw/yumc/Yum/manager/RepositoryManager.java
Normal file
229
src/main/java/pw/yumc/Yum/manager/RepositoryManager.java
Normal file
@ -0,0 +1,229 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package pw.yumc.Yum.manager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import cn.citycraft.PluginHelper.PluginHelperLogger;
|
||||
import cn.citycraft.PluginHelper.jsonresult.JsonHandle;
|
||||
import cn.citycraft.PluginHelper.utils.IOUtil;
|
||||
import cn.citycraft.PluginHelper.utils.StringUtil;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.PackageInfo;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.Plugin;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.Repositories;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.Repository;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.TagInfo;
|
||||
import pw.yumc.Yum.manager.RepoSerialization.URLType;
|
||||
|
||||
/**
|
||||
* 仓库管理类
|
||||
*
|
||||
* @author 喵♂呜
|
||||
* @since 2016年1月9日 上午10:02:57
|
||||
*/
|
||||
public class RepositoryManager {
|
||||
PluginHelperLogger logger = PluginHelperLogger.getLogger();
|
||||
org.bukkit.plugin.Plugin main;
|
||||
RepoCache repocache;
|
||||
|
||||
public RepositoryManager(final org.bukkit.plugin.Plugin plugin) {
|
||||
this.main = plugin;
|
||||
repocache = new RepoCache();
|
||||
}
|
||||
|
||||
public boolean addPackage(final CommandSender sender, final String urlstring) {
|
||||
final String json = IOUtil.getData(urlstring);
|
||||
if (json == null || json.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
final PackageInfo pkg = jsonToPackage(json);
|
||||
if (pkg == null) {
|
||||
return false;
|
||||
}
|
||||
updatePackage(sender, pkg);
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean addRepositories(final CommandSender sender, final String urlstring) {
|
||||
final String url = handerRepoUrl(urlstring);
|
||||
final Repositories repo = repocache.addRepo(url);
|
||||
if (repo == null) {
|
||||
return false;
|
||||
}
|
||||
return updateRepositories(sender, repo);
|
||||
}
|
||||
|
||||
public void clean() {
|
||||
repocache.getPlugins().clear();
|
||||
}
|
||||
|
||||
public boolean delRepositories(final CommandSender sender, final String urlstring) {
|
||||
return repocache.removeRepo(handerRepoUrl(urlstring));
|
||||
}
|
||||
|
||||
public List<PluginInfo> getAllPlugin() {
|
||||
final List<PluginInfo> li = new ArrayList<PluginInfo>();
|
||||
for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
|
||||
li.add(plugin.getValue());
|
||||
}
|
||||
return li;
|
||||
}
|
||||
|
||||
public List<String> getAllPluginName() {
|
||||
final List<String> li = new ArrayList<String>();
|
||||
for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
|
||||
li.add(plugin.getValue().name);
|
||||
}
|
||||
return li;
|
||||
}
|
||||
|
||||
public List<String> getAllPluginsInfo() {
|
||||
final List<String> li = new ArrayList<String>();
|
||||
li.add("§d仓库名称 §a插件名称 §e插件描述");
|
||||
for (final Entry<String, PluginInfo> pi : repocache.getPlugins().entrySet()) {
|
||||
final Plugin plugin = pi.getValue().plugin;
|
||||
li.add(String.format("§d%s §a%s §6- §e%s", pi.getValue().repo, pi.getValue().name, plugin.description));
|
||||
if (plugin.tags != null) {
|
||||
li.add(" §b┗Tags §c标签 §a版本 §e类型");
|
||||
final List<TagInfo> taglist = plugin.tags;
|
||||
for (int i = 0; i < taglist.size(); i++) {
|
||||
final TagInfo tag = taglist.get(i);
|
||||
li.add(" §b" + (i == taglist.size() - 1 ? "┗ " : "┣ ") + String.format("§c%s §a%s §e%s", tag.tag, tag.version, tag.type != null ? tag.type : URLType.Maven));
|
||||
}
|
||||
}
|
||||
}
|
||||
return li;
|
||||
}
|
||||
|
||||
public PluginInfo getPlugin(final String name) {
|
||||
for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
|
||||
if (plugin.getValue().name.equalsIgnoreCase(name)) {
|
||||
return plugin.getValue();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<PluginInfo> getPluginInfo(final String name) {
|
||||
final List<PluginInfo> li = new ArrayList<PluginInfo>();
|
||||
for (final Entry<String, PluginInfo> plugin : repocache.getPlugins().entrySet()) {
|
||||
if (plugin.getValue().name.equalsIgnoreCase(name)) {
|
||||
li.add(plugin.getValue());
|
||||
}
|
||||
}
|
||||
return li;
|
||||
}
|
||||
|
||||
public PluginInfo getPluginInfo(final String groupId, final String artifactId) {
|
||||
return repocache.getPlugins().get(groupId + "." + artifactId);
|
||||
}
|
||||
|
||||
public Map<String, PluginInfo> getPlugins() {
|
||||
return repocache.getPlugins();
|
||||
}
|
||||
|
||||
public RepoCache getRepoCache() {
|
||||
return repocache;
|
||||
}
|
||||
|
||||
public Repositories getRepoCache(final String urlstring) {
|
||||
return repocache.repos.get(handerRepoUrl(urlstring));
|
||||
}
|
||||
|
||||
public Map<String, Repositories> getRepos() {
|
||||
return repocache.getRepos();
|
||||
}
|
||||
|
||||
public boolean getRepositories(final CommandSender sender, final String urlstring) {
|
||||
final int urllength = urlstring.length();
|
||||
final String url = urlstring.substring(0, urlstring.endsWith("/") ? urllength - 1 : urllength);
|
||||
handerRepoUrl(url);
|
||||
final Repositories repo = repocache.addRepo(urlstring);
|
||||
if (repo == null) {
|
||||
return false;
|
||||
}
|
||||
return updateRepositories(sender, repo);
|
||||
}
|
||||
|
||||
public PackageInfo jsonToPackage(final String json) {
|
||||
try {
|
||||
return JsonHandle.fromJson(json, PackageInfo.class);
|
||||
} catch (final Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Repositories jsonToRepositories(final String json) {
|
||||
return JsonHandle.fromJson(json, Repositories.class);
|
||||
}
|
||||
|
||||
public void updatePackage(final CommandSender sender, final PackageInfo pkg) {
|
||||
for (final Plugin plugin : pkg.plugins) {
|
||||
final PluginInfo pi = new PluginInfo();
|
||||
pi.name = StringUtil.getNotNull(plugin.name, plugin.artifactId);
|
||||
pi.branch = StringUtil.getNotNull(plugin.branch, "master");
|
||||
pi.pom = StringUtil.getNotNull(plugin.pom, pkg.pom);
|
||||
pi.url = StringUtil.getNotNull(plugin.url, pkg.url);
|
||||
pi.type = plugin.type != null ? plugin.type : pkg.type;
|
||||
pi.type = pi.type != null ? pi.type : URLType.Maven;
|
||||
pi.plugin = plugin;
|
||||
pi.repo = pkg.name;
|
||||
repocache.getPlugins().put(plugin.groupId + "." + plugin.artifactId, pi);
|
||||
}
|
||||
sender.sendMessage("§6仓库: §e" + pkg.name + " §a更新成功!");
|
||||
}
|
||||
|
||||
public boolean updateRepositories(final CommandSender sender) {
|
||||
repocache.getPlugins().clear();
|
||||
if (repocache.getRepos().isEmpty()) {
|
||||
repocache.addRepo("https://coding.net/u/502647092/p/YumData/git/raw/master/yumcenter/repo.info");
|
||||
}
|
||||
final Iterator<Entry<String, Repositories>> keys = repocache.getRepos().entrySet().iterator();
|
||||
while (keys.hasNext()) {
|
||||
final Entry<String, Repositories> string = keys.next();
|
||||
final Repositories repo = repocache.getRepo(string.getKey());
|
||||
if (updateRepositories(sender, repo)) {
|
||||
sender.sendMessage("§6源: §e" + repo.name + " §a更新成功!");
|
||||
} else {
|
||||
sender.sendMessage("§6源: §e" + string.getKey() + " §c未找到任何仓库信息 已删除!");
|
||||
keys.remove();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean updateRepositories(CommandSender sender, final Repositories repocenter) {
|
||||
if (sender == null) {
|
||||
sender = Bukkit.getConsoleSender();
|
||||
}
|
||||
if (repocenter == null || repocenter.repos.isEmpty()) {
|
||||
logger.debug("源地址为Null或源列表为空!");
|
||||
return false;
|
||||
}
|
||||
for (final Repository repo : repocenter.repos) {
|
||||
addPackage(sender, repo.url);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private String handerRepoUrl(String url) {
|
||||
final int urllength = url.length();
|
||||
url = url.substring(0, url.endsWith("/") ? urllength - 1 : urllength);
|
||||
if (!url.startsWith("http://")) {
|
||||
url = "http://" + url;
|
||||
}
|
||||
if (!url.endsWith("repo.info")) {
|
||||
url = url + "/repo.info";
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user