add repo manager fix unload error...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2015-08-31 21:28:02 +08:00
parent f498e3ea3f
commit dba9a1df20
23 changed files with 1189 additions and 127 deletions

View File

@ -3,8 +3,9 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.citycraft</groupId>
<artifactId>Yum</artifactId>
<version>1.3</version>
<version>1.3-bate1</version>
<name>Yum</name>
<description>Minecraft 服务器插件管理系统</description>
<build>
<finalName>${project.name}</finalName>
<resources>

View File

@ -7,7 +7,9 @@ import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.Yum.commands.CommandHandler;
import cn.citycraft.Yum.config.FileConfig;
import cn.citycraft.Yum.utils.DownloadManager;
import cn.citycraft.Yum.manager.DownloadManager;
import cn.citycraft.Yum.manager.PluginsManager;
import cn.citycraft.Yum.repository.RepositoryManager;
/**
* MC插件仓库
@ -16,14 +18,26 @@ import cn.citycraft.Yum.utils.DownloadManager;
*/
public class Yum extends JavaPlugin {
public DownloadManager download;
public PluginsManager plugman;
public RepositoryManager repo;
public FileConfig config;
@Override
public void onEnable() {
plugman = new PluginsManager(this);
download = new DownloadManager(this);
repo = new RepositoryManager(this);
config = new FileConfig(this, "config.yml");
repo.jsonToCache(config.getString("cache"));
CommandHandler cmdhandler = new CommandHandler(this);
this.getCommand("yum").setExecutor(cmdhandler);
this.getCommand("yum").setTabCompleter(cmdhandler);
this.getPluginLoader();
}
@Override
public void onDisable() {
config.set("cache", repo.cacheToJson());
}
}

View File

@ -3,12 +3,12 @@
*/
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.utils.PluginsManager;
/**
* 插件删除命令类
@ -16,22 +16,22 @@ import cn.citycraft.Yum.utils.PluginsManager;
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandDelete extends BaseCommand {
Yum yum;
Yum main;
/**
* @param name
*/
public CommandDelete(Yum main) {
super("delete");
this.yum = main;
this.main = main;
}
@Override
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
String pluginname = args[0];
Plugin plugin = yum.getServer().getPluginManager().getPlugin(pluginname);
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) {
PluginsManager.deletePlugin(sender, plugin);
main.plugman.deletePlugin(sender, plugin);
} else {
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
}

View File

@ -16,7 +16,6 @@ import org.bukkit.entity.Player;
import org.bukkit.util.StringUtil;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.utils.PluginsManager;
/**
* 子命令处理类
@ -51,7 +50,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
/**
* 插件主类
*/
Yum plugin;
Yum main;
/**
* 注册子命令
@ -60,7 +59,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
* - 插件主类
*/
public CommandHandler(Yum yum) {
this.plugin = yum;
this.main = yum;
registerCommand(new CommandList(yum));
registerCommand(new CommandInstall(yum));
registerCommand(new CommandUpdate(yum));
@ -125,7 +124,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
}
if (args.length == 2) {
String partialPlugin = args[1];
List<String> plugins = PluginsManager.getPluginNames(false);
List<String> plugins = main.plugman.getPluginNames(false);
StringUtil.copyPartialMatches(partialPlugin, plugins, completions);
}
Collections.sort(completions);

View File

@ -10,7 +10,6 @@ import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.utils.PluginsManager;
import cn.citycraft.Yum.utils.StringUtil;
/**
@ -19,14 +18,14 @@ import cn.citycraft.Yum.utils.StringUtil;
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandInfo extends BaseCommand {
Yum yum;
Yum main;
/**
* @param name
*/
public CommandInfo(Yum main) {
super("info");
this.yum = main;
this.main = main;
}
@Override
@ -40,7 +39,7 @@ public class CommandInfo extends BaseCommand {
return;
}
String pluginname = args[0];
Plugin plugin = yum.getServer().getPluginManager().getPlugin(pluginname);
Plugin plugin = main.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) {
PluginDescriptionFile desc = plugin.getDescription();
sender.sendMessage("§6插件名称: §3" + plugin.getName());
@ -51,7 +50,7 @@ public class CommandInfo extends BaseCommand {
StringUtil.sendStringArray(sender, desc.getDepend());
sender.sendMessage("§6插件软依赖: §3" + (desc.getSoftDepend().size() == 0 ? "" : ""));
StringUtil.sendStringArray(sender, desc.getSoftDepend());
sender.sendMessage("§6插件物理路径: §3" + PluginsManager.getPluginFile(plugin).getAbsolutePath());
sender.sendMessage("§6插件物理路径: §3" + main.plugman.getPluginFile(plugin).getAbsolutePath());
} else {
sender.sendMessage("§4错误: §c插件 " + pluginname + " 不存在或已卸载!");
}

View File

@ -9,6 +9,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.repository.PluginInfo;
/**
* 插件安装命令类
@ -16,14 +17,14 @@ import cn.citycraft.Yum.Yum;
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandInstall extends BaseCommand {
Yum yum;
Yum main;
/**
* @param name
*/
public CommandInstall(Yum main) {
super("install");
this.yum = main;
this.main = main;
}
@Override
@ -31,10 +32,14 @@ public class CommandInstall extends BaseCommand {
final String pluginname = args[0];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin == null) {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
@Override
public void run() {
PluginInfo pi = main.repo.getPlugin(pluginname);
if (pi != null)
if (main.download.run(sender, pi.getMavenUrl())) {
main.plugman.load(sender, pluginname);
}
}
});
} else {
@ -43,6 +48,24 @@ public class CommandInstall extends BaseCommand {
};
// public static boolean installFromYum(CommandSender sender, String
// filename) {
// if (sender == null) {
// sender = Bukkit.getConsoleSender();
// }
// File file = new File("plugins/YumCenter", filename + ".jar");
// if (!file.exists()) {
// sender.sendMessage("§4错误: §c仓库不存在 " + filename + " 插件!");
// return false;
// }
// File pluginfile = new File("plugins", filename + ".jar");
// FileUtil.copyFile(file, pluginfile);
// if (PluginsManager.load(sender, filename + ".jar")) {
// sender.sendMessage("§6安装: §a从Yum仓库安装插件 " + filename + " 成功!");
// }
// return false;
// }
@Override
public int getMinimumArguments() {
return 1;

View File

@ -9,7 +9,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.utils.PluginsManager;
/**
* 插件查看命令类
@ -17,14 +16,14 @@ import cn.citycraft.Yum.utils.PluginsManager;
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandList extends BaseCommand {
Yum plugin;
Yum main;
/**
* @param name
*/
public CommandList(Yum main) {
super("list");
this.plugin = main;
this.main = main;
}
@Override
@ -36,7 +35,7 @@ public class CommandList extends BaseCommand {
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: ");
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
sender.sendMessage("§6 - " + PluginsManager.getFormattedName(plugin, true));
sender.sendMessage("§6 - " + main.plugman.getFormattedName(plugin, true));
}
}

View File

@ -0,0 +1,55 @@
/**
*
*/
package cn.citycraft.Yum.commands;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import cn.citycraft.Yum.Yum;
/**
* 插件删除命令类
*
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandRepo extends BaseCommand {
Yum main;
/**
* @param name
*/
public CommandRepo(Yum main) {
super("delete");
this.main = main;
}
@Override
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
String cmd = args[0];
switch (cmd) {
case "add":
if (args.length == 2) {
main.repo.addRepositories(args[1]);
}
case "list":
case "clean":
main.repo.clean();
}
};
@Override
public int getMinimumArguments() {
return 1;
}
@Override
public String getPossibleArguments() {
return "<add|del|clean|list> <仓库名称>";
}
@Override
public boolean isOnlyPlayerExecutable() {
return false;
}
}

View File

@ -9,19 +9,20 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.repository.PluginInfo;
/**
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandUpdate extends BaseCommand {
Yum yum;
Yum main;
/**
* @param name
*/
public CommandUpdate(Yum main) {
super("update");
this.yum = main;
this.main = main;
}
@Override
@ -30,10 +31,16 @@ public class CommandUpdate extends BaseCommand {
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
sender.sendMessage("§a开始更新插件: " + pluginname);
if (plugin != null) {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
@Override
public void run() {
PluginInfo pi = main.repo.getPlugin(pluginname);
if (pi != null)
if (main.download.run(sender, pi.getMavenUrl())) {
if (main.plugman.unload(sender, plugin)) {
main.plugman.load(sender, pluginname);
}
}
}
});
} else {

View File

@ -1,7 +1,7 @@
/**
*
*/
package cn.citycraft.Yum.utils;
package cn.citycraft.Yum.manager;
import java.io.BufferedInputStream;
import java.io.File;
@ -14,7 +14,10 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
/**
* @author 蒋天蓓 2015年8月21日下午6:08:09 TODO
* 下载管理类
*
* @author 蒋天蓓
* 2015年8月21日下午6:08:09
*/
public class DownloadManager {
Plugin plugin;
@ -23,14 +26,27 @@ public class DownloadManager {
this.plugin = main;
}
/**
* 从地址获得文件名称
*
* @param url
* - 地址
* @return 文件名称
*/
public String getFileName(String url) {
int end = url.lastIndexOf('/');
return url.substring(end + 1);
}
/**
* 从地址获得文件名称
*
* @param url
* - 地址
* @return 文件名称
*/
public String getFileName(URL url) {
int end = url.getFile().lastIndexOf('/');
return url.getFile().substring(end + 1);
return getFileName(url.getFile());
}
private String getPer(int per) {
@ -47,6 +63,41 @@ public class DownloadManager {
return sb.toString();
}
/**
* 从网络下载文件
*
* @param urlstring
* - 下载地址
* @return 是否成功
*/
public boolean run(String urlstring) {
return run(null, urlstring);
}
/**
* 从网络下载文件
*
* @param sender
* - 命令发送者
* @param urlstring
* - 下载地址
* @return 是否成功
*/
public boolean run(CommandSender sender, String urlstring) {
return run(sender, urlstring, new File("plugins", getFileName(urlstring)));
}
/**
* 从网络下载文件
*
* @param sender
* - 命令发送者
* @param urlstring
* - 下载地址
* @param file
* - 保存文件
* @return 是否成功
*/
public boolean run(CommandSender sender, String urlstring, File file) {
URL url;
try {
@ -58,6 +109,17 @@ public class DownloadManager {
}
}
/**
* 从网络下载文件
*
* @param sender
* - 命令发送者
* @param url
* - 下载地址
* @param file
* - 保存文件
* @return 是否成功
*/
public boolean run(CommandSender sender, URL url, File file) {
BufferedInputStream in = null;
FileOutputStream fout = null;
@ -108,10 +170,28 @@ public class DownloadManager {
}
}
/**
* 从网络下载文件
*
* @param urlstring
* - 下载地址
* @param file
* - 保存文件
* @return 是否成功
*/
public boolean run(String urlstring, File file) {
return run(null, urlstring, file);
}
/**
* 从网络下载文件
*
* @param url
* - 下载地址
* @param file
* - 保存文件
* @return 是否成功
*/
public boolean run(URL url, File file) {
return run(null, url, file);
}

View File

@ -1,13 +1,10 @@
/**
*
*/
package cn.citycraft.Yum.utils;
package cn.citycraft.Yum.manager;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLClassLoader;
@ -28,10 +25,14 @@ import org.bukkit.event.Event;
import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.plugin.InvalidPluginException;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredListener;
import org.bukkit.plugin.UnknownDependencyException;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.utils.StringUtil;
import com.google.common.base.Joiner;
/**
@ -40,43 +41,52 @@ import com.google.common.base.Joiner;
* @author 蒋天蓓 2015年8月21日下午7:03:26
*/
public class PluginsManager {
Yum main;
public static boolean copyFile(File src, File des) {
InputStream inStream = null; // 读入原文件
FileOutputStream fs = null;
try {
int byteread = 0;
if (!src.exists())
return false;
inStream = new FileInputStream(src); // 读入原文件
fs = new FileOutputStream(des);
byte[] buffer = new byte[1024];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
inStream.close();
fs.close();
return true;
} catch (Exception e) {
return false;
}
public PluginsManager(Yum yum) {
this.main = yum;
}
public static boolean deletePlugin(CommandSender sender, Plugin plugin) {
/**
* 删除插件
*
* @param sender
* - 命令发送者
* @param plugin
* - 插件
* @return 是否成功
*/
public boolean deletePlugin(CommandSender sender, Plugin plugin) {
return unload(sender, plugin) && getPluginFile(plugin).delete();
}
public static boolean deletePlugin(Plugin plugin) {
/**
* 删除插件
*
* @param plugin
* - 插件
* @return 是否成功
*/
public boolean deletePlugin(Plugin plugin) {
return deletePlugin(Bukkit.getConsoleSender(), plugin);
}
public static void disable(Plugin plugin) {
/**
* 关闭插件
*
* @param plugin
* - 插件
*/
public void disable(Plugin plugin) {
if ((plugin.isEnabled()) && (plugin != null)) {
Bukkit.getPluginManager().disablePlugin(plugin);
}
}
public static void disableAll() {
/**
* 关闭所有插件
*/
public void disableAll() {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (!isIgnored(plugin)) {
disable(plugin);
@ -84,13 +94,22 @@ public class PluginsManager {
}
}
public static void enable(Plugin plugin) {
/**
* 启用插件
*
* @param plugin
* - 插件
*/
public void enable(Plugin plugin) {
if ((!plugin.isEnabled()) && (plugin != null)) {
Bukkit.getPluginManager().enablePlugin(plugin);
}
}
public static void enableAll() {
/**
* 启用所有插件
*/
public void enableAll() {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (!isIgnored(plugin)) {
enable(plugin);
@ -98,11 +117,27 @@ public class PluginsManager {
}
}
public static String getFormattedName(Plugin plugin) {
/**
* 获得格式化的插件名称
*
* @param plugin
* - 插件
* @return 格式化的插件名称
*/
public String getFormattedName(Plugin plugin) {
return getFormattedName(plugin, false);
}
public static String getFormattedName(Plugin plugin, boolean includeVersions) {
/**
* 获得格式化的插件名称(可带版本)
*
* @param plugin
* - 插件
* @param includeVersions
* - 是否包括版本
* @return 格式化的插件名称
*/
public String getFormattedName(Plugin plugin, boolean includeVersions) {
ChatColor color = plugin.isEnabled() ? ChatColor.GREEN : ChatColor.RED;
String pluginName = color + plugin.getName();
if (includeVersions) {
@ -111,15 +146,36 @@ public class PluginsManager {
return pluginName;
}
public static Plugin getPluginByName(String name) {
/**
* 通过名称获得插件
*
* @param name
* - 名称
* @return 插件
*/
public Plugin getPluginByName(String name) {
return Bukkit.getPluginManager().getPlugin(name);
}
public static Plugin getPluginByName(String[] args, int start) {
/**
* 通过名称获得插件(处理带空格的插件)
*
* @param name
* - 名称
* @return 插件
*/
public Plugin getPluginByName(String[] args, int start) {
return getPluginByName(StringUtil.consolidateStrings(args, start));
}
public static File getPluginFile(Plugin plugin) {
/**
* 获得插件绝对路径
*
* @param plugin
* - 插件
* @return 插件的绝对路径
*/
public File getPluginFile(Plugin plugin) {
File file = null;
ClassLoader cl = plugin.getClass().getClassLoader();
if ((cl instanceof URLClassLoader)) {
@ -131,7 +187,7 @@ public class PluginsManager {
return file;
}
public static List<String> getPluginNames(boolean fullName) {
public List<String> getPluginNames(boolean fullName) {
List<String> plugins = new ArrayList<String>();
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
plugins.add(fullName ? plugin.getDescription().getFullName() : plugin.getName());
@ -139,14 +195,28 @@ public class PluginsManager {
return plugins;
}
public static String getPluginVersion(String name) {
/**
* 获得插件版本
*
* @param name
* - 插件名称
* @return 插件版本
*/
public String getPluginVersion(String name) {
Plugin plugin = getPluginByName(name);
if ((plugin != null) && (plugin.getDescription() != null))
return plugin.getDescription().getVersion();
return null;
}
public static String getUsages(Plugin plugin) {
/**
* 获得插件命令
*
* @param plugin
* - 插件
* @return 插件命令
*/
public String getUsages(Plugin plugin) {
List<String> parsedCommands = new ArrayList<String>();
Map<String, Map<String, Object>> commands = plugin.getDescription().getCommands();
@ -165,28 +235,25 @@ public class PluginsManager {
return Joiner.on(", ").join(parsedCommands);
}
public static boolean installFromYum(CommandSender sender, String filename) {
if (sender == null) {
sender = Bukkit.getConsoleSender();
}
File file = new File("plugins/YumCenter", filename + ".jar");
if (!file.exists()) {
sender.sendMessage("§4错误: §c仓库不存在 " + filename + " 插件!");
return false;
}
File pluginfile = new File("plugins", filename + ".jar");
copyFile(file, pluginfile);
if (load(sender, filename + ".jar")) {
sender.sendMessage("§6安装: §a从Yum仓库安装插件 " + filename + " 成功!");
}
return false;
}
public static boolean isIgnored(Plugin plugin) {
/**
* 判断插件是否在忽略列表
*
* @param plugin
* - 插件
* @return 是否
*/
public boolean isIgnored(Plugin plugin) {
return isIgnored(plugin.getName());
}
public static boolean isIgnored(String plugin) {
/**
* 判断插件是否在忽略列表
*
* @param plugin
* - 插件名称
* @return 是否
*/
public boolean isIgnored(String plugin) {
for (String name : new ArrayList<String>()) {
if (name.equalsIgnoreCase(plugin))
return true;
@ -194,12 +261,16 @@ public class PluginsManager {
return false;
}
public static boolean load(CommandSender sender, Plugin plugin) {
String filename = getPluginFile(plugin).getName();
return load(sender, filename);
}
public static boolean load(CommandSender sender, String name) {
/**
* 载入插件
*
* @param sender
* - 命令发送者
* @param name
* - 插件名称
* @return 是否成功
*/
public boolean load(CommandSender sender, String name) {
Plugin target = null;
if (sender == null) {
@ -221,20 +292,35 @@ public class PluginsManager {
File pluginFile = new File(pluginDir, name);
if (!pluginFile.isFile() && !new File(updateDir, name).isFile()) {
sender.sendMessage("§c在插件目录和更新目录未找到 " + name + " 插件 请确认文件是否存在!");
pluginFile = null;
for (File file : pluginDir.listFiles()) {
if (file.getName().endsWith(".jar")) {
try {
PluginDescriptionFile desc = main.getPluginLoader().getPluginDescription(file);
if (desc.getName().equalsIgnoreCase(name)) {
pluginFile = file;
break;
}
} catch (InvalidDescriptionException e) {
}
}
}
if (pluginFile == null) {
sender.sendMessage("§c在插件目录和更新目录均未找到 " + name + " 插件 请确认文件是否存在!");
return false;
}
}
try {
target = Bukkit.getPluginManager().loadPlugin(pluginFile);
} catch (InvalidDescriptionException e) {
sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name + " 的plugin.yml文件存在错误!");
sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件: " + name + " 的plugin.yml文件存在错误!");
return false;
} catch (InvalidPluginException e) {
sender.sendMessage("§c异常: " + e.getMessage() + " 文件: " + name + " 不是一个可载入的插件!");
sender.sendMessage("§4异常: §c" + e.getMessage() + " 文件: " + name + " 不是一个可载入的插件!");
return false;
} catch (UnknownDependencyException e) {
sender.sendMessage("§c异常: " + e.getMessage() + " 插件: " + name + " 缺少部分依赖项目!");
sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件: " + name + " 缺少部分依赖项目!");
return false;
}
@ -244,25 +330,40 @@ public class PluginsManager {
return true;
}
public static boolean load(Plugin plugin) {
return load(Bukkit.getConsoleSender(), plugin);
}
public static boolean load(String name) {
public boolean load(String name) {
return load(Bukkit.getConsoleSender(), name);
}
public static boolean reload(CommandSender sender, Plugin plugin) {
/**
* 删除重载插件
*
* @param sender
* - 命令发送者
* @param plugin
* - 插件
* @return 是否成功
*/
public boolean reload(CommandSender sender, Plugin plugin) {
if (plugin != null)
return unload(sender, plugin) && load(sender, plugin);
return unload(sender, plugin) && load(sender, plugin.getName());
return false;
}
public static boolean reload(Plugin plugin) {
/**
* 重载插件
*
* @param plugin
* - 插件
* @return 是否成功
*/
public boolean reload(Plugin plugin) {
return reload(Bukkit.getConsoleSender(), plugin);
}
public static void reloadAll() {
/**
* 重载所有插件
*/
public void reloadAll() {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (!isIgnored(plugin)) {
reload(plugin);
@ -270,8 +371,17 @@ public class PluginsManager {
}
}
/**
* 卸载插件
*
* @param sender
* - 命令发送者
* @param plugin
* - 插件
* @return 是否成功
*/
@SuppressWarnings("unchecked")
public static boolean unload(CommandSender sender, Plugin plugin) {
public boolean unload(CommandSender sender, Plugin plugin) {
String name = plugin.getName();
if (sender == null) {
sender = Bukkit.getConsoleSender();
@ -309,7 +419,7 @@ public class PluginsManager {
knownCommandsField.setAccessible(true);
knownCommands = (Map<String, Command>) knownCommandsField.get(commandMap);
} catch (Exception e) {
sender.sendMessage("§c异常: " + e.getMessage() + " 插件 " + name + " 卸载失败!");
sender.sendMessage("§4异常: §c" + e.getMessage() + " 插件 " + name + " 卸载失败!");
return false;
}
}
@ -333,6 +443,7 @@ public class PluginsManager {
if (command.getPlugin() == next) {
command.unregister(commandMap);
it.remove();
sender.sendMessage("§6卸载: §a插件: " + name + " 的命令!");
}
}
}
@ -345,6 +456,7 @@ public class PluginsManager {
RegisteredListener value = it.next();
if (value.getPlugin().getName().equals(name)) {
it.remove();
sender.sendMessage("§6卸载: §a插件: " + name + " 的监听器!");
}
}
}
@ -353,6 +465,7 @@ public class PluginsManager {
if ((cl instanceof URLClassLoader)) {
try {
((URLClassLoader) cl).close();
sender.sendMessage("§6卸载: §a插件: " + name + " 的类加载器!");
} catch (IOException ex) {
}
}
@ -360,7 +473,14 @@ public class PluginsManager {
return true;
}
public static boolean unload(Plugin plugin) {
/**
* 卸载插件
*
* @param plugin
* - 卸载插件
* @return 是否成功
*/
public boolean unload(Plugin plugin) {
return unload(Bukkit.getConsoleSender(), plugin);
}
}

View File

@ -0,0 +1,19 @@
/**
*
*/
package cn.citycraft.Yum.repository;
import java.util.ArrayList;
import java.util.List;
/**
* TODO
*
* @author 蒋天蓓
* 2015年8月31日下午7:42:22
*/
public class Package {
public String name;
public String url;
public List<Plugin> plugins = new ArrayList<>();
}

View File

@ -0,0 +1,16 @@
/**
*
*/
package cn.citycraft.Yum.repository;
/**
* TODO
*
* @author 蒋天蓓
* 2015年8月31日下午7:42:54
*/
public class Plugin {
public String groupId;
public String artifactId;
public String version;
}

View File

@ -0,0 +1,20 @@
package cn.citycraft.Yum.repository;
/**
* TODO
*
* @author 蒋天蓓
* 2015年8月31日下午7:43:19
*/
public class PluginInfo {
public Plugin plugin;
public String url;
public String getMavenUrl() {
return String.format(url + "/%1$s/%2$s/%3$s/%2$s-%3$s.jar", plugin.groupId.replace(".", "/"), plugin.artifactId, plugin.version);
}
public String getFileName() {
return String.format("%1$s-%2$s.jar", plugin.artifactId, plugin.version);
}
}

View File

@ -0,0 +1,16 @@
/**
*
*/
package cn.citycraft.Yum.repository;
/**
* TODO
*
* @author 蒋天蓓
* 2015年8月31日下午7:41:53
*/
public class Repository {
public String id;
public String url;
public String type;
}

View File

@ -0,0 +1,147 @@
/**
*
*/
package cn.citycraft.Yum.repository;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import cn.citycraft.Yum.Yum;
import com.google.common.base.Charsets;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
/**
* 仓库管理类
*
* @author 蒋天蓓
*/
public class RepositoryManager {
Gson gson = new Gson();
HashMap<String, PluginInfo> plugins = new HashMap<String, PluginInfo>();
Yum main;
public RepositoryManager(Yum yum) {
this.main = yum;
}
public void clean() {
plugins.clear();
}
public void update(Package pkg) {
for (Plugin plugin : pkg.plugins) {
PluginInfo pi = new PluginInfo();
pi.plugin = plugin;
pi.url = pkg.url;
plugins.put(plugin.groupId + plugin.artifactId, pi);
}
}
public String cacheToJson() {
return gson.toJson(plugins);
}
public boolean jsonToCache(String json) {
if (json == "") {
return false;
}
try {
plugins = gson.fromJson(json, new TypeToken<HashMap<String, PluginInfo>>() {
}.getType());
return true;
} catch (JsonSyntaxException e) {
return false;
}
}
public boolean addRepositories(String urlstring) {
String json = getHtml(urlstring);
if (json == "") {
return false;
}
List<Repository> lrepo = jsonToRepositories(json);
if (lrepo == null) {
return false;
}
for (Repository repository : lrepo) {
addPackage(repository.url);
}
return true;
}
public boolean addPackage(String urlstring) {
String json = getHtml(urlstring);
if (json == "") {
return false;
}
Package pkg = jsonToPackage(json);
if (pkg == null) {
return false;
}
update(pkg);
return true;
}
public String getHtml(String urlstring) {
String html = "";
try {
URL url = new URL(urlstring);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8));
String line;
while ((line = br.readLine()) != null) {
html += line;
}
return html;
} catch (IOException e) {
return null;
}
}
public PluginInfo getPluginInfo(String groupId, String artifactId) {
return plugins.get(groupId + artifactId);
}
public PluginInfo getPlugin(String name) {
for (Entry<String, PluginInfo> plugin : plugins.entrySet()) {
if (plugin.getValue().plugin.artifactId.equalsIgnoreCase(name))
return plugin.getValue();
}
return null;
}
public List<PluginInfo> getPluginInfo(String name) {
List<PluginInfo> li = new ArrayList<PluginInfo>();
for (Entry<String, PluginInfo> plugin : plugins.entrySet()) {
if (plugin.getValue().plugin.artifactId.equalsIgnoreCase(name))
li.add(plugin.getValue());
}
return li;
}
public List<Repository> jsonToRepositories(String json) {
try {
return gson.fromJson(json, new TypeToken<List<Repository>>() {
}.getType());
} catch (JsonSyntaxException e) {
return null;
}
}
public Package jsonToPackage(String json) {
try {
return gson.fromJson(json, Package.class);
} catch (JsonSyntaxException e) {
return null;
}
}
}

View File

@ -0,0 +1,36 @@
package cn.citycraft.Yum.sql;
// import java.sql.*;
/**
* 数据库操作类
*
* @author 蒋天蓓 2015年7月14日下午3:25:06
*/
public class MySQLHelper extends SQLHelper {
private static String drivername = "com.mysql.jdbc.Driver";
/**
* 初始化连接信息
*
* @param host
* - 域名
* @param port
* - 端口
* @param dbname
* - 数据库
* @param username
* - 用户名
* @param password
* - 密码
*/
public MySQLHelper(String host, String port, String dbname, String username, String password) {
super(password, password, drivername, getUrl(host, port, dbname));
}
public static String getUrl(String host, String port, String dbaName) {
String Encode = "?&useUnicode=true&characterEncoding=utf-8";
return "jdbc:mysql://" + host + ":" + port + "/" + dbaName + Encode;
}
}

View File

@ -0,0 +1,428 @@
package cn.citycraft.Yum.sql;
/*
* 数据库连接选择更新删除演示
*/
// import java.sql.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author 蒋天蓓 2015年7月14日下午3:25:06 数据库操作类
*/
public abstract class SQLHelper {
// ///////////////////////////////////////>>>数据成员 and 构造函数
protected Connection dbconn = null;
protected Statement dbstate = null;
protected ResultSet dbresult = null;
protected String username;
protected String password;
protected String url;
protected String drivername;
/**
* 初始化连接信息
*
* @param username
* - 用户名
* @param password
* - 密码
* @param drivername
* - 驱动名称
* @param url
* - 数据库地址
*/
public SQLHelper(String username, String password, String drivername, String url) {
try {
Class.forName(drivername).newInstance();
} catch (Exception e) {
}
this.username = username;
this.password = password;
this.drivername = drivername;
this.url = url;
}
/**
* 创建数据表
*
* @param tableName
* - 表名
* @param fields
* - 字段参数
* @param Conditions
* -附加值
* @return 运行结果
*/
public boolean createTables(String tableName, HashMap<String, String> fields, String Conditions) {
if (!dbConnection())
return false;
String kv = "";
for (Entry<String, String> kvs : fields.entrySet()) {
kv += "`" + kvs.getKey() + "` " + kvs.getValue() + " NOT NULL , ";
}
kv = kv.substring(0, kv.length() - 2);// 根据String的索引提取子串
String sql = "CREATE TABLE `" + tableName + "` ( " + kv + (Conditions == "" ? "" : " , " + Conditions)
+ " ) ENGINE = InnoDB DEFAULT CHARSET=UTF8";
try {
PreparedStatement state = dbconn.prepareStatement(sql);
state.executeUpdate();
state.close();
return true;
} catch (final Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
}
return false;
}
/**
* 断开数据库
*
* @return bool值成功返回true失败返回false
*/
public boolean dbClose() {
try {
dbconn.close();
return true;
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
return false;
}
}// end dbClose()
/**
* 连接到数据库
*
* @return 是否成功
*/
public boolean dbConnection() {
try {
dbconn = DriverManager.getConnection(url, username, password);
dbstate = dbconn.createStatement();
return true;
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());// 得到出错信息
print("登录URL: " + url); // 发生错误时将连接数据库信息打印出来
print("登录账户: " + username);
print("登录密码: " + password);
return false;
}
}
/**
* 对数据库表中的记录进行删除操作
*
* @param tableName
* @param condition
* @return bool值表示删除成功或者失败
*/
public boolean dbDelete(String tableName, HashMap<String, String> selConditions) {// >>>删除操作
if (!dbConnection())
return false;
String selCondition = "";
if (selConditions != null && !selConditions.isEmpty()) {
for (Entry<String, String> kvs : selConditions.entrySet()) {
selCondition += kvs.getKey() + "='" + kvs.getValue() + "', ";
}
selCondition = " WHERE " + selCondition.substring(0, selCondition.length() - 2);// 根据String的索引提取子串
}
String sql = "DELETE FROM `" + tableName + "` " + selCondition;
try {
dbstate.executeUpdate(sql);
return true;
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
return false;
}
}// end dbDelete()
/**
* 判断数据库某个值是否存在!
*
* @param tableName
* 数据库表名
* @param fieles
* 字段名
* @param selCondition
* 选择条件
* @return 首个符合条件的结果
*/
public boolean dbExist(String tableName, HashMap<String, String> selConditions) {
if (!dbConnection())
return false;
String selCondition = "";
if (selConditions != null && !selConditions.isEmpty()) {
for (Entry<String, String> kvs : selConditions.entrySet()) {
selCondition += kvs.getKey() + "='" + kvs.getValue() + "', ";
}
selCondition = " WHERE " + selCondition.substring(0, selCondition.length() - 2);// 根据String的索引提取子串
}
String sql = "SELECT * FROM " + tableName + selCondition;
try {
dbresult = dbstate.executeQuery(sql);
return dbresult.next();
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
}
return false;
}
/**
* 对数据库表进行插入操作
*
* @param tabName
* - 表名
* @param values
* - 带键值的HashMap
* @return bool值成功返回true失败返回false
*/
public boolean dbInsert(String tabName, HashMap<String, String> values) {
if (!dbConnection())
return false;
String sql = "";
String insertFields = "";
String insertValues = "";
for (Entry<String, String> kvs : values.entrySet()) {
insertFields += "`" + kvs.getKey() + "`, ";
insertValues += "'" + kvs.getValue() + "', ";
}
insertFields = insertFields.substring(0, insertFields.length() - 2);
insertValues = insertValues.substring(0, insertValues.length() - 2);
sql += "INSERT INTO `" + tabName + "` (" + insertFields + ") VALUES" + "(" + insertValues + ")";
try {
dbstate.executeUpdate(sql);
return true;
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
return false;
}
}// end dbInsert()
/**
* 对数据库表进行选择操作
*
* @param tableName
* 数据库表名
* @param fieles
* 字段名
* @param selCondition
* 选择条件
* @return 一个含有map的List列表
*/
@SuppressWarnings({
"rawtypes",
"unchecked"
})
public List dbSelect(String tableName, List<String> fields, String selCondition) {
if (!dbConnection())
return null;
List mapInList = new ArrayList();
String selFields = "";
for (int i = 0; i < fields.size(); ++i) {
selFields += fields.get(i) + ", ";
}
String selFieldsTem = selFields.substring(0, selFields.length() - 2);// 根据String的索引提取子串
String sql = "SELECT " + selFieldsTem + " FROM `" + tableName + "`" + selCondition == "" ? "" : " WHERE " + selCondition;
try {
dbstate = dbconn.createStatement();
try {
dbresult = dbstate.executeQuery(sql);
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
}
while (dbresult.next()) {
Map selResult = new HashMap();
for (String col : fields) {
selResult.put(col, dbresult.getString(col));
}
mapInList.add(selResult);
}
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
}
return mapInList;
}// end String dbSelect()
/**
* 对数据库表进行选择操作
*
* @param tableName
* 数据库表名
* @param fieles
* 字段名
* @param selCondition
* 选择条件
* @return 首个符合条件的结果
*/
public String dbSelectFirst(String tableName, String fields, HashMap<String, String> selConditions) {
if (!dbConnection())
return null;
String selFieldsTem = fields;
String selCondition = "";
if (selConditions != null && !selConditions.isEmpty()) {
for (Entry<String, String> kvs : selConditions.entrySet()) {
selCondition += kvs.getKey() + "='" + kvs.getValue() + "', ";
}
selCondition = " WHERE " + selCondition.substring(0, selCondition.length() - 2);// 根据String的索引提取子串
}
String sql = "SELECT " + selFieldsTem + " FROM " + tableName + selCondition + " limit 1";
try {
dbresult = dbstate.executeQuery(sql);
if (dbresult.next())
return dbresult.getString(fields);
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
}
return null;
}
/**
* 对数据库表中记录进行更新操作
*
* @param tabName
* @param reCount
* @return bool值成功返回true失败返回false
*/
@SuppressWarnings({
"rawtypes"
})
public boolean dbUpdate(String tabName, HashMap reCount, String upCondition) {
if (!dbConnection())
return false;
String Values = "";
Iterator keyValues = reCount.entrySet().iterator();
for (int i = 0; i < reCount.size(); ++i) {
Map.Entry entry = (Map.Entry) keyValues.next();
Object key = entry.getKey();
Object value = entry.getValue();
Values += key + "='" + value + "'" + ", ";
}
String updateValues = Values.substring(0, Values.length() - 2);
String sql = "UPDATE `" + tabName + "` SET " + updateValues + " " + upCondition;
try {
dbstate.executeUpdate(sql);
return true;
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL查询语句: " + sql);
return false;
}
}// end dbUpdate()
/**
* 判断数据表是否存在
*
* @param table
* - 表名
* @return 是否存在
*/
public boolean isTableExists(final String table) {
try {
if (!dbConnection())
return false;
final DatabaseMetaData dbm = dbconn.getMetaData();
final ResultSet tables = dbm.getTables(null, null, table, null);
return tables.next();
} catch (final SQLException e) {
e.printStackTrace();
}
return false;
}
public void print(String str)// 简化输出
{
System.out.println(str);
}
/**
* 运行SQL语句
*
* @param sql
* - SQL语句
* @return 运行结果
*/
public boolean runSql(String sql) {
if (!dbConnection())
return false;
try {
PreparedStatement state = dbconn.prepareStatement(sql);
state.executeUpdate();
state.close();
return true;
} catch (final Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL语句: " + sql);
}
return false;
}
/**
* 运行SQL文件
*
* @param sql
* - SQL文件
* @return 运行结果
*/
public boolean runSqlfile(File file) {
BufferedReader br = null;
Statement state = null;
String sql = null;
if (!file.exists())
return false;
if (!dbConnection())
return false;
try {
print("执行SQL文件: " + file.getName() + " ...");
br = new BufferedReader(new FileReader(file));
state = dbconn.createStatement();
while ((sql = br.readLine()) != null) {
if (sql != "") {
try {
state.executeUpdate(sql);
} catch (Exception e) {
print("数据库操作出错: " + e.getMessage());
print("SQL语句: " + sql);
}
}
}
return true;
} catch (Exception e) {
print("执行SQL文件 " + file.getName() + "出错: " + e.getMessage());
return false;
} finally {
try {
state.close();
br.close();
} catch (Exception e) {
}
}
}
}

View File

@ -0,0 +1,39 @@
package cn.citycraft.Yum.sql;
// import java.sql.*;
/**
* 数据库操作类
*
* @author 蒋天蓓 2015年7月14日下午3:25:06
*/
public class SQLiteHelper extends SQLHelper {
private static String drivername = "org.sqlite.JDBC";
/**
* 初始化连接信息
*
* @param host
* - 域名
* @param port
* - 端口
* @param dbname
* - 数据库
* @param username
* - 用户名
* @param password
* - 密码
*/
public SQLiteHelper(String filepath, String username, String password) {
super(password, password, drivername, getUrl(filepath));
}
public static String getUrl(String filepath) {
return "jdbc:sqlite:" + filepath;
}
//
// Class.forName("org.sqlite.JDBC");
// Connection conn =
// DriverManager.getConnection("jdbc:sqlite:filename");//filename为你的SQLite数据名称
}

View File

@ -0,0 +1,47 @@
/**
*
*/
package cn.citycraft.Yum.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* 文件处理工具
*
* @author 蒋天蓓
* 2015年8月31日上午9:09:54
*/
public class FileUtil {
/**
* 复制文件
*
* @param src
* - 源文件
* @param des
* - 目标文件
* @return 是否成功
*/
public static boolean copyFile(File src, File des) {
InputStream inStream = null;
FileOutputStream fs = null;
try {
int byteread = 0;
if (!src.exists())
return false;
inStream = new FileInputStream(src); // 读入原文件
fs = new FileOutputStream(des);
byte[] buffer = new byte[1024];
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread); // 写入到目标文件
}
inStream.close();
fs.close();
return true;
} catch (Exception e) {
return false;
}
}
}

View File

@ -1,13 +0,0 @@
/**
*
*/
package cn.citycraft.Yum.utils;
/**
* 仓库管理类
*
* @author 蒋天蓓
*/
public class RepositoryManager {
}

View File

@ -8,9 +8,10 @@ import java.util.Collection;
import org.bukkit.command.CommandSender;
/**
* 字符串工具
*
* @author 蒋天蓓
* 2015年8月22日下午12:41:59
* TODO
*/
public class StringUtil {
/**
@ -31,9 +32,17 @@ public class StringUtil {
return ret;
}
public static void sendStringArray(CommandSender player, Collection<String> msg) {
/**
* 给玩家或控制台发送消息组
*
* @param sender
* - 接收消息的玩家
* @param msg
* - 消息组
*/
public static void sendStringArray(CommandSender sender, Collection<String> msg) {
for (String string : msg) {
player.sendMessage("§6 - §3" + string);
sender.sendMessage("§6 - §3" + string);
}
}
}

View File

@ -1,4 +1,5 @@
name: ${project.artifactId}
description: ${project.description}
main: ${project.groupId}.${project.artifactId}.${project.artifactId}
version: ${project.version}
auther: 喵♂呜