mirror of
https://e.coding.net/circlecloud/Yum.git
synced 2024-11-22 22:38:46 +00:00
Release Version...
Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
parent
5a4a55fd9f
commit
c1cf312381
2
pom.xml
2
pom.xml
@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>cn.CityCraft</groupId>
|
||||
<artifactId>Yum</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<version>1.1</version>
|
||||
<name>Yum</name>
|
||||
<build>
|
||||
<finalName>${project.name}</finalName>
|
||||
|
@ -3,20 +3,10 @@
|
||||
*/
|
||||
package cn.citycraft.Yum;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.util.StringUtil;
|
||||
|
||||
import cn.citycraft.Yum.utils.DownloadUtils;
|
||||
import cn.citycraft.Yum.utils.PluginUtil;
|
||||
import cn.citycraft.Yum.commands.CommandHandler;
|
||||
import cn.citycraft.Yum.utils.DownloadManager;
|
||||
|
||||
/**
|
||||
* MC插件仓库
|
||||
@ -24,80 +14,12 @@ import cn.citycraft.Yum.utils.PluginUtil;
|
||||
* @author 蒋天蓓 2015年8月21日下午5:14:39
|
||||
*/
|
||||
public class Yum extends JavaPlugin {
|
||||
DownloadUtils download;
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
switch (args.length) {
|
||||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
switch (args[0]) {
|
||||
case "list":
|
||||
sender.sendMessage("§3服务器已安装插件: ");
|
||||
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
|
||||
sender.sendMessage("§6 - " + PluginUtil.getFormattedName(plugin, true));
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
Plugin plugin = this.getServer().getPluginManager().getPlugin(args[1]);
|
||||
switch (args[0]) {
|
||||
case "install":
|
||||
if (plugin != null) {
|
||||
if (download.download(sender, args[1])) {
|
||||
sender.sendMessage(PluginUtil.load(args[1]));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§c插件已安装在服务器!");
|
||||
}
|
||||
break;
|
||||
case "remove":
|
||||
if (plugin != null) {
|
||||
sender.sendMessage(PluginUtil.unload(plugin));
|
||||
} else {
|
||||
sender.sendMessage("§c插件不存在或已卸载!");
|
||||
}
|
||||
break;
|
||||
case "update":
|
||||
if (plugin != null) {
|
||||
sender.sendMessage(PluginUtil.unload(plugin));
|
||||
if (download.download(sender, args[1])) {
|
||||
sender.sendMessage(PluginUtil.load(args[1]));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§c插件不存在或已卸载!");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
public DownloadManager download;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
download = new DownloadUtils(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
|
||||
final String[] COMMANDS = { "install", "remove", "list", "update" };
|
||||
if (sender.isOp() || sender.hasPermission("yum.admin") || sender.hasPermission("yum." + args[0])) {
|
||||
List<String> completions = new ArrayList<>();
|
||||
if (args.length == 1) {
|
||||
String partialCommand = args[0];
|
||||
List<String> commands = new ArrayList<>(Arrays.asList(COMMANDS));
|
||||
StringUtil.copyPartialMatches(partialCommand, commands, completions);
|
||||
}
|
||||
if (args.length == 2) {
|
||||
String partialPlugin = args[1];
|
||||
List<String> plugins = PluginUtil.getPluginNames(false);
|
||||
StringUtil.copyPartialMatches(partialPlugin, plugins, completions);
|
||||
}
|
||||
Collections.sort(completions);
|
||||
return completions;
|
||||
}
|
||||
return null;
|
||||
download = new DownloadManager(this);
|
||||
this.getCommand("yum").setExecutor(new CommandHandler(this));
|
||||
this.getCommand("yum").setTabCompleter(new CommandHandler(this));
|
||||
}
|
||||
}
|
||||
|
137
src/main/java/cn/citycraft/Yum/commands/BaseCommand.java
Normal file
137
src/main/java/cn/citycraft/Yum/commands/BaseCommand.java
Normal file
@ -0,0 +1,137 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package cn.citycraft.Yum.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.CommandException;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
/**
|
||||
* 基础命令类
|
||||
*
|
||||
* @author 蒋天蓓
|
||||
* 2015年8月12日下午12:49:34
|
||||
*/
|
||||
public abstract class BaseCommand {
|
||||
private String name;
|
||||
private String permission;
|
||||
private String[] aliases;
|
||||
|
||||
public BaseCommand(String name) {
|
||||
this(name, new String[0]);
|
||||
}
|
||||
|
||||
public BaseCommand(String name, String... aliases) {
|
||||
this.name = name;
|
||||
this.aliases = aliases;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取命令名称
|
||||
*
|
||||
* @return 命令名称
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置命令权限
|
||||
*
|
||||
* @param permission
|
||||
* - 命令权限
|
||||
*/
|
||||
public void setPermission(String permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得命令权限
|
||||
*
|
||||
* @return 目录命令权限
|
||||
*/
|
||||
public String getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查Sender权限
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @return 是否有权限执行命令
|
||||
*/
|
||||
public final boolean hasPermission(CommandSender sender) {
|
||||
if (permission == null)
|
||||
return true;
|
||||
return sender.hasPermission(permission);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得可能的参数
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public abstract String getPossibleArguments();
|
||||
|
||||
/**
|
||||
* 获得最小参数个数
|
||||
*
|
||||
* @return 最小参数个数
|
||||
*/
|
||||
public abstract int getMinimumArguments();
|
||||
|
||||
/**
|
||||
* 执行命令参数
|
||||
*
|
||||
* @param sender
|
||||
* - 命令发送者
|
||||
* @param label
|
||||
* - 命令
|
||||
* @param args
|
||||
* - 命令附加参数
|
||||
* @throws CommandException
|
||||
* - 命令异常
|
||||
*/
|
||||
public abstract void execute(CommandSender sender, String label, String[] args) throws CommandException;
|
||||
|
||||
/**
|
||||
* 是否只有玩家才能执行此命令
|
||||
*
|
||||
* @return 是否为玩家命令
|
||||
*/
|
||||
public abstract boolean isOnlyPlayerExecutable();
|
||||
|
||||
/**
|
||||
* 命令匹配检测
|
||||
*
|
||||
* @param name
|
||||
* - 命令
|
||||
* @return 是否匹配
|
||||
*/
|
||||
public final boolean isValidTrigger(String name) {
|
||||
if (this.name.equalsIgnoreCase(name)) {
|
||||
return true;
|
||||
}
|
||||
if (aliases != null) {
|
||||
for (String alias : aliases) {
|
||||
if (alias.equalsIgnoreCase(name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<String> getCommandList() {
|
||||
List<String> cmds = new ArrayList<String>();
|
||||
cmds.add(name);
|
||||
cmds.addAll(Arrays.asList(aliases));
|
||||
return cmds;
|
||||
}
|
||||
|
||||
}
|
149
src/main/java/cn/citycraft/Yum/commands/CommandHandler.java
Normal file
149
src/main/java/cn/citycraft/Yum/commands/CommandHandler.java
Normal file
@ -0,0 +1,149 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package cn.citycraft.Yum.commands;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandException;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.TabCompleter;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
|
||||
import cn.citycraft.Yum.Yum;
|
||||
import cn.citycraft.Yum.utils.PluginsManager;
|
||||
|
||||
/**
|
||||
* 子命令处理类
|
||||
*
|
||||
* @author 蒋天蓓
|
||||
* 2015年8月22日上午8:29:44
|
||||
*/
|
||||
public class CommandHandler implements CommandExecutor, TabCompleter {
|
||||
/**
|
||||
* 已注册命令列表(包括别名)
|
||||
*/
|
||||
List<String> RegisterCommandList = new ArrayList<String>();
|
||||
/**
|
||||
* 命令监听类列表
|
||||
*/
|
||||
private List<BaseCommand> commandlist;
|
||||
/**
|
||||
* 插件主类
|
||||
*/
|
||||
Yum plugin;
|
||||
|
||||
/**
|
||||
* 获得已注册的命令列表
|
||||
*
|
||||
* @return - 返回已注册的命令List
|
||||
*/
|
||||
public List<String> getRegisterCommands() {
|
||||
List<String> cmds = new ArrayList<String>();
|
||||
for (BaseCommand command : commandlist) {
|
||||
cmds.addAll(command.getCommandList());
|
||||
}
|
||||
return cmds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (sender.isOp() || sender.hasPermission("yum.admin") || sender.hasPermission("yum." + args[0])) {
|
||||
List<String> completions = new ArrayList<>();
|
||||
if (args.length == 1) {
|
||||
String partialCommand = args[0];
|
||||
List<String> commands = RegisterCommandList;
|
||||
StringUtil.copyPartialMatches(partialCommand, commands, completions);
|
||||
}
|
||||
if (args.length == 2) {
|
||||
String partialPlugin = args[1];
|
||||
List<String> plugins = PluginsManager.getPluginNames(false);
|
||||
StringUtil.copyPartialMatches(partialPlugin, plugins, completions);
|
||||
}
|
||||
Collections.sort(completions);
|
||||
return completions;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册子命令
|
||||
*
|
||||
* @param yum
|
||||
* - 插件主类
|
||||
*/
|
||||
public CommandHandler(Yum yum) {
|
||||
this.plugin = yum;
|
||||
registerCommand(new CommandList(yum));
|
||||
registerCommand(new CommandInstall(yum));
|
||||
registerCommand(new CommandUpdate(yum));
|
||||
registerCommand(new CommandRemove(yum));
|
||||
registerCommand(new CommandInfo(yum));
|
||||
|
||||
RegisterCommandList = getRegisterCommands();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
|
||||
if (args.length == 0) {
|
||||
|
||||
return true;
|
||||
}
|
||||
String subcmd = args[0];
|
||||
String[] subargs = moveStrings(args, 1);
|
||||
for (BaseCommand command : commandlist) {
|
||||
if (command.isValidTrigger(subcmd)) {
|
||||
if (!command.hasPermission(sender)) {
|
||||
sender.sendMessage("你没有此命令的权限!");
|
||||
return true;
|
||||
}
|
||||
if (command.isOnlyPlayerExecutable() && !(sender instanceof Player)) {
|
||||
sender.sendMessage("控制台无法使用此命令!");
|
||||
return true;
|
||||
}
|
||||
if (subargs.length >= command.getMinimumArguments()) {
|
||||
try {
|
||||
command.execute(sender, subcmd, subargs);
|
||||
return true;
|
||||
} catch (CommandException e) {
|
||||
sender.sendMessage(e.getMessage());
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("错误的参数 /yum " + command.getName() + command.getPossibleArguments());
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册命令
|
||||
*
|
||||
* @param command
|
||||
* - 被注册的命令类
|
||||
*/
|
||||
public void registerCommand(BaseCommand command) {
|
||||
commandlist.add(command);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转移数组
|
||||
*
|
||||
* @param args
|
||||
* - 原数组
|
||||
* @param start
|
||||
* - 数组开始位置
|
||||
* @return 转移后的数组字符串
|
||||
*/
|
||||
public static String[] moveStrings(String[] args, int start) {
|
||||
String[] ret = new String[args.length - start];
|
||||
System.arraycopy(args, args.length - start, ret, 0, ret.length);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
70
src/main/java/cn/citycraft/Yum/commands/CommandInfo.java
Normal file
70
src/main/java/cn/citycraft/Yum/commands/CommandInfo.java
Normal file
@ -0,0 +1,70 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package cn.citycraft.Yum.commands;
|
||||
|
||||
import org.bukkit.command.CommandException;
|
||||
import org.bukkit.command.CommandSender;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 插件删除命令类
|
||||
*
|
||||
* @author 蒋天蓓 2015年8月12日下午2:04:05
|
||||
*/
|
||||
public class CommandInfo extends BaseCommand {
|
||||
Yum yum;
|
||||
|
||||
/**
|
||||
* @param name
|
||||
*/
|
||||
public CommandInfo(Yum main) {
|
||||
super("info");
|
||||
this.yum = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnlyPlayerExecutable() {
|
||||
return false;
|
||||
};
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
if (args.length == 0) {
|
||||
return;
|
||||
}
|
||||
String pluginname = args[0];
|
||||
Plugin plugin = yum.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin != null) {
|
||||
PluginDescriptionFile desc = plugin.getDescription();
|
||||
sender.sendMessage("§6插件名称: §3" + plugin.getName());
|
||||
sender.sendMessage("§6插件版本: §3" + desc.getVersion());
|
||||
sender.sendMessage("§6插件作者: §3" + desc.getAuthors().toArray().toString());
|
||||
sender.sendMessage("§6插件描述: §3" + desc.getDescription());
|
||||
sender.sendMessage("§6插件依赖: §3");
|
||||
StringUtil.sendStringArray(sender, desc.getDepend());
|
||||
sender.sendMessage("§6插件软依赖: §3");
|
||||
StringUtil.sendStringArray(sender, desc.getSoftDepend());
|
||||
sender.sendMessage("§6插件载入前: §3");
|
||||
StringUtil.sendStringArray(sender, desc.getLoadBefore());
|
||||
sender.sendMessage("§6插件物理路径: §3" + PluginsManager.getPluginFile(plugin).getAbsolutePath());
|
||||
} else {
|
||||
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinimumArguments() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPossibleArguments() {
|
||||
return "<插件名称>";
|
||||
}
|
||||
}
|
57
src/main/java/cn/citycraft/Yum/commands/CommandInstall.java
Normal file
57
src/main/java/cn/citycraft/Yum/commands/CommandInstall.java
Normal file
@ -0,0 +1,57 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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 CommandInstall extends BaseCommand {
|
||||
Yum yum;
|
||||
|
||||
/**
|
||||
* @param name
|
||||
*/
|
||||
public CommandInstall(Yum main) {
|
||||
super("install");
|
||||
this.yum = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnlyPlayerExecutable() {
|
||||
return false;
|
||||
};
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
String pluginname = args[0];
|
||||
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin != null) {
|
||||
if (yum.download.run(sender, pluginname)) {
|
||||
sender.sendMessage(PluginsManager.load(pluginname));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§c插件已安装在服务器 需要更新请使用yum update " + pluginname + "!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinimumArguments() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPossibleArguments() {
|
||||
return "<插件名称>";
|
||||
}
|
||||
}
|
52
src/main/java/cn/citycraft/Yum/commands/CommandList.java
Normal file
52
src/main/java/cn/citycraft/Yum/commands/CommandList.java
Normal file
@ -0,0 +1,52 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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 CommandList extends BaseCommand {
|
||||
Yum plugin;
|
||||
|
||||
/**
|
||||
* @param name
|
||||
*/
|
||||
public CommandList(Yum main) {
|
||||
super("list");
|
||||
this.plugin = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnlyPlayerExecutable() {
|
||||
return false;
|
||||
};
|
||||
|
||||
@Override
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinimumArguments() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPossibleArguments() {
|
||||
return "";
|
||||
}
|
||||
}
|
54
src/main/java/cn/citycraft/Yum/commands/CommandRemove.java
Normal file
54
src/main/java/cn/citycraft/Yum/commands/CommandRemove.java
Normal file
@ -0,0 +1,54 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package cn.citycraft.Yum.commands;
|
||||
|
||||
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 CommandRemove extends BaseCommand {
|
||||
Yum yum;
|
||||
|
||||
/**
|
||||
* @param name
|
||||
*/
|
||||
public CommandRemove(Yum main) {
|
||||
super("remove");
|
||||
this.yum = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnlyPlayerExecutable() {
|
||||
return false;
|
||||
};
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
String pluginname = args[0];
|
||||
Plugin plugin = yum.getServer().getPluginManager().getPlugin(pluginname);
|
||||
if (plugin != null) {
|
||||
sender.sendMessage(PluginsManager.unload(plugin));
|
||||
} else {
|
||||
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinimumArguments() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPossibleArguments() {
|
||||
return "<插件名称>";
|
||||
}
|
||||
}
|
57
src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java
Normal file
57
src/main/java/cn/citycraft/Yum/commands/CommandUpdate.java
Normal file
@ -0,0 +1,57 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
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 CommandUpdate extends BaseCommand {
|
||||
Yum yum;
|
||||
|
||||
/**
|
||||
* @param name
|
||||
*/
|
||||
public CommandUpdate(Yum main) {
|
||||
super("update");
|
||||
this.yum = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnlyPlayerExecutable() {
|
||||
return false;
|
||||
};
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String label, String[] args) throws CommandException {
|
||||
String pluginname = args[0];
|
||||
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
|
||||
sender.sendMessage("§a开始更新插件: " + pluginname);
|
||||
if (plugin != null) {
|
||||
sender.sendMessage(PluginsManager.unload(plugin));
|
||||
if (yum.download.run(sender, pluginname)) {
|
||||
sender.sendMessage(PluginsManager.load(pluginname));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getMinimumArguments() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPossibleArguments() {
|
||||
return "<插件名称>";
|
||||
}
|
||||
}
|
81
src/main/java/cn/citycraft/Yum/config/ConfigLoader.java
Normal file
81
src/main/java/cn/citycraft/Yum/config/ConfigLoader.java
Normal file
@ -0,0 +1,81 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
108
src/main/java/cn/citycraft/Yum/config/FileConfig.java
Normal file
108
src/main/java/cn/citycraft/Yum/config/FileConfig.java
Normal file
@ -0,0 +1,108 @@
|
||||
package cn.citycraft.Yum.config;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
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.yaml.snakeyaml.DumperOptions;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
import org.yaml.snakeyaml.representer.Representer;
|
||||
|
||||
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
|
||||
* 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 final DumperOptions yamlOptions = new DumperOptions();
|
||||
|
||||
protected final Representer yamlRepresenter = new YamlRepresenter();
|
||||
|
||||
protected final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
|
||||
|
||||
@Override
|
||||
public void load(File file) throws FileNotFoundException, IOException, InvalidConfigurationException {
|
||||
Validate.notNull(file, "File cannot be null");
|
||||
final FileInputStream stream = new FileInputStream(file);
|
||||
load(new InputStreamReader(stream, Charsets.UTF_8));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(Reader reader) throws IOException, InvalidConfigurationException {
|
||||
BufferedReader input = (reader instanceof BufferedReader) ? (BufferedReader) reader
|
||||
: new BufferedReader(reader);
|
||||
StringBuilder builder = new StringBuilder();
|
||||
try {
|
||||
String line;
|
||||
while ((line = input.readLine()) != null) {
|
||||
builder.append(line);
|
||||
builder.append('\n');
|
||||
}
|
||||
} finally {
|
||||
input.close();
|
||||
}
|
||||
loadFromString(builder.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(File file) throws IOException {
|
||||
Validate.notNull(file, "File cannot be null");
|
||||
Files.createParentDirs(file);
|
||||
String data = saveToString();
|
||||
Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
|
||||
try {
|
||||
writer.write(data);
|
||||
} finally {
|
||||
writer.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String saveToString() {
|
||||
yamlOptions.setIndent(options().indent());
|
||||
yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
|
||||
String header = buildHeader();
|
||||
String dump = yaml.dump(getValues(false));
|
||||
if (dump.equals(BLANK_CONFIG)) {
|
||||
dump = "";
|
||||
}
|
||||
return header + dump;
|
||||
}
|
||||
}
|
@ -15,15 +15,17 @@ import org.bukkit.plugin.Plugin;
|
||||
/**
|
||||
* @author 蒋天蓓 2015年8月21日下午6:08:09 TODO
|
||||
*/
|
||||
public class DownloadUtils {
|
||||
public class DownloadManager {
|
||||
Plugin plugin;
|
||||
|
||||
public DownloadUtils(Plugin main) {
|
||||
public DownloadManager(Plugin main) {
|
||||
this.plugin = main;
|
||||
}
|
||||
|
||||
public boolean download(CommandSender sender, String pluginname) {
|
||||
String url = "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
|
||||
public boolean run(CommandSender sender, String pluginname) {
|
||||
// String url =
|
||||
// "http://ci.citycraft.cn:8800/jenkins/job/%1$s/lastSuccessfulBuild/artifact/target/%1$s.jar";
|
||||
String url = "https://502647092.github.io/plugins/%1$s/%1$s.jar";
|
||||
BufferedInputStream in = null;
|
||||
FileOutputStream fout = null;
|
||||
if (sender == null) {
|
@ -6,6 +6,7 @@ package cn.citycraft.Yum.utils;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
@ -13,8 +14,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.SortedSet;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
@ -27,6 +26,7 @@ import org.bukkit.plugin.InvalidPluginException;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.RegisteredListener;
|
||||
import org.bukkit.plugin.UnknownDependencyException;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
|
||||
@ -35,7 +35,7 @@ import com.google.common.base.Joiner;
|
||||
*
|
||||
* @author 蒋天蓓 2015年8月21日下午7:03:26
|
||||
*/
|
||||
public class PluginUtil {
|
||||
public class PluginsManager {
|
||||
|
||||
public static void disable(Plugin plugin) {
|
||||
if ((plugin.isEnabled()) && (plugin != null)) {
|
||||
@ -98,6 +98,26 @@ public class PluginUtil {
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public static File getPluginFile(Plugin plugin) {
|
||||
File file = null;
|
||||
ClassLoader cl = plugin.getClass().getClassLoader();
|
||||
if ((cl instanceof URLClassLoader)) {
|
||||
@SuppressWarnings("resource")
|
||||
URLClassLoader ucl = (URLClassLoader) cl;
|
||||
URL url = ucl.getURLs()[0];
|
||||
file = new File(url.getPath(), url.getFile());
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public static boolean deletePlugin(Plugin plugin) {
|
||||
ClassLoader cl = plugin.getClass().getClassLoader();
|
||||
if ((cl instanceof URLClassLoader)) {
|
||||
} else {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getPluginVersion(String name) {
|
||||
Plugin plugin = getPluginByName(name);
|
||||
if ((plugin != null) && (plugin.getDescription() != null))
|
||||
@ -146,26 +166,31 @@ public class PluginUtil {
|
||||
File pluginDir = new File("plugins");
|
||||
|
||||
if (!pluginDir.isDirectory()) {
|
||||
// TODO 提示
|
||||
return "§c插件目录不存在或IO错误!";
|
||||
}
|
||||
|
||||
File pluginFile = new File(pluginDir, name + ".jar");
|
||||
|
||||
if (!pluginFile.isFile())
|
||||
return "§c在plugins目录未找到 " + name + " 插件 请确认文件是否存在!";
|
||||
|
||||
try {
|
||||
target = Bukkit.getPluginManager().loadPlugin(pluginFile);
|
||||
} catch (InvalidDescriptionException e) {
|
||||
e.printStackTrace();
|
||||
return "§c插件: " + name + " 的plugin.yml文件存在错误!";
|
||||
return "§c异常: " + e.getMessage() + " 插件: " + name + " 的plugin.yml文件存在错误!";
|
||||
} catch (InvalidPluginException e) {
|
||||
e.printStackTrace();
|
||||
return "§c文件: " + name + " 不是一个可载入的插件!";
|
||||
return "§c异常: " + e.getMessage() + " 文件: " + name + " 不是一个可载入的插件!";
|
||||
} catch (UnknownDependencyException e) {
|
||||
e.printStackTrace();
|
||||
return "§c异常: " + e.getMessage() + " 插件: " + name + " 缺少部分依赖!";
|
||||
}
|
||||
|
||||
target.onLoad();
|
||||
Bukkit.getPluginManager().enablePlugin(target);
|
||||
|
||||
return "§a插件 " + name + " 已成功载入到服务器!";
|
||||
return "§a插件: " + name + " 已成功载入到服务器!";
|
||||
}
|
||||
|
||||
public static void reload(Plugin plugin) {
|
||||
@ -186,19 +211,13 @@ public class PluginUtil {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static String unload(Plugin plugin) {
|
||||
String name = plugin.getName();
|
||||
|
||||
PluginManager pluginManager = Bukkit.getPluginManager();
|
||||
|
||||
SimpleCommandMap commandMap = null;
|
||||
|
||||
List<Plugin> plugins = null;
|
||||
|
||||
Map<String, Plugin> names = null;
|
||||
Map<String, Command> commands = null;
|
||||
Map<Event, SortedSet<RegisteredListener>> listeners = null;
|
||||
|
||||
boolean reloadlisteners = true;
|
||||
|
||||
if (pluginManager != null) {
|
||||
try {
|
||||
Field pluginsField = Bukkit.getPluginManager().getClass().getDeclaredField("plugins");
|
||||
@ -225,23 +244,19 @@ public class PluginUtil {
|
||||
commands = (Map<String, Command>) knownCommandsField.get(commandMap);
|
||||
} catch (NoSuchFieldException e) {
|
||||
e.printStackTrace();
|
||||
return "§c插件 " + name + " 卸载失败!";
|
||||
return "§c异常: " + e.getMessage() + " 插件 " + name + " 卸载失败!";
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
return "§c插件 " + name + " 卸载失败!";
|
||||
return "§c异常: " + e.getMessage() + " 插件 " + name + " 卸载失败!";
|
||||
}
|
||||
}
|
||||
|
||||
pluginManager.disablePlugin(plugin);
|
||||
|
||||
if (plugins != null && plugins.contains(plugin)) {
|
||||
plugins.remove(plugin);
|
||||
}
|
||||
|
||||
if (names != null && names.containsKey(name)) {
|
||||
names.remove(name);
|
||||
}
|
||||
|
||||
if (listeners != null && reloadlisteners) {
|
||||
for (SortedSet<RegisteredListener> set : listeners.values()) {
|
||||
for (Iterator<RegisteredListener> it = set.iterator(); it.hasNext();) {
|
||||
@ -252,7 +267,6 @@ public class PluginUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commandMap != null) {
|
||||
for (Iterator<Map.Entry<String, Command>> it = commands.entrySet().iterator(); it.hasNext();) {
|
||||
Map.Entry<String, Command> entry = it.next();
|
||||
@ -265,18 +279,14 @@ public class PluginUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ClassLoader cl = plugin.getClass().getClassLoader();
|
||||
|
||||
if ((cl instanceof URLClassLoader)) {
|
||||
try {
|
||||
((URLClassLoader) cl).close();
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(PluginUtil.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
System.gc();
|
||||
return "§a插件 " + name + " 已成功卸载!";
|
||||
return "§a插件: " + name + " 已成功卸载!";
|
||||
}
|
||||
}
|
@ -3,22 +3,24 @@
|
||||
*/
|
||||
package cn.citycraft.Yum.utils;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
/**
|
||||
* 字符串工具
|
||||
*
|
||||
* @author 蒋天蓓
|
||||
* 2015年8月21日下午7:05:51
|
||||
* 2015年8月22日下午12:41:59
|
||||
* TODO
|
||||
*/
|
||||
public class StringUtil {
|
||||
|
||||
/**
|
||||
* 转移数组
|
||||
* 转移数组后获取字符串
|
||||
*
|
||||
* @param args
|
||||
* - 原数组
|
||||
* @param start
|
||||
* - 数组开始位置
|
||||
* @return 转移后的数组
|
||||
* @return 转移后的数组字符串
|
||||
*/
|
||||
public static String consolidateStrings(String[] args, int start) {
|
||||
String ret = args[start];
|
||||
@ -28,4 +30,10 @@ public class StringUtil {
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static void sendStringArray(CommandSender player, Collection<String> msg) {
|
||||
for (String string : msg) {
|
||||
player.sendMessage("§6 - §3" + string);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
name: Yum
|
||||
main: cn.citycraft.Yum.Yum
|
||||
website: http://ci.citycraft.cn:8800/jenkins/job/Yum/
|
||||
version: 1.0
|
||||
version: 1.1
|
||||
commands:
|
||||
yum:
|
||||
description: MC插件仓库
|
||||
|
Loading…
Reference in New Issue
Block a user