mirror of
https://github.com/CarmJos/EasyPlugin.git
synced 2026-06-04 16:48:16 +08:00
feat(command): 为子命令提供父命令处理器的泛型,便于统一调用方法
This commit is contained in:
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -11,13 +11,14 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@SuppressWarnings("UnusedReturnValue")
|
||||||
public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
||||||
|
|
||||||
protected final @NotNull JavaPlugin plugin;
|
protected final @NotNull JavaPlugin plugin;
|
||||||
protected final @NotNull String cmd;
|
protected final @NotNull String cmd;
|
||||||
protected final @NotNull List<String> aliases;
|
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, CommandHandler> registeredHandlers = new HashMap<>();
|
||||||
|
|
||||||
protected final @NotNull Map<String, String> aliasesMap = 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);
|
this.aliases = Arrays.asList(aliases);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void noArgs(CommandSender sender);
|
public abstract Void noArgs(CommandSender sender);
|
||||||
|
|
||||||
public void unknownCommand(CommandSender sender, String[] args) {
|
public Void unknownCommand(CommandSender sender, String[] args) {
|
||||||
noArgs(sender);
|
return noArgs(sender);
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void noPermission(CommandSender sender);
|
public abstract Void noPermission(CommandSender sender);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull List<String> getAliases() {
|
public @NotNull List<String> getAliases() {
|
||||||
@@ -54,7 +55,7 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
|||||||
return this.cmd;
|
return this.cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerSubCommand(SubCommand command) {
|
public void registerSubCommand(SubCommand<?> command) {
|
||||||
String name = command.getName().toLowerCase();
|
String name = command.getName().toLowerCase();
|
||||||
this.registeredCommands.put(name, command);
|
this.registeredCommands.put(name, command);
|
||||||
command.getAliases().forEach(alias -> this.aliasesMap.put(alias.toLowerCase(), name));
|
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);
|
SubCommand<?> sub = getSubCommand(input);
|
||||||
if (subCommand == null) {
|
if (sub == null) {
|
||||||
this.unknownCommand(sender, args);
|
this.unknownCommand(sender, args);
|
||||||
} else if (!subCommand.hasPermission(sender)) {
|
} else if (!sub.hasPermission(sender)) {
|
||||||
this.noPermission(sender);
|
this.noPermission(sender);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
subCommand.execute(this.plugin, sender, this.shortenArgs(args));
|
sub.execute(this.plugin, sender, this.shortenArgs(args));
|
||||||
} catch (ArrayIndexOutOfBoundsException var9) {
|
} catch (ArrayIndexOutOfBoundsException var9) {
|
||||||
this.unknownCommand(sender, args);
|
this.unknownCommand(sender, args);
|
||||||
}
|
}
|
||||||
@@ -117,9 +118,9 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
|||||||
return handler.onTabComplete(sender, command, alias, this.shortenArgs(args));
|
return handler.onTabComplete(sender, command, alias, this.shortenArgs(args));
|
||||||
}
|
}
|
||||||
|
|
||||||
SubCommand subCommand = getSubCommand(input);
|
SubCommand<?> sub = getSubCommand(input);
|
||||||
if (subCommand != null && subCommand.hasPermission(sender)) {
|
if (sub != null && sub.hasPermission(sender)) {
|
||||||
return subCommand.tabComplete(this.plugin, sender, this.shortenArgs(args));
|
return sub.tabComplete(this.plugin, sender, this.shortenArgs(args));
|
||||||
}
|
}
|
||||||
|
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
@@ -144,8 +145,8 @@ public abstract class CommandHandler implements TabExecutor, NamedExecutor {
|
|||||||
else return this.registeredHandlers.get(nameFromAlias);
|
else return this.registeredHandlers.get(nameFromAlias);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected @Nullable SubCommand getSubCommand(@NotNull String name) {
|
protected @Nullable SubCommand<?> getSubCommand(@NotNull String name) {
|
||||||
SubCommand fromName = this.registeredCommands.get(name);
|
SubCommand<?> fromName = this.registeredCommands.get(name);
|
||||||
if (fromName != null) return fromName;
|
if (fromName != null) return fromName;
|
||||||
|
|
||||||
String nameFromAlias = this.aliasesMap.get(name);
|
String nameFromAlias = this.aliasesMap.get(name);
|
||||||
|
|||||||
@@ -2,28 +2,38 @@ package cc.carm.lib.easyplugin.command;
|
|||||||
|
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Unmodifiable;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@SuppressWarnings("UnusedReturnValue")
|
@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 String name;
|
||||||
private final List<String> aliases;
|
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.name = name;
|
||||||
this.aliases = Arrays.asList(aliases);
|
this.aliases = Arrays.asList(aliases);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @NotNull C getParent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return this.name;
|
return this.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@Unmodifiable
|
||||||
public List<String> getAliases() {
|
public List<String> getAliases() {
|
||||||
return this.aliases;
|
return this.aliases;
|
||||||
}
|
}
|
||||||
@@ -34,5 +44,4 @@ public abstract class SubCommand implements NamedExecutor {
|
|||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
<parent>
|
<parent>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<relativePath>../../pom.xml</relativePath>
|
<relativePath>../../pom.xml</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
<groupId>cc.carm.lib</groupId>
|
<groupId>cc.carm.lib</groupId>
|
||||||
<artifactId>easyplugin-parent</artifactId>
|
<artifactId>easyplugin-parent</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>1.4.13</version>
|
<version>1.4.14</version>
|
||||||
<modules>
|
<modules>
|
||||||
|
|
||||||
<module>base/main</module>
|
<module>base/main</module>
|
||||||
|
|||||||
Reference in New Issue
Block a user