clean up and use PluginHelper...

Signed-off-by: 502647092 <jtb1@163.com>
dev
502647092 2015-09-29 20:44:17 +08:00
parent 081522be3d
commit bf2a870984
15 changed files with 86 additions and 395 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cn.citycraft</groupId>
<artifactId>Yum</artifactId>
<version>1.4</version>
<version>1.5</version>
<name>Yum</name>
<description>Minecraft 服务器插件管理系统</description>
<build>

View File

@ -8,9 +8,19 @@ import java.io.IOException;
import org.bukkit.plugin.java.JavaPlugin;
import org.mcstats.Metrics;
import cn.citycraft.PluginHelper.commands.HandlerSubCommand;
import cn.citycraft.PluginHelper.config.FileConfig;
import cn.citycraft.PluginHelper.utils.VersionChecker;
import cn.citycraft.Yum.commands.HandlerCommand;
import cn.citycraft.Yum.commands.CommandDelete;
import cn.citycraft.Yum.commands.CommandInfo;
import cn.citycraft.Yum.commands.CommandInstall;
import cn.citycraft.Yum.commands.CommandList;
import cn.citycraft.Yum.commands.CommandLoad;
import cn.citycraft.Yum.commands.CommandReload;
import cn.citycraft.Yum.commands.CommandRepo;
import cn.citycraft.Yum.commands.CommandUnload;
import cn.citycraft.Yum.commands.CommandUpdate;
import cn.citycraft.Yum.commands.CommandUpgrade;
import cn.citycraft.Yum.manager.YumManager;
/**
@ -22,6 +32,22 @@ public class Yum extends JavaPlugin {
public FileConfig config;
public YumManager yumgr;
public void initCommands() {
final HandlerSubCommand cmdhandler = new HandlerSubCommand(this);
cmdhandler.registerCommand(new CommandList(this));
cmdhandler.registerCommand(new CommandInstall(this));
cmdhandler.registerCommand(new CommandUpdate(this));
cmdhandler.registerCommand(new CommandDelete(this));
cmdhandler.registerCommand(new CommandInfo(this));
cmdhandler.registerCommand(new CommandRepo(this));
cmdhandler.registerCommand(new CommandReload(this));
cmdhandler.registerCommand(new CommandLoad(this));
cmdhandler.registerCommand(new CommandUnload(this));
cmdhandler.registerCommand(new CommandUpgrade(this));
this.getCommand("yum").setExecutor(cmdhandler);
this.getCommand("yum").setTabCompleter(cmdhandler);
}
@Override
public void onDisable() {
YumManager.repo.cacheToJson(config);
@ -30,9 +56,7 @@ public class Yum extends JavaPlugin {
@Override
public void onEnable() {
final HandlerCommand cmdhandler = new HandlerCommand(this);
this.getCommand("yum").setExecutor(cmdhandler);
this.getCommand("yum").setTabCompleter(cmdhandler);
this.initCommands();
yumgr = new YumManager(this);
YumManager.repo.jsonToCache(config);
new VersionChecker(this);

View File

@ -6,7 +6,8 @@ package cn.citycraft.Yum.api;
/**
* YumAPI
*
* @author 20158224:43:41
* @author
* @since 20158224:43:41
*/
public class YumApi {

View File

@ -1,193 +0,0 @@
/**
*
*/
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
* 201581212:49:34
*/
public abstract class BaseCommand {
private final String[] aliases;
private final String description;
private int minimumArguments = 0;
private final String name;
private boolean onlyPlayerExecutable = false;
private String permission;
private String possibleArguments = "";
public BaseCommand(final String name, final String description) {
this(name, description, new String[0]);
}
public BaseCommand(final String name, final String description, final String... aliases) {
this.name = name;
this.description = description;
this.aliases = aliases;
}
/**
*
*
* @param sender
* -
* @param label
* -
* @param args
* -
* @throws CommandException
* -
*/
public abstract void execute(CommandSender sender, String label, String[] args) throws CommandException;
/**
*
*
* @return
*/
public List<String> getCommandList() {
final List<String> cmds = new ArrayList<String>();
cmds.add(name);
cmds.addAll(Arrays.asList(aliases));
return cmds;
}
/**
*
*
* @return
*/
public String getDescription() {
return description;
}
/**
*
*
* @return
*/
public int getMinimumArguments() {
return minimumArguments;
}
/**
*
*
* @return
*/
public String getName() {
return name;
}
/**
*
*
* @return
*/
public String getPermission() {
return permission;
}
/**
*
*
* @return
*/
public String getPossibleArguments() {
return possibleArguments;
}
/**
* Sender
*
* @param sender
* -
* @return
*/
public final boolean hasPermission(final CommandSender sender) {
if (permission == null) {
return true;
}
return sender.hasPermission(permission);
}
/**
*
*
* @return
*/
public boolean isOnlyPlayerExecutable() {
return onlyPlayerExecutable;
}
/**
*
*
* @param name
* -
* @return
*/
public final boolean isValidTrigger(final String name) {
if (this.name.equalsIgnoreCase(name)) {
return true;
}
if (aliases != null) {
for (final String alias : aliases) {
if (alias.equalsIgnoreCase(name)) {
return true;
}
}
}
return false;
}
/**
*
*
* @param minimumArguments
* -
*/
public void setMinimumArguments(final int minimumArguments) {
this.minimumArguments = minimumArguments;
}
/**
*
*
* @param onlyPlayerExecutable
* -
*/
public void setOnlyPlayerExecutable(final boolean onlyPlayerExecutable) {
this.onlyPlayerExecutable = onlyPlayerExecutable;
}
/**
*
*
* @param permission
* -
*/
public void setPermission(final String permission) {
this.permission = permission;
}
/**
*
*
* @param possibleArguments
* -
*/
public void setPossibleArguments(final String possibleArguments) {
this.possibleArguments = possibleArguments;
}
}

View File

@ -4,10 +4,12 @@
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -23,14 +25,15 @@ public class CommandDelete extends BaseCommand {
* @param name
*/
public CommandDelete(final Yum main) {
super("delete", "删除插件");
super("delete", "remove");
this.main = main;
setMinimumArguments(1);
setDescription("删除插件");
setPossibleArguments("<插件名称>");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final String pluginname = args[0];
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) {
@ -42,5 +45,5 @@ public class CommandDelete extends BaseCommand {
} else {
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
}
};
}
}

View File

@ -4,11 +4,13 @@
package cn.citycraft.Yum.commands;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.PluginHelper.utils.StringUtil;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -25,16 +27,15 @@ public class CommandInfo extends BaseCommand {
* @param name
*/
public CommandInfo(final Yum main) {
super("info", "查看插件详情");
super("info");
this.main = main;
setMinimumArguments(1);
setDescription("查看插件详情");
setPossibleArguments("<插件名称>");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
if (args.length == 0) {
return;
}
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final String pluginname = args[0];
final Plugin plugin = main.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) {

View File

@ -4,10 +4,12 @@
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -23,14 +25,15 @@ public class CommandInstall extends BaseCommand {
* @param name
*/
public CommandInstall(final Yum main) {
super("install", "安装插件");
super("install");
this.main = main;
setMinimumArguments(1);
setDescription("安装插件");
setPossibleArguments("<插件名称>");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final String pluginname = args[0];
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin == null) {
@ -45,8 +48,7 @@ public class CommandInstall extends BaseCommand {
}
});
} else {
sender.sendMessage("§c插件已安装在服务器 需要更新请使用yum update " + pluginname + "!");
sender.sendMessage("§c插件" + pluginname + "已安装在服务器 需要更新请使用yum update " + pluginname + "!");
}
};
}
}

View File

@ -4,10 +4,12 @@
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -23,12 +25,13 @@ public class CommandList extends BaseCommand {
* @param name
*/
public CommandList(final Yum main) {
super("list", "列出已安装插件列表");
super("list");
this.main = main;
setDescription("列出已安装插件列表");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: ");
for (final Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
sender.sendMessage("§6 - " + YumManager.plugman.getFormattedName(plugin, true));

View File

@ -4,10 +4,12 @@
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -23,14 +25,15 @@ public class CommandLoad extends BaseCommand {
* @param name
*/
public CommandLoad(final Yum main) {
super("load", "载入插件");
super("load");
this.main = main;
setMinimumArguments(1);
setDescription("载入插件");
setPossibleArguments("<插件名称>");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final String pluginname = args[0];
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin == null) {

View File

@ -4,10 +4,12 @@
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -23,14 +25,15 @@ public class CommandReload extends BaseCommand {
* @param name
*/
public CommandReload(final Yum main) {
super("reload", "重载插件");
super("reload");
this.main = main;
setMinimumArguments(1);
setDescription("重载插件");
setPossibleArguments("<插件名称|all|*>");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final String pluginname = args[0];
if (pluginname.equalsIgnoreCase("all") || pluginname.equalsIgnoreCase("*")) {
YumManager.plugman.reloadAll(sender);

View File

@ -3,9 +3,11 @@
*/
package cn.citycraft.Yum.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.PluginHelper.utils.StringUtil;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -22,14 +24,15 @@ public class CommandRepo extends BaseCommand {
* @param name
*/
public CommandRepo(final Yum main) {
super("repo", "插件源命令");
super("repo");
this.main = main;
setMinimumArguments(1);
setDescription("插件源命令");
setPossibleArguments("<add|del|clean|list> <仓库名称>");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final String cmd = args[0];
switch (cmd) {
case "add":

View File

@ -4,10 +4,12 @@
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -23,14 +25,15 @@ public class CommandUnload extends BaseCommand {
* @param name
*/
public CommandUnload(final Yum main) {
super("unload", "卸载插件");
super("unload");
this.main = main;
setMinimumArguments(1);
setDescription("卸载插件");
setPossibleArguments("<插件名称>");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final String pluginname = args[0];
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) {

View File

@ -4,10 +4,12 @@
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -21,14 +23,15 @@ public class CommandUpdate extends BaseCommand {
* @param name
*/
public CommandUpdate(final Yum main) {
super("update", "更新插件");
super("update");
this.main = main;
setMinimumArguments(1);
setDescription("更新插件");
setPossibleArguments("<插件名称> [插件版本]");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
final String pluginname = args[0];
final Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
sender.sendMessage("§a开始更新插件: " + pluginname);
@ -44,7 +47,7 @@ public class CommandUpdate extends BaseCommand {
}
});
} else {
sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!");
sender.sendMessage("§c插件" + pluginname + "未安装或已卸载 需要安装请使用/yum install " + pluginname + "!");
}
};
}

View File

@ -4,10 +4,12 @@
package cn.citycraft.Yum.commands;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import cn.citycraft.PluginHelper.commands.BaseCommand;
import cn.citycraft.Yum.Yum;
import cn.citycraft.Yum.manager.YumManager;
@ -21,13 +23,14 @@ public class CommandUpgrade extends BaseCommand {
* @param name
*/
public CommandUpgrade(final Yum main) {
super("upgrade", "升级插件");
super("upgrade");
this.main = main;
setDescription("升级插件");
setPossibleArguments("[插件名称]");
}
@Override
public void execute(final CommandSender sender, final String label, final String[] args) throws CommandException {
public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
Bukkit.getScheduler().runTaskAsynchronously(main, new Runnable() {
@Override
public void run() {

View File

@ -1,168 +0,0 @@
/**
*
*/
package cn.citycraft.Yum.commands;
import java.util.ArrayList;
import java.util.Arrays;
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.manager.YumManager;
/**
*
*
* @author 20158228:29:44
*/
public class HandlerCommand implements CommandExecutor, TabCompleter {
/**
*
*/
private final List<BaseCommand> commandlist = new ArrayList<BaseCommand>();
/**
*
*/
Yum main;;
/**
* ()
*/
List<String> RegisterCommandList = new ArrayList<String>();
/**
*
*
* @param yum
* -
*/
public HandlerCommand(final Yum yum) {
this.main = yum;
registerCommand(new CommandList(yum));
registerCommand(new CommandInstall(yum));
registerCommand(new CommandUpdate(yum));
registerCommand(new CommandDelete(yum));
registerCommand(new CommandInfo(yum));
registerCommand(new CommandRepo(yum));
registerCommand(new CommandReload(yum));
registerCommand(new CommandLoad(yum));
registerCommand(new CommandUnload(yum));
registerCommand(new CommandUpgrade(yum));
RegisterCommandList = getRegisterCommands();
}
/**
*
*
* @param args
* -
* @param start
* -
* @return
*/
public static String[] moveStrings(final String[] args, final int start) {
final String[] ret = new String[args.length - start];
System.arraycopy(args, start, ret, 0, ret.length);
return ret;
}
/**
*
*
* @return - List
*/
public List<String> getRegisterCommands() {
final List<String> cmds = new ArrayList<String>();
for (final BaseCommand command : commandlist) {
cmds.addAll(command.getCommandList());
}
return cmds;
}
@Override
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) {
if (args.length == 0) {
return true;
}
final String subcmd = args[0];
if (subcmd.equalsIgnoreCase("help")) {
sender.sendMessage("§6=========YUM插件帮助列表=========");
for (final BaseCommand command : commandlist) {
sender.sendMessage(String.format("§6/yum §a%1$s %2$s §6- §b%3$s", command.getName(), command.getPossibleArguments(), command.getDescription()));
}
return true;
}
final String[] subargs = moveStrings(args, 1);
for (final BaseCommand command : commandlist) {
if (command.isValidTrigger(subcmd)) {
if (!command.hasPermission(sender)) {
sender.sendMessage("§c你没有此命令的权限!");
return true;
}
if (command.isOnlyPlayerExecutable() && !(sender instanceof Player)) {
sender.sendMessage("§c控制台无法使用此命令!");
return true;
}
if (subargs.length >= command.getMinimumArguments()) {
try {
command.execute(sender, subcmd, subargs);
return true;
} catch (final CommandException e) {
sender.sendMessage(e.getMessage());
}
} else {
sender.sendMessage("§c错误的参数 §e使用方法 /yum " + command.getName() + command.getPossibleArguments());
}
}
}
return false;
}
@Override
public List<String> onTabComplete(final CommandSender sender, final Command command, final String label, final String[] args) {
if (sender.isOp() || sender.hasPermission("yum.admin") || sender.hasPermission("yum." + args[0])) {
final List<String> completions = new ArrayList<>();
if (args.length == 1) {
final String partialCommand = args[0];
final List<String> commands = RegisterCommandList;
StringUtil.copyPartialMatches(partialCommand, commands, completions);
}
if (args.length == 2) {
final String partialPlugin = args[1];
List<String> plugins = null;
if (args[0].equalsIgnoreCase("install")) {
plugins = YumManager.repo.getAllPluginName();
} else if (args[0].equalsIgnoreCase("repo")) {
plugins = Arrays.asList(new String[] { "add", "list", "clean", "update" });
} else {
plugins = YumManager.plugman.getPluginNames(false);
}
StringUtil.copyPartialMatches(partialPlugin, plugins, completions);
}
Collections.sort(completions);
return completions;
}
return null;
}
/**
*
*
* @param command
* -
*/
public void registerCommand(final BaseCommand command) {
commandlist.add(command);
}
}