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