feat: 优化代码
This commit is contained in:
@@ -11,6 +11,10 @@ public abstract class ChildCommand implements TabExecutor {
|
||||
@NotNull
|
||||
public abstract String getUsage();
|
||||
|
||||
public int getArgumentCount() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public abstract String getDescription();
|
||||
|
||||
|
@@ -14,10 +14,10 @@ import java.util.stream.Collectors;
|
||||
@SuppressWarnings("unused")
|
||||
public abstract class ParentCommand extends ChildCommand {
|
||||
@NotNull
|
||||
private final List<ChildCommand> childCommands;
|
||||
private final Map<String, ChildCommand> childCommands;
|
||||
|
||||
public ParentCommand() {
|
||||
childCommands = new ArrayList<>();
|
||||
childCommands = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -33,8 +33,8 @@ public abstract class ParentCommand extends ChildCommand {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<ChildCommand> getChildCommands() {
|
||||
return childCommands;
|
||||
public Collection<ChildCommand> getChildCommands() {
|
||||
return childCommands.values();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -78,7 +78,7 @@ public abstract class ParentCommand extends ChildCommand {
|
||||
@NotNull
|
||||
public List<ChildCommand> getEndChildCommands() {
|
||||
ArrayList<ChildCommand> list = new ArrayList<>();
|
||||
for (ChildCommand command : childCommands) {
|
||||
for (ChildCommand command : getChildCommands()) {
|
||||
if (command instanceof ParentCommand) {
|
||||
list.addAll(((ParentCommand) command).getEndChildCommands());
|
||||
} else {
|
||||
@@ -89,13 +89,16 @@ public abstract class ParentCommand extends ChildCommand {
|
||||
}
|
||||
|
||||
public void addChildCommand(@NotNull ChildCommand command) {
|
||||
childCommands.add(command);
|
||||
if (childCommands.containsKey(command.getName())) {
|
||||
throw new IllegalArgumentException("command " + command.getName() + " already exists!");
|
||||
}
|
||||
childCommands.put(command.getName(), command);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Map<String, String> getCommandHelp(CommandSender sender) {
|
||||
Map<String, String> map = new LinkedHashMap<>();
|
||||
for (ChildCommand child : childCommands) {
|
||||
for (ChildCommand child : getChildCommands()) {
|
||||
if (!child.hasPermission(sender)) {
|
||||
continue;
|
||||
}
|
||||
@@ -131,7 +134,7 @@ public abstract class ParentCommand extends ChildCommand {
|
||||
sendHelp(sender);
|
||||
return true;
|
||||
}
|
||||
for (ChildCommand childCommand : childCommands) {
|
||||
for (ChildCommand childCommand : getChildCommands()) {
|
||||
if (!childCommand.getName().equalsIgnoreCase(args[0])) {
|
||||
continue;
|
||||
}
|
||||
@@ -139,6 +142,10 @@ public abstract class ParentCommand extends ChildCommand {
|
||||
CoreMessage.COMMAND_NOT_HAS_PERMISSION.show(sender);
|
||||
return true;
|
||||
}
|
||||
if (args.length - 1 < getArgumentCount()) {
|
||||
sender.sendMessage(getUsage() + " " + childCommand.getUsage());
|
||||
return true;
|
||||
}
|
||||
return childCommand.onCommand(sender, command, label, Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
CoreMessage.COMMAND_NOT_FOUND.show(sender);
|
||||
@@ -148,18 +155,18 @@ public abstract class ParentCommand extends ChildCommand {
|
||||
@Override
|
||||
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, String[] args) {
|
||||
if (args.length == 0) {
|
||||
return childCommands.stream()
|
||||
return getChildCommands().stream()
|
||||
.filter(o -> o.hasPermission(sender))
|
||||
.map(ChildCommand::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
for (ChildCommand child : childCommands) {
|
||||
for (ChildCommand child : getChildCommands()) {
|
||||
if (args[0].equalsIgnoreCase(child.getName())) {
|
||||
return child.onTabComplete(sender, command, alias, Arrays.copyOfRange(args, 1, args.length));
|
||||
}
|
||||
}
|
||||
args[0] = args[0].toLowerCase();
|
||||
return childCommands.stream()
|
||||
return getChildCommands().stream()
|
||||
.filter(o -> o.hasPermission(sender))
|
||||
.map(ChildCommand::getName)
|
||||
.filter(o -> o.toLowerCase().startsWith(args[0].toLowerCase()))
|
||||
|
Reference in New Issue
Block a user