1
mirror of https://github.com/CarmJos/EasyPlugin.git synced 2024-09-19 19:25:45 +00:00

feat(command): 为子命令提供父命令处理器的泛型,便于统一调用方法

This commit is contained in:
Carm Jos 2022-08-25 01:52:04 +08:00
parent 429eb9c6ec
commit 8f03c2a1b3
16 changed files with 43 additions and 33 deletions

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -11,13 +11,14 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.stream.Collectors;
@SuppressWarnings("UnusedReturnValue")
public abstract class CommandHandler implements TabExecutor, NamedExecutor {
protected final @NotNull JavaPlugin plugin;
protected final @NotNull String cmd;
protected final @NotNull List<String> aliases;
protected final @NotNull Map<String, SubCommand> registeredCommands = new HashMap<>();
protected final @NotNull Map<String, SubCommand<?>> registeredCommands = new HashMap<>();
protected final @NotNull Map<String, CommandHandler> registeredHandlers = new HashMap<>();
protected final @NotNull Map<String, String> aliasesMap = new HashMap<>();
@ -36,13 +37,13 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
this.aliases = Arrays.asList(aliases);
}
public abstract void noArgs(CommandSender sender);
public abstract Void noArgs(CommandSender sender);
public void unknownCommand(CommandSender sender, String[] args) {
noArgs(sender);
public Void unknownCommand(CommandSender sender, String[] args) {
return noArgs(sender);
}
public abstract void noPermission(CommandSender sender);
public abstract Void noPermission(CommandSender sender);
@Override
public @NotNull List<String> getAliases() {
@ -54,7 +55,7 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
return this.cmd;
}
public void registerSubCommand(SubCommand command) {
public void registerSubCommand(SubCommand<?> command) {
String name = command.getName().toLowerCase();
this.registeredCommands.put(name, command);
command.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
@ -82,14 +83,14 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
}
}
SubCommand subCommand = getSubCommand(input);
if (subCommand == null) {
SubCommand<?> sub = getSubCommand(input);
if (sub == null) {
this.unknownCommand(sender, args);
} else if (!subCommand.hasPermission(sender)) {
} else if (!sub.hasPermission(sender)) {
this.noPermission(sender);
} else {
try {
subCommand.execute(this.plugin, sender, this.shortenArgs(args));
sub.execute(this.plugin, sender, this.shortenArgs(args));
} catch (ArrayIndexOutOfBoundsException var9) {
this.unknownCommand(sender, args);
}
@ -117,9 +118,9 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
return handler.onTabComplete(sender, command, alias, this.shortenArgs(args));
}
SubCommand subCommand = getSubCommand(input);
if (subCommand != null && subCommand.hasPermission(sender)) {
return subCommand.tabComplete(this.plugin, sender, this.shortenArgs(args));
SubCommand<?> sub = getSubCommand(input);
if (sub != null && sub.hasPermission(sender)) {
return sub.tabComplete(this.plugin, sender, this.shortenArgs(args));
}
return Collections.emptyList();
@ -144,8 +145,8 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
else return this.registeredHandlers.get(nameFromAlias);
}
protected @Nullable SubCommand getSubCommand(@NotNull String name) {
SubCommand fromName = this.registeredCommands.get(name);
protected @Nullable SubCommand<?> getSubCommand(@NotNull String name) {
SubCommand<?> fromName = this.registeredCommands.get(name);
if (fromName != null) return fromName;
String nameFromAlias = this.aliasesMap.get(name);

View File

@ -2,28 +2,38 @@ package cc.carm.lib.easyplugin.command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@SuppressWarnings("UnusedReturnValue")
public abstract class SubCommand implements NamedExecutor {
public abstract class SubCommand<C extends CommandHandler> implements NamedExecutor {
private final @NotNull C parent;
private final String name;
private final List<String> aliases;
public SubCommand(String name, String... aliases) {
public SubCommand(@NotNull C parent, String name, String... aliases) {
this.parent = parent;
this.name = name;
this.aliases = Arrays.asList(aliases);
}
public @NotNull C getParent() {
return parent;
}
@Override
public String getName() {
return this.name;
}
@Override
@Unmodifiable
public List<String> getAliases() {
return this.aliases;
}
@ -33,6 +43,5 @@ public abstract class SubCommand implements NamedExecutor {
public List<String> tabComplete(JavaPlugin plugin, CommandSender sender, String[] args) {
return Collections.emptyList();
}
}

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -5,7 +5,7 @@
<parent>
<artifactId>easyplugin-parent</artifactId>
<groupId>cc.carm.lib</groupId>
<version>1.4.13</version>
<version>1.4.14</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -15,7 +15,7 @@
<groupId>cc.carm.lib</groupId>
<artifactId>easyplugin-parent</artifactId>
<packaging>pom</packaging>
<version>1.4.13</version>
<version>1.4.14</version>
<modules>
<module>base/main</module>