add Urlinstall Command and so on...

Signed-off-by: 502647092 <jtb1@163.com>
dev
502647092 2015-08-24 20:49:31 +08:00
parent 323b65f5c4
commit d1b0792fcf
13 changed files with 231 additions and 132 deletions

View File

@ -6,6 +6,7 @@ package cn.citycraft.Yum;
import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.Yum.commands.CommandHandler;
import cn.citycraft.Yum.config.FileConfig;
import cn.citycraft.Yum.utils.DownloadManager;
/**
@ -15,10 +16,12 @@ import cn.citycraft.Yum.utils.DownloadManager;
*/
public class Yum extends JavaPlugin {
public DownloadManager download;
public FileConfig config;
@Override
public void onEnable() {
download = new DownloadManager(this);
config = new FileConfig(this, "config.yml");
CommandHandler cmdhandler = new CommandHandler(this);
this.getCommand("yum").setExecutor(cmdhandler);
this.getCommand("yum").setTabCompleter(cmdhandler);

View File

@ -15,14 +15,14 @@ import cn.citycraft.Yum.utils.PluginsManager;
*
* @author 20158122:04:05
*/
public class CommandRemove extends BaseCommand {
public class CommandDelete extends BaseCommand {
Yum yum;
/**
* @param name
*/
public CommandRemove(Yum main) {
super("remove");
public CommandDelete(Yum main) {
super("delete");
this.yum = main;
}
@ -31,7 +31,7 @@ public class CommandRemove extends BaseCommand {
String pluginname = args[0];
Plugin plugin = yum.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) {
PluginsManager.unload(sender, plugin);
PluginsManager.deletePlugin(sender, plugin);
} else {
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
}

View File

@ -64,7 +64,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
registerCommand(new CommandList(yum));
registerCommand(new CommandInstall(yum));
registerCommand(new CommandUpdate(yum));
registerCommand(new CommandRemove(yum));
registerCommand(new CommandDelete(yum));
registerCommand(new CommandInfo(yum));
registerCommand(new CommandReinstall(yum));

View File

@ -51,11 +51,9 @@ 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" + (desc.getLoadBefore().size() == 0 ? "无" : ""));
StringUtil.sendStringArray(sender, desc.getLoadBefore());
sender.sendMessage("§6插件物理路径: §3" + PluginsManager.getPluginFile(plugin).getAbsolutePath());
} else {
sender.sendMessage(c插件 " + pluginname + " 不存在或已卸载!");
sender.sendMessage(4错误: §c插件 " + pluginname + " 不存在或已卸载!");
}
}

View File

@ -28,7 +28,7 @@ public class CommandInstall extends BaseCommand {
}
@Override
public void execute(final CommandSender sender, String label, String[] args) throws CommandException {
public void execute(final CommandSender sender, String label, final String[] args) throws CommandException {
final String pluginname = args[0];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin == null) {
@ -43,6 +43,7 @@ public class CommandInstall extends BaseCommand {
} else {
sender.sendMessage("§c插件已安装在服务器 需要更新请使用yum update " + pluginname + "!");
}
};
@Override

View File

@ -37,7 +37,7 @@ public class CommandReinstall extends BaseCommand {
@Override
public void run() {
if (yum.download.yum(sender, plugin.getName())) {
PluginsManager.deletePlugin(plugin);
PluginsManager.deletePlugin(sender, plugin);
PluginsManager.installFromYum(sender, plugin.getName());
}
}

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;
/**
* @author 20158122:04:05
@ -35,8 +34,7 @@ public class CommandUpdate extends BaseCommand {
@Override
public void run() {
if (yum.download.update(sender, plugin)) {
PluginsManager.unload(sender, plugin);
PluginsManager.load(sender, plugin);
sender.sendMessage("§6更新: §a插件 " + pluginname + " 已下载到update文件夹 重启后生效!");
}
}
});

View File

@ -0,0 +1,69 @@
/**
*
*/
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 20158122: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

@ -1,81 +0,0 @@
package cn.citycraft.Yum.config;
import java.io.File;
import java.io.IOException;
import org.bukkit.plugin.Plugin;
public class ConfigLoader extends FileConfig {
protected FileConfig config;
protected File file;
protected boolean tip = true;
protected Plugin plugin;
public ConfigLoader(Plugin p, File file) {
this.plugin = p;
config = loadConfig(p, file, null, true);
}
public ConfigLoader(Plugin p, String filename) {
this.plugin = p;
config = loadConfig(p, new File(filename), null, true);
}
public FileConfig loadConfig(Plugin p, File file, String ver, boolean res) {
tip = res;
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
p.getLogger().info("创建新的文件夹" + file.getParentFile().getAbsolutePath() + "...");
}
if (!file.exists()) {
fileCreate(p, file, res);
} else {
if (ver != null) {
FileConfig configcheck = init(file);
String version = configcheck.getString("version");
if (version == null || !version.equals(ver)) {
p.getLogger().warning("配置文件: " + file.getName() + " 版本过低 正在升级...");
try {
configcheck.save(new File(file.getParent(), file.getName() + ".backup"));
p.getLogger().warning("配置文件: " + file.getName() + " 已备份为 " + file.getName() + ".backup !");
} catch (IOException e) {
p.getLogger().warning("配置文件: " + file.getName() + "备份失败!");
}
p.saveResource(file.getName(), true);
p.getLogger().info("配置文件: " + file.getName() + "升级成功!");
}
}
}
if (tip)
p.getLogger().info("载入配置文件: " + file.getName() + (ver != null ? " 版本: " + ver : ""));
return init(file);
}
private void fileCreate(Plugin p, File file, boolean res) {
if (res) {
p.saveResource(file.getName(), false);
} else {
try {
p.getLogger().info("创建新的配置文件" + file.getAbsolutePath() + "...");
file.createNewFile();
} catch (IOException e) {
p.getLogger().info("配置文件" + file.getName() + "创建失败...");
e.printStackTrace();
}
}
}
public void saveError(File file) {
plugin.getLogger().info("配置文件" + file.getName() + "保存错误...");
}
public void save() {
try {
config.save(file);
} catch (IOException e) {
saveError(file);
e.printStackTrace();
}
}
}

View File

@ -6,11 +6,12 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang.Validate;
import org.bukkit.Bukkit;
@ -19,6 +20,7 @@ import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.YamlConstructor;
import org.bukkit.configuration.file.YamlRepresenter;
import org.bukkit.plugin.Plugin;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.representer.Representer;
@ -27,28 +29,14 @@ import com.google.common.base.Charsets;
import com.google.common.io.Files;
/**
* An implementation of {@link Configuration} which saves all files in Yaml. Note that this
* An implementation of {@link Configuration} which saves all files in Yaml.
* Note that this
* implementation is not synchronized.
*/
public class FileConfig extends YamlConfiguration {
public static FileConfig init(File file) {
return FileConfig.loadConfiguration(file);
}
public static FileConfig loadConfiguration(File file) {
Validate.notNull(file, "File cannot be null");
FileConfig config = new FileConfig();
try {
config.load(file);
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
} catch (InvalidConfigurationException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Cannot load " + file, ex);
}
return config;
}
protected File file;
protected Logger loger;
protected Plugin plugin;
protected final DumperOptions yamlOptions = new DumperOptions();
@ -56,6 +44,92 @@ public class FileConfig extends YamlConfiguration {
protected final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
private FileConfig(File file) {
Validate.notNull(file, "File cannot be null");
this.file = file;
loger = Bukkit.getLogger();
init(file);
}
private FileConfig(InputStream stream) {
loger = Bukkit.getLogger();
init(stream);
}
public FileConfig(Plugin plugin, File file) {
Validate.notNull(file, "File cannot be null");
Validate.notNull(plugin, "Plugin cannot be null");
this.plugin = plugin;
this.file = file;
loger = plugin.getLogger();
check(file);
init(file);
}
public FileConfig(Plugin plugin, String filename) {
this(plugin, new File(plugin.getDataFolder(), filename));
}
private void check(File file) {
String filename = file.getName();
InputStream stream = plugin.getResource(filename);
try {
if (!file.exists()) {
if (stream == null) {
file.createNewFile();
loger.info("配置文件 " + filename + " 创建失败...");
} else {
plugin.saveResource(filename, true);
}
} else {
FileConfig newcfg = new FileConfig(stream);
FileConfig oldcfg = new FileConfig(file);
String newver = newcfg.getString("version");
String oldver = oldcfg.getString("version");
if (newver != null && oldver != null && newver != oldver) {
loger.warning("配置文件: " + filename + " 版本过低 正在升级...");
try {
oldcfg.save(new File(file.getParent(), filename + ".backup"));
loger.warning("配置文件: " + filename + " 已备份为 " + filename + ".backup !");
} catch (IOException e) {
loger.warning("配置文件: " + filename + "备份失败!");
}
plugin.saveResource(filename, true);
loger.info("配置文件: " + filename + "升级成功!");
}
}
} catch (IOException e) {
loger.info("配置文件 " + filename + " 创建失败...");
}
}
private void init(File file) {
Validate.notNull(file, "File cannot be null");
try {
this.load(file);
} catch (FileNotFoundException ex) {
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) {
Validate.notNull(file, "File cannot be null");
try {
this.load(stream);
} catch (FileNotFoundException ex) {
loger.info("配置文件 " + file.getName() + " 不存在...");
} catch (IOException ex) {
loger.info("配置文件 " + file.getName() + " 读取错误...");
} catch (InvalidConfigurationException ex) {
loger.info("配置文件 " + file.getName() + " 格式错误...");
}
}
@Override
public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
Validate.notNull(file, "File cannot be null");
@ -65,8 +139,7 @@ public class FileConfig extends YamlConfiguration {
@Override
public void load(Reader reader) throws IOException, InvalidConfigurationException {
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader
: new BufferedReader(reader);
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader : new BufferedReader(reader);
StringBuilder builder = new StringBuilder();
try {
String line;
@ -80,6 +153,18 @@ public class FileConfig extends YamlConfiguration {
loadFromString(builder.toString());
}
public void save() {
if (file == null) {
loger.info("未定义配置文件路径 保存失败!");
}
try {
this.save(file);
} catch (IOException e) {
loger.info("配置文件 " + file.getName() + " 保存错误...");
e.printStackTrace();
}
}
@Override
public void save(File file) throws IOException {
Validate.notNull(file, "File cannot be null");

View File

@ -29,6 +29,11 @@ public class DownloadManager {
return url.getFile().substring(end + 1);
}
public String getFileName(String url) {
int end = url.lastIndexOf('/');
return url.substring(end + 1);
}
private String getPer(int per) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 11; i++) {
@ -120,6 +125,25 @@ public class DownloadManager {
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()) {

View File

@ -63,9 +63,11 @@ public class PluginsManager {
}
public static boolean deletePlugin(Plugin plugin) {
unload(plugin);
getPluginFile(plugin).delete();
return true;
return deletePlugin(Bukkit.getConsoleSender(), plugin);
}
public static boolean deletePlugin(CommandSender sender, Plugin plugin) {
return unload(sender, plugin) && getPluginFile(plugin).delete();
}
public static void disable(Plugin plugin) {
@ -110,11 +112,7 @@ public class PluginsManager {
}
public static Plugin getPluginByName(String name) {
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
if (name.equalsIgnoreCase(plugin.getName()))
return plugin;
}
return null;
return Bukkit.getPluginManager().getPlugin(name);
}
public static Plugin getPluginByName(String[] args, int start) {
@ -247,18 +245,22 @@ public class PluginsManager {
}
public static boolean load(Plugin plugin) {
return load(null, plugin);
return load(Bukkit.getConsoleSender(), plugin);
}
public static boolean load(String name) {
return load(null, name);
return load(Bukkit.getConsoleSender(), name);
}
public static void reload(Plugin plugin) {
public static boolean reload(Plugin plugin) {
return reload(Bukkit.getConsoleSender(), plugin);
}
public static boolean reload(CommandSender sender, Plugin plugin) {
if (plugin != null) {
unload(plugin);
load(plugin);
return unload(sender, plugin) && load(sender, plugin);
}
return false;
}
public static void reloadAll() {
@ -354,6 +356,6 @@ public class PluginsManager {
}
public static boolean unload(Plugin plugin) {
return unload(null, plugin);
return unload(Bukkit.getConsoleSender(), plugin);
}
}

View File

@ -1,8 +1,8 @@
name: ${project.artifactId}
main: ${project.groupId}.${project.artifactId}.${project.artifactId}
website: http://ci.citycraft.cn:8800/jenkins/job/${project.artifactId}/
version: ${project.version}
auther: 喵♂呜
website: http://ci.citycraft.cn:8800/jenkins/job/${project.artifactId}/
commands:
yum:
description: MC插件仓库