This commit is contained in:
坏黑 2019-04-27 22:56:04 +08:00
parent 8ee634f0c9
commit af5eb86a6a
4 changed files with 42 additions and 88 deletions

View File

@ -5,7 +5,7 @@ plugins {
id 'com.github.johnrengelman.shadow' version '4.0.4' id 'com.github.johnrengelman.shadow' version '4.0.4'
} }
group = 'me.skymc' group = 'me.skymc'
version = '4.77' version = '4.78'
sourceCompatibility = 1.8 sourceCompatibility = 1.8
targetCompatibility = 1.8 targetCompatibility = 1.8

View File

@ -173,6 +173,7 @@ COMMANDS:
# COMMAND-CREATE: '&7自动为插件 &f{0} &7的 &f{1} &7命令注册到服务器' # COMMAND-CREATE: '&7自动为插件 &f{0} &7的 &f{1} &7命令注册到服务器'
# COMMAND-REGISTER: '&7自动为插件 &f{0} &7的 &f{1} &7命令注册 &f{2} &7条子命令' # COMMAND-REGISTER: '&7自动为插件 &f{0} &7的 &f{1} &7命令注册 &f{2} &7条子命令'
COMMAND-HELP: ' §f/{0} {1} {2}§6- §e{3}' COMMAND-HELP: ' §f/{0} {1} {2}§6- §e{3}'
COMMAND-HELP-EMPTY: ' §f/{0} {1} {2}'
COMMAND-ARGUMENT: '§7<§8{0}§7>' COMMAND-ARGUMENT: '§7<§8{0}§7>'
COMMAND-ARGUMENT-REQUIRE: '§7[§8{0}§7]' COMMAND-ARGUMENT-REQUIRE: '§7[§8{0}§7]'
PARAMETER: PARAMETER:

View File

@ -31,13 +31,6 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
private List<Class<?>> linkClasses = new CopyOnWriteArrayList<>(); private List<Class<?>> linkClasses = new CopyOnWriteArrayList<>();
private List<BaseSubCommand> subCommands = new CopyOnWriteArrayList<>(); private List<BaseSubCommand> subCommands = new CopyOnWriteArrayList<>();
/**
* 指令标题
*
* @return 文本
*/
abstract public String getCommandTitle();
public static BaseMainCommand createCommandExecutor(String command, BaseMainCommand baseMainCommand) { public static BaseMainCommand createCommandExecutor(String command, BaseMainCommand baseMainCommand) {
Preconditions.checkArgument(Bukkit.getPluginCommand(command) != null, "PluginCommand \"" + command + "\" not found"); Preconditions.checkArgument(Bukkit.getPluginCommand(command) != null, "PluginCommand \"" + command + "\" not found");
Preconditions.checkArgument(baseMainCommand != null, "Executor cannot be null"); Preconditions.checkArgument(baseMainCommand != null, "Executor cannot be null");
@ -71,14 +64,16 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
fields.forEach(commandField -> { fields.forEach(commandField -> {
try { try {
commandField.getField().setAccessible(true); commandField.getField().setAccessible(true);
baseMainCommand.registerSubCommand((BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance())); BaseSubCommand subCommand = (BaseSubCommand) commandField.getField().get(commandField.getParent().newInstance());
subCommand.setLabel(commandField.getField().getName());
baseMainCommand.registerSubCommand(subCommand);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
}); });
} }
if (methods.size() + fields.size() > 0) { if (methods.size() + fields.size() > 0) {
TabooLib.debug("Registered " + (methods.size() + fields.size()) + " sub-command with " + baseMainCommand.getRegisterCommand().getName() + " (" + baseMainCommand.getRegisterCommand().getPlugin().getName() + ")"); TabooLib.debug("Registered " + (methods.size() + fields.size()) + " sub-command with " + baseMainCommand.getRegisterCommand().getName() + " (" + baseMainCommand.getRegisterCommand().getPlugin().getName() + ")");
// TLocale.Logger.info("COMMANDS.INTERNAL.COMMAND-REGISTER", baseMainCommand.getRegisterCommand().getPlugin().getName(), baseMainCommand.getRegisterCommand().getName(), String.valueOf(methods.size() + fields.size())); // TLocale.Logger.info("COMMANDS.INTERNAL.COMMAND-REGISTER", baseMainCommand.getRegisterCommand().getPlugin().getName(), baseMainCommand.getRegisterCommand().getName(), String.valueOf(methods.size() + fields.size()));
} }
} }
@ -107,6 +102,16 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
subCommands.add(subCommand); subCommands.add(subCommand);
} }
public void onCommandHelp(CommandSender sender, Command command, String label, String[] args) {
sender.sendMessage(getEmptyLine());
sender.sendMessage(getCommandTitle());
sender.sendMessage(getEmptyLine());
subCommands.stream().filter(subCommands -> !hideInHelp(subCommands) && hasPermission(sender, subCommands)).map(subCommand -> subCommand == null ? getEmptyLine() : subCommand.getCommandString(label)).forEach(sender::sendMessage);
sender.sendMessage(getEmptyLine());
}
abstract public String getCommandTitle();
@Override @Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) { public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] args) {
return args.length == 1 ? subCommands.stream().filter(subCommand -> !hideInHelp(subCommand) && hasPermission(commandSender, subCommand) && (args[0].isEmpty() || subCommand.getLabel().toLowerCase().startsWith(args[0].toLowerCase()))).map(BaseSubCommand::getLabel).collect(Collectors.toList()) : null; return args.length == 1 ? subCommands.stream().filter(subCommand -> !hideInHelp(subCommand) && hasPermission(commandSender, subCommand) && (args[0].isEmpty() || subCommand.getLabel().toLowerCase().startsWith(args[0].toLowerCase()))).map(BaseSubCommand::getLabel).collect(Collectors.toList()) : null;
@ -115,10 +120,10 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) { if (args.length == 0) {
helpCommand(sender, label); onCommandHelp(sender, command, label, args);
} else { } else {
for (BaseSubCommand subCommand : subCommands) { for (BaseSubCommand subCommand : subCommands) {
if (subCommand == null || !args[0].equalsIgnoreCase(subCommand.getLabel()) || !hasPermission(sender, subCommand)) { if (subCommand == null || !(args[0].equalsIgnoreCase(subCommand.getLabel()) || Arrays.stream(subCommand.getAliases()).anyMatch(args[0]::equalsIgnoreCase)) || !hasPermission(sender, subCommand)) {
continue; continue;
} }
if (!isConfirmType(sender, subCommand.getType())) { if (!isConfirmType(sender, subCommand.getType())) {
@ -185,14 +190,6 @@ public abstract class BaseMainCommand implements CommandExecutor, TabExecutor {
|| (sender instanceof ConsoleCommandSender && commandType == CommandType.CONSOLE); || (sender instanceof ConsoleCommandSender && commandType == CommandType.CONSOLE);
} }
private void helpCommand(CommandSender sender, String label) {
sender.sendMessage(getEmptyLine());
sender.sendMessage(getCommandTitle());
sender.sendMessage(getEmptyLine());
subCommands.stream().filter(subCommands -> !hideInHelp(subCommands) && hasPermission(sender, subCommands)).map(subCommand -> subCommand == null ? getEmptyLine() : subCommand.getCommandString(label)).forEach(sender::sendMessage);
sender.sendMessage(getEmptyLine());
}
private void disguisedPlugin() { private void disguisedPlugin() {
linkClasses.forEach(clazz -> disguisedPlugin(clazz, (JavaPlugin) registerCommand.getPlugin())); linkClasses.forEach(clazz -> disguisedPlugin(clazz, (JavaPlugin) registerCommand.getPlugin()));
} }

View File

@ -16,99 +16,55 @@ import java.util.stream.IntStream;
*/ */
public abstract class BaseSubCommand { public abstract class BaseSubCommand {
/** private String label;
* 指令名
*
* @return 文本
*/
abstract public String getLabel();
/** public void setLabel(String label) {
* 指令描述 this.label = label;
* }
* @return 文本
*/
abstract public String getDescription();
/** public String getLabel() {
* 指令参数 return label;
* }
* @return {@link CommandArgument}
*/
abstract public CommandArgument[] getArguments();
/** public String getDescription() {
* 指令执行方法 return null;
* }
* @param sender 指令使用者
* @param command 指令对象 public String[] getAliases() {
* @param label 主命令 return new String[0];
* @param args 参数不含主命令及子命令 }
*/
abstract public void onCommand(CommandSender sender, Command command, String label, String[] args); public CommandArgument[] getArguments() {
return new CommandArgument[0];
}
/**
* 指令执行者
*
* @return {@link CommandType}
*/
public CommandType getType() { public CommandType getType() {
return CommandType.ALL; return CommandType.ALL;
} }
/**
* 参数是否屏蔽子命令名
*
* @return boolean
*/
public boolean ignoredLabel() { public boolean ignoredLabel() {
return true; return true;
} }
/**
* 是否需要玩家在线
*
* @return boolean
*/
public boolean requiredPlayer() { public boolean requiredPlayer() {
return false; return false;
} }
/**
* 需要权限
*
* @return boolean
*/
public String getPermission() { public String getPermission() {
return null; return null;
} }
/** public boolean hideInHelp() {
* 参数是否符合 return false;
* }
* @param args 参数
* @return boolean
*/
public boolean isParameterConform(String[] args) { public boolean isParameterConform(String[] args) {
return IntStream.range(0, getArguments().length).noneMatch(i -> getArguments()[i].isRequired() && (args == null || args.length <= i)); return IntStream.range(0, getArguments().length).noneMatch(i -> getArguments()[i].isRequired() && (args == null || args.length <= i));
} }
/**
* 获取帮助文本
*
* @param label 子命令标题
* @return String
*/
public String getCommandString(String label) { public String getCommandString(String label) {
return TLocale.asString("COMMANDS.INTERNAL.COMMAND-HELP", label, getLabel(), Arrays.stream(getArguments()).map(parameter -> parameter.toString() + " ").collect(Collectors.joining()), getDescription()); return TLocale.asString(getDescription() == null ? "COMMANDS.INTERNAL.COMMAND-HELP-EMPTY" : "COMMANDS.INTERNAL.COMMAND-HELP", label, getLabel(), Arrays.stream(getArguments()).map(parameter -> parameter.toString() + " ").collect(Collectors.joining()), getDescription());
} }
/** abstract public void onCommand(CommandSender sender, Command command, String label, String[] args);
* 是否在命令帮助中隐藏
*
* @return boolean
*/
public boolean hideInHelp() {
return false;
}
} }