add help command and add getDescription on BaseCommand...

Signed-off-by: j502647092 <jtb1@163.com>
This commit is contained in:
j502647092 2015-09-07 22:58:26 +08:00
parent 9cd2259672
commit 304f4dad59
13 changed files with 177 additions and 127 deletions

View File

@ -5,7 +5,7 @@ package cn.citycraft.Yum;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import cn.citycraft.Yum.commands.CommandHandler; import cn.citycraft.Yum.commands.HandlerCommand;
import cn.citycraft.Yum.manager.YumManager; import cn.citycraft.Yum.manager.YumManager;
import cn.citycraft.config.FileConfig; import cn.citycraft.config.FileConfig;
import cn.citycraft.utils.VersionChecker; import cn.citycraft.utils.VersionChecker;
@ -27,7 +27,7 @@ public class Yum extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
CommandHandler cmdhandler = new CommandHandler(this); HandlerCommand cmdhandler = new HandlerCommand(this);
this.getCommand("yum").setExecutor(cmdhandler); this.getCommand("yum").setExecutor(cmdhandler);
this.getCommand("yum").setTabCompleter(cmdhandler); this.getCommand("yum").setTabCompleter(cmdhandler);
yumgr = new YumManager(this); yumgr = new YumManager(this);

View File

@ -1,5 +1,5 @@
/** /**
* *
*/ */
package cn.citycraft.Yum.commands; package cn.citycraft.Yum.commands;
@ -12,7 +12,7 @@ import org.bukkit.command.CommandSender;
/** /**
* 基础命令类 * 基础命令类
* *
* @author 蒋天蓓 * @author 蒋天蓓
* 2015年8月12日下午12:49:34 * 2015年8月12日下午12:49:34
*/ */
@ -30,64 +30,9 @@ public abstract class BaseCommand {
this.aliases = aliases; 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 sender
* - 命令发送者 * - 命令发送者
* @param label * @param label
@ -99,34 +44,6 @@ public abstract class BaseCommand {
*/ */
public abstract void execute(CommandSender sender, String label, String[] 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() { public List<String> getCommandList() {
List<String> cmds = new ArrayList<String>(); List<String> cmds = new ArrayList<String>();
cmds.add(name); cmds.add(name);
@ -134,4 +51,90 @@ public abstract class BaseCommand {
return cmds; return cmds;
} }
/**
* 获得命令描述
*
* @return 命令描述
*/
public abstract String getDescription();
/**
* 获得最小参数个数
*
* @return 最小参数个数
*/
public abstract int getMinimumArguments();
/**
* 获取命令名称
*
* @return 命令名称
*/
public String getName() {
return name;
}
/**
* 获得命令权限
*
* @return 目录命令权限
*/
public String getPermission() {
return permission;
}
/**
* 获得可能的参数
*
* @return
*/
public abstract String getPossibleArguments();
/**
* 检查Sender权限
*
* @param sender
* - 命令发送者
* @return 是否有权限执行命令
*/
public final boolean hasPermission(CommandSender sender) {
if (permission == null)
return true;
return sender.hasPermission(permission);
}
/**
* 是否只有玩家才能执行此命令
*
* @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;
}
/**
* 设置命令权限
*
* @param permission
* - 命令权限
*/
public void setPermission(String permission) {
this.permission = permission;
}
} }

View File

@ -31,13 +31,17 @@ public class CommandDelete extends BaseCommand {
public void execute(CommandSender sender, String label, String[] args) throws CommandException { public void execute(CommandSender sender, String label, String[] args) throws CommandException {
String pluginname = args[0]; String pluginname = args[0];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) { if (plugin != null)
YumManager.plugman.deletePlugin(sender, plugin); YumManager.plugman.deletePlugin(sender, plugin);
} else { else
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
}
}; };
@Override
public String getDescription() {
return "删除插件";
}
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
return 1; return 1;

View File

@ -15,7 +15,7 @@ import cn.citycraft.Yum.utils.StringUtil;
/** /**
* 插件删除命令类 * 插件删除命令类
* *
* @author 蒋天蓓 2015年8月12日下午2:04:05 * @author 蒋天蓓 2015年8月12日下午2:04:05
*/ */
public class CommandInfo extends BaseCommand { public class CommandInfo extends BaseCommand {
@ -29,16 +29,10 @@ public class CommandInfo extends BaseCommand {
this.main = main; this.main = main;
} }
@Override
public boolean isOnlyPlayerExecutable() {
return false;
};
@Override @Override
public void execute(CommandSender sender, String label, String[] args) throws CommandException { public void execute(CommandSender sender, String label, String[] args) throws CommandException {
if (args.length == 0) { if (args.length == 0)
return; return;
}
String pluginname = args[0]; String pluginname = args[0];
Plugin plugin = main.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = main.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) { if (plugin != null) {
@ -52,9 +46,13 @@ public class CommandInfo extends BaseCommand {
sender.sendMessage("§6插件软依赖: §3" + (desc.getSoftDepend().size() == 0 ? "" : "")); sender.sendMessage("§6插件软依赖: §3" + (desc.getSoftDepend().size() == 0 ? "" : ""));
StringUtil.sendStringArray(sender, desc.getSoftDepend()); StringUtil.sendStringArray(sender, desc.getSoftDepend());
sender.sendMessage("§6插件物理路径: §3" + YumManager.plugman.getPluginFile(plugin).getAbsolutePath()); sender.sendMessage("§6插件物理路径: §3" + YumManager.plugman.getPluginFile(plugin).getAbsolutePath());
} else { } else
sender.sendMessage("§4错误: §c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§4错误: §c插件 " + pluginname + " 不存在或已卸载!");
} };
@Override
public String getDescription() {
return "查看插件详情";
} }
@Override @Override
@ -66,4 +64,9 @@ public class CommandInfo extends BaseCommand {
public String getPossibleArguments() { public String getPossibleArguments() {
return "<插件名称>"; return "<插件名称>";
} }
@Override
public boolean isOnlyPlayerExecutable() {
return false;
}
} }

View File

@ -43,6 +43,11 @@ public class CommandInstall extends BaseCommand {
}; };
@Override
public String getDescription() {
return "安装插件";
}
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
return 1; return 1;

View File

@ -13,7 +13,7 @@ import cn.citycraft.Yum.manager.YumManager;
/** /**
* 插件查看命令类 * 插件查看命令类
* *
* @author 蒋天蓓 2015年8月12日下午2:04:05 * @author 蒋天蓓 2015年8月12日下午2:04:05
*/ */
public class CommandList extends BaseCommand { public class CommandList extends BaseCommand {
@ -28,16 +28,15 @@ public class CommandList extends BaseCommand {
} }
@Override @Override
public boolean isOnlyPlayerExecutable() { public void execute(CommandSender sender, String label, String[] args) throws CommandException {
return false; sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: ");
for (Plugin plugin : Bukkit.getPluginManager().getPlugins())
sender.sendMessage("§6 - " + YumManager.plugman.getFormattedName(plugin, true));
}; };
@Override @Override
public void execute(CommandSender sender, String label, String[] args) throws CommandException { public String getDescription() {
sender.sendMessage("§6[Yum仓库]§3服务器已安装插件: "); return "列出已安装插件列表";
for (Plugin plugin : Bukkit.getPluginManager().getPlugins()) {
sender.sendMessage("§6 - " + YumManager.plugman.getFormattedName(plugin, true));
}
} }
@Override @Override
@ -49,4 +48,9 @@ public class CommandList extends BaseCommand {
public String getPossibleArguments() { public String getPossibleArguments() {
return ""; return "";
} }
@Override
public boolean isOnlyPlayerExecutable() {
return false;
}
} }

View File

@ -31,13 +31,17 @@ public class CommandLoad extends BaseCommand {
public void execute(CommandSender sender, String label, String[] args) throws CommandException { public void execute(CommandSender sender, String label, String[] args) throws CommandException {
String pluginname = args[0]; String pluginname = args[0];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin == null) { if (plugin == null)
YumManager.plugman.load(sender, pluginname); YumManager.plugman.load(sender, pluginname);
} else { else
sender.sendMessage("§c错误: 插件 " + pluginname + " 已加载到服务器!"); sender.sendMessage("§c错误: 插件 " + pluginname + " 已加载到服务器!");
}
}; };
@Override
public String getDescription() {
return "载入插件";
}
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
return 1; return 1;

View File

@ -35,13 +35,17 @@ public class CommandReload extends BaseCommand {
return; return;
} }
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) { if (plugin != null)
YumManager.plugman.reload(sender, plugin); YumManager.plugman.reload(sender, plugin);
} else { else
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
}
}; };
@Override
public String getDescription() {
return "重载插件";
}
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
return 1; return 1;

View File

@ -32,14 +32,12 @@ public class CommandRepo extends BaseCommand {
switch (cmd) { switch (cmd) {
case "add": case "add":
if (args.length == 2) { if (args.length == 2) {
if (YumManager.repo.addRepositories(args[1])) { if (YumManager.repo.addRepositories(args[1]))
sender.sendMessage("§6仓库: §a插件信息已缓存!"); sender.sendMessage("§6仓库: §a插件信息已缓存!");
} else { else
sender.sendMessage("§6仓库: §c源地址未找到仓库信息或无法访问!"); sender.sendMessage("§6仓库: §c源地址未找到仓库信息或无法访问!");
} } else
} else {
sender.sendMessage("§6仓库: §c请输入源地址!"); sender.sendMessage("§6仓库: §c请输入源地址!");
}
break; break;
case "list": case "list":
sender.sendMessage("§6仓库: §b缓存的插件信息如下 "); sender.sendMessage("§6仓库: §b缓存的插件信息如下 ");
@ -56,6 +54,11 @@ public class CommandRepo extends BaseCommand {
} }
}; };
@Override
public String getDescription() {
return "插件源命令";
}
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
return 1; return 1;

View File

@ -31,13 +31,17 @@ public class CommandUnload extends BaseCommand {
public void execute(CommandSender sender, String label, String[] args) throws CommandException { public void execute(CommandSender sender, String label, String[] args) throws CommandException {
String pluginname = args[0]; String pluginname = args[0];
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname); Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(pluginname);
if (plugin != null) { if (plugin != null)
YumManager.plugman.unload(sender, plugin); YumManager.plugman.unload(sender, plugin);
} else { else
sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!"); sender.sendMessage("§c插件 " + pluginname + " 不存在或已卸载!");
}
}; };
@Override
public String getDescription() {
return "卸载插件";
}
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
return 1; return 1;

View File

@ -41,6 +41,11 @@ public class CommandUpdate extends BaseCommand {
sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!"); sender.sendMessage("§c插件未安装或已卸载 需要安装请使用yum install " + pluginname + "!");
}; };
@Override
public String getDescription() {
return "更新插件";
}
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
return 1; return 1;
@ -48,7 +53,7 @@ public class CommandUpdate extends BaseCommand {
@Override @Override
public String getPossibleArguments() { public String getPossibleArguments() {
return "<插件名称> <插件版本>"; return "<插件名称> [插件版本]";
} }
@Override @Override

View File

@ -42,6 +42,11 @@ public class CommandUpgrade extends BaseCommand {
}); });
}; };
@Override
public String getDescription() {
return "升级插件";
}
@Override @Override
public int getMinimumArguments() { public int getMinimumArguments() {
return 0; return 0;
@ -49,7 +54,7 @@ public class CommandUpgrade extends BaseCommand {
@Override @Override
public String getPossibleArguments() { public String getPossibleArguments() {
return "<插件名称> <插件版本>"; return "[插件名称]";
} }
@Override @Override

View File

@ -24,7 +24,7 @@ import cn.citycraft.Yum.manager.YumManager;
* *
* @author 蒋天蓓 2015年8月22日上午8:29:44 * @author 蒋天蓓 2015年8月22日上午8:29:44
*/ */
public class CommandHandler implements CommandExecutor, TabCompleter { public class HandlerCommand implements CommandExecutor, TabCompleter {
/** /**
* 已注册命令列表(包括别名) * 已注册命令列表(包括别名)
*/ */
@ -45,7 +45,7 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
* @param yum * @param yum
* - 插件主类 * - 插件主类
*/ */
public CommandHandler(Yum yum) { public HandlerCommand(Yum yum) {
this.main = yum; this.main = yum;
registerCommand(new CommandList(yum)); registerCommand(new CommandList(yum));
registerCommand(new CommandInstall(yum)); registerCommand(new CommandInstall(yum));
@ -93,6 +93,12 @@ public class CommandHandler implements CommandExecutor, TabCompleter {
if (args.length == 0) if (args.length == 0)
return true; return true;
String subcmd = args[0]; String subcmd = args[0];
if (subcmd.equalsIgnoreCase("help")) {
sender.sendMessage("§6=========YUM插件帮助列表=========");
for (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;
}
String[] subargs = moveStrings(args, 1); String[] subargs = moveStrings(args, 1);
for (BaseCommand command : commandlist) for (BaseCommand command : commandlist)
if (command.isValidTrigger(subcmd)) { if (command.isValidTrigger(subcmd)) {