diff --git a/base/color/pom.xml b/base/color/pom.xml index c551743..29c18d7 100644 --- a/base/color/pom.xml +++ b/base/color/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyplugin-parent - 1.5.4 + 1.5.5 ../../pom.xml diff --git a/base/command-alias/pom.xml b/base/command-alias/pom.xml new file mode 100644 index 0000000..d78b5b5 --- /dev/null +++ b/base/command-alias/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + + cc.carm.lib + easyplugin-parent + 1.5.5 + ../../pom.xml + + + ${project.jdk.version} + ${project.jdk.version} + UTF-8 + UTF-8 + + easyplugin-command-alias + jar + + EasyPlugin-Command-Alias + 轻松插件指令别名映射模块,支持将插件内复杂的子指令简化为一个单独的指令,方便玩家使用。 + https://github.com/CarmJos/EasyPlugin + + + + CarmJos + Carm Jos + carm@carm.cc + https://www.carm.cc + + + + + + The MIT License + https://opensource.org/licenses/MIT + + + + + GitHub Issues + https://github.com/CarmJos/EasyPlugin/issues + + + + GitHub Actions + https://github.com/CarmJos/EasyPlugin/actions/workflows/maven.yml + + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + + \ No newline at end of file diff --git a/base/command-alias/src/main/java/cc/carm/lib/easyplugin/command/alias/AliasCommand.java b/base/command-alias/src/main/java/cc/carm/lib/easyplugin/command/alias/AliasCommand.java new file mode 100644 index 0000000..13c57f7 --- /dev/null +++ b/base/command-alias/src/main/java/cc/carm/lib/easyplugin/command/alias/AliasCommand.java @@ -0,0 +1,51 @@ +package cc.carm.lib.easyplugin.command.alias; + +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +public class AliasCommand extends Command { + + protected final AliasCommandManager aliasCommandManager; + protected final String targetCommand; + + public AliasCommand(String name, AliasCommandManager aliasCommandManager, String targetCommand) { + super(name); + this.aliasCommandManager = aliasCommandManager; + this.targetCommand = targetCommand; + } + + protected SimpleCommandMap getCommandMap() { + return this.aliasCommandManager.getCommandMap(); + } + + protected String buildCommand(String[] args) { + return this.targetCommand + " " + String.join(" ", args); + } + + @Override + public boolean execute(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] args) { + return getCommandMap().dispatch(sender, buildCommand(args)); + } + + @NotNull + @Override + public List tabComplete(@NotNull CommandSender sender, @NotNull String alias, + @NotNull String[] args, @Nullable Location location) throws IllegalArgumentException { + return Optional.ofNullable(getCommandMap().tabComplete(sender, buildCommand(args))).orElse(Collections.emptyList()); + } + + @NotNull + @Override + public List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException { + return tabComplete(sender, alias, args, null); + } + +} \ No newline at end of file diff --git a/base/command-alias/src/main/java/cc/carm/lib/easyplugin/command/alias/AliasCommandManager.java b/base/command-alias/src/main/java/cc/carm/lib/easyplugin/command/alias/AliasCommandManager.java new file mode 100644 index 0000000..d06bd72 --- /dev/null +++ b/base/command-alias/src/main/java/cc/carm/lib/easyplugin/command/alias/AliasCommandManager.java @@ -0,0 +1,88 @@ +package cc.carm.lib.easyplugin.command.alias; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.SimpleCommandMap; +import org.bukkit.plugin.SimplePluginManager; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +/** + * 指令简化别名(简化映射)管理器 + *
支持将插件内复杂的子指令简化为一个单独的指令,方便玩家使用。 + */ +public class AliasCommandManager { + + protected final @NotNull JavaPlugin plugin; + protected final @NotNull String prefix; + + protected final @NotNull SimpleCommandMap commandMap; + protected final @NotNull Field knownCommandsFiled; + + protected final @NotNull Map registeredCommands = new HashMap<>(); + + public AliasCommandManager(@NotNull JavaPlugin plugin) throws Exception { + this(plugin, plugin.getName()); + } + + public AliasCommandManager(@NotNull JavaPlugin plugin, @NotNull String prefix) throws Exception { + this.plugin = plugin; + this.prefix = prefix.trim(); + + SimplePluginManager manager = (SimplePluginManager) Bukkit.getPluginManager(); + Field commandMapField = SimplePluginManager.class.getDeclaredField("commandMap"); + commandMapField.setAccessible(true); + this.commandMap = (SimpleCommandMap) commandMapField.get(manager); + + this.knownCommandsFiled = SimpleCommandMap.class.getDeclaredField("knownCommands"); + this.knownCommandsFiled.setAccessible(true); + } + + @SuppressWarnings("unchecked") + protected Map getKnownCommands() { + try { + return (Map) knownCommandsFiled.get(commandMap); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + return new HashMap<>(); + } + + protected @NotNull SimpleCommandMap getCommandMap() { + return commandMap; + } + + public String getCommandPrefix() { + return this.prefix; + } + + public void register(@NotNull String alias, @NotNull String subCommand) { + AliasCommand current = this.registeredCommands.get(alias); + if (current != null) current.unregister(getCommandMap()); + + AliasCommand cmd = new AliasCommand(alias, this, getCommandPrefix() + " " + subCommand); + this.registeredCommands.put(alias, cmd); + getCommandMap().register(plugin.getName(), cmd); + } + + public void unregister(@NotNull String alias) { + AliasCommand current = this.registeredCommands.remove(alias); + if (current != null) { + getKnownCommands().remove(alias); + current.unregister(getCommandMap()); + } + } + + public void unregisterAll() { + registeredCommands.forEach((k, v) -> { + getKnownCommands().remove(k); + v.unregister(getCommandMap()); + }); + registeredCommands.clear(); + } + +} diff --git a/base/command/pom.xml b/base/command/pom.xml index 54576bf..1a946b1 100644 --- a/base/command/pom.xml +++ b/base/command/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/base/gui/pom.xml b/base/gui/pom.xml index e2e29fd..a0a24fd 100644 --- a/base/gui/pom.xml +++ b/base/gui/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/base/main/pom.xml b/base/main/pom.xml index dec83c5..f1eaff0 100644 --- a/base/main/pom.xml +++ b/base/main/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/base/messages/pom.xml b/base/messages/pom.xml index 2093f08..5758401 100644 --- a/base/messages/pom.xml +++ b/base/messages/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyplugin-parent - 1.5.4 + 1.5.5 ../../pom.xml diff --git a/base/storage/pom.xml b/base/storage/pom.xml index 51bf993..306427d 100644 --- a/base/storage/pom.xml +++ b/base/storage/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/base/user/pom.xml b/base/user/pom.xml index 26ed5b6..d403179 100644 --- a/base/user/pom.xml +++ b/base/user/pom.xml @@ -6,7 +6,7 @@ cc.carm.lib easyplugin-parent - 1.5.4 + 1.5.5 ../../pom.xml diff --git a/base/utils/pom.xml b/base/utils/pom.xml index cbba4dd..735abe6 100644 --- a/base/utils/pom.xml +++ b/base/utils/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/collection/all/pom.xml b/collection/all/pom.xml index 8eaddff..fe25432 100644 --- a/collection/all/pom.xml +++ b/collection/all/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/collection/bom/pom.xml b/collection/bom/pom.xml index 59984c3..18b1f9f 100644 --- a/collection/bom/pom.xml +++ b/collection/bom/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/collection/common/pom.xml b/collection/common/pom.xml index 9ae770e..164c2db 100644 --- a/collection/common/pom.xml +++ b/collection/common/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/extension/gh-checker/pom.xml b/extension/gh-checker/pom.xml index 028df07..e03e190 100644 --- a/extension/gh-checker/pom.xml +++ b/extension/gh-checker/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/extension/papi/pom.xml b/extension/papi/pom.xml index 816612e..922be6f 100644 --- a/extension/papi/pom.xml +++ b/extension/papi/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/extension/vault/pom.xml b/extension/vault/pom.xml index e6b4179..a5e8512 100644 --- a/extension/vault/pom.xml +++ b/extension/vault/pom.xml @@ -5,7 +5,7 @@ easyplugin-parent cc.carm.lib - 1.5.4 + 1.5.5 ../../pom.xml 4.0.0 diff --git a/pom.xml b/pom.xml index 3bf5df3..9e44c1f 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ cc.carm.lib easyplugin-parent pom - 1.5.4 + 1.5.5 base/color base/utils @@ -24,6 +24,7 @@ base/messages base/gui base/command + base/command-alias base/storage base/user