modify plugin frame...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092 2015-08-27 18:36:44 +08:00
parent d1b0792fcf
commit cc092d6e0c
7 changed files with 31 additions and 207 deletions

View File

@ -66,7 +66,6 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
registerCommand(new CommandUpdate(yum)); registerCommand(new CommandUpdate(yum));
registerCommand(new CommandDelete(yum)); registerCommand(new CommandDelete(yum));
registerCommand(new CommandInfo(yum)); registerCommand(new CommandInfo(yum));
registerCommand(new CommandReinstall(yum));
RegisterCommandList = getRegisterCommands(); RegisterCommandList = getRegisterCommands();
} }

View File

@ -9,7 +9,6 @@ import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import cn.citycraft.Yum.Yum; import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.utils.PluginsManager;
/** /**
* 插件安装命令类 * 插件安装命令类
@ -35,9 +34,7 @@ public class CommandInstall extends BaseCommand {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
@Override @Override
public void run() { public void run() {
if (yum.download.install(sender, pluginname)) {
PluginsManager.load(sender, pluginname);
}
} }
}); });
} else { } else {

View File

@ -1,64 +0,0 @@
/**
*
*/
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;
/**
* 插件安装命令类
*
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandReinstall extends BaseCommand {
Yum yum;
/**
* @param name
*/
public CommandReinstall(Yum main) {
super("reinstall");
this.yum = main;
}
@Override
public void execute(final CommandSender sender, String label, String[] args) throws CommandException {
final String pluginname = args[0];
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
sender.sendMessage("§a开始重装插件: " + pluginname);
if (plugin != null) {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
@Override
public void run() {
if (yum.download.yum(sender, plugin.getName())) {
PluginsManager.deletePlugin(sender, plugin);
PluginsManager.installFromYum(sender, plugin.getName());
}
}
});
} else {
sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!");
}
};
@Override
public int getMinimumArguments() {
return 1;
}
@Override
public String getPossibleArguments() {
return "<插件名称>";
}
@Override
public boolean isOnlyPlayerExecutable() {
return false;
}
}

View File

@ -33,9 +33,7 @@ public class CommandUpdate extends BaseCommand {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
@Override @Override
public void run() { public void run() {
if (yum.download.update(sender, plugin)) {
sender.sendMessage("§6更新: §a插件 " + pluginname + " 已下载到update文件夹 重启后生效!");
}
} }
}); });
} else { } else {

View File

@ -1,69 +0,0 @@
/**
*
*/
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;
/**
* 插件安装命令类
*
* @author 蒋天蓓 2015年8月12日下午2:04:05
*/
public class CommandUrlInstall extends BaseCommand {
Yum yum;
/**
* @param name
*/
public CommandUrlInstall(Yum main) {
super("urlinstall");
this.yum = main;
}
@Override
public void execute(final CommandSender sender, String label, final String[] args) throws CommandException {
final String url = args[0];
final String pluginname = args[1];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin == null) {
Bukkit.getScheduler().runTaskAsynchronously(yum, new Runnable() {
@Override
public void run() {
switch (args.length) {
case 1:
yum.download.yumdl(sender, url);
break;
case 2:
yum.download.yumdl(sender, url, pluginname);
break;
}
PluginsManager.installFromYum(sender, pluginname);
}
});
} else {
sender.sendMessage("§c插件已安装在服务器 需要更新请使用yum update " + pluginname + "!");
}
};
@Override
public int getMinimumArguments() {
return 1;
}
@Override
public String getPossibleArguments() {
return "<插件名称>";
}
@Override
public boolean isOnlyPlayerExecutable() {
return false;
}
}

View File

@ -29,8 +29,7 @@ import com.google.common.base.Charsets;
import com.google.common.io.Files; import com.google.common.io.Files;
/** /**
* An implementation of {@link Configuration} which saves all files in Yaml. * An implementation of {@link Configuration} which saves all files in Yaml. Note that this
* Note that this
* implementation is not synchronized. * implementation is not synchronized.
*/ */
public class FileConfig extends YamlConfiguration { public class FileConfig extends YamlConfiguration {
@ -80,14 +79,15 @@ public class FileConfig extends YamlConfiguration {
loger.info("配置文件 " + filename + " 创建失败..."); loger.info("配置文件 " + filename + " 创建失败...");
} else { } else {
plugin.saveResource(filename, true); plugin.saveResource(filename, true);
loger.info("配置文件 " + filename + " 不存在 从插件释放...");
} }
} else { } else {
FileConfig newcfg = new FileConfig(stream); FileConfig newcfg = new FileConfig(stream);
FileConfig oldcfg = new FileConfig(file); FileConfig oldcfg = new FileConfig(file);
String newver = newcfg.getString("version"); String newver = newcfg.getString("version");
String oldver = oldcfg.getString("version"); String oldver = oldcfg.getString("version");
if (newver != null && oldver != null && newver != oldver) { if (newver != null && newver != oldver) {
loger.warning("配置文件: " + filename + " 版本过低 正在升级..."); loger.warning("配置文件: " + filename + " 版本 " + oldver + " 过低 正在升级" + newver + " ...");
try { try {
oldcfg.save(new File(file.getParent(), filename + ".backup")); oldcfg.save(new File(file.getParent(), filename + ".backup"));
loger.warning("配置文件: " + filename + " 已备份为 " + filename + ".backup !"); loger.warning("配置文件: " + filename + " 已备份为 " + filename + ".backup !");
@ -105,24 +105,19 @@ public class FileConfig extends YamlConfiguration {
private void init(File file) { private void init(File file) {
Validate.notNull(file, "File cannot be null"); Validate.notNull(file, "File cannot be null");
FileInputStream stream;
try { try {
this.load(file); stream = new FileInputStream(file);
} catch (FileNotFoundException ex) { init(stream);
} catch (FileNotFoundException e) {
loger.info("配置文件 " + file.getName() + " 不存在..."); loger.info("配置文件 " + file.getName() + " 不存在...");
} catch (IOException ex) {
loger.info("配置文件 " + file.getName() + " 读取错误...");
} catch (InvalidConfigurationException ex) {
loger.info("配置文件 " + file.getName() + " 格式错误...");
} }
} }
@SuppressWarnings("deprecation")
private void init(InputStream stream) { private void init(InputStream stream) {
Validate.notNull(file, "File cannot be null"); Validate.notNull(stream, "Stream cannot be null");
try { try {
this.load(stream); this.load(new InputStreamReader(stream, Charsets.UTF_8));
} catch (FileNotFoundException ex) {
loger.info("配置文件 " + file.getName() + " 不存在...");
} catch (IOException ex) { } catch (IOException ex) {
loger.info("配置文件 " + file.getName() + " 读取错误..."); loger.info("配置文件 " + file.getName() + " 读取错误...");
} catch (InvalidConfigurationException ex) { } catch (InvalidConfigurationException ex) {
@ -153,6 +148,10 @@ public class FileConfig extends YamlConfiguration {
loadFromString(builder.toString()); loadFromString(builder.toString());
} }
public void reload() {
init(file);
}
public void save() { public void save() {
if (file == null) { if (file == null) {
loger.info("未定义配置文件路径 保存失败!"); loger.info("未定义配置文件路径 保存失败!");

View File

@ -17,23 +17,22 @@ import org.bukkit.plugin.Plugin;
* @author 蒋天蓓 2015年8月21日下午6:08:09 TODO * @author 蒋天蓓 2015年8月21日下午6:08:09 TODO
*/ */
public class DownloadManager { public class DownloadManager {
String yumurl = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
Plugin plugin; Plugin plugin;
public DownloadManager(Plugin main) { public DownloadManager(Plugin main) {
this.plugin = main; this.plugin = main;
} }
public String getFileName(URL url) {
int end = url.getFile().lastIndexOf('/');
return url.getFile().substring(end + 1);
}
public String getFileName(String url) { public String getFileName(String url) {
int end = url.lastIndexOf('/'); int end = url.lastIndexOf('/');
return url.substring(end + 1); return url.substring(end + 1);
} }
public String getFileName(URL url) {
int end = url.getFile().lastIndexOf('/');
return url.getFile().substring(end + 1);
}
private String getPer(int per) { private String getPer(int per) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < 11; i++) { for (int i = 0; i < 11; i++) {
@ -48,18 +47,17 @@ public class DownloadManager {
return sb.toString(); return sb.toString();
} }
public URL getUrl(String pluginname) { public boolean run(CommandSender sender, String urlstring, File file) {
URL url;
try { try {
return new URL(String.format(yumurl, pluginname)); url = new URL(urlstring);
return run(sender, url, file);
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
return null; sender.sendMessage("§4错误: §c无法识别的URL地址...");
return false;
} }
} }
public boolean install(CommandSender sender, String pluginname) {
return run(sender, getUrl(pluginname), new File("plugins", pluginname + ".jar"));
}
public boolean run(CommandSender sender, URL url, File file) { public boolean run(CommandSender sender, URL url, File file) {
BufferedInputStream in = null; BufferedInputStream in = null;
FileOutputStream fout = null; FileOutputStream fout = null;
@ -110,46 +108,12 @@ public class DownloadManager {
} }
} }
public boolean run(String urlstring, File file) {
return run(null, urlstring, file);
}
public boolean run(URL url, File file) { public boolean run(URL url, File file) {
return run(null, url, file); return run(null, url, file);
} }
public boolean update(CommandSender sender, Plugin plugin) {
String pluginname = plugin.getName();
String filename = PluginsManager.getPluginFile(plugin).getName();
URL url = getUrl(pluginname);
if (url == null) {
sender.sendMessage("§4错误: §cURL地址解析失败!");
return false;
}
return run(sender, url, new File("plugins/update", filename));
}
public boolean yumdl(CommandSender sender, String address, String pluginname) {
try {
URL url = new URL(address);
File yumplugin = new File("plugins/YumCenter", pluginname + ".jar");
if (yumplugin.exists()) {
sender.sendMessage("§6更新: §e仓库已存在插件 " + pluginname + " 开始更新...");
yumplugin.delete();
}
return run(sender, url, yumplugin);
} catch (MalformedURLException e) {
return false;
}
}
public boolean yumdl(CommandSender sender, String address) {
String pluginname = getFileName(address);
return yumdl(sender, pluginname, address);
}
public boolean yum(CommandSender sender, String pluginname) {
File yumplugin = new File("plugins/YumCenter", pluginname + ".jar");
if (yumplugin.exists()) {
sender.sendMessage("§6更新: §e仓库已存在插件 " + pluginname + " 开始更新...");
yumplugin.delete();
}
return run(sender, getUrl(pluginname), yumplugin);
}
} }